Fixed #1881 (false positive: The function '...' can be const (nonconst code is hidden inside #if))
This commit is contained in:
parent
54121a74ff
commit
050011d287
|
@ -1849,7 +1849,7 @@ bool CheckClass::isVirtual(const std::vector<std::string> &derivedFrom, const To
|
||||||
// Can a function be const?
|
// Can a function be const?
|
||||||
void CheckClass::checkConst()
|
void CheckClass::checkConst()
|
||||||
{
|
{
|
||||||
if (!_settings->_checkCodingStyle)
|
if (!_settings->_checkCodingStyle || _settings->ifcfg)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
createSymbolDatabase();
|
createSymbolDatabase();
|
||||||
|
|
|
@ -711,6 +711,8 @@ unsigned int CppCheck::check()
|
||||||
preprocessor.preprocess(fin, filedata, configurations, fname, _settings._includePaths);
|
preprocessor.preprocess(fin, filedata, configurations, fname, _settings._includePaths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_settings.ifcfg = bool(configurations.size() > 1);
|
||||||
|
|
||||||
if (!_settings.userDefines.empty())
|
if (!_settings.userDefines.empty())
|
||||||
{
|
{
|
||||||
configurations.clear();
|
configurations.clear();
|
||||||
|
|
|
@ -42,6 +42,7 @@ Settings::Settings()
|
||||||
_terminate = false;
|
_terminate = false;
|
||||||
inconclusive = false;
|
inconclusive = false;
|
||||||
test_2_pass = false;
|
test_2_pass = false;
|
||||||
|
ifcfg = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Settings::Suppressions::parseFile(std::istream &istr)
|
std::string Settings::Suppressions::parseFile(std::istream &istr)
|
||||||
|
|
|
@ -170,6 +170,12 @@ public:
|
||||||
|
|
||||||
/** @brief Experimentat 2 pass checking of files */
|
/** @brief Experimentat 2 pass checking of files */
|
||||||
bool test_2_pass;
|
bool test_2_pass;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Is there any #if configurations in the source code?
|
||||||
|
* As usual, include guards are not counted.
|
||||||
|
*/
|
||||||
|
bool ifcfg;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
|
|
|
@ -140,6 +140,7 @@ private:
|
||||||
TEST_CASE(constLPVOID); // a function that returns LPVOID can't be const
|
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(constFunc); // a function that calls const functions can be const
|
||||||
TEST_CASE(constVirtualFunc);
|
TEST_CASE(constVirtualFunc);
|
||||||
|
TEST_CASE(constIfCfg); // ticket #1881 - fp when there are #if
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the operator Equal
|
// Check the operator Equal
|
||||||
|
@ -2349,7 +2350,7 @@ private:
|
||||||
"[test.cpp:3]: (style) Suspicious pointer subtraction\n", errout.str());
|
"[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..
|
// Tokenize..
|
||||||
Tokenizer tokenizer;
|
Tokenizer tokenizer;
|
||||||
|
@ -2362,7 +2363,10 @@ private:
|
||||||
|
|
||||||
// Check..
|
// Check..
|
||||||
Settings settings;
|
Settings settings;
|
||||||
settings._checkCodingStyle = true;
|
if (s)
|
||||||
|
settings = *s;
|
||||||
|
else
|
||||||
|
settings._checkCodingStyle = true;
|
||||||
CheckClass checkClass(&tokenizer, &settings, this);
|
CheckClass checkClass(&tokenizer, &settings, this);
|
||||||
checkClass.checkConst();
|
checkClass.checkConst();
|
||||||
}
|
}
|
||||||
|
@ -3848,6 +3852,24 @@ private:
|
||||||
"[test.cpp:11]: (style) The function 'Y::getY' can be const\n"
|
"[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());
|
"[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)
|
REGISTER_TEST(TestClass)
|
||||||
|
|
Loading…
Reference in New Issue