Fix if() command and CMP0012 OLD/NEW behavior

The commit "modified the if command to address bug 9123 some" changed
the if() command behavior with respect to named boolean constants.  It
introduced policy CMP0012 to provide compatibility.  However, it also
changed behavior with respect to numbers (like '2') but did not cover
the change with the policy.  Also, the behavior it created for numbers
is confusing ('2' is false).

This commit teaches if() to recognize numbers again, and treats them
like the C language does in terms of boolean conversion.  We also fix
the CMP0012 check to trigger in all cases where the result of boolean
coersion differs from that produced by CMake 2.6.4.
This commit is contained in:
Brad King 2009-10-27 09:07:39 -04:00
parent 9f43fa602d
commit cb185d93d2
4 changed files with 137 additions and 104 deletions

View File

@ -213,84 +213,69 @@ bool cmIfCommand
namespace namespace
{ {
//========================================================================= //=========================================================================
// returns true if succesfull, the resulting bool parsed is stored in result bool GetBooleanValue(std::string& arg, cmMakefile* mf)
bool GetBooleanValue(std::string &newArg,
cmMakefile *makefile,
bool &result,
std::string &errorString,
cmPolicies::PolicyStatus Policy12Status,
cmake::MessageType &status)
{ {
if (Policy12Status != cmPolicies::OLD && // Check basic constants.
Policy12Status != cmPolicies::WARN) if (arg == "0")
{
// please note IsOn(var) does not always equal !IsOff(var)
// that is why each is called
if (cmSystemTools::IsOn(newArg.c_str()))
{
result = true;
return true;
}
if (cmSystemTools::IsOff(newArg.c_str()))
{
result = false;
return true;
}
return false;
}
// Old policy is more complex...
// 0 and 1 are very common, test for them first quickly
if (newArg == "0")
{ {
result = false;
return true;
}
if (newArg == "1")
{
result = true;
return true;
}
// old behavior is to dereference the var
if (Policy12Status == cmPolicies::OLD)
{
return false;
}
// now test for values that may be the name of a variable
// warn if used
if (cmSystemTools::IsOn(newArg.c_str()))
{
// only warn if the value would change
const char *def = makefile->GetDefinition(newArg.c_str());
if (cmSystemTools::IsOff(def))
{
cmPolicies* policies = makefile->GetPolicies();
errorString = "A variable or argument named \""
+ newArg
+ "\" appears in a conditional statement. "
+ policies->GetPolicyWarning(cmPolicies::CMP0012);
status = cmake::AUTHOR_WARNING;
}
return false;
}
if (cmSystemTools::IsOff(newArg.c_str()))
{
// only warn if the value would change
const char *def = makefile->GetDefinition(newArg.c_str());
if (!cmSystemTools::IsOff(def))
{
cmPolicies* policies = makefile->GetPolicies();
errorString = "A variable or argument named \""
+ newArg
+ "\" appears in a conditional statement. "
+ policies->GetPolicyWarning(cmPolicies::CMP0012);
status = cmake::AUTHOR_WARNING;
}
return false;
}
return false; return false;
}
if (arg == "1")
{
return true;
}
// Check named constants.
if (cmSystemTools::IsOn(arg.c_str()))
{
return true;
}
if (cmSystemTools::IsOff(arg.c_str()))
{
return false;
}
// Check for numbers.
if(!arg.empty())
{
char* end;
double d = strtod(arg.c_str(), &end);
if(*end == '\0')
{
// The whole string is a number. Use C conversion to bool.
return d? true:false;
}
}
// Check definition.
const char* def = mf->GetDefinition(arg.c_str());
return !cmSystemTools::IsOff(def);
}
//=========================================================================
// Boolean value behavior from CMake 2.6.4 and below.
bool GetBooleanValueOld(std::string const& arg, cmMakefile* mf, bool one)
{
if(one)
{
// Old IsTrue behavior for single argument.
if(arg == "0")
{ return false; }
else if(arg == "1")
{ return true; }
else
{ return !cmSystemTools::IsOff(mf->GetDefinition(arg.c_str())); }
}
else
{
// Old GetVariableOrNumber behavior.
const char* def = mf->GetDefinition(arg.c_str());
if(!def && atoi(arg.c_str()))
{
def = arg.c_str();
}
return !cmSystemTools::IsOff(def);
}
} }
//========================================================================= //=========================================================================
@ -300,16 +285,51 @@ namespace
cmMakefile *makefile, cmMakefile *makefile,
std::string &errorString, std::string &errorString,
cmPolicies::PolicyStatus Policy12Status, cmPolicies::PolicyStatus Policy12Status,
cmake::MessageType &status) cmake::MessageType &status,
bool oneArg = false)
{ {
bool result = false; // Use the policy if it is set.
if (GetBooleanValue(newArg, makefile, result, if (Policy12Status == cmPolicies::NEW)
errorString, Policy12Status, status))
{ {
return result; return GetBooleanValue(newArg, makefile);
} }
const char *def = makefile->GetDefinition(newArg.c_str()); else if (Policy12Status == cmPolicies::OLD)
return !cmSystemTools::IsOff(def); {
return GetBooleanValueOld(newArg, makefile, oneArg);
}
// Check policy only if old and new results differ.
bool newResult = GetBooleanValue(newArg, makefile);
bool oldResult = GetBooleanValueOld(newArg, makefile, oneArg);
if(newResult != oldResult)
{
switch(Policy12Status)
{
case cmPolicies::WARN:
{
cmPolicies* policies = makefile->GetPolicies();
errorString = "An argument named \"" + newArg
+ "\" appears in a conditional statement. "
+ policies->GetPolicyWarning(cmPolicies::CMP0012);
status = cmake::AUTHOR_WARNING;
}
case cmPolicies::OLD:
return oldResult;
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
{
cmPolicies* policies = makefile->GetPolicies();
errorString = "An argument named \"" + newArg
+ "\" appears in a conditional statement. "
+ policies->GetRequiredPolicyError(cmPolicies::CMP0012);
status = cmake::FATAL_ERROR;
}
case cmPolicies::NEW:
break;
}
}
return newResult;
} }
//========================================================================= //=========================================================================
@ -898,7 +918,7 @@ bool cmIfCommand::IsTrue(const std::vector<std::string> &args,
makefile, makefile,
errorString, errorString,
Policy12Status, Policy12Status,
status); status, true);
} }
//========================================================================= //=========================================================================

