Skip to content

Commit 5a37a35

Browse files
authored
Merge pull request #131 from sx-aurora-dev/feature/merge-upstream-20211221
Feature/merge upstream 20211221
2 parents 6be0d40 + 262a5a8 commit 5a37a35

File tree

768 files changed

+72697
-107921
lines changed

Some content is hidden

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

768 files changed

+72697
-107921
lines changed

clang-tools-extra/clang-include-fixer/SymbolIndexManager.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88

99
#include "SymbolIndexManager.h"
1010
#include "find-all-symbols/SymbolInfo.h"
11-
#include "llvm/ADT/DenseMap.h"
11+
#include "llvm/ADT/STLExtras.h"
1212
#include "llvm/ADT/SmallVector.h"
13+
#include "llvm/ADT/StringMap.h"
1314
#include "llvm/Support/Debug.h"
1415
#include "llvm/Support/Path.h"
1516

@@ -47,7 +48,7 @@ static double similarityScore(llvm::StringRef FileName,
4748

4849
static void rank(std::vector<SymbolAndSignals> &Symbols,
4950
llvm::StringRef FileName) {
50-
llvm::DenseMap<llvm::StringRef, double> Score;
51+
llvm::StringMap<double> Score;
5152
for (const auto &Symbol : Symbols) {
5253
// Calculate a score from the similarity of the header the symbol is in
5354
// with the current file and the popularity of the symbol.
@@ -58,14 +59,14 @@ static void rank(std::vector<SymbolAndSignals> &Symbols,
5859
}
5960
// Sort by the gathered scores. Use file name as a tie breaker so we can
6061
// deduplicate.
61-
std::sort(Symbols.begin(), Symbols.end(),
62-
[&](const SymbolAndSignals &A, const SymbolAndSignals &B) {
63-
auto AS = Score[A.Symbol.getFilePath()];
64-
auto BS = Score[B.Symbol.getFilePath()];
65-
if (AS != BS)
66-
return AS > BS;
67-
return A.Symbol.getFilePath() < B.Symbol.getFilePath();
68-
});
62+
llvm::sort(Symbols.begin(), Symbols.end(),
63+
[&](const SymbolAndSignals &A, const SymbolAndSignals &B) {
64+
auto AS = Score[A.Symbol.getFilePath()];
65+
auto BS = Score[B.Symbol.getFilePath()];
66+
if (AS != BS)
67+
return AS > BS;
68+
return A.Symbol.getFilePath() < B.Symbol.getFilePath();
69+
});
6970
}
7071

7172
std::vector<find_all_symbols::SymbolInfo>

clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ void UpgradeGoogletestCaseCheck::registerMatchers(MatchFinder *Finder) {
196196
usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(TestCaseTypeAlias)))
197197
.bind("using"),
198198
this);
199+
Finder->addMatcher(
200+
typeLoc(loc(usingType(hasUnderlyingType(
201+
typedefType(hasDeclaration(TestCaseTypeAlias))))),
202+
unless(hasAncestor(decl(isImplicit()))), LocationFilter)
203+
.bind("typeloc"),
204+
this);
199205
}
200206

201207
static llvm::StringRef getNewMethodName(llvm::StringRef CurrentName) {

clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ AST_MATCHER_P(DeducedTemplateSpecializationType, refsToTemplatedDecl,
4545
return DeclMatcher.matches(*TD, Finder, Builder);
4646
return false;
4747
}
48+
4849
} // namespace
4950

5051
// A function that helps to tell whether a TargetDecl in a UsingDecl will be
@@ -60,13 +61,10 @@ static bool shouldCheckDecl(const Decl *TargetDecl) {
6061
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
6162
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
6263
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
63-
Finder->addMatcher(loc(enumType(DeclMatcher)), this);
64-
Finder->addMatcher(loc(recordType(DeclMatcher)), this);
6564
Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
6665
Finder->addMatcher(loc(deducedTemplateSpecializationType(
6766
refsToTemplatedDecl(namedDecl().bind("used")))),
6867
this);
69-
Finder->addMatcher(declRefExpr().bind("used"), this);
7068
Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))),
7169
this);
7270
Finder->addMatcher(
@@ -76,6 +74,12 @@ void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
7674
Finder->addMatcher(loc(templateSpecializationType(forEachTemplateArgument(
7775
templateArgument().bind("used")))),
7876
this);
77+
// Cases where we can identify the UsingShadowDecl directly, rather than
78+
// just its target.
79+
// FIXME: cover more cases in this way, as the AST supports it.
80+
auto ThroughShadowMatcher = throughUsingDecl(namedDecl().bind("usedShadow"));
81+
Finder->addMatcher(declRefExpr(ThroughShadowMatcher), this);
82+
Finder->addMatcher(loc(usingType(ThroughShadowMatcher)), this);
7983
}
8084

