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.
This commit is contained in:
Brad King
2021-12-07 16:05:55 -05:00
parent c749982c13
commit 56dc22d488
3 changed files with 26 additions and 12 deletions
+14 -12
View File
@@ -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 <sstream>
#include <utility>
#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<std::string> 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<std::string> topSource)
{
this->TopSource = std::move(topSource);
}
+6
View File
@@ -7,6 +7,8 @@
#include <iosfwd>
#include <string>
#include <cm/optional>
#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<std::string> 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<std::string> TopSource;
bool SuppressDevWarnings = false;
bool SuppressDeprecatedWarnings = false;
bool DevWarningsAsErrors = false;
+6
View File
@@ -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