/src/CMake/Source/cmExprParserHelper.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmExprParserHelper.h" |
4 | | |
5 | | #include <iostream> |
6 | | #include <sstream> |
7 | | #include <stdexcept> |
8 | | #include <utility> |
9 | | |
10 | | #include "cmExprLexer.h" |
11 | | #include "cmStringAlgorithms.h" |
12 | | |
13 | | int cmExpr_yyparse(yyscan_t yyscanner); |
14 | | // |
15 | | cmExprParserHelper::cmExprParserHelper() |
16 | 1.44k | { |
17 | 1.44k | this->FileLine = -1; |
18 | 1.44k | this->FileName = nullptr; |
19 | 1.44k | this->Result = 0; |
20 | 1.44k | } |
21 | | |
22 | 1.44k | cmExprParserHelper::~cmExprParserHelper() = default; |
23 | | |
24 | | int cmExprParserHelper::ParseString(char const* str, int verb) |
25 | 1.44k | { |
26 | 1.44k | if (!str) { |
27 | 0 | return 0; |
28 | 0 | } |
29 | | // printf("Do some parsing: %s\n", str); |
30 | | |
31 | 1.44k | this->Verbose = verb; |
32 | 1.44k | this->InputBuffer = str; |
33 | 1.44k | this->InputBufferPos = 0; |
34 | 1.44k | this->CurrentLine = 0; |
35 | | |
36 | 1.44k | this->Result = 0; |
37 | | |
38 | 1.44k | yyscan_t yyscanner; |
39 | 1.44k | cmExpr_yylex_init(&yyscanner); |
40 | 1.44k | cmExpr_yyset_extra(this, yyscanner); |
41 | | |
42 | 1.44k | try { |
43 | 1.44k | int res = cmExpr_yyparse(yyscanner); |
44 | 1.44k | if (res != 0) { |
45 | 256 | std::string e = |
46 | 256 | cmStrCat("cannot parse the expression: \"", this->InputBuffer, |
47 | 256 | "\": ", this->ErrorString, '.'); |
48 | 256 | this->SetError(std::move(e)); |
49 | 256 | } |
50 | 1.44k | } catch (std::runtime_error const& fail) { |
51 | 5 | std::string e = cmStrCat("cannot evaluate the expression: \"", |
52 | 5 | this->InputBuffer, "\": ", fail.what(), '.'); |
53 | 5 | this->SetError(std::move(e)); |
54 | 80 | } catch (std::out_of_range const&) { |
55 | 80 | std::string e = |
56 | 80 | cmStrCat("cannot evaluate the expression: \"", this->InputBuffer, |
57 | 80 | "\": a numeric value is out of range."); |
58 | 80 | this->SetError(std::move(e)); |
59 | 80 | } catch (...) { |
60 | 0 | std::string e = |
61 | 0 | cmStrCat("cannot parse the expression: \"", this->InputBuffer, "\"."); |
62 | 0 | this->SetError(std::move(e)); |
63 | 0 | } |
64 | 1.44k | cmExpr_yylex_destroy(yyscanner); |
65 | 1.44k | if (!this->ErrorString.empty()) { |
66 | 341 | return 0; |
67 | 341 | } |
68 | | |
69 | 1.10k | if (this->Verbose) { |
70 | 0 | std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]" |
71 | 0 | << std::endl; |
72 | 0 | } |
73 | 1.10k | return 1; |
74 | 1.44k | } |
75 | | |
76 | | int cmExprParserHelper::LexInput(char* buf, int maxlen) |
77 | 165k | { |
78 | | // std::cout << "JPLexInput "; |
79 | | // std::cout.write(buf, maxlen); |
80 | | // std::cout << std::endl; |
81 | 165k | if (maxlen < 1) { |
82 | 0 | return 0; |
83 | 0 | } |
84 | 165k | if (this->InputBufferPos < this->InputBuffer.size()) { |
85 | 163k | buf[0] = this->InputBuffer[this->InputBufferPos++]; |
86 | 163k | if (buf[0] == '\n') { |
87 | 281 | this->CurrentLine++; |
88 | 281 | } |
89 | 163k | return (1); |
90 | 163k | } |
91 | 1.32k | buf[0] = '\n'; |
92 | 1.32k | return (0); |
93 | 165k | } |
94 | | |
95 | | void cmExprParserHelper::Error(char const* str) |
96 | 256 | { |
97 | 256 | unsigned long pos = static_cast<unsigned long>(this->InputBufferPos); |
98 | 256 | std::ostringstream ostr; |
99 | 256 | ostr << str << " (" << pos << ")"; |
100 | 256 | this->ErrorString = ostr.str(); |
101 | 256 | } |
102 | | |
103 | | void cmExprParserHelper::Warning(std::string str) |
104 | 6.12k | { |
105 | 6.12k | this->WarningString = cmStrCat(this->WarningString, std::move(str), '\n'); |
106 | 6.12k | } |
107 | | |
108 | | void cmExprParserHelper::UnexpectedChar(char c) |
109 | 14.2k | { |
110 | 14.2k | unsigned long pos = static_cast<unsigned long>(this->InputBufferPos); |
111 | 14.2k | std::ostringstream ostr; |
112 | 14.2k | ostr << "Unexpected character in expression at position " << pos << ": " << c |
113 | 14.2k | << "\n"; |
114 | 14.2k | this->WarningString += ostr.str(); |
115 | 14.2k | } |
116 | | |
117 | | void cmExprParserHelper::SetResult(std::int64_t value) |
118 | 1.14k | { |
119 | 1.14k | this->Result = value; |
120 | 1.14k | } |
121 | | |
122 | | void cmExprParserHelper::SetError(std::string errorString) |
123 | 341 | { |
124 | 341 | this->ErrorString = std::move(errorString); |
125 | 341 | } |
126 | | |
127 | | std::int64_t cmExprParserHelper::ShL(std::int64_t l, std::int64_t r) |
128 | 2.77k | { |
129 | 2.77k | if (l < 0) { |
130 | 839 | this->Warning( |
131 | 839 | cmStrCat("left shift of negative value in:\n ", l, " << ", r)); |
132 | 839 | } |
133 | 2.77k | if (r < 0) { |
134 | 776 | this->Warning( |
135 | 776 | cmStrCat("shift exponent is negative in:\n ", l, " << ", r)); |
136 | 776 | r &= 0x3F; |
137 | 776 | } |
138 | 2.77k | if (r >= 64) { |
139 | 915 | this->Warning( |
140 | 915 | cmStrCat("shift exponent is too large in:\n ", l, " << ", r)); |
141 | 915 | r &= 0x3F; |
142 | 915 | } |
143 | 2.77k | return static_cast<std::int64_t>(static_cast<std::uint64_t>(l) << r); |
144 | 2.77k | } |
145 | | |
146 | | std::int64_t cmExprParserHelper::ShR(std::int64_t l, std::int64_t r) |
147 | 1.58k | { |
148 | 1.58k | if (r < 0) { |
149 | 701 | this->Warning( |
150 | 701 | cmStrCat("shift exponent is negative in:\n ", l, " >> ", r)); |
151 | 701 | r &= 0x3F; |
152 | 701 | } |
153 | 1.58k | if (r >= 64) { |
154 | 511 | this->Warning( |
155 | 511 | cmStrCat("shift exponent is too large in:\n ", l, " >> ", r)); |
156 | 511 | r &= 0x3F; |
157 | 511 | } |
158 | 1.58k | return l >> r; |
159 | 1.58k | } |
160 | | |
161 | | std::int64_t cmExprParserHelper::Add(std::int64_t l, std::int64_t r) |
162 | 848 | { |
163 | 848 | std::int64_t sum; |
164 | 848 | if (this->AddOverflow(l, r, &sum)) { |
165 | 228 | this->Warning(cmStrCat("signed integer overflow in:\n ", l, " + ", r)); |
166 | 228 | } |
167 | 848 | return sum; |
168 | 848 | } |
169 | | |
170 | | std::int64_t cmExprParserHelper::Sub(std::int64_t l, std::int64_t r) |
171 | 2.63k | { |
172 | 2.63k | std::int64_t diff; |
173 | 2.63k | if (this->SubOverflow(l, r, &diff)) { |
174 | 368 | this->Warning(cmStrCat("signed integer overflow in:\n ", l, " - ", r)); |
175 | 368 | } |
176 | 2.63k | return diff; |
177 | 2.63k | } |
178 | | |
179 | | std::int64_t cmExprParserHelper::Mul(std::int64_t l, std::int64_t r) |
180 | 1.01k | { |
181 | 1.01k | std::int64_t prod; |
182 | 1.01k | if (this->MulOverflow(l, r, &prod)) { |
183 | 322 | this->Warning(cmStrCat("signed integer overflow in:\n ", l, " * ", r)); |
184 | 322 | } |
185 | 1.01k | return prod; |
186 | 1.01k | } |
187 | | |
188 | | std::int64_t cmExprParserHelper::Div(std::int64_t l, std::int64_t r) |
189 | 739 | { |
190 | 739 | if (r == 0) { |
191 | 2 | throw std::overflow_error("divide by zero"); |
192 | 2 | } |
193 | 737 | if (l == INT64_MIN && r == -1) { |
194 | 0 | throw std::overflow_error("signed integer overflow in division"); |
195 | 0 | } |
196 | 737 | return l / r; |
197 | 737 | } |
198 | | |
199 | | std::int64_t cmExprParserHelper::Mod(std::int64_t l, std::int64_t r) |
200 | 734 | { |
201 | 734 | if (r == 0) { |
202 | 3 | throw std::overflow_error("modulo by zero"); |
203 | 3 | } |
204 | 731 | if (l == INT64_MIN && r == -1) { |
205 | 0 | throw std::overflow_error("signed integer overflow in modulo"); |
206 | 0 | } |
207 | 731 | return l % r; |
208 | 731 | } |
209 | | |
210 | | std::int64_t cmExprParserHelper::Neg(std::int64_t x) |
211 | 5.71k | { |
212 | 5.71k | if (static_cast<std::uint64_t>(x) == 0x8000000000000000) { |
213 | 1.46k | this->Warning(cmStrCat("signed integer cannot negate:\n ", x)); |
214 | 1.46k | return x; |
215 | 1.46k | } |
216 | 4.24k | return -x; |
217 | 5.71k | } |
218 | | |
219 | | // The __has_builtin preprocessor check was added in Clang 2.6 and GCC 10. |
220 | | // The __builtin_X_overflow intrinsics were added in Clang 3.4 and GCC 5. |
221 | | #ifndef __has_builtin |
222 | | # if defined(__GNUC__) && __GNUC__ >= 5 |
223 | | # define __has_builtin(x) 1 |
224 | | # else |
225 | | # define __has_builtin(x) 0 |
226 | | # endif |
227 | | #endif |
228 | | |
229 | | bool cmExprParserHelper::AddOverflow(long l, long r, long* p) |
230 | 848 | { |
231 | 848 | #if __has_builtin(__builtin_saddl_overflow) |
232 | 848 | return __builtin_saddl_overflow(l, r, p); |
233 | | #else |
234 | | *p = l + r; |
235 | | return false; |
236 | | #endif |
237 | 848 | } |
238 | | |
239 | | bool cmExprParserHelper::AddOverflow(long long l, long long r, long long* p) |
240 | 0 | { |
241 | 0 | #if __has_builtin(__builtin_saddll_overflow) |
242 | 0 | return __builtin_saddll_overflow(l, r, p); |
243 | | #else |
244 | | *p = l + r; |
245 | | return false; |
246 | | #endif |
247 | 0 | } |
248 | | |
249 | | bool cmExprParserHelper::SubOverflow(long l, long r, long* p) |
250 | 2.63k | { |
251 | 2.63k | #if __has_builtin(__builtin_ssubl_overflow) |
252 | 2.63k | return __builtin_ssubl_overflow(l, r, p); |
253 | | #else |
254 | | *p = l - r; |
255 | | return false; |
256 | | #endif |
257 | 2.63k | } |
258 | | |
259 | | bool cmExprParserHelper::SubOverflow(long long l, long long r, long long* p) |
260 | 0 | { |
261 | 0 | #if __has_builtin(__builtin_ssubll_overflow) |
262 | 0 | return __builtin_ssubll_overflow(l, r, p); |
263 | | #else |
264 | | *p = l - r; |
265 | | return false; |
266 | | #endif |
267 | 0 | } |
268 | | |
269 | | bool cmExprParserHelper::MulOverflow(long l, long r, long* p) |
270 | 1.01k | { |
271 | 1.01k | #if __has_builtin(__builtin_smull_overflow) |
272 | 1.01k | return __builtin_smull_overflow(l, r, p); |
273 | | #else |
274 | | *p = l * r; |
275 | | return false; |
276 | | #endif |
277 | 1.01k | } |
278 | | |
279 | | bool cmExprParserHelper::MulOverflow(long long l, long long r, long long* p) |
280 | 0 | { |
281 | 0 | #if __has_builtin(__builtin_smulll_overflow) |
282 | 0 | return __builtin_smulll_overflow(l, r, p); |
283 | | #else |
284 | | *p = l * r; |
285 | | return false; |
286 | | #endif |
287 | 0 | } |