diff options
author | Ben Boeckel <ben.boeckel@kitware.com> | 2010-09-14 19:22:31 (GMT) |
---|---|---|
committer | Ben Boeckel <ben.boeckel@kitware.com> | 2010-09-14 21:10:04 (GMT) |
commit | 980e048a7d5356a881dbaaf25b1595091fe5cb8b (patch) | |
tree | 2c4bf3e45adfb460e4007aa1b5369e20a02674c2 | |
parent | 83acb0a4b22aa57d3c345320333b75301a99b2ba (diff) | |
download | CMake-980e048a7d5356a881dbaaf25b1595091fe5cb8b.zip CMake-980e048a7d5356a881dbaaf25b1595091fe5cb8b.tar.gz CMake-980e048a7d5356a881dbaaf25b1595091fe5cb8b.tar.bz2 |
Factor out checks for unused variables
-rw-r--r-- | Source/cmMakefile.cxx | 49 | ||||
-rw-r--r-- | Source/cmMakefile.h | 5 |
2 files changed, 36 insertions, 18 deletions
diff --git a/Source/cmMakefile.cxx b/Source/cmMakefile.cxx index c15f5b3..c174bd6 100644 --- a/Source/cmMakefile.cxx +++ b/Source/cmMakefile.cxx @@ -1649,6 +1649,11 @@ void cmMakefile::AddDefinition(const char* name, const char* value) #endif this->Internal->VarStack.top().Set(name, value); + if (this->Internal->VarUsageStack.size() > 1) + { + this->CheckForUnused("changing definition", name); + this->Internal->VarUsageStack.top().erase(name); + } #ifdef CMAKE_BUILD_WITH_CMAKE cmVariableWatch* vv = this->GetVariableWatch(); @@ -1714,6 +1719,11 @@ void cmMakefile::AddDefinition(const char* name, bool value) { this->Internal->VarStack.top().Set(name, value? "ON" : "OFF"); this->Internal->VarInitStack.top().insert(name); + if (this->Internal->VarUsageStack.size() > 1) + { + this->CheckForUnused("changing definition", name); + this->Internal->VarUsageStack.top().erase(name); + } #ifdef CMAKE_BUILD_WITH_CMAKE cmVariableWatch* vv = this->GetVariableWatch(); if ( vv ) @@ -1751,15 +1761,32 @@ bool cmMakefile::VariableCleared(const char* var) const return false; } +bool cmMakefile::CheckForUnused(const char* reason, const char* name) +{ + if (this->WarnUnused && !this->VariableUsed(name)) + { + const char* cdir = this->ListFileStack.back().c_str(); + if (this->CheckSystemVars || + cmSystemTools::IsSubDirectory(cdir, this->GetHomeDirectory()) || + cmSystemTools::IsSubDirectory(cdir, this->GetHomeOutputDirectory())) + { + cmOStringStream msg; + const cmListFileContext* file = this->CallStack.back().Context; + msg << file->FilePath << ":" << file->Line << ":" << + " warning: (" << reason << ") unused variable \'" << name << "\'"; + cmSystemTools::Message(msg.str().c_str()); + return true; + } + } + return false; +} + void cmMakefile::RemoveDefinition(const char* name) { this->Internal->VarStack.top().Set(name, 0); this->Internal->VarRemoved.insert(name); this->Internal->VarInitStack.top().insert(name); - if (this->WarnUnused) - { - this->Internal->VarUsageStack.top().insert(name); - } + this->CheckForUnused("unsetting", name); #ifdef CMAKE_BUILD_WITH_CMAKE cmVariableWatch* vv = this->GetVariableWatch(); if ( vv ) @@ -3391,19 +3418,7 @@ void cmMakefile::PopScope() for (; it != locals.end(); ++it) { init.erase(*it); - if (this->WarnUnused && usage.find(*it) == usage.end()) - { - const char* cdir = this->ListFileStack.back().c_str(); - if (this->CheckSystemVars || - cmSystemTools::IsSubDirectory(cdir, this->GetHomeDirectory()) || - cmSystemTools::IsSubDirectory(cdir, this->GetHomeOutputDirectory())) - { - cmOStringStream m; - m << "unused variable \'" << *it << "\'"; - this->IssueMessage(cmake::AUTHOR_WARNING, m.str()); - } - } - else + if (!this->CheckForUnused("out of scope", it->c_str())) { usage.erase(*it); } diff --git a/Source/cmMakefile.h b/Source/cmMakefile.h index f1ad54d..89979d6 100644 --- a/Source/cmMakefile.h +++ b/Source/cmMakefile.h @@ -840,7 +840,10 @@ public: protected: // add link libraries and directories to the target void AddGlobalLinkInformation(const char* name, cmTarget& target); - + + // Check for a an unused variable + bool CheckForUnused(const char* reason, const char* name); + std::string Prefix; std::vector<std::string> AuxSourceDirectories; // |