Skip to content

Commit bdc6974

Browse files
committed
[clang] Implement ElaboratedType sugaring for types written bare
Without this patch, clang will not wrap in an ElaboratedType node types written without a keyword and nested name qualifier, which goes against the intent that we should produce an AST which retains enough details to recover how things are written. The lack of this sugar is incompatible with the intent of the type printer default policy, which is to print types as written, but to fall back and print them fully qualified when they are desugared. An ElaboratedTypeLoc without keyword / NNS uses no storage by itself, but still requires pointer alignment due to pre-existing bug in the TypeLoc buffer handling. Signed-off-by: Matheus Izvekov <[email protected]> Differential Revision: https://reviews.llvm.org/D112374
1 parent ee88c0c commit bdc6974

File tree

285 files changed

+2348
-2139
lines changed

Some content is hidden

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

285 files changed

+2348
-2139
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,12 @@ void ChangeNamespaceTool::run(
567567
if (Loc.getTypeLocClass() == TypeLoc::Elaborated) {
568568
NestedNameSpecifierLoc NestedNameSpecifier =
569569
Loc.castAs<ElaboratedTypeLoc>().getQualifierLoc();
570-
// This happens for friend declaration of a base class with injected class
571-
// name.
572-
if (!NestedNameSpecifier.getNestedNameSpecifier())
573-
return;
574-
const Type *SpecifierType =
575-
NestedNameSpecifier.getNestedNameSpecifier()->getAsType();
576-
if (SpecifierType && SpecifierType->isRecordType())
577-
return;
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+
}
578576
}
579577
fixTypeLoc(Result, startLocationForType(Loc), endLocationForType(Loc), Loc);
580578
} else if (const auto *VarRef =

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
@@ -215,7 +215,8 @@ void FindAllSymbols::registerMatchers(MatchFinder *MatchFinder) {
215215
// Uses of most types: just look at what the typeLoc refers to.
216216
MatchFinder->addMatcher(
217217
typeLoc(isExpansionInMainFile(),
218-
loc(qualType(hasDeclaration(Types.bind("use"))))),
218+
loc(qualType(allOf(unless(elaboratedType()),
219+
hasDeclaration(Types.bind("use")))))),
219220
this);
220221
// Uses of typedefs: these are often transparent to hasDeclaration, so we need
221222
// to handle them explicitly.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
8787
const auto ConstantExpr = ignoringParenImpCasts(
8888
anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
8989
binaryOperator(hasLHS(IntegerExpr), hasRHS(IntegerExpr))));
90-
const auto IntegerCallExpr = ignoringParenImpCasts(
91-
callExpr(anyOf(hasType(isInteger()), hasType(enumType())),
92-
unless(isInTemplateInstantiation())));
90+
const auto IntegerCallExpr = ignoringParenImpCasts(callExpr(
91+
anyOf(hasType(isInteger()), hasType(hasCanonicalType(enumType()))),
92+
unless(isInTemplateInstantiation())));
9393
const auto SizeOfExpr = sizeOfExpr(hasArgumentOfType(
9494
hasUnqualifiedDesugaredType(type().bind("sizeof-arg-type"))));
9595
const auto SizeOfZero =
@@ -147,8 +147,8 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
147147
const auto StructAddrOfExpr = unaryOperator(
148148
hasOperatorName("&"), hasUnaryOperand(ignoringParenImpCasts(
149149
hasType(hasCanonicalType(recordType())))));
150-
const auto PointerToStructType =
151-
hasUnqualifiedDesugaredType(pointerType(pointee(recordType())));
150+
const auto PointerToStructType = hasUnqualifiedDesugaredType(
151+
pointerType(pointee(hasCanonicalType(recordType()))));
152152
const auto PointerToStructExpr = ignoringParenImpCasts(expr(
153153
hasType(hasCanonicalType(PointerToStructType)), unless(cxxThisExpr())));
154154

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ void SmartPtrArrayMismatchCheck::registerMatchers(MatchFinder *Finder) {
6767
auto FindConstructExpr =
6868
cxxConstructExpr(
6969
hasDeclaration(FindConstructor), argumentCountIs(1),
70-
hasArgument(
71-
0, cxxNewExpr(isArray(), hasType(pointerType(pointee(
72-
equalsBoundNode(PointerTypeN)))))
73-
.bind(NewExprN)))
70+
hasArgument(0,
71+
cxxNewExpr(isArray(),
72+
hasType(hasCanonicalType(pointerType(
73+
pointee(equalsBoundNode(PointerTypeN))))))
74+
.bind(NewExprN)))
7475
.bind(ConstructExprN);
7576
Finder->addMatcher(FindConstructExpr, this);
7677
}
@@ -101,7 +102,7 @@ void SmartPtrArrayMismatchCheck::check(const MatchFinder::MatchResult &Result) {
101102
SourceRange TemplateArgumentRange = TSTypeLoc.getArgLoc(0)
102103
.getTypeSourceInfo()
103104
->getTypeLoc()
104-
.getLocalSourceRange();
105+
.getSourceRange();
105106
D << TemplateArgumentRange;
106107

107108
if (isInSingleDeclStmt(VarOrField)) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
130130
// case of overloaded functions, so detection of redundant casts is trickier
131131
// in this case. Don't emit "redundant cast" warnings for function
132132
// pointer/reference types.
133-
if (SourceTypeAsWritten == DestTypeAsWritten) {
133+
QualType Src = SourceTypeAsWritten, Dst = DestTypeAsWritten;
134+
if (const auto *ElTy = dyn_cast<ElaboratedType>(Src))
135+
Src = ElTy->getNamedType();
136+
if (const auto *ElTy = dyn_cast<ElaboratedType>(Dst))
137+
Dst = ElTy->getNamedType();
138+
if (Src == Dst) {
134139
diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
135140
<< FixItHint::CreateRemoval(ReplaceRange);
136141
return;

clang-tools-extra/clang-tidy/hicpp/MultiwayPathsCoveredCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ void MultiwayPathsCoveredCheck::registerMatchers(MatchFinder *Finder) {
3636
// otherwise the matcher does not work correctly, because it
3737
// will not explicitly ignore enum conditions.
3838
unless(ignoringImpCasts(
39-
declRefExpr(hasType(enumType())).bind("enum-condition"))))))
39+
declRefExpr(hasType(hasCanonicalType(enumType())))
40+
.bind("enum-condition"))))))
4041
.bind("switch"),
4142
this);
4243

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ void MisplacedConstCheck::registerMatchers(MatchFinder *Finder) {
2121
pointee(anyOf(isConstQualified(), ignoringParens(functionType()))))));
2222

