[clang-tools-extra] e70ca7b - [clang] Build UsingType for elaborated type specifiers.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 19 05:21:18 PST 2023
Author: Haojian Wu
Date: 2023-01-19T14:20:38+01:00
New Revision: e70ca7b35319a3621f9d9c6475926428f8c5c000
URL: https://github.com/llvm/llvm-project/commit/e70ca7b35319a3621f9d9c6475926428f8c5c000
DIFF: https://github.com/llvm/llvm-project/commit/e70ca7b35319a3621f9d9c6475926428f8c5c000.diff
LOG: [clang] Build UsingType for elaborated type specifiers.
Support building UsingType for elaborated type specifiers:
```
namespace ns { class Foo {}; }
using ns::Foo;
// The TypeLoc of `Foo` below should be a ElaboratedTypeLoc with an
// inner UsingTypeLoc rather than the underlying `CXXRecordTypeLoc`
class Foo foo;
```
Differential Revision: https://reviews.llvm.org/D141280
Added:
Modified:
clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
clang/include/clang/Sema/DeclSpec.h
clang/include/clang/Sema/Sema.h
clang/lib/Parse/ParseDecl.cpp
clang/lib/Parse/ParseDeclCXX.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclCXX.cpp
clang/lib/Sema/SemaTemplate.cpp
clang/lib/Sema/SemaType.cpp
clang/test/CXX/drs/dr2xx.cpp
clang/test/CXX/drs/dr4xx.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
index 3b97cc8cdfd55..e5b99ef9a3a00 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -150,6 +150,9 @@ TEST(WalkAST, Using) {
}
using ns::$explicit^Y;)cpp",
"^Y<int> x;");
+ testWalk(R"cpp(
+ namespace ns { class Foo {}; }
+ )cpp", "using ns::$explicit^Foo; class ^Foo foo;");
}
TEST(WalkAST, Namespaces) {
diff --git a/clang/include/clang/Sema/DeclSpec.h b/clang/include/clang/Sema/DeclSpec.h
index 69fe2c541607b..a95fe5686009b 100644
--- a/clang/include/clang/Sema/DeclSpec.h
+++ b/clang/include/clang/Sema/DeclSpec.h
@@ -506,8 +506,16 @@ class DeclSpec {
assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
return TypeRep;
}
+ // Returns the underlying decl, if any.
Decl *getRepAsDecl() const {
- assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
+ auto *D = getRepAsFoundDecl();
+ if (const auto *Using = dyn_cast_or_null<UsingShadowDecl>(D))
+ return Using->getTargetDecl();
+ return D;
+ }
+ // Returns the originally found decl, if any.
+ Decl *getRepAsFoundDecl() const {
+ assert(isDeclRep((TST)TypeSpecType) && "DeclSpec does not store a decl");
return DeclRep;
}
Expr *getRepAsExpr() const {
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 7a95989ee6d44..e504111fce059 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -3324,7 +3324,9 @@ class Sema final {
SourceLocation ScopedEnumKWLoc,
bool ScopedEnumUsesClassTag, TypeResult UnderlyingType,
bool IsTypeSpecifier, bool IsTemplateParamOrArg,
- OffsetOfKind OOK, SkipBodyInfo *SkipBody = nullptr);
+ OffsetOfKind OOK,
+ UsingShadowDecl*& FoundUsingShadow,
+ SkipBodyInfo *SkipBody = nullptr);
DeclResult ActOnTemplatedFriendTag(Scope *S, SourceLocation FriendLoc,
unsigned TagSpec, SourceLocation TagLoc,
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index e6812ac72c885..c7fd1156928ca 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4965,6 +4965,7 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
bool IsDependent = false;
const char *PrevSpec = nullptr;
unsigned DiagID;
+ UsingShadowDecl* FoundUsing = nullptr;
Decl *TagDecl =
Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS,
Name, NameLoc, attrs, AS, DS.getModulePrivateSpecLoc(),
@@ -4973,7 +4974,7 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
BaseType, DSC == DeclSpecContext::DSC_type_specifier,
DSC == DeclSpecContext::DSC_template_param ||
DSC == DeclSpecContext::DSC_template_type_arg,
- OffsetOfState, &SkipBody).get();
+ OffsetOfState, FoundUsing, &SkipBody).get();
if (SkipBody.ShouldSkip) {
assert(TUK == Sema::TUK_Definition && "can only skip a definition");
@@ -4983,8 +4984,8 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
T.skipToEnd();
if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
- NameLoc.isValid() ? NameLoc : StartLoc,
- PrevSpec, DiagID, TagDecl, Owned,
+ NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
+ DiagID, FoundUsing ? FoundUsing : TagDecl, Owned,
Actions.getASTContext().getPrintingPolicy()))
Diag(StartLoc, DiagID) << PrevSpec;
return;
@@ -5038,8 +5039,8 @@ void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
}
if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
- NameLoc.isValid() ? NameLoc : StartLoc,
- PrevSpec, DiagID, TagDecl, Owned,
+ NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
+ DiagID, FoundUsing ? FoundUsing : TagDecl, Owned,
Actions.getASTContext().getPrintingPolicy()))
Diag(StartLoc, DiagID) << PrevSpec;
}
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 227c1df2bdddd..b26ec00cfedf3 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -1934,6 +1934,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
// Create the tag portion of the class or class template.
DeclResult TagOrTempResult = true; // invalid
TypeResult TypeResult = true; // invalid
+ UsingShadowDecl *FoundUsing = nullptr;
bool Owned = false;
Sema::SkipBodyInfo SkipBody;
@@ -2074,7 +2075,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
DSC == DeclSpecContext::DSC_type_specifier,
DSC == DeclSpecContext::DSC_template_param ||
DSC == DeclSpecContext::DSC_template_type_arg,
- OffsetOfState, &SkipBody);
+ OffsetOfState, FoundUsing, &SkipBody);
// If ActOnTag said the type was dependent, try again with the
// less common call.
@@ -2133,7 +2134,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
} else if (!TagOrTempResult.isInvalid()) {
Result = DS.SetTypeSpecType(
TagType, StartLoc, NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
- DiagID, TagOrTempResult.get(), Owned, Policy);
+ DiagID, FoundUsing ? FoundUsing : TagOrTempResult.get(), Owned, Policy);
} else {
DS.SetTypeSpecError();
return;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c94b7ece01297..faa75f671fde2 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16592,7 +16592,8 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
bool &IsDependent, SourceLocation ScopedEnumKWLoc,
bool ScopedEnumUsesClassTag, TypeResult UnderlyingType,
bool IsTypeSpecifier, bool IsTemplateParamOrArg,
- OffsetOfKind OOK, SkipBodyInfo *SkipBody) {
+ OffsetOfKind OOK, UsingShadowDecl *&FoundUsingShadow,
+ SkipBodyInfo *SkipBody) {
// If this is not a definition, it must have a name.
IdentifierInfo *OrigName = Name;
assert((Name != nullptr || TUK == TUK_Definition) &&
@@ -17027,6 +17028,7 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
// redefinition if either context is within the other.
if (auto *Shadow = dyn_cast<UsingShadowDecl>(DirectPrevDecl)) {
auto *OldTag = dyn_cast<TagDecl>(PrevDecl);
+ FoundUsingShadow = Shadow;
if (SS.isEmpty() && TUK != TUK_Reference && TUK != TUK_Friend &&
isDeclInScope(Shadow, SearchDC, S, isMemberSpecialization) &&
!(OldTag && isAcceptableTagRedeclContext(
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c8d384c643b3d..80cc6b0c01f37 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -16972,6 +16972,7 @@ DeclResult Sema::ActOnTemplatedFriendTag(
if (SS.isEmpty()) {
bool Owned = false;
bool IsDependent = false;
+ UsingShadowDecl* FoundUsing = nullptr;
return ActOnTag(S, TagSpec, TUK_Friend, TagLoc, SS, Name, NameLoc, Attr,
AS_public,
/*ModulePrivateLoc=*/SourceLocation(),
@@ -16980,7 +16981,7 @@ DeclResult Sema::ActOnTemplatedFriendTag(
/*ScopedEnumUsesClassTag=*/false,
/*UnderlyingType=*/TypeResult(),
/*IsTypeSpecifier=*/false,
- /*IsTemplateParamOrArg=*/false, /*OOK=*/OOK_Outside);
+ /*IsTemplateParamOrArg=*/false, /*OOK=*/OOK_Outside, FoundUsing);
}
NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index b40bd0978a8ab..4b144c239fa47 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -10181,11 +10181,14 @@ Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc,
bool Owned = false;
bool IsDependent = false;
- Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, KWLoc, SS, Name,
- NameLoc, Attr, AS_none, /*ModulePrivateLoc=*/SourceLocation(),
+ UsingShadowDecl* FoundUsing = nullptr;
+ Decl *TagD =
+ ActOnTag(S, TagSpec, Sema::TUK_Reference, KWLoc, SS, Name, NameLoc, Attr,
+ AS_none, /*ModulePrivateLoc=*/SourceLocation(),
MultiTemplateParamsArg(), Owned, IsDependent, SourceLocation(),
false, TypeResult(), /*IsTypeSpecifier*/ false,
- /*IsTemplateParamOrArg*/ false, /*OOK=*/OOK_Outside).get();
+ /*IsTemplateParamOrArg*/ false, /*OOK=*/OOK_Outside, FoundUsing)
+ .get();
assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
if (!TagD)
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index ac00e87944845..e124db71af18b 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -1588,6 +1588,9 @@ static QualType ConvertDeclSpecToType(TypeProcessingState &state) {
// TypeQuals handled by caller.
Result = Context.getTypeDeclType(D);
+ if (const auto *Using =
+ dyn_cast_or_null<UsingShadowDecl>(DS.getRepAsFoundDecl()))
+ Result = Context.getUsingType(Using, Result);
// In both C and C++, make an ElaboratedType.
ElaboratedTypeKeyword Keyword
diff --git a/clang/test/CXX/drs/dr2xx.cpp b/clang/test/CXX/drs/dr2xx.cpp
index 31ac31b088105..8a462203ca9a9 100644
--- a/clang/test/CXX/drs/dr2xx.cpp
+++ b/clang/test/CXX/drs/dr2xx.cpp
@@ -992,7 +992,7 @@ namespace dr284 { // dr284: no
}
struct B::V {}; // expected-error {{no struct named 'V'}}
struct B::W {};
- struct B::X {}; // FIXME: ill-formed
+ struct B::X {}; // expected-error {{forward declaration of struct cannot have}}
enum B::Y e; // ok per dr417
class B::Z z; // ok per dr417
@@ -1009,7 +1009,7 @@ namespace dr284 { // dr284: no
};
struct D::V {}; // expected-error {{no struct named 'V'}}
struct D::W {};
- struct D::X {}; // FIXME: ill-formed
+ struct D::X {}; // expected-error {{forward declaration of struct cannot have}}
enum D::Y e2; // ok per dr417
class D::Z z2; // ok per dr417
}
diff --git a/clang/test/CXX/drs/dr4xx.cpp b/clang/test/CXX/drs/dr4xx.cpp
index 476d80ef0720d..158eb39fe633f 100644
--- a/clang/test/CXX/drs/dr4xx.cpp
+++ b/clang/test/CXX/drs/dr4xx.cpp
@@ -301,9 +301,8 @@ namespace dr417 { // dr417: no
struct F;
struct H;
}
- // FIXME: This is ill-formed.
using N::D;
- struct dr417::D {}; // expected-warning {{extra qualification}}
+ struct dr417::D {}; // expected-error {{forward declaration of struct cannot}} expected-warning {{extra qualification}}
using namespace N;
struct dr417::E {}; // expected-warning {{extra qualification}} expected-error {{no struct named 'E'}}
struct N::F {};
More information about the cfe-commits
mailing list