Coverage Report

Created: 2026-03-12 06:35

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