Factor out checks for unused variables

This commit is contained in:
Ben Boeckel 2010-09-14 15:22:31 -04:00
parent 83acb0a4b2
commit 980e048a7d
2 changed files with 36 additions and 18 deletions

View File

@ -1649,6 +1649,11 @@ void cmMakefile::AddDefinition(const char* name, const char* value)
#endif #endif
this->Internal->VarStack.top().Set(name, value); 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 #ifdef CMAKE_BUILD_WITH_CMAKE
cmVariableWatch* vv = this->GetVariableWatch(); 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->VarStack.top().Set(name, value? "ON" : "OFF");
this->Internal->VarInitStack.top().insert(name); 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 #ifdef CMAKE_BUILD_WITH_CMAKE
cmVariableWatch* vv = this->GetVariableWatch(); cmVariableWatch* vv = this->GetVariableWatch();
if ( vv ) if ( vv )
@ -1751,15 +1761,32 @@ bool cmMakefile::VariableCleared(const char* var) const
return false; 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) void cmMakefile::RemoveDefinition(const char* name)
{ {
this->Internal->VarStack.top().Set(name, 0); this->Internal->VarStack.top().Set(name, 0);
this->Internal->VarRemoved.insert(name); this->Internal->VarRemoved.insert(name);
this->Internal->VarInitStack.top().insert(name); this->Internal->VarInitStack.top().insert(name);
if (this->WarnUnused) this->CheckForUnused("unsetting", name);
{
this->Internal->VarUsageStack.top().insert(name);
}
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
cmVariableWatch* vv = this->GetVariableWatch(); cmVariableWatch* vv = this->GetVariableWatch();
if ( vv ) if ( vv )
@ -3391,19 +3418,7 @@ void cmMakefile::PopScope()
for (; it != locals.end(); ++it) for (; it != locals.end(); ++it)
{ {
init.erase(*it); init.erase(*it);
if (this->WarnUnused && usage.find(*it) == usage.end()) if (!this->CheckForUnused("out of scope", it->c_str()))
{
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
{ {
usage.erase(*it); usage.erase(*it);
} }

View File

@ -841,6 +841,9 @@ protected:
// add link libraries and directories to the target // add link libraries and directories to the target
void AddGlobalLinkInformation(const char* name, cmTarget& 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::string Prefix;
std::vector<std::string> AuxSourceDirectories; // std::vector<std::string> AuxSourceDirectories; //