r361172 - Dump macro expansion information as needed when outputting the AST to JSON.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon May 20 09:46:44 PDT 2019
Author: aaronballman
Date: Mon May 20 09:46:44 2019
New Revision: 361172
URL: http://llvm.org/viewvc/llvm-project?rev=361172&view=rev
Log:
Dump macro expansion information as needed when outputting the AST to JSON.
Added:
cfe/trunk/test/AST/ast-dump-macro-json.c
Modified:
cfe/trunk/include/clang/AST/JSONNodeDumper.h
cfe/trunk/lib/AST/JSONNodeDumper.cpp
Modified: cfe/trunk/include/clang/AST/JSONNodeDumper.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/JSONNodeDumper.h?rev=361172&r1=361171&r2=361172&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/JSONNodeDumper.h (original)
+++ cfe/trunk/include/clang/AST/JSONNodeDumper.h Mon May 20 09:46:44 2019
@@ -132,6 +132,12 @@ class JSONNodeDumper
JOS.attribute(Key, Value);
}
+ // Creates a single SourceLocation JSON representation of the given location.
+ llvm::json::Object createBareSourceLocation(SourceLocation Loc);
+ // Creates a JSON representation of a SourceLocation based on its presumed
+ // spelling location. If the given location represents a macro invocation,
+ // this outputs two sub-objects: one for the spelling and one for the
+ // expansion location.
llvm::json::Object createSourceLocation(SourceLocation Loc);
llvm::json::Object createSourceRange(SourceRange R);
std::string createPointerRepresentation(const void *Ptr);
Modified: cfe/trunk/lib/AST/JSONNodeDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/JSONNodeDumper.cpp?rev=361172&r1=361171&r2=361172&view=diff
==============================================================================
--- cfe/trunk/lib/AST/JSONNodeDumper.cpp (original)
+++ cfe/trunk/lib/AST/JSONNodeDumper.cpp Mon May 20 09:46:44 2019
@@ -146,9 +146,9 @@ void JSONNodeDumper::Visit(const Generic
attributeOnlyIfTrue("selected", A.isSelected());
}
-llvm::json::Object JSONNodeDumper::createSourceLocation(SourceLocation Loc) {
- SourceLocation Spelling = SM.getSpellingLoc(Loc);
- PresumedLoc Presumed = SM.getPresumedLoc(Spelling);
+llvm::json::Object
+JSONNodeDumper::createBareSourceLocation(SourceLocation Loc) {
+ PresumedLoc Presumed = SM.getPresumedLoc(Loc);
if (Presumed.isInvalid())
return llvm::json::Object{};
@@ -158,6 +158,28 @@ llvm::json::Object JSONNodeDumper::creat
{"col", Presumed.getColumn()}};
}
+llvm::json::Object JSONNodeDumper::createSourceLocation(SourceLocation Loc) {
+ SourceLocation Spelling = SM.getSpellingLoc(Loc);
+ SourceLocation Expansion = SM.getExpansionLoc(Loc);
+
+ llvm::json::Object SLoc = createBareSourceLocation(Spelling);
+ if (Expansion != Spelling) {
+ // If the expansion and the spelling are different, output subobjects
+ // describing both locations.
+ llvm::json::Object ELoc = createBareSourceLocation(Expansion);
+
+ // If there is a macro expansion, add extra information if the interesting
+ // bit is the macro arg expansion.
+ if (SM.isMacroArgExpansion(Loc))
+ ELoc["isMacroArgExpansion"] = true;
+
+ return llvm::json::Object{{"spellingLoc", std::move(SLoc)},
+ {"expansionLoc", std::move(ELoc)}};
+ }
+
+ return SLoc;
+}
+
llvm::json::Object JSONNodeDumper::createSourceRange(SourceRange R) {
return llvm::json::Object{{"begin", createSourceLocation(R.getBegin())},
{"end", createSourceLocation(R.getEnd())}};
Added: cfe/trunk/test/AST/ast-dump-macro-json.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/AST/ast-dump-macro-json.c?rev=361172&view=auto
==============================================================================
--- cfe/trunk/test/AST/ast-dump-macro-json.c (added)
+++ cfe/trunk/test/AST/ast-dump-macro-json.c Mon May 20 09:46:44 2019
@@ -0,0 +1,179 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ast-dump=json %s | FileCheck %s
+
+#define FOO frobble
+#define BAR FOO
+
+void FOO(void);
+void BAR(void);
+
+#define BING(x) x
+
+void BING(quux)(void);
+
+#define BLIP(x, y) x ## y
+#define BLAP(x, y) BLIP(x, y)
+
+void BLAP(foo, __COUNTER__)(void);
+void BLAP(foo, __COUNTER__)(void);
+
+
+// CHECK: "kind": "FunctionDecl",
+// CHECK-NEXT: "loc": {
+// CHECK-NEXT: "expansionLoc": {
+// CHECK-NEXT: "col": 6,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 6
+// CHECK-NEXT: },
+// CHECK-NEXT: "spellingLoc": {
+// CHECK-NEXT: "col": 13,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 3
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "range": {
+// CHECK-NEXT: "begin": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 6
+// CHECK-NEXT: },
+// CHECK-NEXT: "end": {
+// CHECK-NEXT: "col": 14,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 6
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "name": "frobble",
+// CHECK-NEXT: "type": {
+// CHECK-NEXT: "qualType": "void (void)"
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+
+// CHECK: "kind": "FunctionDecl",
+// CHECK-NEXT: "loc": {
+// CHECK-NEXT: "expansionLoc": {
+// CHECK-NEXT: "col": 6,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 7
+// CHECK-NEXT: },
+// CHECK-NEXT: "spellingLoc": {
+// CHECK-NEXT: "col": 13,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 3
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "range": {
+// CHECK-NEXT: "begin": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 7
+// CHECK-NEXT: },
+// CHECK-NEXT: "end": {
+// CHECK-NEXT: "col": 14,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 7
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "previousDecl": "0x{{.*}}",
+// CHECK-NEXT: "name": "frobble",
+// CHECK-NEXT: "type": {
+// CHECK-NEXT: "qualType": "void (void)"
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+
+// CHECK: "kind": "FunctionDecl",
+// CHECK-NEXT: "loc": {
+// CHECK-NEXT: "expansionLoc": {
+// CHECK-NEXT: "col": 6,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "isMacroArgExpansion": true,
+// CHECK-NEXT: "line": 11
+// CHECK-NEXT: },
+// CHECK-NEXT: "spellingLoc": {
+// CHECK-NEXT: "col": 11,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 11
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "range": {
+// CHECK-NEXT: "begin": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 11
+// CHECK-NEXT: },
+// CHECK-NEXT: "end": {
+// CHECK-NEXT: "col": 21,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 11
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "name": "quux",
+// CHECK-NEXT: "type": {
+// CHECK-NEXT: "qualType": "void (void)"
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+
+// CHECK: "kind": "FunctionDecl",
+// CHECK-NEXT: "loc": {
+// CHECK-NEXT: "expansionLoc": {
+// CHECK-NEXT: "col": 6,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 16
+// CHECK-NEXT: },
+// CHECK-NEXT: "spellingLoc": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "<scratch space>",
+// CHECK-NEXT: "line": 3
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "range": {
+// CHECK-NEXT: "begin": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 16
+// CHECK-NEXT: },
+// CHECK-NEXT: "end": {
+// CHECK-NEXT: "col": 33,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 16
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "name": "foo0",
+// CHECK-NEXT: "type": {
+// CHECK-NEXT: "qualType": "void (void)"
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+
+// CHECK: "kind": "FunctionDecl",
+// CHECK-NEXT: "loc": {
+// CHECK-NEXT: "expansionLoc": {
+// CHECK-NEXT: "col": 6,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 17
+// CHECK-NEXT: },
+// CHECK-NEXT: "spellingLoc": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "<scratch space>",
+// CHECK-NEXT: "line": 5
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "range": {
+// CHECK-NEXT: "begin": {
+// CHECK-NEXT: "col": 1,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 17
+// CHECK-NEXT: },
+// CHECK-NEXT: "end": {
+// CHECK-NEXT: "col": 33,
+// CHECK-NEXT: "file": "{{.*}}",
+// CHECK-NEXT: "line": 17
+// CHECK-NEXT: }
+// CHECK-NEXT: },
+// CHECK-NEXT: "name": "foo1",
+// CHECK-NEXT: "type": {
+// CHECK-NEXT: "qualType": "void (void)"
+// CHECK-NEXT: }
+// CHECK-NEXT: }
More information about the cfe-commits
mailing list