8185
void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
@@ -137,6 +141,12 @@ void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
137141
return;
138142
}
139143

144+
if (const auto *UsedShadow =
145+
Result.Nodes.getNodeAs<UsingShadowDecl>("usedShadow")) {
146+
removeFromFoundDecls(UsedShadow->getTargetDecl());
147+
return;
148+
}
149+
140150
if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) {
141151
if (Used->getKind() == TemplateArgument::Template) {
142152
if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl())

clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
7070
TL.getAs<TypedefTypeLoc>().getTypePtr()->getDecl()->getName()))
7171
return false;
7272
break;
73+
case TypeLoc::Using:
74+
if (visitUnqualName(TL.getAs<UsingTypeLoc>()
75+
.getTypePtr()
76+
->getFoundDecl()
77+
->getName()))
78+
return false;
79+
break;
7380
default:
7481
break;
7582
}

clang-tools-extra/clangd/FindTarget.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@ struct TargetFinder {
364364
Outer.add(ET->desugar(), Flags);
365365
}
366366

367+
void VisitUsingType(const UsingType *ET) {
368+
Outer.add(ET->getFoundDecl(), Flags);
369+
}
370+
367371
void VisitInjectedClassNameType(const InjectedClassNameType *ICNT) {
368372
Outer.add(ICNT->getDecl(), Flags);
369373
}
@@ -855,6 +859,13 @@ refInTypeLoc(TypeLoc L, const HeuristicResolver *Resolver) {
855859
}
856860
}
857861

