Skip to content

Commit 2cd1f3c

Browse files
authored
Merge pull request #53 from sx-aurora-dev/feature/merge-upstream-20210504
Feature/merge upstream 20210504
2 parents 5f0e8e1 + 3b6f030 commit 2cd1f3c

File tree

4,892 files changed

+322983
-181198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,892 files changed

+322983
-181198
lines changed

clang-tools-extra/clang-query/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ clang_target_link_libraries(clangQuery
1919
clangBasic
2020
clangDynamicASTMatchers
2121
clangFrontend
22+
clangTooling
2223
clangSerialization
2324
)
2425

clang-tools-extra/clang-query/Query.cpp

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "clang/ASTMatchers/ASTMatchFinder.h"
1313
#include "clang/Frontend/ASTUnit.h"
1414
#include "clang/Frontend/TextDiagnostic.h"
15+
#include "clang/Tooling/NodeIntrospection.h"
1516
#include "llvm/Support/raw_ostream.h"
1617

1718
using namespace clang::ast_matchers;
@@ -66,6 +67,8 @@ bool HelpQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
6667
"Diagnostic location for bound nodes.\n"
6768
" detailed-ast "
6869
"Detailed AST output for bound nodes.\n"
70+
" srcloc "
71+
"Source locations and ranges for bound nodes.\n"
6972
" dump "
7073
"Detailed AST output for bound nodes (alias of detailed-ast).\n\n";
7174
return true;
@@ -86,6 +89,90 @@ struct CollectBoundNodes : MatchFinder::MatchCallback {
8689
}
8790
};
8891

92+
void dumpLocations(llvm::raw_ostream &OS, DynTypedNode Node, ASTContext &Ctx,
93+
const DiagnosticsEngine &Diags, SourceManager const &SM) {
94+
auto Locs = clang::tooling::NodeIntrospection::GetLocations(Node);
95+
96+
auto PrintLocations = [](llvm::raw_ostream &OS, auto Iter, auto End) {
97+
auto CommonEntry = Iter->first;
98+
auto Scout = Iter;
99+
SmallVector<std::string> LocationStrings;
100+
while (Scout->first == CommonEntry) {
101+
LocationStrings.push_back(
102+
tooling::LocationCallFormatterCpp::format(*Iter->second));
103+
if (Scout == End)
104+
break;
105+
++Scout;
106+
if (Scout->first == CommonEntry)
107+
++Iter;
108+
}
109+
llvm::sort(LocationStrings);
110+
for (auto &LS : LocationStrings) {
111+
OS << " * \"" << LS << "\"\n";
112+
}
113+
return Iter;
114+
};
115+
116+
TextDiagnostic TD(OS, Ctx.getLangOpts(), &Diags.getDiagnosticOptions());
117+
118+
for (auto Iter = Locs.LocationAccessors.begin();
119+
Iter != Locs.LocationAccessors.end(); ++Iter) {
120+
if (!Iter->first.isValid())
121+
continue;
122+
123+
TD.emitDiagnostic(FullSourceLoc(Iter->first, SM), DiagnosticsEngine::Note,
124+
"source locations here", None, None);
125+
126+
Iter = PrintLocations(OS, Iter, Locs.LocationAccessors.end());
127+
OS << '\n';
128+
}
129+
130+
for (auto Iter = Locs.RangeAccessors.begin();
131+
Iter != Locs.RangeAccessors.end(); ++Iter) {
132+
133+
if (!Iter->first.getBegin().isValid())
134+
continue;
135+
136+
if (SM.getPresumedLineNumber(Iter->first.getBegin()) !=
137+
SM.getPresumedLineNumber(Iter->first.getEnd()))
138+
continue;
139+
140+
TD.emitDiagnostic(FullSourceLoc(Iter->first.getBegin(), SM),
141+
DiagnosticsEngine::Note,
142+
"source ranges here " + Iter->first.printToString(SM),
143+
CharSourceRange::getTokenRange(Iter->first), None);
144+
145+
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
146+
}
147+
for (auto Iter = Locs.RangeAccessors.begin();
148+
Iter != Locs.RangeAccessors.end(); ++Iter) {
149+
150+
if (!Iter->first.getBegin().isValid())
151+
continue;
152+
153+
if (SM.getPresumedLineNumber(Iter->first.getBegin()) ==
154+
SM.getPresumedLineNumber(Iter->first.getEnd()))
155+
continue;
156+
157+
TD.emitDiagnostic(
158+
FullSourceLoc(Iter->first.getBegin(), SM), DiagnosticsEngine::Note,
159+
"source range " + Iter->first.printToString(SM) + " starting here...",
160+
CharSourceRange::getTokenRange(Iter->first), None);
161+
162+
auto ColNum = SM.getPresumedColumnNumber(Iter->first.getEnd());
163+
auto LastLineLoc = Iter->first.getEnd().getLocWithOffset(-(ColNum - 1));
164+
165+
TD.emitDiagnostic(FullSourceLoc(Iter->first.getEnd(), SM),
166+
DiagnosticsEngine::Note, "... ending here",
167+
CharSourceRange::getTokenRange(
168+
SourceRange(LastLineLoc, Iter->first.getEnd())),
169+
None);
170+
171+
Iter = PrintLocations(OS, Iter, Locs.RangeAccessors.end());
172+
}
173+
OS << "\n";
174+
}
175+
89176
} // namespace
90177

