Add a warning when variables are used uninitialized.

This commit is contained in:
Bill Hoffman 2010-04-13 09:21:31 -04:00 committed by Ben Boeckel
parent cd626ea66e
commit 48b5b85593
3 changed files with 21 additions and 1 deletions

View File

@ -123,6 +123,11 @@ char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
const char* value = this->Makefile->GetDefinition(var); const char* value = this->Makefile->GetDefinition(var);
if(!value && !this->RemoveEmpty) if(!value && !this->RemoveEmpty)
{ {
if(!this->Makefile->VariableCleared(var))
{
std::cerr << this->FileName << ":" << this->FileLine << ":" <<
" warning: uninitialized variable \'" << var << "\'\n";
}
return 0; return 0;
} }
if (this->EscapeQuotes && value) if (this->EscapeQuotes && value)

View File

@ -43,6 +43,7 @@ class cmMakefile::Internals
{ {
public: public:
std::stack<cmDefinitions, std::list<cmDefinitions> > VarStack; std::stack<cmDefinitions, std::list<cmDefinitions> > VarStack;
std::set<cmStdString> VarRemoved;
}; };
// default is not to be building executables // default is not to be building executables
@ -1694,9 +1695,19 @@ void cmMakefile::AddDefinition(const char* name, bool value)
#endif #endif
} }
bool cmMakefile::VariableCleared(const char* var) const
{
if(this->Internal->VarRemoved.find(var) != this->Internal->VarRemoved.end())
{
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);
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
cmVariableWatch* vv = this->GetVariableWatch(); cmVariableWatch* vv = this->GetVariableWatch();
if ( vv ) if ( vv )

View File

@ -61,6 +61,10 @@ public:
unsigned int GetCacheMajorVersion(); unsigned int GetCacheMajorVersion();
unsigned int GetCacheMinorVersion(); unsigned int GetCacheMinorVersion();
/* return true if a variable has been set with
set(foo )
*/
bool VariableCleared(const char* ) const;
/** Return whether compatibility features needed for a version of /** Return whether compatibility features needed for a version of
the cache or lower should be enabled. */ the cache or lower should be enabled. */
bool NeedCacheCompatibility(int major, int minor); bool NeedCacheCompatibility(int major, int minor);