Skip to content

Commit bbb53fb

Browse files
committed
Revert "[clang] Improve nested name specifier AST representation (llvm#147835)"
needs dependent PR [clang][CGRecordLayout] Remove dependency on isZeroSize (llvm#96422) This reverts commit 91cdd35.
1 parent 461fcbd commit bbb53fb

File tree

539 files changed

+10585
-11229
lines changed

Some content is hidden

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

539 files changed

+10585
-11229
lines changed

clang-tools-extra/clang-change-namespace/ChangeNamespace.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,24 @@ llvm::SmallVector<llvm::StringRef, 4> splitSymbolName(llvm::StringRef Name) {
3131
return Splitted;
3232
}
3333

34+
SourceLocation startLocationForType(TypeLoc TLoc) {
35+
// For elaborated types (e.g. `struct a::A`) we want the portion after the
36+
// `struct` but including the namespace qualifier, `a::`.
37+
if (TLoc.getTypeLocClass() == TypeLoc::Elaborated) {
38+
NestedNameSpecifierLoc NestedNameSpecifier =
39+
TLoc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
40+
if (NestedNameSpecifier.getNestedNameSpecifier())
41+
return NestedNameSpecifier.getBeginLoc();
42+
TLoc = TLoc.getNextTypeLoc();
43+
}
44+
return TLoc.getBeginLoc();
45+
}
46+
3447
SourceLocation endLocationForType(TypeLoc TLoc) {
35-
if (auto QTL = TLoc.getAs<QualifiedTypeLoc>())
36-
TLoc = QTL.getUnqualifiedLoc();
48+
// Dig past any namespace or keyword qualifications.
49+
while (TLoc.getTypeLocClass() == TypeLoc::Elaborated ||
50+
TLoc.getTypeLocClass() == TypeLoc::Qualified)
51+
TLoc = TLoc.getNextTypeLoc();
3752

3853
// The location for template specializations (e.g. Foo<int>) includes the
3954
// templated types in its location range. We want to restrict this to just
@@ -535,8 +550,8 @@ void ChangeNamespaceTool::run(
535550
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
536551
"nested_specifier_loc")) {
537552
SourceLocation Start = Specifier->getBeginLoc();
538-
SourceLocation End = endLocationForType(Specifier->castAsTypeLoc());
539-
fixTypeLoc(Result, Start, End, Specifier->castAsTypeLoc());
553+
SourceLocation End = endLocationForType(Specifier->getTypeLoc());
554+
fixTypeLoc(Result, Start, End, Specifier->getTypeLoc());
540555
} else if (const auto *BaseInitializer =
541556
Result.Nodes.getNodeAs<CXXCtorInitializer>(
542557
"base_initializer")) {
@@ -547,16 +562,19 @@ void ChangeNamespaceTool::run(
547562
// filtered by matchers in some cases, e.g. the type is templated. We should
548563
// handle the record type qualifier instead.
549564
TypeLoc Loc = *TLoc;
550-
if (auto QTL = Loc.getAs<QualifiedTypeLoc>())
551-
Loc = QTL.getUnqualifiedLoc();
552-
// FIXME: avoid changing injected class names.
553-
if (NestedNameSpecifier NestedNameSpecifier =
554-
Loc.getPrefix().getNestedNameSpecifier();
555-
NestedNameSpecifier.getKind() == NestedNameSpecifier::Kind::Type &&
556-
NestedNameSpecifier.getAsType()->isRecordType())
557-
return;
558-
fixTypeLoc(Result, Loc.getNonElaboratedBeginLoc(), endLocationForType(Loc),
559-
Loc);
565+
while (Loc.getTypeLocClass() == TypeLoc::Qualified)
566+
Loc = Loc.getNextTypeLoc();
567+
if (Loc.getTypeLocClass() == TypeLoc::Elaborated) {
568+
NestedNameSpecifierLoc NestedNameSpecifier =
569+
Loc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
570+
// FIXME: avoid changing injected class names.
571+
if (auto *NNS = NestedNameSpecifier.getNestedNameSpecifier()) {
572+
const Type *SpecifierType = NNS->getAsType();
573+
if (SpecifierType && SpecifierType->isRecordType())
574+
return;
575+
}
576+
}
577+
fixTypeLoc(Result, startLocationForType(Loc), endLocationForType(Loc), Loc);
560578
} else if (const auto *VarRef =
561579
Result.Nodes.getNodeAs<DeclRefExpr>("var_ref")) {
562580
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var_decl");
@@ -570,9 +588,10 @@ void ChangeNamespaceTool::run(
570588
} else if (const auto *EnumConstRef =
571589
Result.Nodes.getNodeAs<DeclRefExpr>("enum_const_ref")) {
572590
// Do not rename the reference if it is already scoped by the EnumDecl name.
573-
if (NestedNameSpecifier Qualifier = EnumConstRef->getQualifier();
574-
Qualifier.getKind() == NestedNameSpecifier::Kind::Type &&
575-
Qualifier.getAsType()->isEnumeralType())
591+
if (EnumConstRef->hasQualifier() &&
592+
EnumConstRef->getQualifier()->getKind() ==
593+
NestedNameSpecifier::SpecifierKind::TypeSpec &&
594+
EnumConstRef->getQualifier()->getAsType()->isEnumeralType())
576595
return;
577596
const auto *EnumConstDecl =
578597
Result.Nodes.getNodeAs<EnumConstantDecl>("enum_const_decl");

clang-tools-extra/clang-doc/Serialize.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,8 @@ parseBases(RecordInfo &I, const CXXRecordDecl *D, bool IsFileInRootDir,
902902
return;
903903
for (const CXXBaseSpecifier &B : D->bases()) {
904904
if (const RecordType *Ty = B.getType()->getAs<RecordType>()) {
905-
if (const CXXRecordDecl *Base = cast_or_null<CXXRecordDecl>(
906-
Ty->getOriginalDecl()->getDefinition())) {
905+
if (const CXXRecordDecl *Base =
906+
cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition())) {
907907
// Initialized without USR and name, this will be set in the following
908908
// if-else stmt.
909909
BaseRecordInfo BI(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ 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(hasDeclaration(Types.bind("use"))))),
219+
loc(qualType(allOf(unless(elaboratedType()),
220+
hasDeclaration(Types.bind("use")))))),
220221
this);
221222
// Uses of typedefs: these are often transparent to hasDeclaration, so we need
222223
// to handle them explicitly.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,7 @@ void ClangTidyDiagnosticConsumer::forwardDiagnostic(const Diagnostic &Info) {
533533
Builder << reinterpret_cast<const NamedDecl *>(Info.getRawArg(Index));
534534
break;
535535
case clang::DiagnosticsEngine::ak_nestednamespec:
536-
Builder << NestedNameSpecifier::getFromVoidPointer(
537-
reinterpret_cast<void *>(Info.getRawArg(Index)));
536+
Builder << reinterpret_cast<NestedNameSpecifier *>(Info.getRawArg(Index));
538537
break;
539538
case clang::DiagnosticsEngine::ak_declcontext:
540539
Builder << reinterpret_cast<DeclContext *>(Info.getRawArg(Index));

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

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

46-
return declaresSameEntity(FriendType->getType()->getAsCXXRecordDecl(),
47-
Derived);
46+
return FriendType->getType()->getAsCXXRecordDecl() == Derived;
4847
});
4948
}
5049

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

6361
return AnyOf ? CRTP->getSpecializedTemplate()

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

Lines changed: 6 additions & 8 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, ParenType>(T);
580+
return isa<AttributedType, DecayedType, ElaboratedType, ParenType>(T);
581581
}
582582

583583
namespace {
@@ -1040,9 +1040,7 @@ 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{
1044-
ToRecord->getASTContext().getCanonicalTagType(ToRecord)->getTypePtr(),
1045-
FastQualifiersToApply};
1043+
WorkType = QualType{ToRecord->getTypeForDecl(), FastQualifiersToApply};
10461044
}
10471045

10481046
if (Ctx.getLangOpts().CPlusPlus17 && FromPtr && ToPtr) {
@@ -1074,9 +1072,9 @@ approximateStandardConversionSequence(const TheCheck &Check, QualType From,
10741072
WorkType = To;
10751073
}
10761074

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

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

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

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

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

12771275
ConversionSequence Result{FromType, RecordType};
12781276
Result.AfterFirstStandard = SelectedConversion->Seq.AfterFirstStandard;

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ 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-
FriendTypes.insert(
74-
Tsi->getType()->getCanonicalTypeUnqualified().getTypePtr());
72+
if (const TypeSourceInfo *Tsi = Decl->getFriendType()) {
73+
QualType Desugared = Tsi->getType().getDesugaredType(*Result.Context);
74+
FriendTypes.insert(Desugared.getTypePtr());
75+
}
7576
}
7677
}
7778

