From 050011d287d59d599ab2d34f083889c3f6559bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 7 Aug 2010 13:08:36 +0200 Subject: [PATCH] Fixed #1881 (false positive: The function '...' can be const (nonconst code is hidden inside #if)) --- lib/checkclass.cpp | 2 +- lib/cppcheck.cpp | 2 ++ lib/settings.cpp | 1 + lib/settings.h | 6 ++++++ test/testclass.cpp | 26 ++++++++++++++++++++++++-- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 63b761d2f..fc7f6a448 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1849,7 +1849,7 @@ bool CheckClass::isVirtual(const std::vector &derivedFrom, const To // Can a function be const? void CheckClass::checkConst() { - if (!_settings->_checkCodingStyle) + if (!_settings->_checkCodingStyle || _settings->ifcfg) return; createSymbolDatabase(); diff --git a/lib/cppcheck.cpp b/lib/cppcheck.cpp index 334af55d0..da817c29e 100644 --- a/lib/cppcheck.cpp +++ b/lib/cppcheck.cpp @@ -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(); diff --git a/lib/settings.cpp b/lib/settings.cpp index ecfa133c4..bd3b78d64 100644 --- a/lib/settings.cpp +++ b/lib/settings.cpp @@ -42,6 +42,7 @@ Settings::Settings() _terminate = false; inconclusive = false; test_2_pass = false; + ifcfg = false; } std::string Settings::Suppressions::parseFile(std::istream &istr) diff --git a/lib/settings.h b/lib/settings.h index 4c9e2d133..746e3c530 100644 --- a/lib/settings.h +++ b/lib/settings.h @@ -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; }; /// @} diff --git a/test/testclass.cpp b/test/testclass.cpp index 355c86714..f133cc6cf 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -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,7 +2363,10 @@ private: // Check.. Settings settings; - settings._checkCodingStyle = true; + 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)