Skip to content

Commit 4a9eaad

Browse files
zwuismizvekov
andauthored
[Clang][AST][NFC] Introduce NamespaceBaseDecl (#149123)
Add `NamespaceBaseDecl` as common base class of `NamespaceDecl` and `NamespaceAliasDecl`. This simplifies `NestedNameSpecifier` a bit. Co-authored-by: Matheus Izvekov <[email protected]>
1 parent 28417e6 commit 4a9eaad

Some content is hidden

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

47 files changed

+168
-364
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void replaceMoveWithForward(const UnresolvedLookupExpr *Callee,
4545
// We still conservatively put a "std::" in front of the forward because
4646
// we don't know whether the code also had a "using std::forward;".
4747
Diag << FixItHint::CreateReplacement(CallRange, "std::" + ForwardName);
48-
} else if (const NamespaceDecl *Namespace = NNS->getAsNamespace()) {
48+
} else if (const NamespaceBaseDecl *Namespace = NNS->getAsNamespace()) {
4949
if (Namespace->getName() == "std") {
5050
if (!NNS->getPrefix()) {
5151
// Called as "std::move".

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ void UnusedAliasDeclsCheck::check(const MatchFinder::MatchResult &Result) {
3636

3737
if (const auto *NestedName =
3838
Result.Nodes.getNodeAs<NestedNameSpecifier>("nns")) {
39-
if (const auto *AliasDecl = NestedName->getAsNamespaceAlias()) {
39+
if (const auto *AliasDecl = dyn_cast_if_present<NamespaceAliasDecl>(
40+
NestedName->getAsNamespace())) {
4041
FoundDecls[AliasDecl] = CharSourceRange();
4142
}
4243
}

clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ class RenamerClangTidyVisitor
282282

283283
bool TraverseNestedNameSpecifierLoc(NestedNameSpecifierLoc Loc) {
284284
if (const NestedNameSpecifier *Spec = Loc.getNestedNameSpecifier()) {
285-
if (const NamespaceDecl *Decl = Spec->getAsNamespace())
285+
if (const auto *Decl =
286+
dyn_cast_if_present<NamespaceDecl>(Spec->getAsNamespace()))
286287
Check->addUsage(Decl, Loc.getLocalSourceRange(), SM);
287288
}
288289

clang-tools-extra/clangd/AST.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,12 +666,14 @@ std::string getQualification(ASTContext &Context,
666666
return getQualification(
667667
Context, DestContext, ND->getDeclContext(),
668668
[&](NestedNameSpecifier *NNS) {
669-
if (NNS->getKind() != NestedNameSpecifier::Namespace)
669+
const NamespaceDecl *NS =
670+
dyn_cast_if_present<NamespaceDecl>(NNS->getAsNamespace());
671+
if (!NS)
670672
return false;
671-
const auto *CanonNSD = NNS->getAsNamespace()->getCanonicalDecl();
673+
NS = NS->getCanonicalDecl();
672674
return llvm::any_of(VisibleNamespaceDecls,
673-
[CanonNSD](const NamespaceDecl *NSD) {
674-
return NSD->getCanonicalDecl() == CanonNSD;
675+
[NS](const NamespaceDecl *NSD) {
676+
return NSD->getCanonicalDecl() == NS;
675677
});
676678
});
677679
}

clang-tools-extra/clangd/CodeComplete.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,6 @@ bool allowIndex(CodeCompletionContext &CC) {
14701470
switch (NameSpec->getKind()) {
14711471
case NestedNameSpecifier::Global:
14721472
case NestedNameSpecifier::Namespace:
1473-
case NestedNameSpecifier::NamespaceAlias:
14741473
return true;
14751474
case NestedNameSpecifier::Super:
14761475
case NestedNameSpecifier::TypeSpec:

clang-tools-extra/clangd/DumpAST.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
158158
NNS_KIND(TypeSpec);
159159
NNS_KIND(Global);
160160
NNS_KIND(Super);
161-
NNS_KIND(NamespaceAlias);
162161
#undef NNS_KIND
163162
}
164163
llvm_unreachable("Unhandled SpecifierKind enum");
@@ -281,8 +280,6 @@ class DumpVisitor : public RecursiveASTVisitor<DumpVisitor> {
281280
return NNS.getAsIdentifier()->getName().str() + "::";
282281
case NestedNameSpecifier::Namespace:
283282
return NNS.getAsNamespace()->getNameAsString() + "::";
284-
case NestedNameSpecifier::NamespaceAlias:
285-
return NNS.getAsNamespaceAlias()->getNameAsString() + "::";
286283
default:
287284
return "";
288285
}

clang-tools-extra/clangd/FindTarget.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,6 @@ struct TargetFinder {
491491
case NestedNameSpecifier::Namespace:
492492
add(NNS->getAsNamespace(), Flags);
493493
return;
494-
case NestedNameSpecifier::NamespaceAlias:
495-
add(NNS->getAsNamespaceAlias(), Flags);
496-
return;
497494
case NestedNameSpecifier::Identifier:
498495
if (Resolver) {
499496
add(Resolver->resolveNestedNameSpecifierToType(NNS), Flags);

clang-tools-extra/clangd/IncludeFixer.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -403,25 +403,27 @@ std::optional<CheapUnresolvedName> extractUnresolvedNameCheaply(
403403
if (auto *Nested = SS->getScopeRep()) {
404404
if (Nested->getKind() == NestedNameSpecifier::Global) {
405405
Result.ResolvedScope = "";
406-
} else if (const auto *NS = Nested->getAsNamespace()) {
407-
std::string SpecifiedNS = printNamespaceScope(*NS);
408-
std::optional<std::string> Spelling = getSpelledSpecifier(*SS, SM);
409-
410-
// Check the specifier spelled in the source.
411-
// If the resolved scope doesn't end with the spelled scope, the
412-
// resolved scope may come from a sema typo correction. For example,
413-
// sema assumes that "clangd::" is a typo of "clang::" and uses
414-
// "clang::" as the specified scope in:
415-
// namespace clang { clangd::X; }
416-
// In this case, we use the "typo" specifier as extra scope instead
417-
// of using the scope assumed by sema.
418-
if (!Spelling || llvm::StringRef(SpecifiedNS).ends_with(*Spelling)) {
419-
Result.ResolvedScope = std::move(SpecifiedNS);
406+
} else if (const NamespaceBaseDecl *NSB = Nested->getAsNamespace()) {
407+
if (const auto *NS = dyn_cast<NamespaceDecl>(NSB)) {
408+
std::string SpecifiedNS = printNamespaceScope(*NS);
409+
std::optional<std::string> Spelling = getSpelledSpecifier(*SS, SM);
410+
411+
// Check the specifier spelled in the source.
412+
// If the resolved scope doesn't end with the spelled scope, the
413+
// resolved scope may come from a sema typo correction. For example,
414+
// sema assumes that "clangd::" is a typo of "clang::" and uses
415+
// "clang::" as the specified scope in:
416+
// namespace clang { clangd::X; }
417+
// In this case, we use the "typo" specifier as extra scope instead
418+
// of using the scope assumed by sema.
419+
if (!Spelling || llvm::StringRef(SpecifiedNS).ends_with(*Spelling)) {
420+
Result.ResolvedScope = std::move(SpecifiedNS);
421+
} else {
422+
Result.UnresolvedScope = std::move(*Spelling);
423+
}
420424
} else {
421-
Result.UnresolvedScope = std::move(*Spelling);
425+
Result.ResolvedScope = printNamespaceScope(*cast<NamespaceAliasDecl>(NSB)->getNamespace());
422426
}
423-
} else if (const auto *ANS = Nested->getAsNamespaceAlias()) {
424-
Result.ResolvedScope = printNamespaceScope(*ANS->getNamespace());
425427
} else {
426428
// We don't fix symbols in scopes that are not top-level e.g. class
427429
// members, as we don't collect includes for them.

clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ findInsertionPoint(const Tweak::Selection &Inputs,
173173
if (SM.isBeforeInTranslationUnit(Inputs.Cursor, U->getUsingLoc()))
174174
// "Usings" is sorted, so we're done.
175175
break;
176-
if (const auto *Namespace = U->getQualifier()->getAsNamespace()) {
176+
if (const auto *Namespace = dyn_cast_if_present<NamespaceDecl>(
177+
U->getQualifier()->getAsNamespace())) {
177178
if (Namespace->getCanonicalDecl() ==
178179
QualifierToRemove.getNestedNameSpecifier()
179180
->getAsNamespace()
@@ -232,7 +233,10 @@ findInsertionPoint(const Tweak::Selection &Inputs,
232233

233234
bool isNamespaceForbidden(const Tweak::Selection &Inputs,
234235
const NestedNameSpecifier &Namespace) {
235-
std::string NamespaceStr = printNamespaceScope(*Namespace.getAsNamespace());
236+
const auto *NS = dyn_cast<NamespaceDecl>(Namespace.getAsNamespace());
237+
if (!NS)
238+
return true;
239+
std::string NamespaceStr = printNamespaceScope(*NS);
236240

237241
for (StringRef Banned : Config::current().Style.FullyQualifiedNamespaces) {
238242
StringRef PrefixMatch = NamespaceStr;

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
140140
return true;
141141
switch (Qual->getKind()) {
142142
case NestedNameSpecifier::Namespace:
143-
case NestedNameSpecifier::NamespaceAlias:
144143
case NestedNameSpecifier::Global:
145144
return true;
146145
case NestedNameSpecifier::TypeSpec:

0 commit comments

Comments
 (0)