Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.94k
{
17
1.94k
  this->FileLine = -1;
18
1.94k
  this->FileName = nullptr;
19
1.94k
  this->Result = 0;
20
1.94k
}
21
22
1.94k
cmExprParserHelper::~cmExprParserHelper() = default;
23
24
int cmExprParserHelper::ParseString(char const* str, int verb)
25
1.94k
{
26
1.94k
  if (!str) {
27
0
    return 0;
28
0
  }
29
  // printf("Do some parsing: %s\n", str);
30
31
1.94k
  this->Verbose = verb;
32
1.94k
  this->InputBuffer = str;
33
1.94k
  this->InputBufferPos = 0;
34
1.94k
  this->CurrentLine = 0;
35
36
1.94k
  this->Result = 0;
37
38
1.94k
  yyscan_t yyscanner;
39
1.94k
  cmExpr_yylex_init(&yyscanner);
40
1.94k
  cmExpr_yyset_extra(this, yyscanner);
41
42
1.94k
  try {
43
1.94k
    int res = cmExpr_yyparse(yyscanner);
44
1.94k
    if (res != 0) {
45
268
      std::string e =
46
268
        cmStrCat("cannot parse the expression: \"", this->InputBuffer,
47
268
                 "\": ", this->ErrorString, '.');
48
268
      this->SetError(std::move(e));
49
268
    }
50
1.94k
  } catch (std::runtime_error const& fail) {
51
9
    std::string e = cmStrCat("cannot evaluate the expression: \"",
52
9
                             this->InputBuffer, "\": ", fail.what(), '.');
53
9
    this->SetError(std::move(e));
54
76
  } catch (std::out_of_range const&) {
55
76
    std::string e =
56
76
      cmStrCat("cannot evaluate the expression: \"", this->InputBuffer,
57
76
               "\": a numeric value is out of range.");
58
76
    this->SetError(std::move(e));
59
76
  } 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.94k
  cmExpr_yylex_destroy(yyscanner);
65
1.94k
  if (!this->ErrorString.empty()) {
66
353
    return 0;
67
353
  }
68
69
1.59k
  if (this->Verbose) {
70
0
    std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
71
0
              << std::endl;
72
0
  }
73
1.59k
  return 1;
74
1.94k
}
75
76
int cmExprParserHelper::LexInput(char* buf, int maxlen)
77
178k
{
78
  // std::cout << "JPLexInput ";
79
  // std::cout.write(buf, maxlen);
80
  // std::cout << std::endl;
81
178k
  if (maxlen < 1) {
82
0
    return 0;
83
0
  }
84
178k
  if (this->InputBufferPos < this->InputBuffer.size()) {
85
176k
    buf[0] = this->InputBuffer[this->InputBufferPos++];
86
176k
    if (buf[0] == '\n') {
87
250
      this->CurrentLine++;
88
250
    }
89
176k
    return (1);
90
176k
  }
91
1.83k
  buf[0] = '\n';
92
1.83k
  return (0);
93
178k
}
94
95
void cmExprParserHelper::Error(char const* str)
96
268
{
97
268
  unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
98
268
  std::ostringstream ostr;
99
268
  ostr << str << " (" << pos << ")";
100
268
  this->ErrorString = ostr.str();
101
268
}
102
103
void cmExprParserHelper::Warning(std::string str)
104
7.48k
{
105
7.48k
  this->WarningString = cmStrCat(this->WarningString, std::move(str), '\n');
106
7.48k
}
107
108
void cmExprParserHelper::UnexpectedChar(char c)
109
20.9k
{
110
20.9k
  unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
111
20.9k
  std::ostringstream ostr;
112
20.9k
  ostr << "Unexpected character in expression at position " << pos << ": " << c
113
20.9k
       << "\n";
114
20.9k
  this->WarningString += ostr.str();
115
20.9k
}
116
117
void cmExprParserHelper::SetResult(std::int64_t value)
118
1.63k
{
119
1.63k
  this->Result = value;
120
1.63k
}
121
122
void cmExprParserHelper::SetError(std::string errorString)
123
353
{
124
353
  this->ErrorString = std::move(errorString);
125
353
}
126
127
std::int64_t cmExprParserHelper::ShL(std::int64_t l, std::int64_t r)
128
3.40k
{
129
3.40k
  if (l < 0) {
130
1.03k
    this->Warning(
131
1.03k
      cmStrCat("left shift of negative value in:\n  ", l, " << ", r));
132
1.03k
  }
133
3.40k
  if (r < 0) {
134
1.44k
    this->Warning(
135
1.44k
      cmStrCat("shift exponent is negative in:\n  ", l, " << ", r));
136
1.44k
    r &= 0x3F;
137
1.44k
  }
138
3.40k
  if (r >= 64) {
139
622
    this->Warning(
140
622
      cmStrCat("shift exponent is too large in:\n  ", l, " << ", r));
141
622
    r &= 0x3F;
142
622
  }
143
3.40k
  return static_cast<std::int64_t>(static_cast<std::uint64_t>(l) << r);
144
3.40k
}
145
146
std::int64_t cmExprParserHelper::ShR(std::int64_t l, std::int64_t r)
147
1.02k
{
148
1.02k
  if (r < 0) {
149
373
    this->Warning(
150
373
      cmStrCat("shift exponent is negative in:\n  ", l, " >> ", r));
151
373
    r &= 0x3F;
152
373
  }
153
1.02k
  if (r >= 64) {
154
371
    this->Warning(
155
371
      cmStrCat("shift exponent is too large in:\n  ", l, " >> ", r));
156
371
    r &= 0x3F;
157
371
  }
158
1.02k
  return l >> r;
159
1.02k
}
160
161
std::int64_t cmExprParserHelper::Add(std::int64_t l, std::int64_t r)
162
787
{
163
787
  std::int64_t sum;
164
787
  if (this->AddOverflow(l, r, &sum)) {
165
196
    this->Warning(cmStrCat("signed integer overflow in:\n  ", l, " + ", r));
166
196
  }
167
787
  return sum;
168
787
}
169
170
std::int64_t cmExprParserHelper::Sub(std::int64_t l, std::int64_t r)
171
1.74k
{
172
1.74k
  std::int64_t diff;
173
1.74k
  if (this->SubOverflow(l, r, &diff)) {
174
273
    this->Warning(cmStrCat("signed integer overflow in:\n  ", l, " - ", r));
175
273
  }
176
1.74k
  return diff;
177
1.74k
}
178
179
std::int64_t cmExprParserHelper::Mul(std::int64_t l, std::int64_t r)
180
862
{
181
862
  std::int64_t prod;
182
862
  if (this->MulOverflow(l, r, &prod)) {
183
392
    this->Warning(cmStrCat("signed integer overflow in:\n  ", l, " * ", r));
184
392
  }
185
862
  return prod;
186
862
}
187
188
std::int64_t cmExprParserHelper::Div(std::int64_t l, std::int64_t r)
189
1.31k
{
190
1.31k
  if (r == 0) {
191
2
    throw std::overflow_error("divide by zero");
192
2
  }
193
1.31k
  if (l == INT64_MIN && r == -1) {
194
1
    throw std::overflow_error("signed integer overflow in division");
195
1
  }
196
1.31k
  return l / r;
197
1.31k
}
198
199
std::int64_t cmExprParserHelper::Mod(std::int64_t l, std::int64_t r)
200
1.22k
{
201
1.22k
  if (r == 0) {
202
4
    throw std::overflow_error("modulo by zero");
203
4
  }
204
1.21k
  if (l == INT64_MIN && r == -1) {
205
2
    throw std::overflow_error("signed integer overflow in modulo");
206
2
  }
207
1.21k
  return l % r;
208
1.21k
}
209
210
std::int64_t cmExprParserHelper::Neg(std::int64_t x)
211
8.48k
{
212
8.48k
  if (static_cast<std::uint64_t>(x) == 0x8000000000000000) {
213
2.77k
    this->Warning(cmStrCat("signed integer cannot negate:\n  ", x));
214
2.77k
    return x;
215
2.77k
  }
216
5.70k
  return -x;
217
8.48k
}
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
787
{
231
787
#if __has_builtin(__builtin_saddl_overflow)
232
787
  return __builtin_saddl_overflow(l, r, p);
233
#else
234
  *p = l + r;
235
  return false;
236
#endif
237
787
}
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
1.74k
{
251
1.74k
#if __has_builtin(__builtin_ssubl_overflow)
252
1.74k
  return __builtin_ssubl_overflow(l, r, p);
253
#else
254
  *p = l - r;
255
  return false;
256
#endif
257
1.74k
}
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
862
{
271
862
#if __has_builtin(__builtin_smull_overflow)
272
862
  return __builtin_smull_overflow(l, r, p);
273
#else
274
  *p = l * r;
275
  return false;
276
#endif
277
862
}
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
}