[clang] [hmaptool] Add JSON dump option (PR #102648)

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 9 10:20:24 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Shoaib Meenai (smeenai)

<details>
<summary>Changes</summary>

The idea is that the output of `hmaptool dump --json` can be fed
directly back to `hmaptool write` for easy round-tripping.


---
Full diff: https://github.com/llvm/llvm-project/pull/102648.diff


1 Files Affected:

- (modified) clang/utils/hmaptool/hmaptool (+6-1) 


``````````diff
diff --git a/clang/utils/hmaptool/hmaptool b/clang/utils/hmaptool/hmaptool
index d7754632b162bf..ccbb9f7d8a1450 100755
--- a/clang/utils/hmaptool/hmaptool
+++ b/clang/utils/hmaptool/hmaptool
@@ -120,6 +120,9 @@ def action_dump(name, args):
     parser.add_option("-v", "--verbose", dest="verbose",
                       help="show more verbose output [%default]",
                       action="store_true", default=False)
+    parser.add_option("--json", dest="json",
+                      help="output as JSON [%default]",
+                      action="store_true", default=False)
     (opts, args) = parser.parse_args(args)
 
     if len(args) != 1:
@@ -130,7 +133,6 @@ def action_dump(name, args):
     hmap = HeaderMap.frompath(path)
 
     # Dump all of the buckets.
-    print ('Header Map: %s' % (path,))
     if opts.verbose:
         print ('headermap: %r' % (path,))
         print ('  num entries: %d' % (hmap.num_entries,))
@@ -149,7 +151,10 @@ def action_dump(name, args):
 
             print ("  bucket[%d]: %r -> (%r, %r) -- %d" % (
                 i, key, prefix, suffix, (hmap_hash(key) & (len(hmap.buckets) - 1))))
+    elif opts.json:
+        print(json.dumps({"mappings": dict(hmap.mappings)}, indent=4))
     else:
+        print ('Header Map: %s' % (path,))
         mappings = sorted(hmap.mappings)
         for key,value in mappings:
             print ("%s -> %s" % (key, value))

``````````

</details>


https://github.com/llvm/llvm-project/pull/102648


More information about the cfe-commits mailing list