diff options
author | Brad King <brad.king@kitware.com> | 2021-12-07 20:18:17 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2021-12-08 15:03:48 (GMT) |
commit | 3d378541bb22f00e3a22bf5f12e97b7943a81294 (patch) | |
tree | 853fa2666199c276b677e28c6b9c15fce70481c9 /Source/cmMessenger.cxx | |
parent | 642238b3021ccb1923e57563bb61e0abb0111c84 (diff) | |
download | CMake-3d378541bb22f00e3a22bf5f12e97b7943a81294.zip CMake-3d378541bb22f00e3a22bf5f12e97b7943a81294.tar.gz CMake-3d378541bb22f00e3a22bf5f12e97b7943a81294.tar.bz2 |
cmMessenger: Adopt backtrace printing functions
Move backtrace printing functions from `cmListFileBacktrace` over to
`cmMessenger`, their primary caller. Thread `cmMessenger` instances
through APIs needed to update other call sites.
Diffstat (limited to 'Source/cmMessenger.cxx')
-rw-r--r-- | Source/cmMessenger.cxx | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/Source/cmMessenger.cxx b/Source/cmMessenger.cxx index 2eead6b..52c9dd0 100644 --- a/Source/cmMessenger.cxx +++ b/Source/cmMessenger.cxx @@ -4,6 +4,8 @@ #include "cmDocumentationFormatter.h" #include "cmMessageMetadata.h" +#include "cmState.h" +#include "cmStateSnapshot.h" #include "cmStringAlgorithms.h" #include "cmSystemTools.h" @@ -151,6 +153,42 @@ static void displayMessage(MessageType t, std::ostringstream& msg) } } +namespace { +void PrintCallStack(std::ostream& out, cmListFileBacktrace bt) +{ + // The call stack exists only if we have at least two calls on top + // of the bottom. + if (bt.Empty()) { + return; + } + bt = bt.Pop(); + if (bt.Empty()) { + return; + } + + bool first = true; + cmStateSnapshot bottom = bt.GetBottom(); + for (; !bt.Empty(); bt = bt.Pop()) { + cmListFileContext lfc = bt.Top(); + if (lfc.Name.empty() && + lfc.Line != cmListFileContext::DeferPlaceholderLine) { + // Skip this whole-file scope. When we get here we already will + // have printed a more-specific context within the file. + continue; + } + if (first) { + 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); + } + out << " " << lfc << "\n"; + } +} +} + void cmMessenger::IssueMessage(MessageType t, const std::string& text, const cmListFileBacktrace& backtrace) const { @@ -176,12 +214,28 @@ void cmMessenger::DisplayMessage(MessageType t, const std::string& text, } // Add the immediate context. - backtrace.PrintTitle(msg); + this->PrintBacktraceTitle(msg, backtrace); printMessageText(msg, text); // Add the rest of the context. - backtrace.PrintCallStack(msg); + PrintCallStack(msg, backtrace); displayMessage(t, msg); } + +void cmMessenger::PrintBacktraceTitle(std::ostream& out, + cmListFileBacktrace const& bt) const +{ + // The title exists only if we have a call on top of the bottom. + if (bt.Empty()) { + 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); + } + out << (lfc.Line ? " at " : " in ") << lfc; +} |