From b9f9915516c9b426f4f528bb1ec5a79d115e21ab Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sun, 17 May 2015 16:19:55 +0200 Subject: cmMakefile: Remove VarUsageStack. Store usage information in the cmDefintions value instead. We already pay for the memory as padding in the Def struct, so we might as well use it. --- Source/cmDefinitions.cxx | 7 +++--- Source/cmDefinitions.h | 16 ++++++++----- Source/cmMakefile.cxx | 58 +++++++----------------------------------------- Source/cmMakefile.h | 2 -- 4 files changed, 22 insertions(+), 61 deletions(-) diff --git a/Source/cmDefinitions.cxx b/Source/cmDefinitions.cxx index 5b03bd4..2dab169 100644 --- a/Source/cmDefinitions.cxx +++ b/Source/cmDefinitions.cxx @@ -21,9 +21,10 @@ cmDefinitions::Def const& cmDefinitions::GetInternal( const std::string& key, StackIter begin, StackIter end, bool raise) { assert(begin != end); - MapType::const_iterator i = begin->Map.find(key); + MapType::iterator i = begin->Map.find(key); if (i != begin->Map.end()) { + i->second.Used = true; return i->second; } StackIter it = begin; @@ -76,7 +77,7 @@ void cmDefinitions::Set(const std::string& key, const char* value) } //---------------------------------------------------------------------------- -std::vector cmDefinitions::LocalKeys() const +std::vector cmDefinitions::UnusedKeys() const { std::vector keys; keys.reserve(this->Map.size()); @@ -84,7 +85,7 @@ std::vector cmDefinitions::LocalKeys() const for(MapType::const_iterator mi = this->Map.begin(); mi != this->Map.end(); ++mi) { - if (mi->second.Exists) + if (!mi->second.Used) { keys.push_back(mi->first); } diff --git a/Source/cmDefinitions.h b/Source/cmDefinitions.h index bd3d392..5fdcaab 100644 --- a/Source/cmDefinitions.h +++ b/Source/cmDefinitions.h @@ -46,8 +46,7 @@ public: /** Set (or unset if null) a value associated with a key. */ void Set(const std::string& key, const char* value); - /** Get the set of all local keys. */ - std::vector LocalKeys() const; + std::vector UnusedKeys() const; static std::vector ClosureKeys(StackConstIter begin, StackConstIter end); @@ -61,11 +60,16 @@ private: private: typedef std::string std_string; public: - Def(): std_string(), Exists(false) {} - Def(const char* v): std_string(v?v:""), Exists(v?true:false) {} - Def(const std_string& v): std_string(v), Exists(true) {} - Def(Def const& d): std_string(d), Exists(d.Exists) {} + Def(): std_string(), Exists(false), Used(false) {} + Def(const char* v) + : std_string(v ? v : ""), + Exists(v ? true : false), + Used(false) + {} + Def(const std_string& v): std_string(v), Exists(true), Used(false) {} + Def(Def const& d): std_string(d), Exists(d.Exists), Used(d.Used) {} bool Exists; + bool Used; }; static Def NoDef; diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index bad4e6a..31ab66d 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -38,7 +38,6 @@ #include #include -#include #include #include // for isspace #include @@ -47,7 +46,6 @@ class cmMakefile::Internals { public: std::list VarStack; - std::stack > VarUsageStack; bool IsSourceFileTryCompile; void PushDefinitions() @@ -84,9 +82,9 @@ public: this->VarStack.back().Set(name, 0); } - std::vector LocalKeys() const + std::vector UnusedKeys() const { - return this->VarStack.back().LocalKeys(); + return this->VarStack.back().UnusedKeys(); } std::vector ClosureKeys() const @@ -142,7 +140,6 @@ cmMakefile::cmMakefile(cmLocalGenerator* localGenerator) StateSnapshot(localGenerator->GetStateSnapshot()) { this->Internal->PushDefinitions(); - this->Internal->VarUsageStack.push(std::set()); this->Internal->IsSourceFileTryCompile = false; // Initialize these first since AddDefaultDefinitions calls AddDefinition @@ -588,7 +585,6 @@ bool cmMakefile::ReadListFile(const char* listfile, if (res) { - // Check for unused variables this->CheckForUnusedVariables(); } @@ -1726,7 +1722,6 @@ void cmMakefile::AddDefinition(const std::string& name, const char* value) if (this->VariableInitialized(name)) { this->LogUnused("changing definition", name); - this->Internal->VarUsageStack.top().erase(name); } this->Internal->SetDefinition(name, value); @@ -1800,7 +1795,6 @@ void cmMakefile::AddDefinition(const std::string& name, bool value) if (this->VariableInitialized(name)) { this->LogUnused("changing definition", name); - this->Internal->VarUsageStack.top().erase(name); } this->Internal->SetDefinition(name, value ? "ON" : "OFF"); #ifdef CMAKE_BUILD_WITH_CMAKE @@ -1819,9 +1813,9 @@ void cmMakefile::CheckForUnusedVariables() const { return; } - const std::vector& locals = this->Internal->LocalKeys(); - std::vector::const_iterator it = locals.begin(); - for (; it != locals.end(); ++it) + const std::vector& unused = this->Internal->UnusedKeys(); + std::vector::const_iterator it = unused.begin(); + for (; it != unused.end(); ++it) { this->LogUnused("out of scope", *it); } @@ -1829,7 +1823,7 @@ void cmMakefile::CheckForUnusedVariables() const void cmMakefile::MarkVariableAsUsed(const std::string& var) { - this->Internal->VarUsageStack.top().insert(var); + this->Internal->GetDefinition(var); } bool cmMakefile::VariableInitialized(const std::string& var) const @@ -1837,20 +1831,10 @@ bool cmMakefile::VariableInitialized(const std::string& var) const return this->Internal->IsInitialized(var); } -bool cmMakefile::VariableUsed(const std::string& var) const -{ - if(this->Internal->VarUsageStack.top().find(var) != - this->Internal->VarUsageStack.top().end()) - { - return true; - } - return false; -} - void cmMakefile::LogUnused(const char* reason, const std::string& name) const { - if (this->WarnUnused && !this->VariableUsed(name)) + if (this->WarnUnused) { std::string path; cmListFileBacktrace bt(this->GetLocalGenerator()); @@ -1891,7 +1875,6 @@ void cmMakefile::RemoveDefinition(const std::string& name) if (this->VariableInitialized(name)) { this->LogUnused("unsetting", name); - this->Internal->VarUsageStack.top().erase(name); } this->Internal->RemoveDefinition(name); #ifdef CMAKE_BUILD_WITH_CMAKE @@ -2360,7 +2343,6 @@ const char* cmMakefile::GetRequiredDefinition(const std::string& name) const bool cmMakefile::IsDefinitionSet(const std::string& name) const { const char* def = this->Internal->GetDefinition(name); - this->Internal->VarUsageStack.top().insert(name); if(!def) { def = this->GetState()->GetInitializedCacheValue(name); @@ -2381,10 +2363,6 @@ bool cmMakefile::IsDefinitionSet(const std::string& name) const const char* cmMakefile::GetDefinition(const std::string& name) const { - if (this->WarnUnused) - { - this->Internal->VarUsageStack.top().insert(name); - } const char* def = this->Internal->GetDefinition(name); if(!def) { @@ -4312,8 +4290,6 @@ std::string cmMakefile::FormatListFileStack() const void cmMakefile::PushScope() { this->Internal->PushDefinitions(); - const std::set& usage = this->Internal->VarUsageStack.top(); - this->Internal->VarUsageStack.push(usage); this->PushLoopBlockBarrier(); @@ -4330,27 +4306,9 @@ void cmMakefile::PopScope() this->PopLoopBlockBarrier(); - std::set usage = this->Internal->VarUsageStack.top(); - const std::vector& locals = this->Internal->LocalKeys(); - // Remove initialization and usage information for variables in the local - // scope. - std::vector::const_iterator it = locals.begin(); - for (; it != locals.end(); ++it) - { - if (!this->VariableUsed(*it)) - { - this->LogUnused("out of scope", *it); - } - else - { - usage.erase(*it); - } - } + this->CheckForUnusedVariables(); this->Internal->PopDefinitions(); - this->Internal->VarUsageStack.pop(); - // Push usage up to the parent scope. - this->Internal->VarUsageStack.top().insert(usage.begin(), usage.end()); } void cmMakefile::RaiseScope(const std::string& var, const char *varDef) diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index a8873ff..efd73a1 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -69,8 +69,6 @@ public: void MarkVariableAsUsed(const std::string& var); /* return true if a variable has been initialized */ bool VariableInitialized(const std::string& ) const; - /* return true if a variable has been used */ - bool VariableUsed(const std::string& ) const; /** * Construct an empty makefile. -- cgit v0.12