[clang] c1822d6 - [clang][Sema][NFC] Improve readability in `computeDeclContext` (#208010)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 7 08:23:41 PDT 2026
Author: Yanzuo Liu
Date: 2026-07-07T15:23:36Z
New Revision: c1822d6104d7b2cb2093a5273d6ceccd9ebb2fb1
URL: https://github.com/llvm/llvm-project/commit/c1822d6104d7b2cb2093a5273d6ceccd9ebb2fb1
DIFF: https://github.com/llvm/llvm-project/commit/c1822d6104d7b2cb2093a5273d6ceccd9ebb2fb1.diff
LOG: [clang][Sema][NFC] Improve readability in `computeDeclContext` (#208010)
Split off from #190495.
Co-authored-by: Matheus Izvekov <mizvekov at gmail.com>
Added:
Modified:
clang/lib/Sema/SemaCXXScopeSpec.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaCXXScopeSpec.cpp b/clang/lib/Sema/SemaCXXScopeSpec.cpp
index 255c22d9c2a31..00a566cfab7b0 100644
--- a/clang/lib/Sema/SemaCXXScopeSpec.cpp
+++ b/clang/lib/Sema/SemaCXXScopeSpec.cpp
@@ -48,105 +48,99 @@ DeclContext *Sema::computeDeclContext(QualType T) {
DeclContext *Sema::computeDeclContext(const CXXScopeSpec &SS,
bool EnteringContext) {
- if (!SS.isSet() || SS.isInvalid())
- return nullptr;
-
NestedNameSpecifier NNS = SS.getScopeRep();
- if (NNS.isDependent()) {
- // If this nested-name-specifier refers to the current
- // instantiation, return its DeclContext.
- if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
- return Record;
-
- if (EnteringContext) {
- if (NNS.getKind() != NestedNameSpecifier::Kind::Type)
- return nullptr;
- const Type *NNSType = NNS.getAsType();
-
- // Look through type alias templates, per C++0x [temp.dep.type]p1.
- NNSType = Context.getCanonicalType(NNSType);
- if (const auto *SpecType =
- dyn_cast<TemplateSpecializationType>(NNSType)) {
- // We are entering the context of the nested name specifier, so try to
- // match the nested name specifier to either a primary class template
- // or a class template partial specialization.
- if (ClassTemplateDecl *ClassTemplate =
- dyn_cast_or_null<ClassTemplateDecl>(
- SpecType->getTemplateName().getAsTemplateDecl())) {
- // FIXME: The fallback on the search of partial
- // specialization using ContextType should be eventually removed since
- // it doesn't handle the case of constrained template parameters
- // correctly. Currently removing this fallback would change the
- // diagnostic output for invalid code in a number of tests.
- ClassTemplatePartialSpecializationDecl *PartialSpec = nullptr;
- ArrayRef<TemplateParameterList *> TemplateParamLists =
- SS.getTemplateParamLists();
- if (!TemplateParamLists.empty()) {
- unsigned Depth = ClassTemplate->getTemplateParameters()->getDepth();
- auto L = find_if(TemplateParamLists,
+ if (!NNS.isDependent()) {
+ switch (NNS.getKind()) {
+ case NestedNameSpecifier::Kind::Namespace:
+ return const_cast<NamespaceDecl *>(
+ NNS.getAsNamespaceAndPrefix().Namespace->getNamespace());
+
+ case NestedNameSpecifier::Kind::Type:
+ return NNS.getAsType()->castAsTagDecl();
+
+ case NestedNameSpecifier::Kind::Global:
+ return Context.getTranslationUnitDecl();
+
+ case NestedNameSpecifier::Kind::MicrosoftSuper:
+ return NNS.getAsMicrosoftSuper();
+
+ case NestedNameSpecifier::Kind::Null:
+ return nullptr;
+ }
+
+ llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
+ }
+
+ // If this nested-name-specifier refers to the current
+ // instantiation, return its DeclContext.
+ if (CXXRecordDecl *Record = getCurrentInstantiationOf(NNS))
+ return Record;
+
+ if (!EnteringContext || NNS.getKind() != NestedNameSpecifier::Kind::Type)
+ return nullptr;
+ const Type *NNSType = NNS.getAsType();
+
+ // Look through type alias templates, per C++0x [temp.dep.type]p1.
+ NNSType = Context.getCanonicalType(NNSType);
+ if (const auto *SpecType = dyn_cast<TemplateSpecializationType>(NNSType)) {
+ // We are entering the context of the nested name specifier, so try to
+ // match the nested name specifier to either a primary class template
+ // or a class template partial specialization.
+ ClassTemplateDecl *ClassTemplate = dyn_cast_or_null<ClassTemplateDecl>(
+ SpecType->getTemplateName().getAsTemplateDecl());
+ if (!ClassTemplate)
+ return nullptr;
+ ClassTemplatePartialSpecializationDecl *PartialSpec = nullptr;
+ ArrayRef<TemplateParameterList *> TemplateParamLists =
+ SS.getTemplateParamLists();
+ if (!TemplateParamLists.empty()) {
+ unsigned Depth = ClassTemplate->getTemplateParameters()->getDepth();
+ auto L = llvm::find_if(TemplateParamLists,
[Depth](TemplateParameterList *TPL) {
return TPL->getDepth() == Depth;
});
- if (L != TemplateParamLists.end()) {
- void *Pos = nullptr;
- PartialSpec = ClassTemplate->findPartialSpecialization(
- SpecType->template_arguments(), *L, Pos);
- }
- } else {
- PartialSpec =
- ClassTemplate->findPartialSpecialization(QualType(SpecType, 0));
- }
-
- if (PartialSpec) {
- // A declaration of the partial specialization must be visible.
- // We can always recover here, because this only happens when we're
- // entering the context, and that can't happen in a SFINAE context.
- assert(!isSFINAEContext() && "partial specialization scope "
- "specifier in SFINAE context?");
- if (PartialSpec->hasDefinition() &&
- !hasReachableDefinition(PartialSpec))
- diagnoseMissingImport(SS.getLastQualifierNameLoc(), PartialSpec,
- MissingImportKind::PartialSpecialization,
- true);
- return PartialSpec;
- }
-
- // If the type of the nested name specifier is the same as the
- // injected class name of the named class template, we're entering
- // into that class template definition.
- CanQualType Injected =
- ClassTemplate->getCanonicalInjectedSpecializationType(Context);
- if (Context.hasSameType(Injected, QualType(SpecType, 0)))
- return ClassTemplate->getTemplatedDecl();
- }
- } else if (const auto *RecordT = dyn_cast<RecordType>(NNSType)) {
- // The nested name specifier refers to a member of a class template.
- return RecordT->getDecl()->getDefinitionOrSelf();
+ if (L != TemplateParamLists.end()) {
+ void *Pos = nullptr;
+ PartialSpec = ClassTemplate->findPartialSpecialization(
+ SpecType->template_arguments(), *L, Pos);
}
+ } else {
+ // FIXME: The fallback on the search of partial
+ // specialization using ContextType should be eventually removed since
+ // it doesn't handle the case of constrained template parameters
+ // correctly. Currently removing this fallback would change the
+ // diagnostic output for invalid code in a number of tests.
+ PartialSpec =
+ ClassTemplate->findPartialSpecialization(QualType(SpecType, 0));
}
+ if (PartialSpec) {
+ // A declaration of the partial specialization must be visible.
+ // We can always recover here, because this only happens when we're
+ // entering the context, and that can't happen in a SFINAE context.
+ assert(!isSFINAEContext() && "partial specialization scope "
+ "specifier in SFINAE context?");
+ if (PartialSpec->hasDefinition() && !hasReachableDefinition(PartialSpec))
+ diagnoseMissingImport(SS.getLastQualifierNameLoc(), PartialSpec,
+ MissingImportKind::PartialSpecialization, true);
+ return PartialSpec;
+ }
+
+ // If the type of the nested name specifier is the same as the
+ // injected class name of the named class template, we're entering
+ // into that class template definition.
+ CanQualType Injected =
+ ClassTemplate->getCanonicalInjectedSpecializationType(Context);
+ if (Context.hasSameType(Injected, QualType(SpecType, 0)))
+ return ClassTemplate->getTemplatedDecl();
return nullptr;
}
-
- switch (NNS.getKind()) {
- case NestedNameSpecifier::Kind::Namespace:
- return const_cast<NamespaceDecl *>(
- NNS.getAsNamespaceAndPrefix().Namespace->getNamespace());
-
- case NestedNameSpecifier::Kind::Type:
- return NNS.getAsType()->castAsTagDecl();
-
- case NestedNameSpecifier::Kind::Global:
- return Context.getTranslationUnitDecl();
-
- case NestedNameSpecifier::Kind::MicrosoftSuper:
- return NNS.getAsMicrosoftSuper();
-
- case NestedNameSpecifier::Kind::Null:
- llvm_unreachable("unexpected null nested name specifier");
+ if (const auto *RecordT = dyn_cast<RecordType>(NNSType)) {
+ // The nested name specifier refers to a member of a class template.
+ return RecordT->getDecl()->getDefinitionOrSelf();
}
- llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
+ return nullptr;
}
bool Sema::isDependentScopeSpecifier(const CXXScopeSpec &SS) {
More information about the cfe-commits
mailing list