@@ -118,9 +119,7 @@ void ForwardDeclarationNamespaceCheck::onEndOfTranslationUnit() {
118119
if (CurDecl->hasDefinition() || CurDecl->isReferenced()) {
119120
continue; // Skip forward declarations that are used/referenced.
120121
}
121-
if (FriendTypes.contains(CurDecl->getASTContext()
122-
.getCanonicalTagType(CurDecl)
123-
->getTypePtr())) {
122+
if (FriendTypes.contains(CurDecl->getTypeForDecl())) {
124123
continue; // Skip forward declarations referenced as friend.
125124
}
126125
if (CurDecl->getLocation().isMacroID() ||

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,21 @@ AST_MATCHER(QualType, isEnableIf) {
3333
BaseType = BaseType->getPointeeType().getTypePtr();
3434
}
3535
// Case: type parameter dependent (enable_if<is_integral<T>>).
36-
if (const auto *Dependent = BaseType->getAs<DependentNameType>())
37-
BaseType = Dependent->getQualifier().getAsType();
36+
if (const auto *Dependent = BaseType->getAs<DependentNameType>()) {
37+
BaseType = Dependent->getQualifier()->getAsType();
38+
}
3839
if (!BaseType)
3940
return false;
4041
if (CheckTemplate(BaseType->getAs<TemplateSpecializationType>()))
4142
return true; // Case: enable_if_t< >.
42-
if (const auto *TT = BaseType->getAs<TypedefType>())
43-
if (NestedNameSpecifier Q = TT->getQualifier();
44-
Q.getKind() == NestedNameSpecifier::Kind::Type)
45-
if (CheckTemplate(Q.getAsType()->getAs<TemplateSpecializationType>()))
46-
return true; // Case: enable_if< >::type.
43+
if (const auto *Elaborated = BaseType->getAs<ElaboratedType>()) {
44+
if (const auto *Q = Elaborated->getQualifier())
45+
if (const auto *Qualifier = Q->getAsType()) {
46+
if (CheckTemplate(Qualifier->getAs<TemplateSpecializationType>())) {
47+
return true; // Case: enable_if< >::type.
48+
}
49+
}
50+
}
4751
return false;
4852
}
4953
AST_MATCHER_P(TemplateTypeParmDecl, hasDefaultArgument,

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ AST_MATCHER_P(TemplateTypeParmDecl, hasUnnamedDefaultArgument,
3232
void IncorrectEnableIfCheck::registerMatchers(MatchFinder *Finder) {
3333
Finder->addMatcher(
3434
templateTypeParmDecl(
35-
hasUnnamedDefaultArgument(templateSpecializationTypeLoc(
36-
loc(qualType(hasDeclaration(namedDecl(
37-
hasName("::std::enable_if"))))))
38-
.bind("enable_if_specialization")))
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")))
3942
.bind("enable_if"),
4043
this);
4144
}
4245

4346
void IncorrectEnableIfCheck::check(const MatchFinder::MatchResult &Result) {
4447
const auto *EnableIf =
4548
Result.Nodes.getNodeAs<TemplateTypeParmDecl>("enable_if");
49+
const auto *ElaboratedLoc =
50+
Result.Nodes.getNodeAs<ElaboratedTypeLoc>("elaborated");
4651
const auto *EnableIfSpecializationLoc =
4752
Result.Nodes.getNodeAs<TemplateSpecializationTypeLoc>(
4853
"enable_if_specialization");
4954

50-
if (!EnableIf || !EnableIfSpecializationLoc)
55+
if (!EnableIf || !ElaboratedLoc || !EnableIfSpecializationLoc)
5156
return;
5257

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ class FindEnumMember : public TypeVisitor<FindEnumMember, bool> {
6767
return Visit(T->getElementType().getTypePtr());
6868
}
6969
bool VisitEnumType(const EnumType *T) {
70-
if (isCompleteAndHasNoZeroValue(T->getOriginalDecl())) {
70+
if (isCompleteAndHasNoZeroValue(T->getDecl())) {
7171
FoundEnum = T;
7272
return true;
7373
}
7474
return false;
7575
}
7676
bool VisitRecordType(const RecordType *T) {
77-
const RecordDecl *RD = T->getOriginalDecl()->getDefinition();
78-
if (!RD || RD->isUnion())
77+
const RecordDecl *RD = T->getDecl();
78+
if (RD->isUnion())
7979
return false;
8080
auto VisitField = [this](const FieldDecl *F) {
8181
return Visit(F->getType().getTypePtr());
@@ -125,7 +125,7 @@ void InvalidEnumDefaultInitializationCheck::check(
125125
if (!Finder.Visit(InitList->getArrayFiller()->getType().getTypePtr()))
126126
return;
127127
InitExpr = InitList;
128-
Enum = Finder.FoundEnum->getOriginalDecl();
128+
Enum = Finder.FoundEnum->getDecl();
129129
}
130130

131131
if (!InitExpr || !Enum)

0 commit comments

Comments
 (0)