Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmMacroCommand.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 "cmMacroCommand.h"
4
5
#include <functional>
6
#include <utility>
7
8
#include <cm/memory>
9
#include <cm/string_view>
10
#include <cmext/algorithm>
11
#include <cmext/string_view>
12
13
#include "cmDiagnostics.h"
14
#include "cmExecutionStatus.h"
15
#include "cmFunctionBlocker.h"
16
#include "cmList.h"
17
#include "cmListFileCache.h"
18
#include "cmMakefile.h"
19
#include "cmPolicies.h"
20
#include "cmRange.h"
21
#include "cmState.h"
22
#include "cmStringAlgorithms.h"
23
#include "cmSystemTools.h"
24
25
namespace {
26
27
// define the class for macro commands
28
class cmMacroHelperCommand
29
{
30
public:
31
  /**
32
   * This is called when the command is first encountered in
33
   * the CMakeLists.txt file.
34
   */
35
  bool operator()(std::vector<cmListFileArgument> const& args,
36
                  cmExecutionStatus& inStatus) const;
37
38
  std::vector<std::string> Args;
39
  std::vector<cmListFileFunction> Functions;
40
  cmPolicies::PolicyMap Policies;
41
  cmDiagnostics::DiagnosticMap Diagnostics;
42
  std::string FilePath;
43
};
44
45
bool cmMacroHelperCommand::operator()(
46
  std::vector<cmListFileArgument> const& args,
47
  cmExecutionStatus& inStatus) const
48
0
{
49
0
  cmMakefile& makefile = inStatus.GetMakefile();
50
51
  // Expand the argument list to the macro.
52
0
  std::vector<std::string> expandedArgs;
53
0
  makefile.ExpandArguments(args, expandedArgs);
54
55
  // make sure the number of arguments passed is at least the number
56
  // required by the signature
57
0
  if (expandedArgs.size() < this->Args.size() - 1) {
58
0
    std::string errorMsg =
59
0
      cmStrCat("Macro invoked with incorrect arguments for macro named: ",
60
0
               this->Args[0]);
61
0
    inStatus.SetError(errorMsg);
62
0
    return false;
63
0
  }
64
65
0
  cmMakefile::MacroPushPop macroScope(&makefile, this->FilePath,
66
0
                                      this->Policies, this->Diagnostics);
67
68
  // set the value of argc
69
0
  std::string argcDef = std::to_string(expandedArgs.size());
70
71
0
  auto expIt = expandedArgs.begin() + (this->Args.size() - 1);
72
0
  std::string expandedArgn =
73
0
    cmList::to_string(cmMakeRange(expIt, expandedArgs.end()));
74
0
  std::string expandedArgv = cmList::to_string(expandedArgs);
75
0
  std::vector<std::string> variables;
76
0
  variables.reserve(this->Args.size() - 1);
77
0
  for (unsigned int j = 1; j < this->Args.size(); ++j) {
78
0
    variables.emplace_back(cmStrCat("${", this->Args[j], '}'));
79
0
  }
80
0
  std::vector<std::string> argVs;
81
0
  argVs.reserve(expandedArgs.size());
82
0
  for (unsigned int j = 0; j < expandedArgs.size(); ++j) {
83
0
    argVs.emplace_back(cmStrCat("${ARGV", j, '}'));
84
0
  }
85
  // Invoke all the functions that were collected in the block.
86
  // for each function
87
0
  for (cmListFileFunction const& func : this->Functions) {
88
    // Replace the formal arguments and then invoke the command.
89
0
    std::vector<cmListFileArgument> newLFFArgs;
90
0
    newLFFArgs.reserve(func.Arguments().size());
91
92
    // for each argument of the current function
93
0
    for (cmListFileArgument const& k : func.Arguments()) {
94
0
      cmListFileArgument arg;
95
0
      arg.Value = k.Value;
96
0
      if (k.Delim != cmListFileArgument::Bracket) {
97
        // replace formal arguments
98
0
        for (unsigned int j = 0; j < variables.size(); ++j) {
99
0
          cmSystemTools::ReplaceString(arg.Value, variables[j],
100
0
                                       expandedArgs[j]);
101
0
        }
102
        // replace argc
103
0
        cmSystemTools::ReplaceString(arg.Value, "${ARGC}", argcDef);
104
105
0
        cmSystemTools::ReplaceString(arg.Value, "${ARGN}", expandedArgn);
106
0
        cmSystemTools::ReplaceString(arg.Value, "${ARGV}", expandedArgv);
107
108
        // if the current argument of the current function has ${ARGV in it
109
        // then try replacing ARGV values
110
0
        if (arg.Value.find("${ARGV") != std::string::npos) {
111
0
          for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
112
0
            cmSystemTools::ReplaceString(arg.Value, argVs[t], expandedArgs[t]);
113
0
          }
114
0
        }
115
0
      }
116
0
      arg.Delim = k.Delim;
117
0
      arg.Line = k.Line;
118
0
      newLFFArgs.push_back(std::move(arg));
119
0
    }
120
0
    cmListFileFunction newLFF{ func.OriginalName(), func.Line(),
121
0
                               func.LineEnd(), std::move(newLFFArgs) };
122
0
    cmExecutionStatus status(makefile);
123
0
    if (!makefile.ExecuteCommand(newLFF, status) || status.GetNestedError()) {
124
      // The error message should have already included the call stack
125
      // so we do not need to report an error here.
126
0
      macroScope.Quiet();
127
0
      inStatus.SetNestedError();
128
0
      return false;
129
0
    }
130
0
    if (status.GetReturnInvoked()) {
131
0
      inStatus.SetReturnInvoked(status.GetReturnVariables());
132
0
      return true;
133
0
    }
134
0
    if (status.GetBreakInvoked()) {
135
0
      inStatus.SetBreakInvoked();
136
0
      return true;
137
0
    }
138
0
    if (status.HasExitCode()) {
139
0
      inStatus.SetExitCode(status.GetExitCode());
140
0
      return true;
141
0
    }
142
0
  }
143
0
  return true;
144
0
}
145
146
class cmMacroFunctionBlocker : public cmFunctionBlocker
147
{
148
public:
149
0
  cm::string_view StartCommandName() const override { return "macro"_s; }
150
0
  cm::string_view EndCommandName() const override { return "endmacro"_s; }
151
152
  bool ArgumentsMatch(cmListFileFunction const&,
153
                      cmMakefile& mf) const override;
154
155
  bool Replay(std::vector<cmListFileFunction> functions,
156
              cmExecutionStatus& status) override;
157
158
  std::vector<std::string> Args;
159
};
160
161
bool cmMacroFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
162
                                            cmMakefile& mf) const
163
0
{
164
0
  std::vector<std::string> expandedArguments;
165
0
  mf.ExpandArguments(lff.Arguments(), expandedArguments);
166
0
  return expandedArguments.empty() || expandedArguments[0] == this->Args[0];
167
0
}
168
169
bool cmMacroFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
170
                                    cmExecutionStatus& status)
171
0
{
172
0
  cmMakefile& mf = status.GetMakefile();
173
0
  mf.AppendProperty("MACROS", this->Args[0]);
174
  // create a new command and add it to cmake
175
0
  cmMacroHelperCommand f;
176
0
  f.Args = this->Args;
177
0
  f.Functions = std::move(functions);
178
0
  f.FilePath = this->GetStartingContext().FilePath;
179
0
  mf.RecordPolicies(f.Policies);
180
0
  mf.RecordDiagnostics(f.Diagnostics);
181
0
  return mf.GetState()->AddScriptedCommand(
182
0
    this->Args[0],
183
0
    BT<cmState::Command>(std::move(f),
184
0
                         mf.GetBacktrace().Push(this->GetStartingContext())),
185
0
    mf);
186
0
}
187
}
188
189
bool cmMacroCommand(std::vector<std::string> const& args,
190
                    cmExecutionStatus& status)
191
0
{
192
0
  if (args.empty()) {
193
0
    status.SetError("called with incorrect number of arguments");
194
0
    return false;
195
0
  }
196
197
  // create a function blocker
198
0
  {
199
0
    auto fb = cm::make_unique<cmMacroFunctionBlocker>();
200
0
    cm::append(fb->Args, args);
201
0
    status.GetMakefile().AddFunctionBlocker(std::move(fb));
202
0
  }
203
0
  return true;
204
0
}