862+
void VisitUsingTypeLoc(UsingTypeLoc L) {
863+
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
864+
L.getLocalSourceRange().getBegin(),
865+
/*IsDecl=*/false,
866+
{L.getFoundDecl()}});
867+
}
868+
858869
void VisitTagTypeLoc(TagTypeLoc L) {
859870
Refs.push_back(ReferenceLoc{NestedNameSpecifierLoc(),
860871
L.getNameLoc(),

clang-tools-extra/clangd/IncludeCleaner.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class ReferencedLocationCrawler
7474
return true;
7575
}
7676

77+
bool VisitUsingType(UsingType *UT) {
78+
add(UT->getFoundDecl());
79+
return true;
80+
}
81+
7782
bool VisitTypedefType(TypedefType *TT) {
7883
add(TT->getDecl());
7984
return true;

clang-tools-extra/clangd/XRefs.cpp

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,8 +1330,6 @@ void getOverriddenMethods(const CXXMethodDecl *CMD,
13301330

13311331
ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
13321332
const SymbolIndex *Index) {
1333-
if (!Limit)
1334-
Limit = std::numeric_limits<uint32_t>::max();
13351333
ReferencesResult Results;
13361334
const SourceManager &SM = AST.getSourceManager();
13371335
auto MainFilePath =
@@ -1347,7 +1345,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
13471345
return {};
13481346
}
13491347

1350-
llvm::DenseSet<SymbolID> IDs, OverriddenMethods;
1348+
llvm::DenseSet<SymbolID> IDsToQuery, OverriddenMethods;
13511349

13521350
const auto *IdentifierAtCursor =
13531351
syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens());
@@ -1372,7 +1370,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
13721370
Results.References.push_back(std::move(Result));
13731371
}
13741372
}
1375-
IDs.insert(MacroSID);
1373+
IDsToQuery.insert(MacroSID);
13761374
}
13771375
} else {
13781376
// Handle references to Decls.
@@ -1381,10 +1379,19 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
13811379
DeclRelation::TemplatePattern | DeclRelation::Alias;
13821380
std::vector<const NamedDecl *> Decls =
13831381
getDeclAtPosition(AST, *CurLoc, Relations);
1384-
llvm::DenseSet<SymbolID> Targets;
1385-
for (const NamedDecl *D : Decls)
1386-
if (auto ID = getSymbolID(D))
1387-
Targets.insert(ID);
1382+
llvm::DenseSet<SymbolID> TargetsInMainFile;
1383+
for (const NamedDecl *D : Decls) {
1384+
auto ID = getSymbolID(D);
1385+
if (!ID)
1386+
continue;
1387+
TargetsInMainFile.insert(ID);
1388+
// Not all symbols can be referenced from outside (e.g. function-locals).
1389+
// TODO: we could skip TU-scoped symbols here (e.g. static functions) if
1390+
// we know this file isn't a header. The details might be tricky.
1391+
if (D->getParentFunctionOrMethod())
1392+
continue;
1393+
IDsToQuery.insert(ID);
1394+
}
13881395

13891396
RelationsRequest OverriddenBy;
13901397
if (Index) {
@@ -1403,7 +1410,7 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
14031410
}
14041411

14051412
// We traverse the AST to find references in the main file.
1406-
auto MainFileRefs = findRefs(Targets, AST, /*PerToken=*/false);
1413+
auto MainFileRefs = findRefs(TargetsInMainFile, AST, /*PerToken=*/false);
14071414
// We may get multiple refs with the same location and different Roles, as
14081415
// cross-reference is only interested in locations, we deduplicate them
14091416
// by the location to avoid emitting duplicated locations.
@@ -1427,56 +1434,52 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
14271434
Results.References.push_back(std::move(Result));
14281435
}
14291436
// Add decl/def of overridding methods.
1430-
if (Index && Results.References.size() <= Limit &&
1431-
!OverriddenBy.Subjects.empty())
1432-
Index->relations(
1433-
OverriddenBy, [&](const SymbolID &Subject, const Symbol &Object) {
1434-
const auto LSPLocDecl =
1435-
toLSPLocation(Object.CanonicalDeclaration, *MainFilePath);
1436-
const auto LSPLocDef =
1437-
toLSPLocation(Object.Definition, *MainFilePath);
1438-
if (LSPLocDecl && LSPLocDecl != LSPLocDef) {
1439-
ReferencesResult::Reference Result;
1440-
Result.Loc = std::move(*LSPLocDecl);
1441-
Result.Attributes =
1442-
ReferencesResult::Declaration | ReferencesResult::Override;
1443-
Results.References.push_back(std::move(Result));
1444-
}
1445-
if (LSPLocDef) {
1446-
ReferencesResult::Reference Result;
1447-
Result.Loc = std::move(*LSPLocDef);
1448-
Result.Attributes = ReferencesResult::Declaration |
1449-
ReferencesResult::Definition |
1450-
ReferencesResult::Override;
1451-
Results.References.push_back(std::move(Result));
1452-
}
1453-
});
1454-
1455-
if (Index && Results.References.size() <= Limit) {
1456-
for (const Decl *D : Decls) {
1457-
// Not all symbols can be referenced from outside (e.g.
1458-
// function-locals).
1459-
// TODO: we could skip TU-scoped symbols here (e.g. static functions) if
1460-
// we know this file isn't a header. The details might be tricky.
1461-
if (D->getParentFunctionOrMethod())
1462-
continue;
1463-
if (auto ID = getSymbolID(D))
1464-
IDs.insert(ID);
1465-
}
1437+
if (Index && !OverriddenBy.Subjects.empty()) {
1438+
Index->relations(OverriddenBy, [&](const SymbolID &Subject,
1439+
const Symbol &Object) {
1440+
if (Limit && Results.References.size() >= Limit) {
1441+
Results.HasMore = true;
1442+
return;
1443+
}
1444+
const auto LSPLocDecl =
1445+
toLSPLocation(Object.CanonicalDeclaration, *MainFilePath);
1446+
const auto LSPLocDef = toLSPLocation(Object.Definition, *MainFilePath);
1447+
if (LSPLocDecl && LSPLocDecl != LSPLocDef) {
1448+
ReferencesResult::Reference Result;
1449+
Result.Loc = std::move(*LSPLocDecl);
1450+
Result.Attributes =
1451+
ReferencesResult::Declaration | ReferencesResult::Override;
1452+
Results.References.push_back(std::move(Result));
1453+
}
1454+
if (LSPLocDef) {
1455+
ReferencesResult::Reference Result;
1456+
Result.Loc = std::move(*LSPLocDef);
1457+
Result.Attributes = ReferencesResult::Declaration |
1458+
ReferencesResult::Definition |
1459+
ReferencesResult::Override;
1460+
Results.References.push_back(std::move(Result));
1461+
}
1462+
});
14661463
}
14671464
}
14681465
// Now query the index for references from other files.
14691466
auto QueryIndex = [&](llvm::DenseSet<SymbolID> IDs, bool AllowAttributes,
14701467
bool AllowMainFileSymbols) {
1468+
if (IDs.empty() || !Index || Results.HasMore)
1469+
return;
14711470
RefsRequest Req;
14721471
Req.IDs = std::move(IDs);
1473-
Req.Limit = Limit;
1474-
if (Req.IDs.empty() || !Index || Results.References.size() > Limit)
1475-
return;
1472+
if (Limit) {
1473+
if (Limit < Results.References.size()) {
1474+
// We've already filled our quota, still check the index to correctly
1475+
// return the `HasMore` info.
1476+
Req.Limit = 0;
1477+
} else {
1478+
// Query index only for the remaining size.
1479+
Req.Limit = Limit - Results.References.size();
1480+
}
1481+
}
14761482
Results.HasMore |= Index->refs(Req, [&](const Ref &R) {
1477-
// No need to continue process if we reach the limit.
1478-
if (Results.References.size() > Limit)
1479-
return;
14801483
auto LSPLoc = toLSPLocation(R.Location, *MainFilePath);
14811484
// Avoid indexed results for the main file - the AST is authoritative.
14821485
if (!LSPLoc ||
@@ -1495,17 +1498,13 @@ ReferencesResult findReferences(ParsedAST &AST, Position Pos, uint32_t Limit,
14951498
Results.References.push_back(std::move(Result));
14961499
});
14971500
};
1498-
QueryIndex(std::move(IDs), /*AllowAttributes=*/true,
1501+
QueryIndex(std::move(IDsToQuery), /*AllowAttributes=*/true,
14991502
/*AllowMainFileSymbols=*/false);
15001503
// For a virtual method: Occurrences of BaseMethod should be treated as refs
15011504
// and not as decl/def. Allow symbols from main file since AST does not report
15021505
// these.
15031506
QueryIndex(std::move(OverriddenMethods), /*AllowAttributes=*/false,
15041507
/*AllowMainFileSymbols=*/true);
1505-
if (Results.References.size() > Limit) {
1506-
Results.HasMore = true;
1507-
Results.References.resize(Limit);
1508-
}
15091508
return Results;
15101509
}
15111510

clang-tools-extra/clangd/refactor/Rename.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ std::string toString(InvalidName::Kind K) {
455455
}
456456

457457
llvm::Error makeError(InvalidName Reason) {
458-
auto Message = [](InvalidName Reason) {
458+
auto Message = [](const InvalidName &Reason) {
459459
switch (Reason.K) {
460460
case InvalidName::Keywords:
461461
return llvm::formatv("the chosen name \"{0}\" is a keyword",
@@ -733,7 +733,7 @@ llvm::Expected<RenameResult> rename(const RenameInputs &RInputs) {
733733
return makeError(ReasonToReject::SameName);
734734
auto Invalid = checkName(RenameDecl, RInputs.NewName);
735735
if (Invalid)
736-
return makeError(*Invalid);
736+
return makeError(std::move(*Invalid));
737737

738738
auto Reject = renameable(RenameDecl, RInputs.MainFilePath, RInputs.Index);
739739
if (Reject)

clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ TEST(IncludeCleaner, ReferencedLocations) {
8585
"struct Foo; struct ^Foo{}; typedef Foo ^Bar;",
8686
"Bar b;",
8787
},
88+
{
89+
"namespace ns { class X; }; using ns::^X;",
90+
"X *y;",
91+
},
8892
// MemberExpr
8993
{
9094
"struct ^X{int ^a;}; X ^foo();",
@@ -198,14 +202,6 @@ TEST(IncludeCleaner, ReferencedLocations) {
198202
{
199203
"enum class ^Color : char {};",
200204
"Color *c;",
201-
},
202-
{
203-
// When a type is resolved via a using declaration, the
204-
// UsingShadowDecl is not referenced in the AST.
205-
// Compare to TypedefType, or DeclRefExpr::getFoundDecl().
206-
// ^
207-
"namespace ns { class ^X; }; using ns::X;",
208-
"X *y;",
209205
}};
210206
for (const TestCase &T : Cases) {
211207
TestTU TU;

clang-tools-extra/clangd/unittests/RenameTests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,11 @@ TEST(RenameTest, Renameable) {
10601060
)cpp",
10611061
"conflict", !HeaderFile, "Conflict"},
10621062

1063+
{R"cpp(
1064+
int V^ar;
1065+
)cpp",
1066+
"\"const\" is a keyword", !HeaderFile, "const"},
1067+
10631068
{R"cpp(// Trying to rename into the same name, SameName == SameName.
10641069
void func() {
10651070
int S^ameName;

0 commit comments

Comments
 (0)