]> granicus.if.org Git - clang/commitdiff
Dump actual line numbers when dumping the AST to JSON.
authorAaron Ballman <aaron@aaronballman.com>
Fri, 12 Jul 2019 16:53:57 +0000 (16:53 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Fri, 12 Jul 2019 16:53:57 +0000 (16:53 +0000)
The "line" attribute is now the physical line within the source file for the location. A "presumedLine" attribute is printed when the presumed line number does not match the given source line number. We continue to not print repeated line information in subsequent source locations, but we track presumed and actual lines separately.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@365919 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/JSONNodeDumper.h
lib/AST/JSONNodeDumper.cpp
test/AST/ast-dump-stmt-json.c

index fe155adda0fd3553d5595b199fe112f2232e8070..238e43aad78b02fee723af35644a76329c2724d8 100644 (file)
@@ -125,7 +125,7 @@ class JSONNodeDumper
   PrintingPolicy PrintPolicy;
   const comments::CommandTraits *Traits;
   StringRef LastLocFilename;
-  unsigned LastLocLine;
+  unsigned LastLocLine, LastLocPresumedLine;
 
   using InnerAttrVisitor = ConstAttrVisitor<JSONNodeDumper>;
   using InnerCommentVisitor =
@@ -142,7 +142,7 @@ class JSONNodeDumper
   }
 
   // Writes the attributes of a SourceLocation object without.
-  void writeBareSourceLocation(SourceLocation Loc);
+  void writeBareSourceLocation(SourceLocation Loc, bool IsSpelling);
 
   // Writes the attributes of a SourceLocation to JSON based on its presumed
   // spelling location. If the given location represents a macro invocation,
@@ -181,7 +181,7 @@ public:
                  const PrintingPolicy &PrintPolicy,
                  const comments::CommandTraits *Traits)
       : NodeStreamer(OS), SM(SrcMgr), Ctx(Ctx), PrintPolicy(PrintPolicy),
-        Traits(Traits), LastLocLine(0) {}
+        Traits(Traits), LastLocLine(0), LastLocPresumedLine(0) {}
 
   void Visit(const Attr *A);
   void Visit(const Stmt *Node);
index 60f3c0f8601f17e87901d06fc61d4b8e03f35fc0..04b933b0fb3065bfeb7b3772f0e63daa5d327e6b 100644 (file)
@@ -171,20 +171,28 @@ void JSONNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
   attributeOnlyIfTrue("selected", A.isSelected());
 }
 
-void JSONNodeDumper::writeBareSourceLocation(SourceLocation Loc) {
+void JSONNodeDumper::writeBareSourceLocation(SourceLocation Loc,
+                                             bool IsSpelling) {
   PresumedLoc Presumed = SM.getPresumedLoc(Loc);
-
+  unsigned ActualLine = IsSpelling ? SM.getSpellingLineNumber(Loc)
+                                   : SM.getExpansionLineNumber(Loc);
   if (Presumed.isValid()) {
     if (LastLocFilename != Presumed.getFilename()) {
       JOS.attribute("file", Presumed.getFilename());
-      JOS.attribute("line", Presumed.getLine());
-    } else if (LastLocLine != Presumed.getLine())
-      JOS.attribute("line", Presumed.getLine());
+      JOS.attribute("line", ActualLine);
+    } else if (LastLocLine != ActualLine)
+      JOS.attribute("line", ActualLine);
+
+    unsigned PresumedLine = Presumed.getLine();
+    if (ActualLine != PresumedLine && LastLocPresumedLine != PresumedLine)
+      JOS.attribute("presumedLine", PresumedLine);
+
     JOS.attribute("col", Presumed.getColumn());
     JOS.attribute("tokLen",
                   Lexer::MeasureTokenLength(Loc, SM, Ctx.getLangOpts()));
     LastLocFilename = Presumed.getFilename();
-    LastLocLine = Presumed.getLine();
+    LastLocPresumedLine = PresumedLine;
+    LastLocLine = ActualLine;
   }
 }
 
