clang-parse-diagnostics-file 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python
  2. import os
  3. import plistlib
  4. def main():
  5. from optparse import OptionParser, OptionGroup
  6. parser = OptionParser("""\
  7. Usage: %prog [options] <path>
  8. Utility for dumping Clang-style logged diagnostics.\
  9. """)
  10. parser.add_option("-a", "--all", action="store_true", dest="all",
  11. default=False, help="dump all messages.")
  12. parser.add_option("-e", "--error", action="store_true", dest="error",
  13. default=False, help="dump 'error' messages.")
  14. parser.add_option("-f", "--fatal", action="store_true", dest="fatal",
  15. default=False, help="dump 'fatal error' messages.")
  16. parser.add_option("-i", "--ignored", action="store_true", dest="ignored",
  17. default=False, help="dump 'ignored' messages.")
  18. parser.add_option("-n", "--note", action="store_true", dest="note",
  19. default=False, help="dump 'note' messages.")
  20. parser.add_option("-w", "--warning", action="store_true", dest="warning",
  21. default=False, help="dump 'warning' messages.")
  22. (opts, args) = parser.parse_args()
  23. if len(args) != 1:
  24. parser.error("invalid number of arguments")
  25. levels = {'error': False, 'fatal error': False, 'ignored': False,
  26. 'note': False, 'warning': False}
  27. if opts.error:
  28. levels['error'] = True
  29. if opts.fatal:
  30. levels['fatal error'] = True
  31. if opts.ignored:
  32. levels['ignored'] = True
  33. if opts.note:
  34. levels['note'] = True
  35. if opts.warning:
  36. levels['warning'] = True
  37. path, = args
  38. # Read the diagnostics log.
  39. f = open(path)
  40. try:
  41. data = f.read()
  42. finally:
  43. f.close()
  44. # Complete the plist (the log itself is just the chunks).
  45. data = """\
  46. <?xml version="1.0" encoding="UTF-8"?>
  47. <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
  48. "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  49. <plist version="1.0">
  50. <array>
  51. %s
  52. </array>
  53. </plist>""" % data
  54. # Get the list of files and diagnostics to report.
  55. to_report = []
  56. diags = plistlib.readPlistFromString(data)
  57. for file_diags in diags:
  58. file = file_diags.get('main-file')
  59. # Ignore diagnostics for 'conftest.c', which is the file autoconf uses
  60. # for its tests (which frequently will have warnings).
  61. if os.path.basename(file) == 'conftest.c':
  62. continue
  63. # Get the diagnostics for the selected levels.
  64. selected_diags = [d
  65. for d in file_diags.get('diagnostics', ())
  66. if levels[d.get('level')] or opts.all]
  67. if selected_diags:
  68. to_report.append((file, selected_diags))
  69. # If there are no diagnostics to report, show nothing.
  70. if not to_report:
  71. return
  72. # Otherwise, print out the diagnostics.
  73. print
  74. print "**** BUILD DIAGNOSTICS ****"
  75. for file,selected_diags in to_report:
  76. print "*** %s ***" % file
  77. for d in selected_diags:
  78. print " %s:%s:%s: %s: %s" % (
  79. d.get('filename'), d.get('line'), d.get('column'),
  80. d.get('level'), d.get('message'))
  81. if __name__ == "__main__":
  82. main()