Skip to content

Commit 3c3048c

Browse files
committed
Include the mangled name in -ast-dump=json
I am planning to use this feature to make update_cc_test_checks.py less fragile by obtaining the mangled names directly from -ast-dump=json. Currently, it uses c-index-test which ignores the -triple=, etc. arguments that are in the RUN: line and therefore does not generate checks for some targets. The AST dump tests were updated using the following command: `python $LLVM_BINDIR/gen_ast_dump_json_test.py --update --source $LLVM_SRC/clang/test/AST/*-json.*` Reviewers: aaron.ballman Reviewed By: aaron.ballman Subscribers: rsmith, MaskRay, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69564
1 parent 709ea17 commit 3c3048c

17 files changed

+285
-4
lines changed

clang/include/clang/AST/JSONNodeDumper.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
#define LLVM_CLANG_AST_JSONNODEDUMPER_H
1717

1818
#include "clang/AST/ASTContext.h"
19-
#include "clang/AST/ASTNodeTraverser.h"
2019
#include "clang/AST/ASTDumperUtils.h"
20+
#include "clang/AST/ASTNodeTraverser.h"
2121
#include "clang/AST/AttrVisitor.h"
2222
#include "clang/AST/CommentCommandTraits.h"
2323
#include "clang/AST/CommentVisitor.h"
2424
#include "clang/AST/ExprCXX.h"
25+
#include "clang/AST/Mangle.h"
2526
#include "llvm/Support/JSON.h"
2627

2728
namespace clang {
@@ -122,6 +123,7 @@ class JSONNodeDumper
122123

123124
const SourceManager &SM;
124125
ASTContext& Ctx;
126+
ASTNameGenerator ASTNameGen;
125127
PrintingPolicy PrintPolicy;
126128
const comments::CommandTraits *Traits;
127129
StringRef LastLocFilename;
@@ -182,8 +184,9 @@ class JSONNodeDumper
182184
JSONNodeDumper(raw_ostream &OS, const SourceManager &SrcMgr, ASTContext &Ctx,
183185
const PrintingPolicy &PrintPolicy,
184186
const comments::CommandTraits *Traits)
185-
: NodeStreamer(OS), SM(SrcMgr), Ctx(Ctx), PrintPolicy(PrintPolicy),
186-
Traits(Traits), LastLocLine(0), LastLocPresumedLine(0) {}
187+
: NodeStreamer(OS), SM(SrcMgr), Ctx(Ctx), ASTNameGen(Ctx),
188+
PrintPolicy(PrintPolicy), Traits(Traits), LastLocLine(0),
189+
LastLocPresumedLine(0) {}
187190

188191
void Visit(const Attr *A);
189192
void Visit(const Stmt *Node);

clang/lib/AST/JSONNodeDumper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,12 @@ void JSONNodeDumper::VisitMemberPointerType(const MemberPointerType *MPT) {
688688
}
689689

690690
void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
691-
if (ND && ND->getDeclName())
691+
if (ND && ND->getDeclName()) {
692692
JOS.attribute("name", ND->getNameAsString());
693+
std::string MangledName = ASTNameGen.getName(ND);
694+
if (!MangledName.empty())
695+
JOS.attribute("mangledName", MangledName);
696+
}
693697
}
694698