View File

@ -121,16 +121,24 @@ public:
"Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES " "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
"will be evaluated. Then NOT operators and finally AND, OR operators " "will be evaluated. Then NOT operators and finally AND, OR operators "
"will be evaluated. Possible expressions are:\n" "will be evaluated. Possible expressions are:\n"
" if(variable)\n" " if(<constant>)\n"
"True if the variable's value is not empty, 0, N, NO, OFF, FALSE, " "True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number. "
"NOTFOUND, or <variable>-NOTFOUND.\n" "False if the constant is 0, OFF, NO, FALSE, N, IGNORE, \"\", "
" if(NOT variable)\n" "or ends in the suffix '-NOTFOUND'. "
"True if the variable's value is empty, 0, N, NO, OFF, FALSE, " "Named boolean constants are case-insensitive."
"NOTFOUND, or <variable>-NOTFOUND.\n" "\n"
" if(variable1 AND variable2)\n" " if(<variable>)\n"
"True if both variables would be considered true individually.\n" "True if the variable's value is not a false constant."
" if(variable1 OR variable2)\n" "\n"
"True if either variable would be considered true individually.\n" " if(NOT <expression>)\n"
"True if the expression is not true."
"\n"
" if(<expr1> AND <expr2>)\n"
"True if both expressions would be considered true individually."
"\n"
" if(<expr1> OR <expr2>)\n"
"True if either expression would be considered true individually."
"\n"
" if(COMMAND command-name)\n" " if(COMMAND command-name)\n"
"True if the given name is a command, macro or function that can be " "True if the given name is a command, macro or function that can be "
"invoked.\n" "invoked.\n"

View File

@ -358,19 +358,24 @@ cmPolicies::cmPolicies()
this->DefinePolicy( this->DefinePolicy(
CMP0012, "CMP0012", CMP0012, "CMP0012",
"The if() command can recognize named boolean constants.", "if() recognizes numbers and boolean constants.",
"In CMake versions 2.6.4 and lower the only boolean constants were 0 " "In CMake versions 2.6.4 and lower the if() command implicitly "
"and 1. Other boolean constants such as true, false, yes, no, " "dereferenced arguments corresponding to variables, even those named "
"like numbers or boolean constants, except for 0 and 1. "
"Numbers and boolean constants such as true, false, yes, no, "
"on, off, y, n, notfound, ignore (all case insensitive) were recognized " "on, off, y, n, notfound, ignore (all case insensitive) were recognized "
"in some cases but not all. " "in some cases but not all. "
"For example, the code \"if(TRUE)\" might have evaluated as false. " "For example, the code \"if(TRUE)\" might have evaluated as false. "
"In later versions of cmake these values are " "Numbers such as 2 were recognized only in "
"treated as boolean constants more consistently and should not be used " "boolean expressions like \"if(NOT 2)\" (leading to false) "
"as variable names. " "but not as a single-argument like \"if(2)\" (also leading to false). "
"The OLD behavior for this policy is to allow variables to have names " "Later versions of CMake prefer to treat numbers and boolean constants "
"such as true and to dereference them. " "literally, so they should not be used as variable names."
"The NEW behavior for this policy is to treat strings like true as a " "\n"
"boolean constant.", "The OLD behavior for this policy is to implicitly dereference variables "
"named like numbers and boolean constants. "
"The NEW behavior for this policy is to recognize numbers and "
"boolean constants without dereferencing variables with such names.",
2,8,0, cmPolicies::WARN); 2,8,0, cmPolicies::WARN);
this->DefinePolicy( this->DefinePolicy(

View File

@ -47,7 +47,7 @@ public:
CMP0009, // GLOB_RECURSE should not follow symlinks by default CMP0009, // GLOB_RECURSE should not follow symlinks by default
CMP0010, // Bad variable reference syntax is an error CMP0010, // Bad variable reference syntax is an error
CMP0011, // Strong policy scope for include and find_package CMP0011, // Strong policy scope for include and find_package
CMP0012, // Strong handling of boolean constants CMP0012, // Recognize numbers and boolean constants in if()
CMP0013, // Duplicate binary directories not allowed CMP0013, // Duplicate binary directories not allowed
CMP0014, // Input directories must have CMakeLists.txt CMP0014, // Input directories must have CMakeLists.txt