瀏覽代碼

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.
Brad King 4 年之前
父節點
當前提交
56dc22d488
共有 3 個文件被更改,包括 26 次插入12 次删除
  1. 14 12
      Source/cmMessenger.cxx
  2. 6 0
      Source/cmMessenger.h
  3. 6 0
      Source/cmake.cxx

+ 14 - 12
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 <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 - 0
Source/cmMessenger.h

@@ -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 - 0
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