[clang][bytecode][NFC] Simplify visitDeclRef (#123380)
[llvm-project.git] / llvm / utils / merge-json.py
blobf5b9dcf11c4cdc2cb3af678f8e559a793de7ffbb
1 #!/usr/bin/env python
2 """A command line utility to merge two JSON files.
4 This is a python program that merges two JSON files into a single one. The
5 intended use for this is to combine generated 'compile_commands.json' files
6 created by CMake when performing an LLVM runtime build.
7 """
9 import argparse
10 import json
11 import sys
14 def main():
15 parser = argparse.ArgumentParser(description=__doc__)
16 parser.add_argument(
17 "-o",
18 type=str,
19 help="The output file to write JSON data to",
20 default=None,
21 nargs="?",
23 parser.add_argument(
24 "json_files", type=str, nargs="+", help="Input JSON files to merge"
26 args = parser.parse_args()
28 merged_data = []
30 for json_file in args.json_files:
31 try:
32 with open(json_file, "r") as f:
33 data = json.load(f)
34 merged_data.extend(data)
35 except (IOError, json.JSONDecodeError) as e:
36 continue
38 # Deduplicate by converting each entry to a tuple of sorted key-value pairs
39 unique_data = list({json.dumps(entry, sort_keys=True) for entry in merged_data})
40 unique_data = [json.loads(entry) for entry in unique_data]
42 with open(args.o, "w") as f:
43 json.dump(unique_data, f, indent=2)
46 if __name__ == "__main__":
47 main()