diff options
author | Stefan Behnel <stefan_ml@behnel.de> | 2015-09-29 11:25:44 +0200 |
---|---|---|
committer | Stefan Behnel <stefan_ml@behnel.de> | 2015-09-29 11:25:44 +0200 |
commit | 991a6f6f840e4d3feeda72934c2fc6ec9975b969 (patch) | |
tree | 868a66ad759fe3ea9021eecda0fe73cfe8f763ea | |
parent | 7045b0b6dec68fa43f3a1e5117e8e7530eed4903 (diff) | |
download | cython-991a6f6f840e4d3feeda72934c2fc6ec9975b969.tar.gz |
guess longness suffix of negative decimal integer literals based on their type to prevent the C compiler from truncating their value range
-rw-r--r-- | Cython/Compiler/ExprNodes.py | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index d12a21ea8..79e6d1609 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -1167,7 +1167,15 @@ class IntNode(ConstNode): self.result_code = self.get_constant_c_result_code() def get_constant_c_result_code(self): - return self.value_as_c_integer_string() + self.unsigned + self.longness + unsigned, longness = self.unsigned, self.longness + literal = self.value_as_c_integer_string() + if not (unsigned or longness) and self.type.is_int and literal[0] == '-' and literal[1] != '0': + # negative decimal literal => guess longness from type to prevent wrap-around + if self.type.rank >= PyrexTypes.c_longlong_type.rank: + longness = 'LL' + elif self.type.rank >= PyrexTypes.c_long_type.rank: + longness = 'L' + return literal + unsigned + longness def value_as_c_integer_string(self): value = self.value |