[llvm] [TableGen] Include source location in JSON dump (PR #79028)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 22 10:31:15 PST 2024


https://github.com/ostannard created https://github.com/llvm/llvm-project/pull/79028

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

>From a1e960c164cea6e6ab853cc5d2763e7566e7c161 Mon Sep 17 00:00:00 2001
From: Oliver Stannard <oliver.stannard at arm.com>
Date: Mon, 22 Jan 2024 17:13:43 +0000
Subject: [PATCH] [TableGen] Include source location in JSON dump

This adds a '!loc' field to each record containing the file name and
line number of the record declaration.
---
 llvm/lib/TableGen/JSONBackend.cpp | 2 ++
 llvm/test/TableGen/JSON.td        | 2 ++
 2 files changed, 4 insertions(+)

diff --git a/llvm/lib/TableGen/JSONBackend.cpp b/llvm/lib/TableGen/JSONBackend.cpp
index 2a3f522a9c0ef2..9b489cf7deddbe 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"
@@ -158,6 +159,7 @@ void JSONEmitter::run(raw_ostream &OS) {
 
     obj["!name"] = Name;
     obj["!anonymous"] = Def.isAnonymous();
+    obj["!loc"] = SrcMgr.getFormattedLocationNoOffset(Def.getLoc().front());
 
     root[Name] = std::move(obj);
 
diff --git a/llvm/test/TableGen/JSON.td b/llvm/test/TableGen/JSON.td
index 3fb2ec4014fbcd..20d3c0fcc13198 100644
--- a/llvm/test/TableGen/JSON.td
+++ b/llvm/test/TableGen/JSON.td
@@ -3,12 +3,14 @@
 // CHECK: data['!tablegen_json_version'] == 1
 
 // CHECK: all(data[s]['!name'] == s for s in data if not s.startswith("!"))
+// CHECK: all(data[s]['!loc'].startswith("JSON.td:") for s in data if not s.startswith("!"))
 
 class Base {}
 class Intermediate : Base {}
 class Derived : Intermediate {}
 
 def D : Intermediate {}
+// CHECK: data['D']['!loc'] == 'JSON.td:12'
 // 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