From 61d52e6e77bef903225bd3bad3e381bac73ee557 Mon Sep 17 00:00:00 2001
From: Stephen Kelly <steveire@gmail.com>
Date: Mon, 18 May 2015 21:33:38 +0200
Subject: cmListFileBacktrace: Hide the context-stack implementation detail.

The backtrace will soon not be implemented in terms of a stack of
cmListFileContext objects.  Keep the cmListFileContext in the API
for convenience for now.
---
 Source/cmCommandArgumentParserHelper.cxx |  2 +-
 Source/cmListFileCache.cxx               | 30 ++++++++++++++++++++++++++++++
 Source/cmListFileCache.h                 |  7 ++++++-
 Source/cmMakefile.cxx                    | 10 +++++-----
 Source/cmake.cxx                         | 19 ++-----------------
 5 files changed, 44 insertions(+), 24 deletions(-)

diff --git a/Source/cmCommandArgumentParserHelper.cxx b/Source/cmCommandArgumentParserHelper.cxx
index 0d1c86d..c816c23 100644
--- a/Source/cmCommandArgumentParserHelper.cxx
+++ b/Source/cmCommandArgumentParserHelper.cxx
@@ -143,7 +143,7 @@ char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
         cmListFileContext lfc;
         lfc.FilePath = this->FileName;
         lfc.Line = this->FileLine;
-        bt.push_back(lfc);
+        bt.Append(lfc);
         msg << "uninitialized variable \'" << var << "\'";
         this->Makefile->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
                                                         msg.str(), bt);
diff --git a/Source/cmListFileCache.cxx b/Source/cmListFileCache.cxx
index 3e3d708..2756cd2 100644
--- a/Source/cmListFileCache.cxx
+++ b/Source/cmListFileCache.cxx
@@ -400,6 +400,11 @@ bool cmListFileParser::AddArgument(cmListFileLexer_Token* token,
     }
 }
 
+void cmListFileBacktrace::Append(cmListFileContext const& context)
+{
+  this->push_back(context);
+}
+
 //----------------------------------------------------------------------------
 void cmListFileBacktrace::MakeRelative()
 {
@@ -416,6 +421,31 @@ void cmListFileBacktrace::MakeRelative()
   this->Relative = true;
 }
 
+void cmListFileBacktrace::PrintTitle(std::ostream& out)
+{
+  if (this->empty())
+    {
+    return;
+    }
+  out << (this->front().Line ? " at " : " in ") << this->front();
+}
+
+void cmListFileBacktrace::PrintCallStack(std::ostream& out)
+{
+  if (size() <= 1)
+    {
+    return;
+    }
+
+  const_iterator i = this->begin() + 1;
+  out << "Call Stack (most recent call first):\n";
+  while(i != this->end())
+    {
+    cmListFileContext const& lfc = *i;
+    out << "  " << lfc << "\n";
+    ++i;
+    }
+}
 
 //----------------------------------------------------------------------------
 std::ostream& operator<<(std::ostream& os, cmListFileContext const& lfc)
diff --git a/Source/cmListFileCache.h b/Source/cmListFileCache.h
index d7e29d9..4a1d181 100644
--- a/Source/cmListFileCache.h
+++ b/Source/cmListFileCache.h
@@ -71,7 +71,7 @@ struct cmListFileFunction: public cmListFileContext
   std::vector<cmListFileArgument> Arguments;
 };
 
-class cmListFileBacktrace: public std::vector<cmListFileContext>
+class cmListFileBacktrace: private std::vector<cmListFileContext>
 {
   public:
     cmListFileBacktrace(cmLocalGenerator* localGen)
@@ -80,7 +80,12 @@ class cmListFileBacktrace: public std::vector<cmListFileContext>
     {
     }
 
+    void Append(cmListFileContext const& context);
+
     void MakeRelative();
+
+    void PrintTitle(std::ostream& out);
+    void PrintCallStack(std::ostream& out);
   private:
     cmLocalGenerator* LocalGenerator;
     bool Relative;
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 8631d73..0d5a431 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -358,7 +358,7 @@ void cmMakefile::IssueMessage(cmake::MessageType t,
       lfc.FilePath = this->ListFileStack.back();
       }
     lfc.Line = 0;
-    backtrace.push_back(lfc);
+    backtrace.Append(lfc);
     }
 
   // Issue the message.
@@ -372,7 +372,7 @@ cmListFileBacktrace cmMakefile::GetBacktrace() const
   for(CallStackType::const_reverse_iterator i = this->CallStack.rbegin();
       i != this->CallStack.rend(); ++i)
     {
-    backtrace.push_back(*i->Context);
+    backtrace.Append(*i->Context);
     }
   return backtrace;
 }
@@ -1944,7 +1944,7 @@ void cmMakefile::CheckForUnused(const char* reason,
     if (!this->CallStack.empty())
       {
       cmListFileContext file = this->GetExecutionContext();
-      bt.push_back(file);
+      bt.Append(file);
       path = file.FilePath;
       }
     else
@@ -1954,7 +1954,7 @@ void cmMakefile::CheckForUnused(const char* reason,
       cmListFileContext lfc;
       lfc.FilePath = path;
       lfc.Line = 0;
-      bt.push_back(lfc);
+      bt.Append(lfc);
       }
     if (this->CheckSystemVars ||
         cmSystemTools::IsSubDirectory(path,
@@ -2884,7 +2884,7 @@ cmake::MessageType cmMakefile::ExpandVariablesInStringNew(
                 cmListFileContext lfc;
                 lfc.FilePath = filename;
                 lfc.Line = line;
-                bt.push_back(lfc);
+                bt.Append(lfc);
                 msg << "uninitialized variable \'" << lookup << "\'";
                 this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
                                                        msg.str(), bt);
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index 5c5c428..e447105 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -2485,13 +2485,7 @@ void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
     }
 
   // Add the immediate context.
-  cmListFileBacktrace::const_iterator i = backtrace.begin();
-  if(i != backtrace.end())
-    {
-    cmListFileContext const& lfc = *i;
-    msg << (lfc.Line? " at ": " in ") << lfc;
-    ++i;
-    }
+  backtrace.PrintTitle(msg);
 
   // Add the message text.
   {
@@ -2502,16 +2496,7 @@ void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
   }
 
   // Add the rest of the context.
-  if(i != backtrace.end())
-    {
-    msg << "Call Stack (most recent call first):\n";
-    while(i != backtrace.end())
-      {
-      cmListFileContext const& lfc = *i;
-      msg << "  " << lfc << "\n";
-      ++i;
-      }
-    }
+  backtrace.PrintCallStack(msg);
 
   // Add a note about warning suppression.
   if(t == cmake::AUTHOR_WARNING)
-- 
cgit v0.12