Coverage Report

Created: 2026-02-09 06:05

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
746
{
17
746
  this->FileLine = -1;
18
746
  this->FileName = nullptr;
19
746
  this->Result = 0;
20
746
}
21
22
746
cmExprParserHelper::~cmExprParserHelper() = default;
23
24
int cmExprParserHelper::ParseString(char const* str, int verb)
25
746
{
26
746
  if (!str) {
27
0
    return 0;
28
0
  }
29
  // printf("Do some parsing: %s\n", str);
30
31
746
  this->Verbose = verb;
32
746
  this->InputBuffer = str;
33
746
  this->InputBufferPos = 0;
34
746
  this->CurrentLine = 0;
35
36
746
  this->Result = 0;
37
38
746
  yyscan_t yyscanner;
39
746
  cmExpr_yylex_init(&yyscanner);
40
746
  cmExpr_yyset_extra(this, yyscanner);
41
42
746
  try {
43
746
    int res = cmExpr_yyparse(yyscanner);
44
746
    if (res != 0) {
45
234
      std::string e =
46
234
        cmStrCat("cannot parse the expression: \"", this->InputBuffer,
47
234
                 "\": ", this->ErrorString, '.');
48
234
      this->SetError(std::move(e));
49
234
    }
50
746
  } 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
77
  } catch (std::out_of_range const&) {
55
77
    std::string e =
56
77
      cmStrCat("cannot evaluate the expression: \"", this->InputBuffer,
57
77
               "\": a numeric value is out of range.");
58
77
    this->SetError(std::move(e));
59
77
  } 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
746
  cmExpr_yylex_destroy(yyscanner);
65
746
  if (!this->ErrorString.empty()) {
66
317
    return 0;
67
317
  }
68
69
429
  if (this->Verbose) {
70
0
    std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
71
0
              << std::endl;
72
0
  }
73
429
  return 1;
74
746
}
75
76
int cmExprParserHelper::LexInput(char* buf, int maxlen)
77
129k
{
78
  // std::cout << "JPLexInput ";
79
  // std::cout.write(buf, maxlen);
80
  // std::cout << std::endl;
81
129k
  if (maxlen < 1) {
82
0
    return 0;
83
0
  }
84
129k
  if (this->InputBufferPos < this->InputBuffer.size()) {
85
129k
    buf[0] = this->InputBuffer[this->InputBufferPos++];
86
129k
    if (buf[0] == '\n') {
87
1.37k
      this->CurrentLine++;
88
1.37k
    }
89
129k
    return (1);
90
129k
  }
91
648
  buf[0] = '\n';
92
648
  return (0);
93
129k
}
94
95
void cmExprParserHelper::Error(char const* str)
96
234
{
97
234
  unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
98
234
  std::ostringstream ostr;
99
234
  ostr << str << " (" << pos << ")";
100
234
  this->ErrorString = ostr.str();
101
234
}
102
103
void cmExprParserHelper::UnexpectedChar(char c)
104
6.13k
{
105
6.13k
  unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
106
6.13k
  std::ostringstream ostr;
107
6.13k
  ostr << "Unexpected character in expression at position " << pos << ": " << c
108
6.13k
       << "\n";
109
6.13k
  this->WarningString += ostr.str();
110
6.13k
}
111
112
void cmExprParserHelper::SetResult(KWIML_INT_int64_t value)
113
473
{
114
473
  this->Result = value;
115
473
}
116
117
void cmExprParserHelper::SetError(std::string errorString)
118
317
{
119
317
  this->ErrorString = std::move(errorString);
120
317
}