On 06.05.2015 21:43, David Malcolm wrote:
> gcc-python-plugin is a plugin for GCC 4.6 onwards which embeds the
> CPython interpreter within GCC, allowing you to write new compiler
> warnings in Python, generate code visualizations, etc.
> 
> It ships with "gcc-with-cpychecker", which implements static analysis
> passes for GCC aimed at finding bugs in CPython extensions.  In
> particular, it can automatically detect reference-counting errors:
>  http://gcc-python-plugin.readthedocs.org/en/latest/cpychecker.html
> 
> This release adds support for GCC 5.

I tried to compile it with current trunk on x86_64-unknown-linux-gnu
(CentOS7) with Python 2.7.5 (It's default in CentOS7), but unfortunately
the build failed. The reason is that the plugin #include's <Python.h>
before GCC's headers.

gcc/system.h #define's __STDC_FORMAT_MACROS in order to get PRId64 and
other similar macros defined by inttypes.h. Probably Python.h includes
it earlier without this definition. The recommended way
(https://gcc.gnu.org/onlinedocs/gccint/Plugin-API.html#Plugin-API) is
always include gcc-plugin.h first.

But I don't know, whether it's possible to do it without breaking
something else. So I attached a "quick and dirty" patch which simply
defines __STDC_FORMAT_MACROS in all files which require it. (disclaimer:
I looked into gcc-python-plugin sources for the first time yesterday). I
can't tell for sure that it works, but the testsuite which is run when I
run "make" passes without failures.

-- 
Regards,
    Mikhail Maltsev
diff --git a/.gitignore b/.gitignore
index 09559f8..a34acf4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,6 +34,6 @@ docs/_build
 
 # Logfiles generated during the selftest suite:
 *.c.cpychecker-log.txt
-*.c.*-refcount-errors.html
-*.c.*-refcount-traces.html
+# Changed this to make it work with Python 2 and a C++ test
+tests/**/*.c*-refcount-*.html
 tests/plugin/dumpfiles/input.c.*t.test-pass
diff --git a/cpybuilder.py b/cpybuilder.py
index 8d8cab7..7e64936 100644
--- a/cpybuilder.py
+++ b/cpybuilder.py
@@ -418,9 +418,11 @@ class CompilationUnit:
         self._definitions += text
 
     def as_str(self):
-        return ('/* Autogenerated by cpybuilder */\n' +
+        # Note: added a crutch to preserve line numbers (they are hardcoded in 
tests)
+        return ('/* Autogenerated by cpybuilder */\n'
+                '#define __STDC_FORMAT_MACROS\n' +
                 self._includes +
-                self.make_header('Prototypes') +
+                self.make_header('Prototypes').lstrip() +
                 self._prototypes + 
                 self.make_header('Definitions') +
                 self._definitions)
diff --git a/gcc-python-attribute.c b/gcc-python-attribute.c
index 2b528b4..e8f6473 100644
--- a/gcc-python-attribute.c
+++ b/gcc-python-attribute.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-callbacks.c b/gcc-python-callbacks.c
index e8a42aa..6613042 100644
--- a/gcc-python-callbacks.c
+++ b/gcc-python-callbacks.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 
diff --git a/gcc-python-callgraph.c b/gcc-python-callgraph.c
index b22b7b0..e05fb93 100644
--- a/gcc-python-callgraph.c
+++ b/gcc-python-callgraph.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-cfg.c b/gcc-python-cfg.c
index 84fc4fe..c9aef2a 100644
--- a/gcc-python-cfg.c
+++ b/gcc-python-cfg.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-closure.c b/gcc-python-closure.c
index 8df8a47..c0916b1 100644
--- a/gcc-python-closure.c
+++ b/gcc-python-closure.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include <gcc-plugin.h>
 
diff --git a/gcc-python-diagnostics.c b/gcc-python-diagnostics.c
index fd6f57a..7c87e34 100644
--- a/gcc-python-diagnostics.c
+++ b/gcc-python-diagnostics.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 
diff --git a/gcc-python-function.c b/gcc-python-function.c
index 96dedda..26b901f 100644
--- a/gcc-python-function.c
+++ b/gcc-python-function.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-gimple.c b/gcc-python-gimple.c
index 6fb4359..d71382d 100644
--- a/gcc-python-gimple.c
+++ b/gcc-python-gimple.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-location.c b/gcc-python-location.c
index 8ce09e7..9861d3e 100644
--- a/gcc-python-location.c
+++ b/gcc-python-location.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-option.c b/gcc-python-option.c
index 0f27dc8..eaebc46 100644
--- a/gcc-python-option.c
+++ b/gcc-python-option.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-parameter.c b/gcc-python-parameter.c
index bcd1af2..e47a87f 100644
--- a/gcc-python-parameter.c
+++ b/gcc-python-parameter.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-pass.c b/gcc-python-pass.c
index 2681796..0a3a2b2 100644
--- a/gcc-python-pass.c
+++ b/gcc-python-pass.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-pretty-printer.c b/gcc-python-pretty-printer.c
index 48c7b79..b2d905a 100644
--- a/gcc-python-pretty-printer.c
+++ b/gcc-python-pretty-printer.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-rtl.c b/gcc-python-rtl.c
index 0de8079..1886ddf 100644
--- a/gcc-python-rtl.c
+++ b/gcc-python-rtl.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-tree.c b/gcc-python-tree.c
index 9e2fb1a..af54233 100644
--- a/gcc-python-tree.c
+++ b/gcc-python-tree.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-variable.c b/gcc-python-variable.c
index bab1ec2..5e4e4b1 100644
--- a/gcc-python-variable.c
+++ b/gcc-python-variable.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python-version.c b/gcc-python-version.c
index 9261741..49089ce 100644
--- a/gcc-python-version.c
+++ b/gcc-python-version.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include <structseq.h>
 #include "gcc-python.h"
diff --git a/gcc-python-wrapper.c b/gcc-python-wrapper.c
index 951a04b..84bcc3e 100644
--- a/gcc-python-wrapper.c
+++ b/gcc-python-wrapper.c
@@ -90,6 +90,7 @@
 
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 #include "gcc-python-wrappers.h"
diff --git a/gcc-python.c b/gcc-python.c
index 0e75313..8e2e3bd 100644
--- a/gcc-python.c
+++ b/gcc-python.c
@@ -17,6 +17,7 @@
    <http://www.gnu.org/licenses/>.
 */
 
+#define __STDC_FORMAT_MACROS
 #include <Python.h>
 #include "gcc-python.h"
 
diff --git a/generate-casts-c.py b/generate-casts-c.py
index 55e48e8..6f7d511 100644
--- a/generate-casts-c.py
+++ b/generate-casts-c.py
@@ -59,6 +59,7 @@ End:
 def write_c(registry, c_out):
     write_header(c_out)
 
+    c_out.write('#define __STDC_FORMAT_MACROS\n')
     c_out.write('#include <Python.h>\n')
     c_out.write('#include "gcc-python.h"\n')
     c_out.write('#include "gcc-python-wrappers.h"\n')
diff --git a/testcpychecker.py b/testcpychecker.py
index 3ccf3ab..4834f89 100644
--- a/testcpychecker.py
+++ b/testcpychecker.py
@@ -138,6 +138,7 @@ class PyArg_ParseTupleTests(AnalyzerTests):
         #  machines from CPython's Modules/socket.c; was fixed in svn r34931
         #  FIXME: the original had tab indentation, but what does this mean
         # for "column" offsets in the output?
+        # (see also: PR52899, PR49973 in GCC Bugzilla)
         if six.MAXSIZE == 0x7fffffff:
             raise unittest.SkipTest('Test assumes a 64-bit machine')
 

Reply via email to