Skip to content

Commit 07134c3

Browse files
committed
[PATCH 4/6] [clang] Improve nested name specifier AST representation
clang tools extra changes
1 parent fb1fdfd commit 07134c3

Some content is hidden

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

45 files changed

+289
-334
lines changed

clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllSymbols.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ void FindAllSymbols::registerMatchers(MatchFinder *MatchFinder) {
216216
// Uses of most types: just look at what the typeLoc refers to.
217217
MatchFinder->addMatcher(
218218
typeLoc(isExpansionInMainFile(),
219-
loc(qualType(allOf(unless(elaboratedType()),
220-
hasDeclaration(Types.bind("use")))))),
219+
loc(qualType(hasDeclaration(Types.bind("use"))))),
221220
this);
222221
// Uses of typedefs: these are often transparent to hasDeclaration, so we need
223222
// to handle them explicitly.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ static bool isDerivedClassBefriended(const CXXRecordDecl *CRTP,
4343
return false;
4444
}
4545

46-
return FriendType->getType()->getAsCXXRecordDecl() == Derived;
46+
return declaresSameEntity(FriendType->getType()->getAsCXXRecordDecl(),
47+
Derived);
4748
});
4849
}
4950

@@ -55,7 +56,8 @@ getDerivedParameter(const ClassTemplateSpecializationDecl *CRTP,
5556
CRTP->getTemplateArgs().asArray(), [&](const TemplateArgument &Arg) {
5657
++Idx;
5758
return Arg.getKind() == TemplateArgument::Type &&
58-
Arg.getAsType()->getAsCXXRecordDecl() == Derived;
59+
declaresSameEntity(Arg.getAsType()->getAsCXXRecordDecl(),
60+
Derived);
5961
});
6062

6163
return AnyOf ? CRTP->getSpecializedTemplate()

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ approximateImplicitConversion(const TheCheck &Check, QualType LType,
577577
ImplicitConversionModellingMode ImplicitMode);
578578

579579
static inline bool isUselessSugar(const Type *T) {
580-
return isa<AttributedType, DecayedType, ElaboratedType, ParenType>(T);
580+
return isa<AttributedType, DecayedType, ParenType>(T);
581581
}
582582