2323
Finder->addMatcher(
24-
valueDecl(
25-
hasType(isConstQualified()),
26-
hasType(typedefType(hasDeclaration(anyOf(
27-
typedefDecl(NonConstAndNonFunctionPointerType).bind("typedef"),
28-
typeAliasDecl(NonConstAndNonFunctionPointerType)
29-
.bind("typeAlias"))))))
24+
valueDecl(hasType(qualType(
25+
isConstQualified(),
26+
elaboratedType(namesType(typedefType(hasDeclaration(
27+
anyOf(typedefDecl(NonConstAndNonFunctionPointerType)
28+
.bind("typedef"),
29+
typeAliasDecl(NonConstAndNonFunctionPointerType)
30+
.bind("typeAlias")))))))))
3031
.bind("decl"),
3132
this);
3233
}

clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ static bool canBeModified(ASTContext *Context, const Expr *E) {
400400
return true;
401401
if (const auto *Cast = Parents[0].get<ImplicitCastExpr>()) {
402402
if ((Cast->getCastKind() == CK_NoOp &&
403-
Cast->getType() == E->getType().withConst()) ||
403+
Context->hasSameType(Cast->getType(), E->getType().withConst())) ||
404404
(Cast->getCastKind() == CK_LValueToRValue &&
405405
!Cast->getType().isNull() && Cast->getType()->isFundamentalType()))
406406
return false;

clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ AST_MATCHER(CXXRecordDecl, isMoveConstructible) {
4848

4949
static TypeMatcher notTemplateSpecConstRefType() {
5050
return lValueReferenceType(
51-
pointee(unless(templateSpecializationType()), isConstQualified()));
51+
pointee(unless(elaboratedType(namesType(templateSpecializationType()))),
52+
isConstQualified()));
5253
}
5354

5455
static TypeMatcher nonConstValueType() {

clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,24 @@ static bool isCopyAssignmentAndCanBeDefaulted(ASTContext *Context,
153153
// ((Base*)this)->operator=((Base)Other);
154154
//
155155
// So we are looking for a member call that fulfills:
156-
if (match(traverse(TK_AsIs,
157-
compoundStmt(has(ignoringParenImpCasts(cxxMemberCallExpr(
158-
// - The object is an implicit cast of 'this' to a
159-
// pointer to
160-
// a base class.
161-
onImplicitObjectArgument(implicitCastExpr(
162-
hasImplicitDestinationType(
163-
pointsTo(type(equalsNode(Base)))),
164-
hasSourceExpression(cxxThisExpr()))),
165-
// - The called method is the operator=.
166-
callee(cxxMethodDecl(isCopyAssignmentOperator())),
167-
// - The argument is (an implicit cast to a Base of)
168-
// the argument taken by "Operator".
169-
argumentCountIs(1),
170-
hasArgument(0, declRefExpr(to(varDecl(
171-
equalsNode(Param)))))))))),
156+
if (match(traverse(
157+
TK_AsIs,
158+
compoundStmt(has(ignoringParenImpCasts(cxxMemberCallExpr(
159+
// - The object is an implicit cast of 'this' to a
160+
// pointer to
161+
// a base class.
162+
onImplicitObjectArgument(implicitCastExpr(
163+
hasImplicitDestinationType(hasCanonicalType(pointsTo(
164+
type(equalsNode(Base->getCanonicalTypeInternal()
165+
.getTypePtr()))))),
166+
hasSourceExpression(cxxThisExpr()))),
167+
// - The called method is the operator=.
168+
callee(cxxMethodDecl(isCopyAssignmentOperator())),
169+
// - The argument is (an implicit cast to a Base of)
170+
// the argument taken by "Operator".
171+
argumentCountIs(1),
172+
hasArgument(
173+
0, declRefExpr(to(varDecl(equalsNode(Param)))))))))),
172174
*Compound, *Context)
173175
.empty())
174176
return false;

clang-tools-extra/clang-tidy/modernize/UseTrailingReturnTypeCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ struct UnqualNameVisitor : public RecursiveASTVisitor<UnqualNameVisitor> {
9797
if (TL.getQualifierLoc() &&
9898
!TraverseNestedNameSpecifierLoc(TL.getQualifierLoc()))
9999
return false;
100-
return TraverseTypeLoc(TL.getNamedTypeLoc(), true);
100+
const auto *T = TL.getTypePtr();
101+
return TraverseTypeLoc(TL.getNamedTypeLoc(),
102+
T->getKeyword() != ETK_None || T->getQualifier());
101103
}
102104

103105
bool VisitDeclRefExpr(DeclRefExpr *S) {

clang-tools-extra/clangd/FindTarget.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,10 @@ class ExplicitReferenceCollector
950950
// ElaboratedTypeLoc will reports information for its inner type loc.
951951
// Otherwise we loose information about inner types loc's qualifier.
952952
TypeLoc Inner = L.getNamedTypeLoc().getUnqualifiedLoc();
953-
TypeLocsToSkip.insert(Inner.getBeginLoc());
953+
if (L.getBeginLoc() == Inner.getBeginLoc())
954+
return RecursiveASTVisitor::TraverseTypeLoc(Inner);
955+
else
956+
TypeLocsToSkip.insert(Inner.getBeginLoc());
954957
return RecursiveASTVisitor::TraverseElaboratedTypeLoc(L);
955958
}
956959

clang-tools-extra/clangd/unittests/ASTTests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
7272
template<typename T> class Foo {};
7373
^auto v = Foo<X>();
7474
)cpp",
75-
"Foo<class X>",
75+
"Foo<X>",
7676
},
7777
{
7878
R"cpp( // auto on initializer list.
@@ -93,7 +93,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
9393
return Foo();
9494
}
9595
)cpp",
96-
"struct Foo",
96+
"Foo",
9797
},
9898
{
9999
R"cpp( // decltype in trailing return type
@@ -102,7 +102,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
102102
return Foo();
103103
}
104104
)cpp",
105-
"struct Foo",
105+
"Foo",
106106
},
107107
{
108108
R"cpp( // auto in function return type
@@ -111,7 +111,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
111111
return Foo();
112112
}
113113
)cpp",
114-
"struct Foo",
114+
"Foo",
115115
},
116116
{
117117
R"cpp( // auto& in function return type
@@ -121,7 +121,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
121121
return x;
122122
}
123123
)cpp",
124-
"struct Foo",
124+
"Foo",
125125
},
126126
{
127127
R"cpp( // auto* in function return type
@@ -131,7 +131,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
131131
return x;
132132
}
133133
)cpp",
134-
"struct Foo",
134+
"Foo",
135135
},
136136
{
137137
R"cpp( // const auto& in function return type
@@ -141,7 +141,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
141141
return x;
142142
}
143143
)cpp",
144-
"struct Foo",
144+
"Foo",
145145
},
146146
{
147147
R"cpp( // decltype(auto) in function return (value)
@@ -150,7 +150,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
150150
return Foo();
151151
}
152152
)cpp",
153-
"struct Foo",
153+
"Foo",
154154
},
155155
{
156156
R"cpp( // decltype(auto) in function return (ref)
@@ -160,7 +160,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
160160
return (x);
161161
}
162162
)cpp",
163-
"struct Foo &",
163+
"Foo &",
164164
},
165165
{
166166
R"cpp( // decltype(auto) in function return (const ref)
@@ -170,7 +170,7 @@ TEST(GetDeducedType, KwAutoKwDecltypeExpansion) {
170170
return (x);
171171
}
172172
)cpp",
173-
"const struct Foo &",
173+
"const Foo &",
174174
},
175175
{
176176
R"cpp( // auto on alias

clang-tools-extra/clangd/unittests/DumpASTTests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ declaration: Var - root
121121
expression: DeclRef - operator+
122122
expression: MaterializeTemporary - lvalue
123123
expression: CXXTemporaryObject - Foo
124-
type: Record - Foo
124+
type: Elaborated
125+
type: Record - Foo
125126
expression: IntegerLiteral - 42
126127
)"},
127128
{R"cpp(

clang-tools-extra/clangd/unittests/FindTargetTests.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,13 +1612,14 @@ TEST_F(FindExplicitReferencesTest, All) {
16121612
{
16131613
R"cpp(
16141614
void foo() {
1615-
class {} $0^x;
1616-
int (*$1^fptr)(int $2^a, int) = nullptr;
1615+
$0^class {} $1^x;
1616+
int (*$2^fptr)(int $3^a, int) = nullptr;
16171617
}
16181618
)cpp",
1619-
"0: targets = {x}, decl\n"
1620-
"1: targets = {fptr}, decl\n"
1621-
"2: targets = {a}, decl\n"},
1619+
"0: targets = {}\n"
1620+
"1: targets = {x}, decl\n"
1621+
"2: targets = {fptr}, decl\n"
1622+
"3: targets = {a}, decl\n"},
16221623
// Namespace aliases should be handled properly.
16231624
{
16241625
R"cpp(

0 commit comments

Comments
 (0)