[llvm] 56602a4 - [TableGen] Include source location in JSON dump (#79028)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 24 09:07:24 PST 2024


Author: ostannard
Date: 2024-01-24T17:07:20Z
New Revision: 56602a48c735a1c906a9ec4e03a64fd9c937def3

URL: https://github.com/llvm/llvm-project/commit/56602a48c735a1c906a9ec4e03a64fd9c937def3
DIFF: https://github.com/llvm/llvm-project/commit/56602a48c735a1c906a9ec4e03a64fd9c937def3.diff

LOG: [TableGen] Include source location in JSON dump (#79028)

This adds a '!loc' field to each record containing the file name and
line number of the record declaration.

Added: 
    llvm/test/TableGen/JSON-locs.td

Modified: 
    llvm/docs/TableGen/BackEnds.rst
    llvm/lib/TableGen/JSONBackend.cpp
    llvm/test/TableGen/JSON.td

Removed: 
    


################################################################################
diff  --git a/llvm/docs/TableGen/BackEnds.rst b/llvm/docs/TableGen/BackEnds.rst
index 5cbb3232ef0238..742fea51bcf32b 100644
--- a/llvm/docs/TableGen/BackEnds.rst
+++ b/llvm/docs/TableGen/BackEnds.rst
@@ -506,6 +506,12 @@ following fixed keys:
   specified by the TableGen input (if it is ``false``), or invented by
   TableGen itself (if ``true``).
 
+* ``!locs``: an array of strings giving the source locations associated with
+  this record. For records instantiated from a ``multiclass``, this gives the
+  location of each ``def`` or ``defm``, starting with the inner-most
+  ``multiclass``, and ending with the top-level ``defm``. Each string contains
+  the file name and line number, separated by a colon.
+
 For each variable defined in a record, the ``def`` object for that
 record also has a key for the variable name. The corresponding value
 is a translation into JSON of the variable's value, using the

diff  --git a/llvm/lib/TableGen/JSONBackend.cpp b/llvm/lib/TableGen/JSONBackend.cpp
index 2a3f522a9c0ef2..cd10c22094e45b 100644
--- a/llvm/lib/TableGen/JSONBackend.cpp
+++ b/llvm/lib/TableGen/JSONBackend.cpp
@@ -15,6 +15,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/JSON.h"
+#include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
 #define DEBUG_TYPE "json-emitter"
@@ -159,6 +160,11 @@ void JSONEmitter::run(raw_ostream &OS) {
     obj["!name"] = Name;
     obj["!anonymous"] = Def.isAnonymous();
 
+    json::Array locs;
+    for (const SMLoc Loc : Def.getLoc())
+      locs.push_back(SrcMgr.getFormattedLocationNoOffset(Loc));
+    obj["!locs"] = std::move(locs);
+
     root[Name] = std::move(obj);
 
     // Add this def to the instance list for each of its superclasses.

diff  --git a/llvm/test/TableGen/JSON-locs.td b/llvm/test/TableGen/JSON-locs.td
new file mode 100644
index 00000000000000..38baf469314890
--- /dev/null
+++ b/llvm/test/TableGen/JSON-locs.td
@@ -0,0 +1,44 @@
+// RUN: llvm-tblgen -dump-json %s | %python %S/JSON-check.py %s
+
+def Simple {}
+// CHECK: data['Simple']['!locs'] == ['JSON-locs.td:3']
+
+multiclass Multiclass1 {
+  def Instance1 {}
+  def Instance2 {}
+}
+
+defm DefM1 : Multiclass1;
+
+// CHECK: data['DefM1Instance1']['!locs'] == ['JSON-locs.td:7', 'JSON-locs.td:11']
+// CHECK: data['DefM1Instance2']['!locs'] == ['JSON-locs.td:8', 'JSON-locs.td:11']
+
+multiclass Multiclass2 {
+  def Instance3 {}
+  def Instance4 {}
+}
+
+defm DefM2 : Multiclass1, Multiclass2;
+// CHECK: data['DefM2Instance1']['!locs'] == ['JSON-locs.td:7', 'JSON-locs.td:21']
+// CHECK: data['DefM2Instance2']['!locs'] == ['JSON-locs.td:8', 'JSON-locs.td:21']
+// CHECK: data['DefM2Instance3']['!locs'] == ['JSON-locs.td:17', 'JSON-locs.td:21']
+// CHECK: data['DefM2Instance4']['!locs'] == ['JSON-locs.td:18', 'JSON-locs.td:21']
+
+multiclass Multiclass3 {
+  defm InnerDefM : Multiclass1;
+  def Instance5 {}
+}
+
+defm DefM3: Multiclass3;
+// CHECK: data['DefM3InnerDefMInstance1']['!locs'] == ['JSON-locs.td:7', 'JSON-locs.td:28', 'JSON-locs.td:32']
+// CHECK: data['DefM3InnerDefMInstance2']['!locs'] == ['JSON-locs.td:8', 'JSON-locs.td:28', 'JSON-locs.td:32']
+// CHECK: data['DefM3Instance5']['!locs'] == ['JSON-locs.td:29', 'JSON-locs.td:32']
+
+class BaseClass {}
+class DerivedClass : BaseClass {}
+// Classes do not appear in the JSON, so do not get locations.
+// CHECK: 'BaseClass' not in data
+// CHECK: 'DerivedClass' not in data
+
+def ClassInstance : DerivedClass {}
+// CHECK: data['ClassInstance']['!locs'] == ['JSON-locs.td:43']

diff  --git a/llvm/test/TableGen/JSON.td b/llvm/test/TableGen/JSON.td
index 3fb2ec4014fbcd..4d8c3a466bfd07 100644
--- a/llvm/test/TableGen/JSON.td
+++ b/llvm/test/TableGen/JSON.td
@@ -3,12 +3,15 @@
 // CHECK: data['!tablegen_json_version'] == 1
 
 // CHECK: all(data[s]['!name'] == s for s in data if not s.startswith("!"))
+// CHECK: all('!locs' in data[s] for s in data if not s.startswith("!"))
+// CHECK: all(all(loc.startswith("JSON.td:") for loc in data[s]['!locs']) for s in data if not s.startswith("!"))
 
 class Base {}
 class Intermediate : Base {}
 class Derived : Intermediate {}
 
 def D : Intermediate {}
+// CHECK: data['D']['!locs'] == ['JSON.td:13']
 // CHECK: 'D' in data['!instanceof']['Base']
 // CHECK: 'D' in data['!instanceof']['Intermediate']
 // CHECK: 'D' not in data['!instanceof']['Derived']


        


More information about the llvm-commits mailing list