From 13d10ee61642ab384d506fa81a991e51d90c6488 Mon Sep 17 00:00:00 2001
From: Vitaly Stakhovsky <vvs31415@gitlab.org>
Date: Mon, 10 Sep 2018 09:31:48 -0400
Subject: cmState::GetInitializedCacheValue: Return as const std::string*

---
 Source/QtDialog/QCMake.cxx               |  4 +--
 Source/cmCommandArgumentParserHelper.cxx |  6 ++--
 Source/cmExtraEclipseCDT4Generator.cxx   |  6 ++--
 Source/cmGlobalGenerator.cxx             | 14 ++++-----
 Source/cmMakefile.cxx                    | 30 ++++++++----------
 Source/cmServerProtocol.cxx              |  8 ++---
 Source/cmState.cxx                       |  6 ++--
 Source/cmState.h                         |  2 +-
 Source/cmake.cxx                         | 54 +++++++++++++++++---------------
 9 files changed, 64 insertions(+), 66 deletions(-)

diff --git a/Source/QtDialog/QCMake.cxx b/Source/QtDialog/QCMake.cxx
index 0e14a3f..0133b88 100644
--- a/Source/QtDialog/QCMake.cxx
+++ b/Source/QtDialog/QCMake.cxx
@@ -94,11 +94,11 @@ void QCMake::setBinaryDirectory(const QString& _dir)
     }
     const char* gen = state->GetCacheEntryValue("CMAKE_GENERATOR");
     if (gen) {
-      const char* extraGen =
+      const std::string* extraGen =
         state->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
       std::string curGen =
         cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
-          gen, extraGen ? extraGen : "");
+          gen, extraGen ? *extraGen : "");
       this->setGenerator(QString::fromLocal8Bit(curGen.c_str()));
     }
 
