[clang] a763d98 - [gen_ast_dump_json_test.py] Add a --update flag

Alex Richardson via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 15 04:53:16 PST 2019


Author: Alex Richardson
Date: 2019-11-15T12:52:30Z
New Revision: a763d985012bdc5c5fc7bbc836b0dfddbb9af2d8

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

LOG: [gen_ast_dump_json_test.py] Add a --update flag

This will allow updating the JSON tests for new format changes. Instead of
simply appending the JSON to the input file, the script will now make a
copy of the input file up to the "CHECK lines have been autogenerated"
disclaimer and then append the new JSON.

See https://reviews.llvm.org/D70119

Added: 
    

Modified: 
    clang/test/AST/gen_ast_dump_json_test.py

Removed: 
    


################################################################################
diff  --git a/clang/test/AST/gen_ast_dump_json_test.py b/clang/test/AST/gen_ast_dump_json_test.py
index 54a5109aacff..4f4f37cd26d7 100644
--- a/clang/test/AST/gen_ast_dump_json_test.py
+++ b/clang/test/AST/gen_ast_dump_json_test.py
@@ -1,7 +1,5 @@
 #!/usr/bin/env python
-
 from collections import OrderedDict
-from sets import Set
 from shutil import copyfile
 import argparse
 import json
@@ -9,7 +7,8 @@
 import pprint
 import re
 import subprocess
-    
+import tempfile
+
 def normalize(dict_var):
     for k, v in dict_var.items():
         if isinstance(v, OrderedDict):
@@ -63,6 +62,7 @@ def main():
                         action="store", required=True)
     parser.add_argument("--filters", help="comma separated list of AST filters. Ex: --filters=TypedefDecl,BuiltinType",
                         action="store", default='')
+    parser.add_argument("--update", help="Update the file in-place", action="store_true")
 
     args = parser.parse_args()
 
@@ -76,7 +76,7 @@ def main():
         return -1
 
     options = args.opts.split(' ')
-    filters = Set(args.filters.split(',')) if args.filters else Set([])
+    filters = set(args.filters.split(',')) if args.filters else set()
     
     note = "// NOTE: CHECK lines have been autogenerated by " \
            "gen_ast_dump_json_test.py"
@@ -118,13 +118,14 @@ def main():
         
             filter_json(j, filters, out_asts)
         
-    partition = args.source.rpartition('.')
-    dest_path = '%s-json%s%s' % (partition[0], partition[1], partition[2])
-
-    print("Writing json appended source file to %s." %(dest_path))
-    copyfile(args.source, dest_path)    
-    with open(dest_path, "a") as f:
-        f.write("\n" + note + "\n")        
+    with tempfile.NamedTemporaryFile("w") as f:
+        with open(args.source, "r") as srcf:
+            for line in srcf.readlines():
+                # copy up to the note:
+                if line.rstrip() == note:
+                    break
+                f.write(line)
+        f.write(note + "\n")
         for out_ast in out_asts:
             append_str = json.dumps(out_ast, indent=1, ensure_ascii=False)
             out_str = '\n\n'
@@ -137,7 +138,15 @@ def main():
                     out_str += '// CHECK-NEXT: %s\n' %(append_line.rstrip())
                     
             f.write(out_str)
-    
+        f.flush()
+        if args.update:
+            print("Updating json appended source file to %s." %  args.source)
+            copyfile(f.name, args.source)
+        else:
+            partition = args.source.rpartition('.')
+            dest_path = '%s-json%s%s' % (partition[0], partition[1], partition[2])
+            print("Writing json appended source file to %s." % dest_path)
+            copyfile(f.name, dest_path)
     return 0
         
 if __name__ == '__main__':


        


More information about the cfe-commits mailing list