Coverage Report

Created: 2026-09-14 06:43

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