695699
void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {

clang/test/AST/ast-dump-decl-context-json.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void S::Method() {}
8686
// CHECK-NEXT: }
8787
// CHECK-NEXT: },
8888
// CHECK-NEXT: "name": "Function",
89+
// CHECK-NEXT: "mangledName": "_ZN4Test2NS8FunctionEv",
8990
// CHECK-NEXT: "type": {
9091
// CHECK-NEXT: "qualType": "void ()"
9192
// CHECK-NEXT: }
@@ -116,6 +117,7 @@ void S::Method() {}
116117
// CHECK-NEXT: "parentDeclContextId": "0x{{.*}}",
117118
// CHECK-NEXT: "previousDecl": "0x{{.*}}",
118119
// CHECK-NEXT: "name": "Function",
120+
// CHECK-NEXT: "mangledName": "_ZN4Test2NS8FunctionEv",
119121
// CHECK-NEXT: "type": {
120122
// CHECK-NEXT: "qualType": "void ()"
121123
// CHECK-NEXT: },
@@ -261,6 +263,7 @@ void S::Method() {}
261263
// CHECK-NEXT: }
262264
// CHECK-NEXT: },
263265
// CHECK-NEXT: "name": "Method",
266+
// CHECK-NEXT: "mangledName": "_ZN4Test1S6MethodEv",
264267
// CHECK-NEXT: "type": {
265268
// CHECK-NEXT: "qualType": "void ()"
266269
// CHECK-NEXT: }
@@ -291,6 +294,7 @@ void S::Method() {}
291294
// CHECK-NEXT: "parentDeclContextId": "0x{{.*}}",
292295
// CHECK-NEXT: "previousDecl": "0x{{.*}}",
293296
// CHECK-NEXT: "name": "Method",
297+
// CHECK-NEXT: "mangledName": "_ZN4Test1S6MethodEv",
294298
// CHECK-NEXT: "type": {
295299
// CHECK-NEXT: "qualType": "void ()"
296300
// CHECK-NEXT: },

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ void testParmVarDecl(int TestParmVarDecl);
159159
// CHECK-NEXT: }
160160
// CHECK-NEXT: },
161161
// CHECK-NEXT: "name": "TestLocation",
162+
// CHECK-NEXT: "mangledName": "TestLocation",
162163
// CHECK-NEXT: "type": {
163164
// CHECK-NEXT: "qualType": "int"
164165
// CHECK-NEXT: }
@@ -186,6 +187,7 @@ void testParmVarDecl(int TestParmVarDecl);
186187
// CHECK-NEXT: }
187188
// CHECK-NEXT: },
188189
// CHECK-NEXT: "name": "TestLocationFromInclude",
190+
// CHECK-NEXT: "mangledName": "TestLocationFromInclude",
189191
// CHECK-NEXT: "type": {
190192
// CHECK-NEXT: "desugaredQualType": "int",
191193
// CHECK-NEXT: "qualType": "TestTypedefFromInclude",
@@ -1128,6 +1130,7 @@ void testParmVarDecl(int TestParmVarDecl);
11281130
// CHECK-NEXT: }
11291131
// CHECK-NEXT: },
11301132
// CHECK-NEXT: "name": "TestFunctionDecl",
1133+
// CHECK-NEXT: "mangledName": "TestFunctionDecl",
11311134
// CHECK-NEXT: "type": {
11321135
// CHECK-NEXT: "qualType": "int (int, enum (anonymous enum at {{.*}}:69:29))"
11331136
// CHECK-NEXT: },
@@ -1155,6 +1158,7 @@ void testParmVarDecl(int TestParmVarDecl);
11551158
// CHECK-NEXT: },
11561159
// CHECK-NEXT: "isUsed": true,
11571160
// CHECK-NEXT: "name": "x",
1161+
// CHECK-NEXT: "mangledName": "x",
11581162
// CHECK-NEXT: "type": {
11591163
// CHECK-NEXT: "qualType": "int"
11601164
// CHECK-NEXT: }
@@ -1180,6 +1184,7 @@ void testParmVarDecl(int TestParmVarDecl);
11801184
// CHECK-NEXT: }
11811185
// CHECK-NEXT: },
11821186
// CHECK-NEXT: "name": "y",
1187+
// CHECK-NEXT: "mangledName": "y",
11831188
// CHECK-NEXT: "type": {
11841189
// CHECK-NEXT: "desugaredQualType": "enum (anonymous at {{.*}}:69:29)",
11851190
// CHECK-NEXT: "qualType": "enum (anonymous enum at {{.*}}:69:29)"
@@ -1299,6 +1304,7 @@ void testParmVarDecl(int TestParmVarDecl);
12991304
// CHECK-NEXT: }
13001305
// CHECK-NEXT: },
13011306
// CHECK-NEXT: "name": "TestFunctionDecl2",
1307+
// CHECK-NEXT: "mangledName": "TestFunctionDecl2",
13021308
// CHECK-NEXT: "type": {
13031309
// CHECK-NEXT: "qualType": "int (enum Enum)"
13041310
// CHECK-NEXT: },
@@ -1325,6 +1331,7 @@ void testParmVarDecl(int TestParmVarDecl);
13251331
// CHECK-NEXT: },
13261332
// CHECK-NEXT: "isUsed": true,
13271333
// CHECK-NEXT: "name": "x",
1334+
// CHECK-NEXT: "mangledName": "x",
13281335
// CHECK-NEXT: "type": {
13291336
// CHECK-NEXT: "desugaredQualType": "enum Enum",
13301337
// CHECK-NEXT: "qualType": "enum Enum"
@@ -1468,6 +1475,7 @@ void testParmVarDecl(int TestParmVarDecl);
14681475
// CHECK-NEXT: }
14691476
// CHECK-NEXT: },
14701477
// CHECK-NEXT: "name": "TestFunctionDeclProto",
1478+
// CHECK-NEXT: "mangledName": "TestFunctionDeclProto",
14711479
// CHECK-NEXT: "type": {
14721480
// CHECK-NEXT: "qualType": "int (int)"
14731481
// CHECK-NEXT: },
@@ -1493,6 +1501,7 @@ void testParmVarDecl(int TestParmVarDecl);
14931501
// CHECK-NEXT: }
14941502
// CHECK-NEXT: },
14951503
// CHECK-NEXT: "name": "x",
1504+
// CHECK-NEXT: "mangledName": "x",
14961505
// CHECK-NEXT: "type": {
14971506
// CHECK-NEXT: "qualType": "int"
14981507
// CHECK-NEXT: }
@@ -1522,6 +1531,7 @@ void testParmVarDecl(int TestParmVarDecl);
15221531
// CHECK-NEXT: }
15231532
// CHECK-NEXT: },
15241533
// CHECK-NEXT: "name": "TestFunctionDeclNoProto",
1534+
// CHECK-NEXT: "mangledName": "TestFunctionDeclNoProto",
15251535
// CHECK-NEXT: "type": {
15261536
// CHECK-NEXT: "qualType": "void ()"
15271537
// CHECK-NEXT: }
@@ -1549,6 +1559,7 @@ void testParmVarDecl(int TestParmVarDecl);
15491559
// CHECK-NEXT: }
15501560
// CHECK-NEXT: },
15511561
// CHECK-NEXT: "name": "TestFunctionDeclSC",
1562+
// CHECK-NEXT: "mangledName": "TestFunctionDeclSC",
15521563
// CHECK-NEXT: "type": {
15531564
// CHECK-NEXT: "qualType": "int ()"
15541565
// CHECK-NEXT: },
@@ -1577,6 +1588,7 @@ void testParmVarDecl(int TestParmVarDecl);
15771588
// CHECK-NEXT: }
15781589
// CHECK-NEXT: },
15791590
// CHECK-NEXT: "name": "TestFunctionDeclInline",
1591+
// CHECK-NEXT: "mangledName": "TestFunctionDeclInline",
15801592
// CHECK-NEXT: "type": {
15811593
// CHECK-NEXT: "qualType": "int ()"
15821594
// CHECK-NEXT: },
@@ -1705,6 +1717,7 @@ void testParmVarDecl(int TestParmVarDecl);
17051717
// CHECK-NEXT: }
17061718
// CHECK-NEXT: },
17071719
// CHECK-NEXT: "name": "TestVarDecl",
1720+
// CHECK-NEXT: "mangledName": "TestVarDecl",
17081721
// CHECK-NEXT: "type": {
17091722
// CHECK-NEXT: "qualType": "int"
17101723
// CHECK-NEXT: }
@@ -1732,6 +1745,7 @@ void testParmVarDecl(int TestParmVarDecl);
17321745
// CHECK-NEXT: }
17331746
// CHECK-NEXT: },
17341747
// CHECK-NEXT: "name": "TestVarDeclSC",
1748+
// CHECK-NEXT: "mangledName": "TestVarDeclSC",
17351749
// CHECK-NEXT: "type": {
17361750
// CHECK-NEXT: "qualType": "int"
17371751
// CHECK-NEXT: },
@@ -1760,6 +1774,7 @@ void testParmVarDecl(int TestParmVarDecl);
17601774
// CHECK-NEXT: }
17611775
// CHECK-NEXT: },
17621776
// CHECK-NEXT: "name": "TestVarDeclThread",
1777+
// CHECK-NEXT: "mangledName": "TestVarDeclThread",
17631778
// CHECK-NEXT: "type": {
17641779
// CHECK-NEXT: "qualType": "int"
17651780
// CHECK-NEXT: },
@@ -1788,6 +1803,7 @@ void testParmVarDecl(int TestParmVarDecl);
17881803
// CHECK-NEXT: }
17891804
// CHECK-NEXT: },
17901805
// CHECK-NEXT: "name": "TestVarDeclInit",
1806+
// CHECK-NEXT: "mangledName": "TestVarDeclInit",
17911807
// CHECK-NEXT: "type": {
17921808
// CHECK-NEXT: "qualType": "int"
17931809
// CHECK-NEXT: },
@@ -1839,6 +1855,7 @@ void testParmVarDecl(int TestParmVarDecl);
18391855
// CHECK-NEXT: }
18401856
// CHECK-NEXT: },
18411857
// CHECK-NEXT: "name": "TestParmVarDecl",
1858+
// CHECK-NEXT: "mangledName": "TestParmVarDecl",
18421859
// CHECK-NEXT: "type": {
18431860
// CHECK-NEXT: "qualType": "int"
18441861
// CHECK-NEXT: }

0 commit comments

Comments
 (0)