diff --git a/Source/cmCommandArgumentParserHelper.cxx b/Source/cmCommandArgumentParserHelper.cxx
index c7210b4..650d02d 100644
--- a/Source/cmCommandArgumentParserHelper.cxx
+++ b/Source/cmCommandArgumentParserHelper.cxx
@@ -68,12 +68,12 @@ const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
     return "";
   }
   if (strcmp(key, "CACHE") == 0) {
-    if (const char* c =
+    if (const std::string* c =
           this->Makefile->GetState()->GetInitializedCacheValue(var)) {
       if (this->EscapeQuotes) {
-        return this->AddString(cmSystemTools::EscapeQuotes(c));
+        return this->AddString(cmSystemTools::EscapeQuotes(*c));
       }
-      return this->AddString(c);
+      return this->AddString(*c);
     }
     return "";
   }
diff --git a/Source/cmExtraEclipseCDT4Generator.cxx b/Source/cmExtraEclipseCDT4Generator.cxx
index 313d46b..34f58ad 100644
--- a/Source/cmExtraEclipseCDT4Generator.cxx
+++ b/Source/cmExtraEclipseCDT4Generator.cxx
@@ -215,7 +215,7 @@ void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
 
   std::string cacheEntryName = "CMAKE_ECLIPSE_ENVVAR_";
   cacheEntryName += envVar;
-  const char* cacheValue =
+  const std::string* cacheValue =
     lg->GetState()->GetInitializedCacheValue(cacheEntryName);
 
   // now we have both, decide which one to use
@@ -232,14 +232,14 @@ void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
     mf->GetCMakeInstance()->SaveCache(lg->GetBinaryDirectory());
   } else if (!envVarSet && cacheValue != nullptr) {
     // It is already in the cache, but not in the env, so use it from the cache
-    valueToUse = cacheValue;
+    valueToUse = *cacheValue;
   } else {
     // It is both in the cache and in the env.
     // Use the version from the env. except if the value from the env is
     // completely contained in the value from the cache (for the case that we
     // now have a PATH without MSVC dirs in the env. but had the full PATH with
     // all MSVC dirs during the cmake run which stored the var in the cache:
-    valueToUse = cacheValue;
+    valueToUse = *cacheValue;
     if (valueToUse.find(envVarValue) == std::string::npos) {
       valueToUse = envVarValue;
       mf->AddCacheDefinition(cacheEntryName, valueToUse.c_str(),
diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx
index b5212fc..99135c8 100644
--- a/Source/cmGlobalGenerator.cxx
+++ b/Source/cmGlobalGenerator.cxx
@@ -215,15 +215,15 @@ void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang,
   if (!optional && (path.empty() || !cmSystemTools::FileExists(path))) {
     return;
   }
-  const char* cname =
+  const std::string* cname =
     this->GetCMakeInstance()->GetState()->GetInitializedCacheValue(langComp);
   std::string changeVars;
   if (cname && !optional) {
     std::string cnameString;
-    if (!cmSystemTools::FileIsFullPath(cname)) {
-      cnameString = cmSystemTools::FindProgram(cname);
+    if (!cmSystemTools::FileIsFullPath(*cname)) {
+      cnameString = cmSystemTools::FindProgram(*cname);
     } else {
-      cnameString = cname;
+      cnameString = *cname;
     }
     std::string pathString = path;
     // get rid of potentially multiple slashes:
@@ -239,7 +239,7 @@ void cmGlobalGenerator::ResolveLanguageCompiler(const std::string& lang,
       }
       changeVars += langComp;
       changeVars += ";";
-      changeVars += cname;
+      changeVars += *cname;
       this->GetCMakeInstance()->GetState()->SetGlobalProperty(
         "__CMAKE_DELETE_CACHE_CHANGE_VARS_", changeVars.c_str());
     }
@@ -1970,7 +1970,7 @@ void cmGlobalGenerator::AddMakefile(cmMakefile* mf)
 
   // update progress
   // estimate how many lg there will be
-  const char* numGenC =
+  const std::string* numGenC =
     this->CMakeInstance->GetState()->GetInitializedCacheValue(
       "CMAKE_NUMBER_OF_MAKEFILES");
 
@@ -1988,7 +1988,7 @@ void cmGlobalGenerator::AddMakefile(cmMakefile* mf)
     return;
   }
 
-  int numGen = atoi(numGenC);
+  int numGen = atoi(numGenC->c_str());
   float prog = 0.9f * static_cast<float>(this->Makefiles.size()) /
     static_cast<float>(numGen);
   if (prog > 0.9f) {
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx
index 5498ad2..dce7bd0 100644
--- a/Source/cmMakefile.cxx
+++ b/Source/cmMakefile.cxx
@@ -1718,7 +1718,8 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
                                     cmStateEnums::CacheEntryType type,
                                     bool force)
 {
-  const char* existingValue = this->GetState()->GetInitializedCacheValue(name);
+  const std::string* existingValue =
+    this->GetState()->GetInitializedCacheValue(name);
   // must be outside the following if() to keep it alive long enough
   std::string nvalue;
 
@@ -1728,7 +1729,7 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
     // if this is not a force, then use the value from the cache
     // if it is a force, then use the value being passed in
     if (!force) {
-      value = existingValue;
+      value = existingValue->c_str();
     }
     if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
       std::vector<std::string>::size_type cc;
@@ -1748,7 +1749,7 @@ void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
       }
 
       this->GetCMakeInstance()->AddCacheEntry(name, nvalue.c_str(), doc, type);
-      nvalue = this->GetState()->GetInitializedCacheValue(name);
+      nvalue = *this->GetState()->GetInitializedCacheValue(name);
       value = nvalue.c_str();
     }
   }
@@ -2368,17 +2369,15 @@ std::string cmMakefile::GetRequiredDefinition(const std::string& name) const
 
 bool cmMakefile::IsDefinitionSet(const std::string& name) const
 {
-  const char* def;
-  if (const std::string* d = this->StateSnapshot.GetDefinition(name)) {
-    def = d->c_str();
-  } else {
+  const std::string* def = this->StateSnapshot.GetDefinition(name);
+  if (!def) {
     def = this->GetState()->GetInitializedCacheValue(name);
   }
 #ifdef CMAKE_BUILD_WITH_CMAKE
   if (cmVariableWatch* vv = this->GetVariableWatch()) {
     if (!def) {
       vv->VariableAccessed(
-        name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS, def, this);
+        name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS, nullptr, this);
     }
   }
 #endif
@@ -2387,10 +2386,8 @@ bool cmMakefile::IsDefinitionSet(const std::string& name) const
 
 const char* cmMakefile::GetDefinition(const std::string& name) const
 {
-  const char* def;
-  if (const std::string* d = this->StateSnapshot.GetDefinition(name)) {
-    def = d->c_str();
-  } else {
+  const std::string* def = this->StateSnapshot.GetDefinition(name);
+  if (!def) {
     def = this->GetState()->GetInitializedCacheValue(name);
   }
 #ifdef CMAKE_BUILD_WITH_CMAKE
@@ -2400,21 +2397,20 @@ const char* cmMakefile::GetDefinition(const std::string& name) const
       vv->VariableAccessed(name,
                            def ? cmVariableWatch::VARIABLE_READ_ACCESS
                                : cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS,
-                           def, this);
+                           (def ? def->c_str() : nullptr), this);
 
     if (watch_function_executed) {
       // A callback was executed and may have caused re-allocation of the
       // variable storage.  Look it up again for now.
       // FIXME: Refactor variable storage to avoid this problem.
-      if (const std::string* d = this->StateSnapshot.GetDefinition(name)) {
-        def = d->c_str();
-      } else {
+      def = this->StateSnapshot.GetDefinition(name);
+      if (!def) {
         def = this->GetState()->GetInitializedCacheValue(name);
       }
     }
   }
 #endif
-  return def;
+  return (def ? def->c_str() : nullptr);
 }
 
 const char* cmMakefile::GetSafeDefinition(const std::string& def) const
diff --git a/Source/cmServerProtocol.cxx b/Source/cmServerProtocol.cxx
index af4b466..0ed3953 100644
--- a/Source/cmServerProtocol.cxx
+++ b/Source/cmServerProtocol.cxx
@@ -1340,20 +1340,20 @@ cmServerResponse cmServerProtocol1::ProcessConfigure(
 
   if (cm->LoadCache(buildDir)) {
     // build directory has been set up before
-    const char* cachedSourceDir =
+    const std::string* cachedSourceDir =
       cm->GetState()->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
     if (!cachedSourceDir) {
       return request.ReportError("No CMAKE_HOME_DIRECTORY found in cache.");
     }
     if (sourceDir.empty()) {
-      sourceDir = std::string(cachedSourceDir);
+      sourceDir = *cachedSourceDir;
       cm->SetHomeDirectory(sourceDir);
     }
 
-    const char* cachedGenerator =
+    const std::string* cachedGenerator =
       cm->GetState()->GetInitializedCacheValue("CMAKE_GENERATOR");
     if (cachedGenerator) {
-      if (gg && gg->GetName() != cachedGenerator) {
+      if (gg && gg->GetName() != *cachedGenerator) {
         return request.ReportError("Configured generator does not match with "
                                    "CMAKE_GENERATOR found in cache.");
       }
diff --git a/Source/cmState.cxx b/Source/cmState.cxx
index e01bf71..c8b8653 100644
--- a/Source/cmState.cxx
+++ b/Source/cmState.cxx
@@ -140,10 +140,10 @@ const char* cmState::GetCacheEntryValue(std::string const& key) const
   return e->Value.c_str();
 }
 
-const char* cmState::GetInitializedCacheValue(std::string const& key) const
+const std::string* cmState::GetInitializedCacheValue(
+  std::string const& key) const
 {
-  const std::string* p = this->CacheManager->GetInitializedCacheValue(key);
-  return p ? p->c_str() : nullptr;
+  return this->CacheManager->GetInitializedCacheValue(key);
 }
 
 cmStateEnums::CacheEntryType cmState::GetCacheEntryType(
diff --git a/Source/cmState.h b/Source/cmState.h
index 38bdfec..ca7093a 100644
--- a/Source/cmState.h
+++ b/Source/cmState.h
@@ -68,7 +68,7 @@ public:
 
   std::vector<std::string> GetCacheEntryKeys() const;
   const char* GetCacheEntryValue(std::string const& key) const;
-  const char* GetInitializedCacheValue(std::string const& key) const;
+  const std::string* GetInitializedCacheValue(std::string const& key) const;
   cmStateEnums::CacheEntryType GetCacheEntryType(std::string const& key) const;
   void SetCacheEntryValue(std::string const& key, std::string const& value);
   void SetCacheValue(std::string const& key, std::string const& value);
diff --git a/Source/cmake.cxx b/Source/cmake.cxx
index 1bf8f7d..783dbf2 100644
--- a/Source/cmake.cxx
+++ b/Source/cmake.cxx
@@ -319,9 +319,10 @@ bool cmake::SetCacheArgs(const std::vector<std::string>& args)
         bool haveValue = false;
         std::string cachedValue;
         if (this->WarnUnusedCli) {
-          if (const char* v = this->State->GetInitializedCacheValue(var)) {
+          if (const std::string* v =
+                this->State->GetInitializedCacheValue(var)) {
             haveValue = true;
-            cachedValue = v;
+            cachedValue = *v;
           }
         }
 
@@ -331,7 +332,7 @@ bool cmake::SetCacheArgs(const std::vector<std::string>& args)
 
         if (this->WarnUnusedCli) {
           if (!haveValue ||
-              cachedValue != this->State->GetInitializedCacheValue(var)) {
+              cachedValue != *this->State->GetInitializedCacheValue(var)) {
             this->WatchUnusedCli(var);
           }
         }
@@ -1107,7 +1108,7 @@ int cmake::DoPreConfigureChecks()
   // do a sanity check on some values
   if (this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY")) {
     std::string cacheStart =
-      this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
+      *this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
     cacheStart += "/CMakeLists.txt";
     std::string currentStart = this->GetHomeDirectory();
     currentStart += "/CMakeLists.txt";
@@ -1276,14 +1277,14 @@ int cmake::ActualConfigure()
 
   // no generator specified on the command line
   if (!this->GlobalGenerator) {
-    const char* genName =
+    const std::string* genName =
       this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
-    const char* extraGenName =
+    const std::string* extraGenName =
       this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
     if (genName) {
       std::string fullName =
         cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
-          genName, extraGenName ? extraGenName : "");
+          *genName, extraGenName ? *extraGenName : "");
       this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
     }
     if (this->GlobalGenerator) {
@@ -1301,14 +1302,14 @@ int cmake::ActualConfigure()
     }
   }
 
-  const char* genName =
+  const std::string* genName =
     this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
   if (genName) {
-    if (!this->GlobalGenerator->MatchesGeneratorName(genName)) {
+    if (!this->GlobalGenerator->MatchesGeneratorName(*genName)) {
       std::string message = "Error: generator : ";
       message += this->GlobalGenerator->GetName();
       message += "\nDoes not match the generator used previously: ";
-      message += genName;
+      message += *genName;
       message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
                  "directory or choose a different binary directory.";
       cmSystemTools::Error(message.c_str());
@@ -1325,14 +1326,14 @@ int cmake::ActualConfigure()
                         cmStateEnums::INTERNAL);
   }
 
-  if (const char* instance =
+  if (const std::string* instance =
         this->State->GetInitializedCacheValue("CMAKE_GENERATOR_INSTANCE")) {
     if (!this->GeneratorInstance.empty() &&
-        this->GeneratorInstance != instance) {
+        this->GeneratorInstance != *instance) {
       std::string message = "Error: generator instance: ";
       message += this->GeneratorInstance;
       message += "\nDoes not match the instance used previously: ";
-      message += instance;
+      message += *instance;
       message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
                  "directory or choose a different binary directory.";
       cmSystemTools::Error(message.c_str());
@@ -1344,14 +1345,14 @@ int cmake::ActualConfigure()
       "Generator instance identifier.", cmStateEnums::INTERNAL);
   }
 
-  if (const char* platformName =
+  if (const std::string* platformName =
         this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) {
     if (!this->GeneratorPlatform.empty() &&
-        this->GeneratorPlatform != platformName) {
+        this->GeneratorPlatform != *platformName) {
       std::string message = "Error: generator platform: ";
       message += this->GeneratorPlatform;
       message += "\nDoes not match the platform used previously: ";
-      message += platformName;
+      message += *platformName;
       message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
                  "directory or choose a different binary directory.";
       cmSystemTools::Error(message.c_str());
@@ -1363,13 +1364,13 @@ int cmake::ActualConfigure()
                         "Name of generator platform.", cmStateEnums::INTERNAL);
   }
 
-  if (const char* tsName =
+  if (const std::string* tsName =
         this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) {
-    if (!this->GeneratorToolset.empty() && this->GeneratorToolset != tsName) {
+    if (!this->GeneratorToolset.empty() && this->GeneratorToolset != *tsName) {
       std::string message = "Error: generator toolset: ";
       message += this->GeneratorToolset;
       message += "\nDoes not match the toolset used previously: ";
-      message += tsName;
+      message += *tsName;
       message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
                  "directory or choose a different binary directory.";
       cmSystemTools::Error(message.c_str());
@@ -1685,7 +1686,8 @@ std::string cmake::StripExtension(const std::string& file) const
 
 const char* cmake::GetCacheDefinition(const std::string& name) const
 {
-  return this->State->GetInitializedCacheValue(name);
+  const std::string* p = this->State->GetInitializedCacheValue(name);
+  return p ? p->c_str() : nullptr;
 }
 
 void cmake::AddScriptingCommands()
@@ -1868,14 +1870,14 @@ void cmake::PrintGeneratorList()
 void cmake::UpdateConversionPathTable()
 {
   // Update the path conversion table with any specified file:
-  const char* tablepath =
+  const std::string* tablepath =
     this->State->GetInitializedCacheValue("CMAKE_PATH_TRANSLATION_FILE");
 
   if (tablepath) {
-    cmsys::ifstream table(tablepath);
+    cmsys::ifstream table(tablepath->c_str());
     if (!table) {
-      cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
-                           ". CMake can not open file.");
+      cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ",
+                           tablepath->c_str(), ". CMake can not open file.");
       cmSystemTools::ReportLastSystemError("CMake can not open file.");
     } else {
       std::string a, b;
@@ -2527,11 +2529,11 @@ bool cmake::Open(const std::string& dir, bool dryRun)
     std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
     return false;
   }
-  const char* extraGenName =
+  const std::string* extraGenName =
     this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
   std::string fullName =
     cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
-      genName, extraGenName ? extraGenName : "");
+      genName, extraGenName ? *extraGenName : "");
 
   std::unique_ptr<cmGlobalGenerator> gen(
     this->CreateGlobalGenerator(fullName));
-- 
cgit v0.12