Skip to content

Commit c10bfdc

Browse files
committed
Allow different c++ standards with castxml
This extends the previous commit, and adds a test. See #33. Cherry-picked from the develop branch
1 parent 0f1d7ca commit c10bfdc

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

pygccxml/parser/source_reader.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,36 @@ def __create_command_line_castxml(self, source_file, xmlfile):
161161
else:
162162

163163
# On mac or linux, use gcc or clang (the flag is the same)
164-
if '-std=c++11' in self.__config.cflags:
165-
cmd.append('--castxml-cc-gnu ' + '"(" ' + self.__config.compiler_path + ' -std=c++11 ")"')
164+
cmd.append('--castxml-cc-gnu ')
165+
166+
# Check for -std=xx flags passed to the compiler.
167+
# A regex could be used but this is a moving target.
168+
# See c++1z for example. It is preferable to have a defined
169+
# list of what is allowed. http://clang.llvm.org/cxx_status.html
170+
#
171+
# Version 98 and 03 are only there in the case somebody is using
172+
# these flags; this is the equivalent to not passing these flags.
173+
standards = [
174+
"-std=c++98",
175+
"-std=c++03",
176+
"-std=c++11",
177+
"-std=c++14",
178+
"-std=c++1z"]
179+
180+
std_flag = ""
181+
for standard in standards:
182+
if standard in self.__config.cflags:
183+
std_flag = " " + standard + " "
184+
185+
# A -std= flag was passed, but is not in the list
186+
if "-std=" in self.__config.cflags and std_flag == "":
187+
raise(RuntimeError("Unknown -std=c++xx flag used !"))
188+
189+
if std_flag != "":
190+
cmd.append(
191+
'"(" ' + self.__config.compiler_path + std_flag + '")"')
166192
else:
167-
cmd.append('--castxml-cc-gnu ' + self.__config.compiler_path)
193+
cmd.append(self.__config.compiler_path)
168194

169195
# Tell castxml to output xml compatible files with gccxml
170196
# so that we can parse them with pygccxml

unittests/data/cpp_standards.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2014-2016 Insight Software Consortium.
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// See http://www.boost.org/LICENSE_1_0.txt
4+
5+
// A good test for different cpp standards. iostream is one of the first
6+
// things to fail when the wrong flags are used.
7+
8+
#include <iostream>

unittests/test_all.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import test_utils
6969
import test_va_list_tag_removal
7070
import test_copy_constructor
71+
import test_cpp_standards
7172

7273
testers = [
7374
# , demangled_tester # failing right now
@@ -128,7 +129,8 @@
128129
example_tester,
129130
test_utils,
130131
test_va_list_tag_removal,
131-
test_copy_constructor
132+
test_copy_constructor,
133+
test_cpp_standards
132134
]
133135

134136
if 'posix' in os.name:

unittests/test_cpp_standards.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Copyright 2014-2016 Insight Software Consortium.
2+
# Distributed under the Boost Software License, Version 1.0.
3+
# See http://www.boost.org/LICENSE_1_0.txt
4+
5+
import unittest
6+
import parser_test_case
7+
8+
from pygccxml import parser
9+
from pygccxml import utils
10+
11+
12+
class Test(parser_test_case.parser_test_case_t):
13+
14+
def test(self):
15+
"""
16+
Test different compilation standards by setting cflags.
17+
18+
"""
19+
20+
# Skip this test for gccxml, this is a CastXML feature.
21+
if "gccxml" in self.config.xml_generator:
22+
return True
23+
24+
parser.parse(["cpp_standards.hpp"], self.config)
25+
26+
self.config.cflags = "-std=c++98"
27+
parser.parse(["cpp_standards.hpp"], self.config)
28+
29+
self.config.cflags = "-std=c++03"
30+
parser.parse(["cpp_standards.hpp"], self.config)
31+
32+
self.config.cflags = "-std=c++11"
33+
parser.parse(["cpp_standards.hpp"], self.config)
34+
35+
# This is broken with llvm 3.6.2 (the one from homebrew)
36+
# It should work with never llvms but I keep the test disabled
37+
# See https://llvm.org/bugs/show_bug.cgi?id=24872
38+
# self.config.cflags = "-std=c++14"
39+
# parser.parse(["cpp_standards.hpp"], self.config)
40+
41+
# Same as above
42+
# self.config.cflags = "-std=c++1z"
43+
# parser.parse(["cpp_standards.hpp"], self.config)
44+
45+
# Pass down a flag that does not exist.
46+
# This should raise an exception.
47+
self.config.cflags = "-std=c++00"
48+
self.assertRaises(
49+
RuntimeError,
50+
lambda: parser.parse(["cpp_standards.hpp"], self.config))
51+
52+
53+
def create_suite():
54+
suite = unittest.TestSuite()
55+
suite.addTest(unittest.makeSuite(Test))
56+
return suite
57+
58+
59+
def run_suite():
60+
unittest.TextTestRunner(verbosity=2).run(create_suite())
61+
62+
if __name__ == "__main__":
63+
run_suite()

0 commit comments

Comments
 (0)