From 437d0c16c72a45da8c81919f953859f5af71b19b Mon Sep 17 00:00:00 2001 From: Vitaly Stakhovsky Date: Wed, 5 Sep 2018 20:08:17 -0400 Subject: cmStateSnapshot::GetDefinition(): Return std::string const* Expose std::string type used internally in cmDefinitions instead of const char* --- Source/cmDefinitions.cxx | 6 +++--- Source/cmDefinitions.h | 4 ++-- Source/cmMakefile.cxx | 17 +++++++++++------ Source/cmOptionCommand.cxx | 4 ++-- Source/cmState.cxx | 4 ++-- Source/cmStateSnapshot.cxx | 7 ++++--- Source/cmStateSnapshot.h | 2 +- 7 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Source/cmDefinitions.cxx b/Source/cmDefinitions.cxx index e766854..5fafaf9 100644 --- a/Source/cmDefinitions.cxx +++ b/Source/cmDefinitions.cxx @@ -30,11 +30,11 @@ cmDefinitions::Def const& cmDefinitions::GetInternal(const std::string& key, return begin->Map.insert(MapType::value_type(key, def)).first->second; } -const char* cmDefinitions::Get(const std::string& key, StackIter begin, - StackIter end) +const std::string* cmDefinitions::Get(const std::string& key, StackIter begin, + StackIter end) { Def const& def = cmDefinitions::GetInternal(key, begin, end, false); - return def.Exists ? def.c_str() : nullptr; + return def.Exists ? &def : nullptr; } void cmDefinitions::Raise(const std::string& key, StackIter begin, diff --git a/Source/cmDefinitions.h b/Source/cmDefinitions.h index 528b157..4ab5be6 100644 --- a/Source/cmDefinitions.h +++ b/Source/cmDefinitions.h @@ -23,8 +23,8 @@ class cmDefinitions typedef cmLinkedTree::iterator StackIter; public: - static const char* Get(const std::string& key, StackIter begin, - StackIter end); + static const std::string* Get(const std::string& key, StackIter begin, + StackIter end); static void Raise(const std::string& key, StackIter begin, StackIter end); diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index 6127b57..8d2f932 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -2368,8 +2368,10 @@ const char* cmMakefile::GetRequiredDefinition(const std::string& name) const bool cmMakefile::IsDefinitionSet(const std::string& name) const { - const char* def = this->StateSnapshot.GetDefinition(name); - if (!def) { + const char* def; + if (const std::string* d = this->StateSnapshot.GetDefinition(name)) { + def = d->c_str(); + } else { def = this->GetState()->GetInitializedCacheValue(name); } #ifdef CMAKE_BUILD_WITH_CMAKE @@ -2385,8 +2387,10 @@ bool cmMakefile::IsDefinitionSet(const std::string& name) const const char* cmMakefile::GetDefinition(const std::string& name) const { - const char* def = this->StateSnapshot.GetDefinition(name); - if (!def) { + const char* def; + if (const std::string* d = this->StateSnapshot.GetDefinition(name)) { + def = d->c_str(); + } else { def = this->GetState()->GetInitializedCacheValue(name); } #ifdef CMAKE_BUILD_WITH_CMAKE @@ -2402,8 +2406,9 @@ const char* cmMakefile::GetDefinition(const std::string& name) const // 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. - def = this->StateSnapshot.GetDefinition(name); - if (!def) { + if (const std::string* d = this->StateSnapshot.GetDefinition(name)) { + def = d->c_str(); + } else { def = this->GetState()->GetInitializedCacheValue(name); } } diff --git a/Source/cmOptionCommand.cxx b/Source/cmOptionCommand.cxx index 239cd00..7d620bd 100644 --- a/Source/cmOptionCommand.cxx +++ b/Source/cmOptionCommand.cxx @@ -31,7 +31,7 @@ bool cmOptionCommand::InitialPass(std::vector const& args, bool checkAndWarn = false; { auto status = this->Makefile->GetPolicyStatus(cmPolicies::CMP0077); - const char* existsBeforeSet = + const auto* existsBeforeSet = this->Makefile->GetStateSnapshot().GetDefinition(args[0]); switch (status) { case cmPolicies::WARN: @@ -72,7 +72,7 @@ bool cmOptionCommand::InitialPass(std::vector const& args, args[1].c_str(), cmStateEnums::BOOL); if (checkAndWarn) { - const char* existsAfterSet = + const auto* existsAfterSet = this->Makefile->GetStateSnapshot().GetDefinition(args[0]); if (!existsAfterSet) { std::ostringstream w; diff --git a/Source/cmState.cxx b/Source/cmState.cxx index 5651594..89738f4 100644 --- a/Source/cmState.cxx +++ b/Source/cmState.cxx @@ -298,9 +298,9 @@ cmStateSnapshot cmState::Reset() { std::string srcDir = - cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root); + *cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root); std::string binDir = - cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root); + *cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root); this->VarTree.Clear(); pos->Vars = this->VarTree.Push(this->VarTree.Root()); pos->Parent = this->VarTree.Root(); diff --git a/Source/cmStateSnapshot.cxx b/Source/cmStateSnapshot.cxx index ec428a6..0379e7e 100644 --- a/Source/cmStateSnapshot.cxx +++ b/Source/cmStateSnapshot.cxx @@ -204,7 +204,8 @@ bool cmStateSnapshot::HasDefinedPolicyCMP0011() return !this->Position->Policies->IsEmpty(); } -const char* cmStateSnapshot::GetDefinition(std::string const& name) const +std::string const* cmStateSnapshot::GetDefinition( + std::string const& name) const { assert(this->Position->Vars.IsValid()); return cmDefinitions::Get(name, this->Position->Vars, this->Position->Root); @@ -426,8 +427,8 @@ std::string cmStateSnapshot::GetProjectName() const void cmStateSnapshot::InitializeFromParent_ForSubdirsCommand() { - std::string currentSrcDir = this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR"); - std::string currentBinDir = this->GetDefinition("CMAKE_CURRENT_BINARY_DIR"); + std::string currentSrcDir = *this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR"); + std::string currentBinDir = *this->GetDefinition("CMAKE_CURRENT_BINARY_DIR"); this->InitializeFromParent(); this->SetDefinition("CMAKE_SOURCE_DIR", this->State->GetSourceDirectory()); this->SetDefinition("CMAKE_BINARY_DIR", this->State->GetBinaryDirectory()); diff --git a/Source/cmStateSnapshot.h b/Source/cmStateSnapshot.h index af5653b..014c62e 100644 --- a/Source/cmStateSnapshot.h +++ b/Source/cmStateSnapshot.h @@ -22,7 +22,7 @@ public: cmStateSnapshot(cmState* state = nullptr); cmStateSnapshot(cmState* state, cmStateDetail::PositionType position); - const char* GetDefinition(std::string const& name) const; + std::string const* GetDefinition(std::string const& name) const; bool IsInitialized(std::string const& name) const; void SetDefinition(std::string const& name, std::string const& value); void RemoveDefinition(std::string const& name); -- cgit v0.12