@@ -195,17 +203,18 @@ void JSONNodeDumper::writeSourceLocation(SourceLocation Loc) {
   if (Expansion != Spelling) {
     // If the expansion and the spelling are different, output subobjects
     // describing both locations.
-    JOS.attributeObject(
-        "spellingLoc", [Spelling, this] { writeBareSourceLocation(Spelling); });
+    JOS.attributeObject("spellingLoc", [Spelling, this] {
+      writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
+    });
     JOS.attributeObject("expansionLoc", [Expansion, Loc, this] {
-      writeBareSourceLocation(Expansion);
+      writeBareSourceLocation(Expansion, /*IsSpelling*/ false);
       // If there is a macro expansion, add extra information if the interesting
       // bit is the macro arg expansion.
       if (SM.isMacroArgExpansion(Loc))
         JOS.attribute("isMacroArgExpansion", true);
     });
   } else
-    writeBareSourceLocation(Spelling);
+    writeBareSourceLocation(Spelling, /*IsSpelling*/ true);
 }
 
 void JSONNodeDumper::writeSourceRange(SourceRange R) {
index ed1ccc9be0ce0abdd707827109183691ee53e5c8..03552a382f1a12348edd021ed9505b233d68000c 100644 (file)
@@ -133,6 +133,20 @@ void TestMiscStmts(void) {
   ({int a = 10; a;});
 }
 
+void TestLineNumbers(void) {
+  int a;
+
+#define FOO(x) x
+
+#line 100000
+  int b;
+
+#line 200000
+  FOO(1);
+
+#undef FOO
+}
+
 // NOTE: CHECK lines have been autogenerated by gen_ast_dump_json_test.py
 // using --filters=VarDecl,CompoundStmt
 
@@ -4777,3 +4791,136 @@ void TestMiscStmts(void) {
 // CHECK-NEXT:   }
 // CHECK-NEXT:  ]
 // CHECK-NEXT: }
+
+
+// CHECK:  "kind": "CompoundStmt",
+// CHECK-NEXT:  "range": {
+// CHECK-NEXT:   "begin": {
+// CHECK-NEXT:    "line": 136,
+// CHECK-NEXT:    "col": 28,
+// CHECK-NEXT:    "tokLen": 1
+// CHECK-NEXT:   },
+// CHECK-NEXT:   "end": {
+// CHECK-NEXT:    "line": 148,
+// CHECK-NEXT:    "presumedLine": 200003,
+// CHECK-NEXT:    "col": 1,
+// CHECK-NEXT:    "tokLen": 1
+// CHECK-NEXT:   }
+// CHECK-NEXT:  },
+// CHECK-NEXT:  "inner": [
+// CHECK-NEXT:   {
+// CHECK-NEXT:    "id": "0x{{.*}}",
+// CHECK-NEXT:    "kind": "DeclStmt",
+// CHECK-NEXT:    "range": {
+// CHECK-NEXT:     "begin": {
+// CHECK-NEXT:      "line": 137,
+// CHECK-NEXT:      "col": 3,
+// CHECK-NEXT:      "tokLen": 3
+// CHECK-NEXT:     },
+// CHECK-NEXT:     "end": {
+// CHECK-NEXT:      "col": 8,
+// CHECK-NEXT:      "tokLen": 1
+// CHECK-NEXT:     }
+// CHECK-NEXT:    },
+// CHECK-NEXT:    "inner": [
+// CHECK-NEXT:     {
+// CHECK-NEXT:      "id": "0x{{.*}}",
+// CHECK-NEXT:      "kind": "VarDecl",
+// CHECK-NEXT:      "loc": {
+// CHECK-NEXT:       "col": 7,
+// CHECK-NEXT:       "tokLen": 1
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "range": {
+// CHECK-NEXT:       "begin": {
+// CHECK-NEXT:        "col": 3,
+// CHECK-NEXT:        "tokLen": 3
+// CHECK-NEXT:       },
+// CHECK-NEXT:       "end": {
+// CHECK-NEXT:        "col": 7,
+// CHECK-NEXT:        "tokLen": 1
+// CHECK-NEXT:       }
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "name": "a",
+// CHECK-NEXT:      "type": {
+// CHECK-NEXT:       "qualType": "int"
+// CHECK-NEXT:      }
+// CHECK-NEXT:     }
+// CHECK-NEXT:    ]
+// CHECK-NEXT:   },
+// CHECK-NEXT:   {
+// CHECK-NEXT:    "id": "0x{{.*}}",
+// CHECK-NEXT:    "kind": "DeclStmt",
+// CHECK-NEXT:    "range": {
+// CHECK-NEXT:     "begin": {
+// CHECK-NEXT:      "line": 142,
+// CHECK-NEXT:      "presumedLine": 100000,
+// CHECK-NEXT:      "col": 3,
+// CHECK-NEXT:      "tokLen": 3
+// CHECK-NEXT:     },
+// CHECK-NEXT:     "end": {
+// CHECK-NEXT:      "col": 8,
+// CHECK-NEXT:      "tokLen": 1
+// CHECK-NEXT:     }
+// CHECK-NEXT:    },
+// CHECK-NEXT:    "inner": [
+// CHECK-NEXT:     {
+// CHECK-NEXT:      "id": "0x{{.*}}",
+// CHECK-NEXT:      "kind": "VarDecl",
+// CHECK-NEXT:      "loc": {
+// CHECK-NEXT:       "col": 7,
+// CHECK-NEXT:       "tokLen": 1
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "range": {
+// CHECK-NEXT:       "begin": {
+// CHECK-NEXT:        "col": 3,
+// CHECK-NEXT:        "tokLen": 3
+// CHECK-NEXT:       },
+// CHECK-NEXT:       "end": {
+// CHECK-NEXT:        "col": 7,
+// CHECK-NEXT:        "tokLen": 1
+// CHECK-NEXT:       }
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "name": "b",
+// CHECK-NEXT:      "type": {
+// CHECK-NEXT:       "qualType": "int"
+// CHECK-NEXT:      }
+// CHECK-NEXT:     }
+// CHECK-NEXT:    ]
+// CHECK-NEXT:   },
+// CHECK-NEXT:   {
+// CHECK-NEXT:    "id": "0x{{.*}}",
+// CHECK-NEXT:    "kind": "IntegerLiteral",
+// CHECK-NEXT:    "range": {
+// CHECK-NEXT:     "begin": {
+// CHECK-NEXT:      "spellingLoc": {
+// CHECK-NEXT:       "line": 145,
+// CHECK-NEXT:       "presumedLine": 200000,
+// CHECK-NEXT:       "col": 7,
+// CHECK-NEXT:       "tokLen": 1
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "expansionLoc": {
+// CHECK-NEXT:       "col": 3,
+// CHECK-NEXT:       "tokLen": 3,
+// CHECK-NEXT:       "isMacroArgExpansion": true
+// CHECK-NEXT:      }
+// CHECK-NEXT:     },
+// CHECK-NEXT:     "end": {
+// CHECK-NEXT:      "spellingLoc": {
+// CHECK-NEXT:       "col": 7,
+// CHECK-NEXT:       "tokLen": 1
+// CHECK-NEXT:      },
+// CHECK-NEXT:      "expansionLoc": {
+// CHECK-NEXT:       "col": 3,
+// CHECK-NEXT:       "tokLen": 3,
+// CHECK-NEXT:       "isMacroArgExpansion": true
+// CHECK-NEXT:      }
+// CHECK-NEXT:     }
+// CHECK-NEXT:    },
+// CHECK-NEXT:    "type": {
+// CHECK-NEXT:     "qualType": "int"
+// CHECK-NEXT:    },
+// CHECK-NEXT:    "valueCategory": "rvalue",
+// CHECK-NEXT:    "value": "1"
+// CHECK-NEXT:   }
+// CHECK-NEXT:  ]
+// CHECK-NEXT: }