diff options
author | Daniel Murdin <daniel.murdin@niradynamics.se> | 2016-07-13 08:43:13 +0200 |
---|---|---|
committer | Daniel Murdin <daniel.murdin@niradynamics.se> | 2016-07-14 08:13:05 +0200 |
commit | 752fb40a71f84efb25bafd68560d4ba9685baa32 (patch) | |
tree | 7d91bffa90baa82c312c1dbf2ca912e6c2c2fd82 | |
parent | f76d192122df06813a42553b297d46ba5fd5888f (diff) | |
download | pycparser-752fb40a71f84efb25bafd68560d4ba9685baa32.tar.gz |
Protected expressions in ternary operator with paranthesis.
-rw-r--r-- | pycparser/c_generator.py | 6 | ||||
-rw-r--r-- | tests/test_c_generator.py | 8 |
2 files changed, 11 insertions, 3 deletions
diff --git a/pycparser/c_generator.py b/pycparser/c_generator.py index ad0e89d..662a088 100644 --- a/pycparser/c_generator.py +++ b/pycparser/c_generator.py @@ -196,9 +196,9 @@ class CGenerator(object): return 'continue;' def visit_TernaryOp(self, n): - s = self._visit_expr(n.cond) + ' ? ' - s += self._visit_expr(n.iftrue) + ' : ' - s += self._visit_expr(n.iffalse) + s = '(' + self._visit_expr(n.cond) + ') ? ' + s += '(' + self._visit_expr(n.iftrue) + ') : ' + s += '(' + self._visit_expr(n.iffalse) + ')' return s def visit_If(self, n): diff --git a/tests/test_c_generator.py b/tests/test_c_generator.py index 35afe17..cb3bd08 100644 --- a/tests/test_c_generator.py +++ b/tests/test_c_generator.py @@ -88,6 +88,14 @@ class TestCtoC(unittest.TestCase): #s = 'int test(const char* const* arg);' #parse_to_ast(s).show() + def test_ternary(self): + self._assert_ctoc_correct(''' + int main(void) + { + int a, b; + (a == 0) ? (b = 1) : (b = 2); + }''') + def test_casts(self): self._assert_ctoc_correct(r''' int main() { |