generate_natvis.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. import argparse
  3. import itertools
  4. import jinja2
  5. import os
  6. import re
  7. import sys
  8. def semver(v):
  9. if not re.fullmatch(r'\d+\.\d+\.\d+', v):
  10. raise ValueError
  11. return v
  12. if __name__ == '__main__':
  13. parser = argparse.ArgumentParser()
  14. parser.add_argument('--version', required=True, type=semver, help='Library version number')
  15. parser.add_argument('output', help='Output directory for nlohmann_json.natvis')
  16. args = parser.parse_args()
  17. namespaces = ['nlohmann']
  18. abi_prefix = 'json_abi'
  19. abi_tags = ['_diag', '_ldvcmp']
  20. version = '_v' + args.version.replace('.', '_')
  21. inline_namespaces = []
  22. # generate all combinations of inline namespace names
  23. for n in range(0, len(abi_tags) + 1):
  24. for tags in itertools.combinations(abi_tags, n):
  25. ns = abi_prefix + ''.join(tags)
  26. inline_namespaces += [ns, ns + version]
  27. namespaces += [f'{namespaces[0]}::{ns}' for ns in inline_namespaces]
  28. env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=sys.path[0]), autoescape=True, trim_blocks=True,
  29. lstrip_blocks=True, keep_trailing_newline=True)
  30. template = env.get_template('nlohmann_json.natvis.j2')
  31. natvis = template.render(namespaces=namespaces)
  32. with open(os.path.join(args.output, 'nlohmann_json.natvis'), 'w') as f:
  33. f.write(natvis)