Skip to content

Commit 1bcf9e8

Browse files
authored
[PATCH 5/7] [clang] NNS improvement: getOriginalDecl changes (#149747)
1 parent 209c92e commit 1bcf9e8

File tree

269 files changed

+4841
-4228
lines changed

Some content is hidden

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

269 files changed

+4841
-4228
lines changed

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

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,9 @@ 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-
4734
SourceLocation endLocationForType(TypeLoc TLoc) {
48-
// Dig past any namespace or keyword qualifications.
49-
while (TLoc.getTypeLocClass() == TypeLoc::Elaborated ||
50-
TLoc.getTypeLocClass() == TypeLoc::Qualified)
51-
TLoc = TLoc.getNextTypeLoc();
35+
if (auto QTL = TLoc.getAs<QualifiedTypeLoc>())
36+
TLoc = QTL.getUnqualifiedLoc();
5237

5338
// The location for template specializations (e.g. Foo<int>) includes the
5439
// templated types in its location range. We want to restrict this to just
@@ -550,8 +535,8 @@ void ChangeNamespaceTool::run(
550535
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
551536
"nested_specifier_loc")) {
552537
SourceLocation Start = Specifier->getBeginLoc();
553-
SourceLocation End = endLocationForType(Specifier->getTypeLoc());
554-
fixTypeLoc(Result, Start, End, Specifier->getTypeLoc());
538+
SourceLocation End = endLocationForType(Specifier->castAsTypeLoc());
539+
fixTypeLoc(Result, Start, End, Specifier->castAsTypeLoc());
555540
} else if (const auto *BaseInitializer =
556541
Result.Nodes.getNodeAs<CXXCtorInitializer>(
557542
"base_initializer")) {
@@ -562,19 +547,16 @@ void ChangeNamespaceTool::run(
562547
// filtered by matchers in some cases, e.g. the type is templated. We should
563548
// handle the record type qualifier instead.
564549
TypeLoc Loc = *TLoc;
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);
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);
578560
} else if (const auto *VarRef =
579561
Result.Nodes.getNodeAs<DeclRefExpr>("var_ref")) {
580562
const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var_decl");
@@ -588,10 +570,9 @@ void ChangeNamespaceTool::run(
588570
} else if (const auto *EnumConstRef =
589571
Result.Nodes.getNodeAs<DeclRefExpr>("enum_const_ref")) {
590572
// Do not rename the reference if it is already scoped by the EnumDecl name.
591-
if (EnumConstRef->hasQualifier() &&
592-
EnumConstRef->getQualifier()->getKind() ==
593-
NestedNameSpecifier::SpecifierKind::TypeSpec &&
594-
EnumConstRef->getQualifier()->getAsType()->isEnumeralType())
573+
if (NestedNameSpecifier Qualifier = EnumConstRef->getQualifier();
574+
Qualifier.getKind() == NestedNameSpecifier::Kind::Type &&
575+
Qualifier.getAsType()->isEnumeralType())
595576
return;
596577
const auto *EnumConstDecl =
597578
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 =
906-
cast_or_null<CXXRecordDecl>(Ty->getDecl()->getDefinition())) {
905+
if (const CXXRecordDecl *Base = cast_or_null<CXXRecordDecl>(
906+
Ty->getOriginalDecl()->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-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ 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 << reinterpret_cast<NestedNameSpecifier *>(Info.getRawArg(Index));
536+
Builder << NestedNameSpecifier::getFromVoidPointer(
537+
reinterpret_cast<void *>(Info.getRawArg(Index)));
537538
break;
538539
case clang::DiagnosticsEngine::ak_declcontext:
539540
Builder << reinterpret_cast<DeclContext *>(Info.getRawArg(Index));

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,17 @@ 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();
38-
}
36+
if (const auto *Dependent = BaseType->getAs<DependentNameType>())
37+
BaseType = Dependent->getQualifier().getAsType();
3938
if (!BaseType)
4039
return false;
4140
if (CheckTemplate(BaseType->getAs<TemplateSpecializationType>()))
4241
return true; // Case: enable_if_t< >.
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-
}
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.
5147
return false;
5248
}
5349
AST_MATCHER_P(TemplateTypeParmDecl, hasDefaultArgument,

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,31 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
3939
// std::move(). This will hopefully prevent erroneous replacements if the
4040
// code does unusual things (e.g. create an alias for std::move() in
4141
// another namespace).
42-
NestedNameSpecifier *NNS = Callee->getQualifier();
43-
if (!NNS) {
42+
NestedNameSpecifier NNS = Callee->getQualifier();
43+
switch (NNS.getKind()) {
44+
case NestedNameSpecifier::Kind::Null:
4445
// Called as "move" (i.e. presumably the code had a "using std::move;").
4546
// We still conservatively put a "std::" in front of the forward because
4647
// we don't know whether the code also had a "using std::forward;".
4748
Diag << FixItHint::CreateReplacement(CallRange, "std::" + ForwardName);
48-
} else if (const NamespaceBaseDecl *Namespace = NNS->getAsNamespace()) {
49+
break;
50+
case NestedNameSpecifier::Kind::Namespace: {
51+
auto [Namespace, Prefix] = NNS.getAsNamespaceAndPrefix();
4952
if (Namespace->getName() == "std") {
50-
if (!NNS->getPrefix()) {
53+
if (!Prefix) {
5154
// Called as "std::move".
5255
Diag << FixItHint::CreateReplacement(CallRange,
5356
"std::" + ForwardName);
54-
} else if (NNS->getPrefix()->getKind() == NestedNameSpecifier::Global) {
57+
} else if (Prefix.getKind() == NestedNameSpecifier::Kind::Global) {
5558
// Called as "::std::move".
5659
Diag << FixItHint::CreateReplacement(CallRange,
5760
"::std::" + ForwardName);
5861
}
5962
}
63+
break;
64+
}
65+
default:
66+
return;
6067
}
6168
}
6269
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ struct InitializerInsertion {
190190
// Convenience utility to get a RecordDecl from a QualType.
191191
const RecordDecl *getCanonicalRecordDecl(const QualType &Type) {
192192
if (const auto *RT = Type.getCanonicalType()->getAs<RecordType>())
193-
return RT->getDecl();
193+
return RT->getOriginalDecl();
194194
return nullptr;
195195
}
196196

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void SlicingCheck::diagnoseSlicedOverriddenMethods(
9292
for (const auto &Base : DerivedDecl.bases()) {
9393
if (const auto *BaseRecordType = Base.getType()->getAs<RecordType>()) {
9494
if (const auto *BaseRecord = cast_or_null<CXXRecordDecl>(
95-
BaseRecordType->getDecl()->getDefinition()))
95+
BaseRecordType->getOriginalDecl()->getDefinition()))
9696
diagnoseSlicedOverriddenMethods(Call, *BaseRecord, BaseDecl);
9797
}
9898
}

clang-tools-extra/clang-tidy/fuchsia/MultipleInheritanceCheck.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool MultipleInheritanceCheck::isInterface(const CXXRecordDecl *Node) {
7474
const auto *Ty = I.getType()->getAs<RecordType>();
7575
if (!Ty)
7676
continue;
77-
const RecordDecl *D = Ty->getDecl()->getDefinition();
77+
const RecordDecl *D = Ty->getOriginalDecl()->getDefinition();
7878
if (!D)
7979
continue;
8080
const auto *Base = cast<CXXRecordDecl>(D);
@@ -106,7 +106,8 @@ void MultipleInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
106106
const auto *Ty = I.getType()->getAs<RecordType>();
107107
if (!Ty)
108108
continue;
109-
const auto *Base = cast<CXXRecordDecl>(Ty->getDecl()->getDefinition());
109+
const auto *Base =
110+
cast<CXXRecordDecl>(Ty->getOriginalDecl()->getDefinition());
110111
if (!isInterface(Base))
111112
NumConcrete++;
112113
}
@@ -117,7 +118,8 @@ void MultipleInheritanceCheck::check(const MatchFinder::MatchResult &Result) {
117118
const auto *Ty = V.getType()->getAs<RecordType>();
118119
if (!Ty)
119120
continue;
120-
const auto *Base = cast<CXXRecordDecl>(Ty->getDecl()->getDefinition());
121+
const auto *Base =
122+
cast<CXXRecordDecl>(Ty->getOriginalDecl()->getDefinition());
121123
if (!isInterface(Base))
122124
NumConcrete++;
123125
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static bool isStdInitializerList(QualType Type) {
7272
}
7373
if (const auto *RT = Type->getAs<RecordType>()) {
7474
if (const auto *Specialization =
75-
dyn_cast<ClassTemplateSpecializationDecl>(RT->getDecl()))
75+
dyn_cast<ClassTemplateSpecializationDecl>(RT->getOriginalDecl()))
7676
return declIsStdInitializerList(Specialization->getSpecializedTemplate());
7777
}
7878
return false;

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ static bool incrementWithoutOverflow(const APSInt &Value, APSInt &Result) {
4545
return Value < Result;
4646
}
4747

48-
static bool areEquivalentNameSpecifier(const NestedNameSpecifier *Left,
49-
const NestedNameSpecifier *Right) {
50-
llvm::FoldingSetNodeID LeftID, RightID;
51-
Left->Profile(LeftID);
52-
Right->Profile(RightID);
53-
return LeftID == RightID;
54-
}
55-
5648
static bool areEquivalentExpr(const Expr *Left, const Expr *Right) {
5749
if (!Left || !Right)
5850
return !Left && !Right;

0 commit comments

Comments
 (0)