From 56dc22d48829860b50a441dcc26de14150ad724c Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 7 Dec 2021 16:05:55 -0500 Subject: cmMessenger: Make relative path conversion more explicit Move the decision to convert to call stacks to relative paths out to the client. Avoid using `cmState` to make the decision ourselves. --- Source/cmMessenger.cxx | 26 ++++++++++++++------------ Source/cmMessenger.h | 6 ++++++ Source/cmake.cxx | 6 ++++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Source/cmMessenger.cxx b/Source/cmMessenger.cxx index 52c9dd0..6dd192e 100644 --- a/Source/cmMessenger.cxx +++ b/Source/cmMessenger.cxx @@ -4,8 +4,6 @@ #include "cmDocumentationFormatter.h" #include "cmMessageMetadata.h" -#include "cmState.h" -#include "cmStateSnapshot.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" @@ -14,6 +12,7 @@ #endif #include +#include #include "cmsys/Terminal.h" @@ -154,7 +153,8 @@ static void displayMessage(MessageType t, std::ostringstream& msg) } namespace { -void PrintCallStack(std::ostream& out, cmListFileBacktrace bt) +void PrintCallStack(std::ostream& out, cmListFileBacktrace bt, + cm::optional const& topSource) { // The call stack exists only if we have at least two calls on top // of the bottom. @@ -167,7 +167,6 @@ void PrintCallStack(std::ostream& out, cmListFileBacktrace bt) } bool first = true; - cmStateSnapshot bottom = bt.GetBottom(); for (; !bt.Empty(); bt = bt.Pop()) { cmListFileContext lfc = bt.Top(); if (lfc.Name.empty() && @@ -180,9 +179,8 @@ void PrintCallStack(std::ostream& out, cmListFileBacktrace bt) first = false; out << "Call Stack (most recent call first):\n"; } - if (bottom.GetState()->GetProjectKind() == cmState::ProjectKind::Normal) { - lfc.FilePath = cmSystemTools::RelativeIfUnder( - bottom.GetState()->GetSourceDirectory(), lfc.FilePath); + if (topSource) { + lfc.FilePath = cmSystemTools::RelativeIfUnder(*topSource, lfc.FilePath); } out << " " << lfc << "\n"; } @@ -219,7 +217,7 @@ void cmMessenger::DisplayMessage(MessageType t, const std::string& text, printMessageText(msg, text); // Add the rest of the context. - PrintCallStack(msg, backtrace); + PrintCallStack(msg, backtrace, this->TopSource); displayMessage(t, msg); } @@ -232,10 +230,14 @@ void cmMessenger::PrintBacktraceTitle(std::ostream& out, return; } cmListFileContext lfc = bt.Top(); - cmStateSnapshot bottom = bt.GetBottom(); - if (bottom.GetState()->GetProjectKind() == cmState::ProjectKind::Normal) { - lfc.FilePath = cmSystemTools::RelativeIfUnder( - bottom.GetState()->GetSourceDirectory(), lfc.FilePath); + if (this->TopSource) { + lfc.FilePath = + cmSystemTools::RelativeIfUnder(*this->TopSource, lfc.FilePath); } out << (lfc.Line ? " at " : " in ") << lfc; } + +void cmMessenger::SetTopSource(cm::optional topSource) +{ + this->TopSource = std::move(topSource); +} diff --git a/Source/cmMessenger.h b/Source/cmMessenger.h index 8287e70..451add0 100644 --- a/Source/cmMessenger.h +++ b/Source/cmMessenger.h @@ -7,6 +7,8 @@ #include #include +#include + #include "cmListFileCache.h" #include "cmMessageType.h" @@ -20,6 +22,8 @@ public: void DisplayMessage(MessageType t, std::string const& text, cmListFileBacktrace const& backtrace) const; + void SetTopSource(cm::optional topSource); + void SetSuppressDevWarnings(bool suppress) { this->SuppressDevWarnings = suppress; @@ -56,6 +60,8 @@ private: bool IsMessageTypeVisible(MessageType t) const; MessageType ConvertMessageType(MessageType t) const; + cm::optional TopSource; + bool SuppressDevWarnings = false; bool SuppressDeprecatedWarnings = false; bool DevWarningsAsErrors = false; diff --git a/Source/cmake.cxx b/Source/cmake.cxx index d927d27..9efdb54 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -1706,6 +1706,12 @@ void cmake::SetHomeDirectory(const std::string& dir) if (this->CurrentSnapshot.IsValid()) { this->CurrentSnapshot.SetDefinition("CMAKE_SOURCE_DIR", dir); } + + if (this->State->GetProjectKind() == cmState::ProjectKind::Normal) { + this->Messenger->SetTopSource(this->GetHomeDirectory()); + } else { + this->Messenger->SetTopSource(cm::nullopt); + } } std::string const& cmake::GetHomeDirectory() const -- cgit v0.12