91178
bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
@@ -106,8 +193,10 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
106193
return false;
107194
}
108195

109-
AST->getASTContext().getParentMapContext().setTraversalKind(QS.TK);
110-
Finder.matchAST(AST->getASTContext());
196+
auto &Ctx = AST->getASTContext();
197+
const auto &SM = Ctx.getSourceManager();
198+
Ctx.getParentMapContext().setTraversalKind(QS.TK);
199+
Finder.matchAST(Ctx);
111200

112201
if (QS.PrintMatcher) {
113202
SmallVector<StringRef, 4> Lines;
@@ -159,6 +248,13 @@ bool MatchQuery::run(llvm::raw_ostream &OS, QuerySession &QS) const {
159248
Dumper.Visit(BI->second);
160249
OS << "\n";
161250
}
251+
if (QS.SrcLocOutput) {
252+
OS << "\n \"" << BI->first << "\" Source locations\n";
253+
OS << " " << std::string(19 + BI->first.size(), '-') << '\n';
254+
255+
dumpLocations(OS, BI->second, Ctx, AST->getDiagnostics(), SM);
256+
OS << "\n";
257+
}
162258
}
163259

164260
if (MI->getMap().empty())

clang-tools-extra/clang-query/Query.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
namespace clang {
1919
namespace query {
2020

21-
enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST };
21+
enum OutputKind { OK_Diag, OK_Print, OK_DetailedAST, OK_SrcLoc };
2222

2323
enum QueryKind {
2424
QK_Invalid,
@@ -149,6 +149,7 @@ struct SetExclusiveOutputQuery : Query {
149149
QS.DiagOutput = false;
150150
QS.DetailedASTOutput = false;
151151
QS.PrintOutput = false;
152+
QS.SrcLocOutput = false;
152153
QS.*Var = true;
153154
return true;
154155
}

clang-tools-extra/clang-query/QueryParser.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "QuerySession.h"
1212
#include "clang/ASTMatchers/Dynamic/Parser.h"
1313
#include "clang/Basic/CharInfo.h"
14+
#include "clang/Tooling/NodeIntrospection.h"
1415
#include "llvm/ADT/StringRef.h"
1516
#include "llvm/ADT/StringSwitch.h"
1617
#include <set>
@@ -104,16 +105,19 @@ QueryRef QueryParser::parseSetBool(bool QuerySession::*Var) {
104105

105106
template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
106107
StringRef ValStr;
107-
unsigned OutKind = LexOrCompleteWord<unsigned>(this, ValStr)
108-
.Case("diag", OK_Diag)
109-
.Case("print", OK_Print)
110-
.Case("detailed-ast", OK_DetailedAST)
111-
.Case("dump", OK_DetailedAST)
112-
.Default(~0u);
108+
bool HasIntrospection = tooling::NodeIntrospection::hasIntrospectionSupport();
109+
unsigned OutKind =
110+
LexOrCompleteWord<unsigned>(this, ValStr)
111+
.Case("diag", OK_Diag)
112+
.Case("print", OK_Print)
113+
.Case("detailed-ast", OK_DetailedAST)
114+
.Case("srcloc", OK_SrcLoc, /*IsCompletion=*/HasIntrospection)
115+
.Case("dump", OK_DetailedAST)
116+
.Default(~0u);
113117
if (OutKind == ~0u) {
114-
return new InvalidQuery(
115-
"expected 'diag', 'print', 'detailed-ast' or 'dump', got '" + ValStr +
116-
"'");
118+
return new InvalidQuery("expected 'diag', 'print', 'detailed-ast'" +
119+
StringRef(HasIntrospection ? ", 'srcloc'" : "") +
120+
" or 'dump', got '" + ValStr + "'");
117121
}
118122

119123
switch (OutKind) {
@@ -123,6 +127,10 @@ template <typename QueryType> QueryRef QueryParser::parseSetOutputKind() {
123127
return new QueryType(&QuerySession::DiagOutput);
124128
case OK_Print:
125129
return new QueryType(&QuerySession::PrintOutput);
130+
case OK_SrcLoc:
131+
if (HasIntrospection)
132+
return new QueryType(&QuerySession::SrcLocOutput);
133+
return new InvalidQuery("'srcloc' output support is not available.");
126134
}
127135

128136
llvm_unreachable("Invalid output kind");

clang-tools-extra/clang-query/QuerySession.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ class QuerySession {
2525
public:
2626
QuerySession(llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs)
2727
: ASTs(ASTs), PrintOutput(false), DiagOutput(true),
28-
DetailedASTOutput(false), BindRoot(true), PrintMatcher(false),
29-
Terminate(false), TK(TK_AsIs) {}
28+
DetailedASTOutput(false), SrcLocOutput(false), BindRoot(true),
29+
PrintMatcher(false), Terminate(false), TK(TK_AsIs) {}
3030

3131
llvm::ArrayRef<std::unique_ptr<ASTUnit>> ASTs;
3232

3333
bool PrintOutput;
3434
bool DiagOutput;
3535
bool DetailedASTOutput;
36+
bool SrcLocOutput;
3637

3738
bool BindRoot;
3839
bool PrintMatcher;

clang-tools-extra/clang-tidy/ClangTidyCheck.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ llvm::Optional<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
147147

148148
StringRef Value = Iter->getValue().Value;
149149
StringRef Closest;
150-
unsigned EditDistance = -1;
150+
unsigned EditDistance = 3;
151151
for (const auto &NameAndEnum : Mapping) {
152152
if (IgnoreCase) {
153153
if (Value.equals_lower(NameAndEnum.second))
@@ -159,7 +159,8 @@ llvm::Optional<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
159159
EditDistance = 0;
160160
continue;
161161
}
162-
unsigned Distance = Value.edit_distance(NameAndEnum.second);
162+
unsigned Distance =
163+
Value.edit_distance(NameAndEnum.second, true, EditDistance);
163164
if (Distance < EditDistance) {
164165
EditDistance = Distance;
165166
Closest = NameAndEnum.second;

clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok,
158158
// Skip variable declaration.
159159
bool VarDecl = possibleVarDecl(MI, MI->tokens_begin());
160160

161+
// Skip the goto argument with an arbitrary number of subsequent stars.
162+
bool FoundGoto = false;
163+
161164
for (auto TI = MI->tokens_begin(), TE = MI->tokens_end(); TI != TE; ++TI) {
162165
// First token.
163166
if (TI == MI->tokens_begin())
@@ -179,9 +182,17 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok,
179182
continue;
180183
}
181184

185+
// There should not be extra parentheses for the goto argument.
186+
if (Tok.is(tok::kw_goto)) {
187+
FoundGoto = true;
188+
continue;
189+
}
190+
182191
// Only interested in identifiers.
183-
if (!Tok.isOneOf(tok::identifier, tok::raw_identifier))
192+
if (!Tok.isOneOf(tok::identifier, tok::raw_identifier)) {
193+
FoundGoto = false;
184194
continue;
195+
}
185196

186197
// Only interested in macro arguments.
187198
if (MI->getParameterNum(Tok.getIdentifierInfo()) < 0)
@@ -239,12 +250,16 @@ void MacroParenthesesPPCallbacks::argument(const Token &MacroNameTok,
239250
if (MI->isVariadic())
240251
continue;
241252

242-
Check->diag(Tok.getLocation(), "macro argument should be enclosed in "
243-
"parentheses")
244-
<< FixItHint::CreateInsertion(Tok.getLocation(), "(")
245-
<< FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset(
246-
PP->getSpelling(Tok).length()),
247-
")");
253+
if (!FoundGoto) {
254+
Check->diag(Tok.getLocation(), "macro argument should be enclosed in "
255+
"parentheses")
256+
<< FixItHint::CreateInsertion(Tok.getLocation(), "(")
257+
<< FixItHint::CreateInsertion(Tok.getLocation().getLocWithOffset(
258+
PP->getSpelling(Tok).length()),
259+
")");
260+
}
261+
262+
FoundGoto = false;
248263
}
249264
}
250265

0 commit comments

Comments
 (0)