Fixed #1881 (false positive: The function '...' can be const (nonconst code is hidden inside #if))

This commit is contained in:
Daniel Marjamäki 2010-08-07 13:08:36 +02:00
parent 54121a74ff
commit 050011d287
5 changed files with 34 additions and 3 deletions

View File

@ -1849,7 +1849,7 @@ bool CheckClass::isVirtual(const std::vector<std::string> &derivedFrom, const To
// Can a function be const?
void CheckClass::checkConst()
{
if (!_settings->_checkCodingStyle)
if (!_settings->_checkCodingStyle || _settings->ifcfg)
return;
createSymbolDatabase();

View File

@ -711,6 +711,8 @@ unsigned int CppCheck::check()
preprocessor.preprocess(fin, filedata, configurations, fname, _settings._includePaths);
}
_settings.ifcfg = bool(configurations.size() > 1);
if (!_settings.userDefines.empty())
{
configurations.clear();

View File

@ -42,6 +42,7 @@ Settings::Settings()
_terminate = false;
inconclusive = false;
test_2_pass = false;
ifcfg = false;
}
std::string Settings::Suppressions::parseFile(std::istream &istr)

View File

@ -170,6 +170,12 @@ public:
/** @brief Experimentat 2 pass checking of files */
bool test_2_pass;
/**
* @brief Is there any #if configurations in the source code?
* As usual, include guards are not counted.
*/
bool ifcfg;
};
/// @}

View File

@ -140,6 +140,7 @@ private:
TEST_CASE(constLPVOID); // a function that returns LPVOID can't be const
TEST_CASE(constFunc); // a function that calls const functions can be const
TEST_CASE(constVirtualFunc);
TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if
}
// Check the operator Equal
@ -2349,7 +2350,7 @@ private:
"[test.cpp:3]: (style) Suspicious pointer subtraction\n", errout.str());
}
void checkConst(const char code[])
void checkConst(const char code[], const Settings *s = 0)
{
// Tokenize..
Tokenizer tokenizer;
@ -2362,6 +2363,9 @@ private:
// Check..
Settings settings;
if (s)
settings = *s;
else
settings._checkCodingStyle = true;
CheckClass checkClass(&tokenizer, &settings, this);
checkClass.checkConst();
@ -3848,6 +3852,24 @@ private:
"[test.cpp:11]: (style) The function 'Y::getY' can be const\n"
"[test.cpp:17]: (style) The function 'Z::getZ' can be const\n", errout.str());
}
void constIfCfg()
{
const char code[] = "class foo {\n"
" void f() { }\n"
"};";
Settings settings;
settings._checkCodingStyle = true;
settings.ifcfg = false;
checkConst(code, &settings);
ASSERT_EQUALS("[test.cpp:2]: (style) The function 'foo::f' can be const\n", errout.str());
settings.ifcfg = true;
checkConst(code, &settings);
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestClass)