summaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
authorBraulio Valdivielso Martinez <bvaldivielso@bloomberg.net>2022-03-25 15:17:05 (GMT)
committerBraulio Valdivielso Martinez <bvaldivielso@bloomberg.net>2022-03-25 16:24:38 (GMT)
commitfd46db174533ee20917ae1b328c43b1e70f07c43 (patch)
tree5cbe3de6941a7f54e6bed7d5213e01d6d8151bca /Source
parent89f2d779f245f2fe4c9921931699e3996af066a0 (diff)
downloadCMake-fd46db174533ee20917ae1b328c43b1e70f07c43.zip
CMake-fd46db174533ee20917ae1b328c43b1e70f07c43.tar.gz
CMake-fd46db174533ee20917ae1b328c43b1e70f07c43.tar.bz2
Trace: process else and elseif commands correctly
There have been two bugs reported about the `else` and `elseif` commands in the context of the tracing functionality and the json-v1 format (#23191 #22315). In essence, the reported traces referred to the layer of the stacktrace immediately on top of the expected ones. This MR fixes both issues. My solution adds a new parameter to the `PrintCommandTrace` function, `commandMissingFromStack`, that callers can specify if the command they want to report a trace for is not a regular part of the stack maintained in `cmMakefile`. This is only the case for `else` and `elseif`. The other bug is fixed by having the caller pass a `cmListFileBacktrace`, which helps in getting the right lines, file names... for the reported command. Fixes: #23191 #22315
Diffstat (limited to 'Source')
-rw-r--r--Source/cmIfCommand.cxx12
-rw-r--r--Source/cmMakefile.cxx15
-rw-r--r--Source/cmMakefile.h11
3 files changed, 24 insertions, 14 deletions
diff --git a/Source/cmIfCommand.cxx b/Source/cmIfCommand.cxx
index 55f6453..9cd1943 100644
--- a/Source/cmIfCommand.cxx
+++ b/Source/cmIfCommand.cxx
@@ -73,11 +73,11 @@ bool cmIfFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
}
// watch for our state change
if (scopeDepth == 0 && func.LowerCaseName() == "else") {
+ cmListFileBacktrace elseBT = mf.GetBacktrace().Push(
+ cmListFileContext{ func.OriginalName(),
+ this->GetStartingContext().FilePath, func.Line() });
if (this->ElseSeen) {
- cmListFileBacktrace elseBT = mf.GetBacktrace().Push(cmListFileContext{
- func.OriginalName(), this->GetStartingContext().FilePath,
- func.Line() });
mf.GetCMakeInstance()->IssueMessage(
MessageType::FATAL_ERROR,
"A duplicate ELSE command was found inside an IF block.", elseBT);
@@ -92,7 +92,8 @@ bool cmIfFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
// if trace is enabled, print a (trivially) evaluated "else"
// statement
if (!this->IsBlocking && mf.GetCMakeInstance()->GetTrace()) {
- mf.PrintCommandTrace(func);
+ mf.PrintCommandTrace(func, elseBT,
+ cmMakefile::CommandMissingFromStack::Yes);
}
} else if (scopeDepth == 0 && func.LowerCaseName() == "elseif") {
cmListFileBacktrace elseifBT = mf.GetBacktrace().Push(
@@ -111,7 +112,8 @@ bool cmIfFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
} else {
// if trace is enabled, print the evaluated "elseif" statement
if (mf.GetCMakeInstance()->GetTrace()) {
- mf.PrintCommandTrace(func);
+ mf.PrintCommandTrace(func, elseifBT,
+ cmMakefile::CommandMissingFromStack::Yes);
}
std::string errorString;
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index f0a96a8..a781d59 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -241,14 +241,14 @@ cmListFileBacktrace cmMakefile::GetBacktrace() const
return this->Backtrace;
}
-void cmMakefile::PrintCommandTrace(
- cmListFileFunction const& lff,
- cm::optional<std::string> const& deferId) const
+void cmMakefile::PrintCommandTrace(cmListFileFunction const& lff,
+ cmListFileBacktrace const& bt,
+ CommandMissingFromStack missing) const
{
// Check if current file in the list of requested to trace...
std::vector<std::string> const& trace_only_this_files =
this->GetCMakeInstance()->GetTraceSources();
- std::string const& full_path = this->GetBacktrace().Top().FilePath;
+ std::string const& full_path = bt.Top().FilePath;
std::string const& only_filename = cmSystemTools::GetFilenameName(full_path);
bool trace = trace_only_this_files.empty();
if (!trace) {
@@ -282,6 +282,7 @@ void cmMakefile::PrintCommandTrace(
args.push_back(arg.Value);
}
}
+ cm::optional<std::string> const& deferId = bt.Top().DeferId;
switch (this->GetCMakeInstance()->GetTraceFormat()) {
case cmake::TraceFormat::TRACE_JSON_V1: {
@@ -303,9 +304,9 @@ void cmMakefile::PrintCommandTrace(
val["args"].append(arg);
}
val["time"] = cmSystemTools::GetTime();
- val["frame"] =
+ val["frame"] = (missing == CommandMissingFromStack::Yes ? 1 : 0) +
static_cast<Json::Value::UInt64>(this->ExecutionStatusStack.size());
- val["global_frame"] =
+ val["global_frame"] = (missing == CommandMissingFromStack::Yes ? 1 : 0) +
static_cast<Json::Value::UInt64>(this->RecursionDepth);
msg << Json::writeString(builder, val);
#endif
@@ -427,7 +428,7 @@ bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
if (!cmSystemTools::GetFatalErrorOccured()) {
// if trace is enabled, print out invoke information
if (this->GetCMakeInstance()->GetTrace()) {
- this->PrintCommandTrace(lff, this->Backtrace.Top().DeferId);
+ this->PrintCommandTrace(lff, this->Backtrace);
}
// Try invoking the command.
bool invokeSucceeded = command(lff.Arguments(), status);
diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h
index 6d44e79..c8e1e83 100644
--- a/Source/cmMakefile.h
+++ b/Source/cmMakefile.h
@@ -674,11 +674,18 @@ public:
bool copyonly, bool atOnly, bool escapeQuotes,
mode_t permissions = 0, cmNewLineStyle = cmNewLineStyle());
+ enum class CommandMissingFromStack
+ {
+ No,
+ Yes,
+ };
+
/**
* Print a command's invocation
*/
- void PrintCommandTrace(cmListFileFunction const& lff,
- cm::optional<std::string> const& deferId = {}) const;
+ void PrintCommandTrace(
+ cmListFileFunction const& lff, cmListFileBacktrace const& bt,
+ CommandMissingFromStack missing = CommandMissingFromStack::No) const;
/**
* Set a callback that is invoked whenever ExecuteCommand is called.