Skip to content

Commit f9ead46

Browse files
authored
[AST] Only dump desugared type when visibly different (llvm#65214)
These are an artifact of how types are structured but serve little purpose, merely showing that the type is sugared in some way. For example, ElaboratedType's existence means struct S gets printed as 'struct S':'struct S' in the AST, which is unnecessary visual clutter. Note that skipping the second print when the types have the same string matches what we do for diagnostics, where the aka will be skipped.
1 parent cf07904 commit f9ead46

File tree

60 files changed

+269
-390
lines changed

Some content is hidden

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

60 files changed

+269
-390
lines changed

clang/docs/HowToSetupToolingForLLVM.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Examples:
172172
clang::ASTConsumer *newASTConsumer() (CompoundStmt 0x44da290 </home/alexfh/local/llvm/tools/clang/tools/clang-check/ClangCheck.cpp:64:40, line:72:3>
173173
(IfStmt 0x44d97c8 <line:65:5, line:66:45>
174174
<<<NULL>>>
175-
(ImplicitCastExpr 0x44d96d0 <line:65:9> '_Bool':'_Bool' <UserDefinedConversion>
175+
(ImplicitCastExpr 0x44d96d0 <line:65:9> '_Bool' <UserDefinedConversion>
176176
...
177177
$ clang-check tools/clang/tools/clang-check/ClangCheck.cpp -ast-print -ast-dump-filter ActionFactory::newASTConsumer
178178
Processing: tools/clang/tools/clang-check/ClangCheck.cpp.

clang/docs/ReleaseNotes.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ ABI Changes in This Version
113113
- Following the SystemV ABI for x86-64, ``__int128`` arguments will no longer
114114
be split between a register and a stack slot.
115115

116+
AST Dumping Potentially Breaking Changes
117+
----------------------------------------
118+
- When dumping a sugared type, Clang will no longer print the desugared type if
119+
its textual representation is the same as the sugared one. This applies to
120+
both text dumps of the form ``'foo':'foo'`` which will now be dumped as just
121+
``'foo'``, and JSON dumps of the form:
122+
123+
.. code-block:: json
124+
125+
"type": {
126+
"qualType": "foo",
127+
"desugaredQualType": "foo"
128+
}
129+
130+
which will now be dumped as just:
131+
132+
.. code-block:: json
133+
134+
"type": {
135+
"qualType": "foo"
136+
}
137+
116138
What's New in Clang |release|?
117139
==============================
118140
Some of the major new features and improvements to Clang are listed

clang/lib/AST/JSONNodeDumper.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,16 @@ std::string JSONNodeDumper::createPointerRepresentation(const void *Ptr) {
315315

316316
llvm::json::Object JSONNodeDumper::createQualType(QualType QT, bool Desugar) {
317317
SplitQualType SQT = QT.split();
318-
llvm::json::Object Ret{{"qualType", QualType::getAsString(SQT, PrintPolicy)}};
318+
std::string SQTS = QualType::getAsString(SQT, PrintPolicy);
319+
llvm::json::Object Ret{{"qualType", SQTS}};
319320

320321
if (Desugar && !QT.isNull()) {
321322
SplitQualType DSQT = QT.getSplitDesugaredType();
322-
if (DSQT != SQT)
323-
Ret["desugaredQualType"] = QualType::getAsString(DSQT, PrintPolicy);
323+
if (DSQT != SQT) {
324+
std::string DSQTS = QualType::getAsString(DSQT, PrintPolicy);
325+
if (DSQTS != SQTS)
326+
Ret["desugaredQualType"] = DSQTS;
327+
}
324328
if (const auto *TT = QT->getAs<TypedefType>())
325329
Ret["typeAliasDeclId"] = createPointerRepresentation(TT->getDecl());
326330
}

clang/lib/AST/TextNodeDumper.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,18 @@ void TextNodeDumper::dumpBareType(QualType T, bool Desugar) {
692692
ColorScope Color(OS, ShowColors, TypeColor);
693693

694694
SplitQualType T_split = T.split();
695-
OS << "'" << QualType::getAsString(T_split, PrintPolicy) << "'";
695+
std::string T_str = QualType::getAsString(T_split, PrintPolicy);
696+
OS << "'" << T_str << "'";
696697

697698
if (Desugar && !T.isNull()) {
698-
// If the type is sugared, also dump a (shallow) desugared type.
699+
// If the type is sugared, also dump a (shallow) desugared type when
700+
// it is visibly different.
699701
SplitQualType D_split = T.getSplitDesugaredType();
700-
if (T_split != D_split)
701-
OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'";
702+
if (T_split != D_split) {
703+
std::string D_str = QualType::getAsString(D_split, PrintPolicy);
704+
if (T_str != D_str)
705+
OS << ":'" << QualType::getAsString(D_split, PrintPolicy) << "'";
706+
}
702707
}
703708
}
704709

clang/test/AST/HLSL/this-reference-template.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ void main() {
3535
// CHECK: -CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <line:8:3, line:10:3> line:8:5 used getFirst 'int ()' implicit_instantiation implicit-inline
3636
// CHECK-NEXT:-CompoundStmt 0x{{[0-9A-Fa-f]+}} <col:16, line:10:3>
3737
// CHECK-NEXT:-ReturnStmt 0x{{[0-9A-Fa-f]+}} <line:9:4, col:16>
38-
// CHECK-NEXT:-ImplicitCastExpr 0x{{[0-9A-Fa-f]+}} <col:11, col:16> 'int':'int' <LValueToRValue>
39-
// CHECK-NEXT:-MemberExpr 0x{{[0-9A-Fa-f]+}} <col:11, col:16> 'int':'int' lvalue .First 0x{{[0-9A-Fa-f]+}}
38+
// CHECK-NEXT:-ImplicitCastExpr 0x{{[0-9A-Fa-f]+}} <col:11, col:16> 'int' <LValueToRValue>
39+
// CHECK-NEXT:-MemberExpr 0x{{[0-9A-Fa-f]+}} <col:11, col:16> 'int' lvalue .First 0x{{[0-9A-Fa-f]+}}
4040
// CHECK-NEXT:-CXXThisExpr 0x{{[0-9A-Fa-f]+}} <col:11> 'Pair<int, float>' lvalue this
4141
// CHECK-NEXT:-CXXMethodDecl 0x{{[0-9A-Fa-f]+}} <line:12:3, line:14:3> line:12:5 used getSecond 'float ()' implicit_instantiation implicit-inline
4242
// CHECK-NEXT:-CompoundStmt 0x{{[0-9A-Fa-f]+}} <col:17, line:14:3>
4343
// CHECK-NEXT:-ReturnStmt 0x{{[0-9A-Fa-f]+}} <line:13:5, col:12>
44-
// CHECK-NEXT:-ImplicitCastExpr 0x{{[0-9A-Fa-f]+}} <col:12> 'float':'float' <LValueToRValue>
45-
// CHECK-NEXT:-MemberExpr 0x{{[0-9A-Fa-f]+}} <col:12> 'float':'float' lvalue .Second 0x{{[0-9A-Fa-f]+}}
44+
// CHECK-NEXT:-ImplicitCastExpr 0x{{[0-9A-Fa-f]+}} <col:12> 'float' <LValueToRValue>
45+
// CHECK-NEXT:-MemberExpr 0x{{[0-9A-Fa-f]+}} <col:12> 'float' lvalue .Second 0x{{[0-9A-Fa-f]+}}
4646
// CHECK-NEXT:-CXXThisExpr 0x{{[0-9A-Fa-f]+}} <col:12> 'Pair<int, float>' lvalue implicit this

clang/test/AST/ast-dump-APValue-anon-union.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ union U1 {
3030

3131
void Test() {
3232
constexpr S0 s0{};
33-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s0 'const S0':'const S0' constexpr listinit
33+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s0 'const S0' constexpr listinit
3434
// CHECK-NEXT: | |-value: Struct
3535
// CHECK-NEXT: | | `-field: Union .i Int 42
3636

3737
constexpr U0 u0a{};
38-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u0a 'const U0':'const U0' constexpr listinit
38+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u0a 'const U0' constexpr listinit
3939
// CHECK-NEXT: | |-value: Union None
4040

4141
constexpr U0 u0b{3.1415f};
42-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u0b 'const U0':'const U0' constexpr listinit
42+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u0b 'const U0' constexpr listinit
4343
// CHECK-NEXT: | |-value: Union .U0::(anonymous union at {{.*}}) Union .f Float 3.141500e+00
4444

4545
constexpr U1 u1a{};
46-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u1a 'const U1':'const U1' constexpr listinit
46+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u1a 'const U1' constexpr listinit
4747
// CHECK-NEXT: | |-value: Union .U1::(anonymous union at {{.*}}) Union .f Float 0.000000e+00
4848

4949
constexpr U1 u1b{3.1415f};
50-
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u1b 'const U1':'const U1' constexpr listinit
50+
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u1b 'const U1' constexpr listinit
5151
// CHECK-NEXT: |-value: Union .U1::(anonymous union at {{.*}}) Union .f Float 3.141500e+00
5252
}

clang/test/AST/ast-dump-APValue-struct.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,25 @@ struct S5 : S4 {
6060

6161
void Test() {
6262
constexpr S0 s0{};
63-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s0 'const S0':'const S0' constexpr listinit
63+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s0 'const S0' constexpr listinit
6464
// CHECK-NEXT: | |-value: Struct
6565
// CHECK-NEXT: | | `-fields: Int 0, Union .j Int 0
6666

6767
constexpr S1 s1{};
68-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s1 'const S1':'const S1' constexpr listinit
68+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s1 'const S1' constexpr listinit
6969
// CHECK-NEXT: | |-value: Struct
7070
// CHECK-NEXT: | | |-field: Int 0
7171
// CHECK-NEXT: | | `-field: Union .s
7272
// CHECK-NEXT: | | `-Struct
7373
// CHECK-NEXT: | | `-field: Int 0
7474

7575
constexpr S2 s2{};
76-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s2 'const S2':'const S2' constexpr listinit
76+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s2 'const S2' constexpr listinit
7777
// CHECK-NEXT: | |-value: Struct
7878
// CHECK-NEXT: | | `-fields: Int 0, Union .u Union .j Int 0
7979

8080
constexpr S3 s3{};
81-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s3 'const S3':'const S3' constexpr listinit
81+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s3 'const S3' constexpr listinit
8282
// CHECK-NEXT: | |-value: Struct
8383
// CHECK-NEXT: | | |-field: Int 0
8484
// CHECK-NEXT: | | `-field: Union .u
@@ -87,7 +87,7 @@ void Test() {
8787
// CHECK-NEXT: | | `-field: Int 0
8888

8989
constexpr S4 s4{};
90-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s4 'const S4':'const S4' constexpr listinit
90+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s4 'const S4' constexpr listinit
9191
// CHECK-NEXT: | |-value: Struct
9292
// CHECK-NEXT: | | |-base: Struct
9393
// CHECK-NEXT: | | | `-fields: Int 0, Union .j Int 0
@@ -96,7 +96,7 @@ void Test() {
9696
// CHECK-NEXT: | | `-fields: Int 4, Int 5, Int 6
9797

9898
constexpr S5 s5{};
99-
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s5 'const S5':'const S5' constexpr listinit
99+
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} s5 'const S5' constexpr listinit
100100
// CHECK-NEXT: |-value: Struct
101101
// CHECK-NEXT: | |-base: Struct
102102
// CHECK-NEXT: | | |-base: Struct

clang/test/AST/ast-dump-APValue-union.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ union U3 {
3939

4040
void Test() {
4141
constexpr U0 u0{};
42-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u0 'const U0':'const U0' constexpr listinit
42+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u0 'const U0' constexpr listinit
4343
// CHECK-NEXT: | |-value: Union .i Int 42
4444

4545
constexpr U1 u1{};
46-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u1 'const U1':'const U1' constexpr listinit
46+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u1 'const U1' constexpr listinit
4747
// CHECK-NEXT: | |-value: Union .uinner Union .f Float 3.141500e+00
4848

4949
constexpr U2 u2{};
50-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u2 'const U2':'const U2' constexpr listinit
50+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u2 'const U2' constexpr listinit
5151
// CHECK-NEXT: | |-value: Union .uinner
5252
// CHECK-NEXT: | | `-Union .arr
5353
// CHECK-NEXT: | | `-Array size=2
5454
// CHECK-NEXT: | | `-elements: Int 1, Int 2
5555

5656
constexpr U3 u3a = {.f = 3.1415};
57-
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u3a 'const U3':'const U3' constexpr cinit
57+
// CHECK: | `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u3a 'const U3' constexpr cinit
5858
// CHECK-NEXT: | |-value: Union .f Float 3.141500e+00
5959

6060
constexpr U3 u3b = {.uinner = {}};
61-
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u3b 'const U3':'const U3' constexpr cinit
61+
// CHECK: `-VarDecl {{.*}} <col:{{.*}}, col:{{.*}}> col:{{.*}} u3b 'const U3' constexpr cinit
6262
// CHECK-NEXT: |-value: Union .uinner Union .d Float 3.141500e+00
6363
}

clang/test/AST/ast-dump-attr.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,17 +146,17 @@ struct C { char a[16]; };
146146
// CHECK: ClassTemplateSpecializationDecl {{.*}} struct my_union
147147
// CHECK: CXXRecordDecl {{.*}} implicit struct my_union
148148
// CHECK: FieldDecl {{.*}} buffer 'char[1024]'
149-
// CHECK-NEXT: AlignedAttr {{.*}} alignas 'TestAligns::A':'TestAligns::A'
150-
// CHECK-NEXT: AlignedAttr {{.*}} alignas 'TestAligns::B':'TestAligns::B'
151-
// CHECK-NEXT: AlignedAttr {{.*}} alignas 'TestAligns::C':'TestAligns::C'
149+
// CHECK-NEXT: AlignedAttr {{.*}} alignas 'TestAligns::A'
150+
// CHECK-NEXT: AlignedAttr {{.*}} alignas 'TestAligns::B'
151+
// CHECK-NEXT: AlignedAttr {{.*}} alignas 'TestAligns::C'
152152
my_union<A, B, C> my_union_val;
153153

154154
// CHECK: ClassTemplateSpecializationDecl {{.*}} struct my_union2
155155
// CHECK: CXXRecordDecl {{.*}} implicit struct my_union2
156156
// CHECK: FieldDecl {{.*}} buffer 'char[1024]'
157-
// CHECK-NEXT: AlignedAttr {{.*}} _Alignas 'TestAligns::A':'TestAligns::A'
158-
// CHECK-NEXT: AlignedAttr {{.*}} _Alignas 'TestAligns::B':'TestAligns::B'
159-
// CHECK-NEXT: AlignedAttr {{.*}} _Alignas 'TestAligns::C':'TestAligns::C'
157+
// CHECK-NEXT: AlignedAttr {{.*}} _Alignas 'TestAligns::A'
158+
// CHECK-NEXT: AlignedAttr {{.*}} _Alignas 'TestAligns::B'
159+
// CHECK-NEXT: AlignedAttr {{.*}} _Alignas 'TestAligns::C'
160160
my_union2<A, B, C> my_union2_val;
161161

162162
} // namespace TestAligns

clang/test/AST/ast-dump-decl-json.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,6 @@ void testParmVarDecl(int TestParmVarDecl);
13511351
// CHECK-NEXT: "isUsed": true,
13521352
// CHECK-NEXT: "name": "x",
13531353
// CHECK-NEXT: "type": {
1354-
// CHECK-NEXT: "desugaredQualType": "enum Enum",
13551354
// CHECK-NEXT: "qualType": "enum Enum"
13561355
// CHECK-NEXT: }
13571356
// CHECK-NEXT: },
@@ -1424,7 +1423,6 @@ void testParmVarDecl(int TestParmVarDecl);
14241423
// CHECK-NEXT: }
14251424
// CHECK-NEXT: },
14261425
// CHECK-NEXT: "type": {
1427-
// CHECK-NEXT: "desugaredQualType": "enum Enum",
14281426
// CHECK-NEXT: "qualType": "enum Enum"
14291427
// CHECK-NEXT: },
14301428
// CHECK-NEXT: "valueCategory": "prvalue",
@@ -1446,7 +1444,6 @@ void testParmVarDecl(int TestParmVarDecl);
14461444
// CHECK-NEXT: }
14471445
// CHECK-NEXT: },
14481446
// CHECK-NEXT: "type": {
1449-
// CHECK-NEXT: "desugaredQualType": "enum Enum",
14501447
// CHECK-NEXT: "qualType": "enum Enum"
14511448
// CHECK-NEXT: },
14521449
// CHECK-NEXT: "valueCategory": "lvalue",
@@ -1455,7 +1452,6 @@ void testParmVarDecl(int TestParmVarDecl);
14551452
// CHECK-NEXT: "kind": "ParmVarDecl",
14561453
// CHECK-NEXT: "name": "x",
14571454
// CHECK-NEXT: "type": {
1458-
// CHECK-NEXT: "desugaredQualType": "enum Enum",
14591455
// CHECK-NEXT: "qualType": "enum Enum"
14601456
// CHECK-NEXT: }
14611457
// CHECK-NEXT: }

clang/test/AST/ast-dump-decl-json.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,6 @@ void f(void) {
911911
// CHECK-NEXT: },
912912
// CHECK-NEXT: "name": "T",
913913
// CHECK-NEXT: "type": {
914-
// CHECK-NEXT: "desugaredQualType": "id",
915914
// CHECK-NEXT: "qualType": "id",
916915
// CHECK-NEXT: "typeAliasDeclId": "0x{{.*}}"
917916
// CHECK-NEXT: }

0 commit comments

Comments
 (0)