583583
namespace {
@@ -1040,7 +1040,9 @@ approximateStandardConversionSequence(const TheCheck &Check, QualType From,
10401040
const auto *ToRecord = To->getAsCXXRecordDecl();
10411041
if (isDerivedToBase(FromRecord, ToRecord)) {
10421042
LLVM_DEBUG(llvm::dbgs() << "--- approximateStdConv. Derived To Base.\n");
1043-
WorkType = QualType{ToRecord->getTypeForDecl(), FastQualifiersToApply};
1043+
WorkType = QualType{
1044+
ToRecord->getASTContext().getCanonicalTagType(ToRecord)->getTypePtr(),
1045+
FastQualifiersToApply};
10441046
}
10451047

10461048
if (Ctx.getLangOpts().CPlusPlus17 && FromPtr && ToPtr) {
@@ -1072,9 +1074,9 @@ approximateStandardConversionSequence(const TheCheck &Check, QualType From,
10721074
WorkType = To;
10731075
}
10741076

1075-
if (WorkType == To) {
1077+
if (Ctx.hasSameType(WorkType, To)) {
10761078
LLVM_DEBUG(llvm::dbgs() << "<<< approximateStdConv. Reached 'To' type.\n");
1077-
return {WorkType};
1079+
return {Ctx.getCommonSugaredType(WorkType, To)};
10781080
}
10791081

10801082
LLVM_DEBUG(llvm::dbgs() << "<<< approximateStdConv. Did not reach 'To'.\n");
@@ -1219,7 +1221,7 @@ tryConversionOperators(const TheCheck &Check, const CXXRecordDecl *RD,
12191221

12201222
if (std::optional<UserDefinedConversionSelector::PreparedConversion>
12211223
SelectedConversion = ConversionSet()) {
1222-
QualType RecordType{RD->getTypeForDecl(), 0};
1224+
CanQualType RecordType = RD->getASTContext().getCanonicalTagType(RD);
12231225

12241226
ConversionSequence Result{RecordType, ToType};
12251227
// The conversion from the operator call's return type to ToType was
@@ -1270,7 +1272,7 @@ tryConvertingConstructors(const TheCheck &Check, QualType FromType,
12701272

12711273
if (std::optional<UserDefinedConversionSelector::PreparedConversion>
12721274
SelectedConversion = ConversionSet()) {
1273-
QualType RecordType{RD->getTypeForDecl(), 0};
1275+
CanQualType RecordType = RD->getASTContext().getCanonicalTagType(RD);
12741276

12751277
ConversionSequence Result{FromType, RecordType};
12761278
Result.AfterFirstStandard = SelectedConversion->Seq.AfterFirstStandard;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ void ForwardDeclarationNamespaceCheck::check(
6969
// struct B { friend A; };
7070
// \endcode
7171
// `A` will not be marked as "referenced" in the AST.
72-
if (const TypeSourceInfo *Tsi = Decl->getFriendType()) {
73-
QualType Desugared = Tsi->getType().getDesugaredType(*Result.Context);
74-
FriendTypes.insert(Desugared.getTypePtr());
75-
}
72+
if (const TypeSourceInfo *Tsi = Decl->getFriendType())
73+
FriendTypes.insert(
74+
Tsi->getType()->getCanonicalTypeUnqualified().getTypePtr());
7675
}
7776
}
7877

@@ -119,7 +118,9 @@ void ForwardDeclarationNamespaceCheck::onEndOfTranslationUnit() {
119118
if (CurDecl->hasDefinition() || CurDecl->isReferenced()) {
120119
continue; // Skip forward declarations that are used/referenced.
121120
}
122-
if (FriendTypes.contains(CurDecl->getTypeForDecl())) {
121+
if (FriendTypes.contains(CurDecl->getASTContext()
122+
.getCanonicalTagType(CurDecl)
123+
->getTypePtr())) {
123124
continue; // Skip forward declarations referenced as friend.
124125
}
125126
if (CurDecl->getLocation().isMacroID() ||

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,22 @@ AST_MATCHER_P(TemplateTypeParmDecl, hasUnnamedDefaultArgument,
3232
void IncorrectEnableIfCheck::registerMatchers(MatchFinder *Finder) {
3333
Finder->addMatcher(
3434
templateTypeParmDecl(
35-
hasUnnamedDefaultArgument(
36-
elaboratedTypeLoc(
37-
hasNamedTypeLoc(templateSpecializationTypeLoc(
38-
loc(qualType(hasDeclaration(namedDecl(
39-
hasName("::std::enable_if"))))))
40-
.bind("enable_if_specialization")))
41-
.bind("elaborated")))
35+
hasUnnamedDefaultArgument(templateSpecializationTypeLoc(
36+
loc(qualType(hasDeclaration(namedDecl(
37+
hasName("::std::enable_if"))))))
38+
.bind("enable_if_specialization")))
4239
.bind("enable_if"),
4340
this);
4441
}
4542

4643
void IncorrectEnableIfCheck::check(const MatchFinder::MatchResult &Result) {
4744
const auto *EnableIf =
4845
Result.Nodes.getNodeAs<TemplateTypeParmDecl>("enable_if");
49-
const auto *ElaboratedLoc =
50-
Result.Nodes.getNodeAs<ElaboratedTypeLoc>("elaborated");
5146
const auto *EnableIfSpecializationLoc =
5247
Result.Nodes.getNodeAs<TemplateSpecializationTypeLoc>(
5348
"enable_if_specialization");
5449

55-
if (!EnableIf || !ElaboratedLoc || !EnableIfSpecializationLoc)
50+
if (!EnableIf || !EnableIfSpecializationLoc)
5651
return;
5752

5853
const SourceManager &SM = *Result.SourceManager;
@@ -62,8 +57,10 @@ void IncorrectEnableIfCheck::check(const MatchFinder::MatchResult &Result) {
6257
auto Diag = diag(EnableIf->getBeginLoc(),
6358
"incorrect std::enable_if usage detected; use "
6459
"'typename std::enable_if<...>::type'");
60+
// FIXME: This should handle the enable_if specialization already having an
61+
// elaborated keyword.
6562
if (!getLangOpts().CPlusPlus20) {
66-
Diag << FixItHint::CreateInsertion(ElaboratedLoc->getBeginLoc(),
63+
Diag << FixItHint::CreateInsertion(EnableIfSpecializationLoc->getBeginLoc(),
6764
"typename ");
6865
}
6966
Diag << FixItHint::CreateInsertion(RAngleLoc.getLocWithOffset(1), "::type");

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
425425
"suspicious usage of 'sizeof(array)/sizeof(...)';"
426426
" denominator differs from the size of array elements")
427427
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
428-
} else if (NumTy && DenomTy && NumTy == DenomTy &&
428+
} else if (NumTy && DenomTy && Ctx.hasSameType(NumTy, DenomTy) &&
429429
!NumTy->isDependentType()) {
430430
// Dependent type should not be compared.
431431
diag(E->getOperatorLoc(),
@@ -434,7 +434,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
434434
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
435435
} else if (!WarnOnSizeOfPointer) {
436436
// When 'WarnOnSizeOfPointer' is enabled, these messages become redundant:
437-
if (PointedTy && DenomTy && PointedTy == DenomTy) {
437+
if (PointedTy && DenomTy && Ctx.hasSameType(PointedTy, DenomTy)) {
438438
diag(E->getOperatorLoc(),
439439
"suspicious usage of 'sizeof(...)/sizeof(...)'; size of pointer "
440440
"is divided by size of pointed type")
@@ -463,7 +463,8 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
463463
const auto *SizeOfExpr =
464464
Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("sizeof-ptr-mul-expr");
465465

466-
if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
466+
if (Ctx.hasSameType(LPtrTy, RPtrTy) &&
467+
Ctx.hasSameType(LPtrTy, SizeofArgTy)) {
467468
diag(SizeOfExpr->getBeginLoc(), "suspicious usage of 'sizeof(...)' in "
468469
"pointer arithmetic")
469470
<< SizeOfExpr->getSourceRange() << E->getOperatorLoc()
@@ -477,7 +478,8 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
477478
const auto *SizeOfExpr =
478479
Result.Nodes.getNodeAs<UnaryExprOrTypeTraitExpr>("sizeof-ptr-div-expr");
479480

480-
if ((LPtrTy == RPtrTy) && (LPtrTy == SizeofArgTy)) {
481+
if (Ctx.hasSameType(LPtrTy, RPtrTy) &&
482+
Ctx.hasSameType(LPtrTy, SizeofArgTy)) {
481483
diag(SizeOfExpr->getBeginLoc(), "suspicious usage of 'sizeof(...)' in "
482484
"pointer arithmetic")
483485
<< SizeOfExpr->getSourceRange() << E->getOperatorLoc()

clang-tools-extra/clang-tidy/cppcoreguidelines/NoSuspendWithLockCheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ void NoSuspendWithLockCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
2323
}
2424

2525
void NoSuspendWithLockCheck::registerMatchers(MatchFinder *Finder) {
26-
auto LockType = elaboratedType(namesType(templateSpecializationType(
26+
auto LockType = templateSpecializationType(
2727
hasDeclaration(namedDecl(matchers::matchesAnyListedName(
28-
utils::options::parseStringList(LockGuards)))))));
28+
utils::options::parseStringList(LockGuards)))));
2929

3030
StatementMatcher Lock =
3131
declStmt(has(varDecl(hasType(LockType)).bind("lock-decl")))

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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,30 @@ static StringRef getDestTypeString(const SourceManager &SM,
8989
SM, LangOpts);
9090
}
9191

92+
static bool sameTypeAsWritten(QualType X, QualType Y) {
93+
if (X.getCanonicalType() != Y.getCanonicalType())
94+
return false;
95+
96+
auto TC = X->getTypeClass();
97+
if (TC != Y->getTypeClass())
98+
return false;
99+
100+
switch (TC) {
101+
case Type::Typedef:
102+
return declaresSameEntity(cast<TypedefType>(X)->getDecl(),
103+
cast<TypedefType>(Y)->getDecl());
104+
case Type::Pointer:
105+
return sameTypeAsWritten(cast<PointerType>(X)->getPointeeType(),
106+
cast<PointerType>(Y)->getPointeeType());
107+
case Type::RValueReference:
108+
case Type::LValueReference:
109+
return sameTypeAsWritten(cast<ReferenceType>(X)->getPointeeType(),
110+
cast<ReferenceType>(Y)->getPointeeType());
111+
default:
112+
return true;
113+
}
114+
}
115+
92116
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
93117
const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>("cast");
94118

@@ -128,12 +152,7 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
128152
// case of overloaded functions, so detection of redundant casts is trickier
129153
// in this case. Don't emit "redundant cast" warnings for function
130154
// pointer/reference types.
131-
QualType Src = SourceTypeAsWritten, Dst = DestTypeAsWritten;
132-
if (const auto *ElTy = dyn_cast<ElaboratedType>(Src))
133-
Src = ElTy->getNamedType();
134-
if (const auto *ElTy = dyn_cast<ElaboratedType>(Dst))
135-
Dst = ElTy->getNamedType();
136-
if (Src == Dst) {
155+
if (sameTypeAsWritten(SourceTypeAsWritten, DestTypeAsWritten)) {
137156
diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
138157
<< FixItHint::CreateRemoval(ReplaceRange);
139158
return;

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,13 @@ getAliasNameRange(const MatchFinder::MatchResult &Result) {
257257
return CharSourceRange::getTokenRange(
258258
Using->getNameInfo().getSourceRange());
259259
}
260-
return CharSourceRange::getTokenRange(
261-
Result.Nodes.getNodeAs<TypeLoc>("typeloc")->getSourceRange());
260+
TypeLoc TL = *Result.Nodes.getNodeAs<TypeLoc>("typeloc");
261+
if (auto QTL = TL.getAs<QualifiedTypeLoc>())
262+
TL = QTL.getUnqualifiedLoc();
263+
264+
if (auto TTL = TL.getAs<TypedefTypeLoc>())
265+
return CharSourceRange::getTokenRange(TTL.getNameLoc());
266+
return CharSourceRange::getTokenRange(TL.castAs<UsingTypeLoc>().getNameLoc());
262267
}
263268

264269
void UpgradeGoogletestCaseCheck::check(const MatchFinder::MatchResult &Result) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder *Finder) {
9898
hasType(referenceType(pointee(hasCanonicalType(templateTypeParmType())))),
9999
hasType(referenceType(pointee(substTemplateTypeParmType()))));
100100

101-
const auto AllowedType = hasType(qualType(anyOf(
102-
hasDeclaration(namedDecl(matchers::matchesAnyListedName(AllowedTypes))),
103-
references(namedDecl(matchers::matchesAnyListedName(AllowedTypes))),
104-
pointerType(pointee(hasDeclaration(
105-
namedDecl(matchers::matchesAnyListedName(AllowedTypes))))))));
101+
auto AllowedTypeDecl = namedDecl(
102+
anyOf(matchers::matchesAnyListedName(AllowedTypes), usingShadowDecl()));
103+
104+
const auto AllowedType = hasType(qualType(
105+
anyOf(hasDeclaration(AllowedTypeDecl), references(AllowedTypeDecl),
106+
pointerType(pointee(hasDeclaration(AllowedTypeDecl))))));
106107

107108
const auto AutoTemplateType = varDecl(
108109
anyOf(hasType(autoType()), hasType(referenceType(pointee(autoType()))),

0 commit comments

Comments
 (0)