This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH 22/22] Add contrib/get-static-analysis.py
- From: David Malcolm <dmalcolm at redhat dot com>
- To: gcc-patches at gcc dot gnu dot org
- Cc: David Malcolm <dmalcolm at redhat dot com>
- Date: Fri, 4 Aug 2017 18:04:53 -0400
- Subject: [PATCH 22/22] Add contrib/get-static-analysis.py
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=dmalcolm at redhat dot com
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C57B34E4F3
- References: <1501884293-9047-1-git-send-email-dmalcolm@redhat.com>
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