This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 22/22] Add contrib/get-static-analysis.py


This patch adds a simple Python 2/3 script for reading the
static analysis "watermark" from object files, writing the
JSON to stdout (prettyprinting it with indentation and newlines
for ease of human reading).

contrib/ChangeLog:
	* get-static-analysis.py: New file.
---
 contrib/get-static-analysis.py | 47 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 contrib/get-static-analysis.py

diff --git a/contrib/get-static-analysis.py b/contrib/get-static-analysis.py
new file mode 100644
index 0000000..c246718
--- /dev/null
+++ b/contrib/get-static-analysis.py
@@ -0,0 +1,47 @@
+# FIXME
+# Extract static analysis results from input file and pretty-print JSON to stdout
+# This file is intended to be compatible with both Python 2 and Python 3
+
+import json
+import os
+import subprocess
+import sys
+import tempfile
+
+SECTION_NAME = '.gnu.build.attributes'
+
+def get_json_content(filename):
+    """
+    Extract the JSON from SECTION_NAME from filename, returning
+    as a bytes instance.
+    """
+    with tempfile.NamedTemporaryFile() as outfile:
+        try:
+            subprocess.check_call(['objcopy', '-O', 'binary',
+                                   '--only-section=%s' % SECTION_NAME,
+                                   '--set-section-flags',
+                                   '%s=alloc' % SECTION_NAME,
+                                   filename, outfile.name])
+        except subprocess.CalledProcessError:
+            if not os.path.exists(outfile.name):
+                outfile.delete = False
+            raise
+        with open(outfile.name, 'rb') as f_in:
+            buf = f_in.read()
+            if not buf:
+                raise ValueError('section not found: %s' % SECTION_NAME)
+            # Expect 16 bytes of header, then JSON, then a 0-terminator and padding
+            json_buf = buf[16:].split(b'\x00')[0]
+            return json_buf
+
+filename = sys.argv[1]
+try:
+    json_buf = get_json_content(filename)
+except subprocess.CalledProcessError:
+    sys.exit(1)
+except ValueError as exc:
+    print(exc)
+    sys.exit(1)
+json_str = json_buf.decode('utf-8')
+jv = json.loads(json_str)
+json.dump(jv, sys.stdout, indent=4)
-- 
1.8.5.3


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]