2009-02-05 21:06:39 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2009-02-05 21:06:39 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-02-05 21:06:39 +01:00
|
|
|
*/
|
|
|
|
|
2021-07-08 21:21:35 +02:00
|
|
|
#include "color.h"
|
2009-10-25 12:49:06 +01:00
|
|
|
#include "cppcheck.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "errorlogger.h"
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
#include "filesettings.h"
|
2023-01-27 08:18:32 +01:00
|
|
|
#include "fixture.h"
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
#include "helpers.h"
|
2009-02-05 21:06:39 +01:00
|
|
|
|
2010-05-17 19:18:05 +02:00
|
|
|
#include <algorithm>
|
2011-12-23 22:31:48 +01:00
|
|
|
#include <list>
|
2009-02-05 21:06:39 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class TestCppcheck : public TestFixture {
|
2009-02-05 21:06:39 +01:00
|
|
|
public:
|
2021-08-07 20:51:18 +02:00
|
|
|
TestCppcheck() : TestFixture("TestCppcheck") {}
|
2009-02-05 21:06:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class ErrorLogger2 : public ErrorLogger {
|
2011-01-16 17:06:07 +01:00
|
|
|
public:
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
std::list<std::string> ids;
|
|
|
|
std::list<ErrorMessage> errmsgs;
|
2010-05-13 22:14:29 +02:00
|
|
|
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
private:
|
2022-09-27 20:03:25 +02:00
|
|
|
void reportOut(const std::string & /*outmsg*/, Color /*c*/ = Color::Reset) override {}
|
2010-05-13 22:14:29 +02:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void reportErr(const ErrorMessage &msg) override {
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
ids.push_back(msg.id);
|
|
|
|
errmsgs.push_back(msg);
|
2011-01-16 17:06:07 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void run() override {
|
2011-01-16 17:06:07 +01:00
|
|
|
TEST_CASE(getErrorMessages);
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
TEST_CASE(checkWithFile);
|
|
|
|
TEST_CASE(checkWithFS);
|
|
|
|
TEST_CASE(suppress_error_library);
|
|
|
|
TEST_CASE(unique_errors);
|
2010-05-13 22:14:29 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void getErrorMessages() const {
|
2011-01-16 17:06:07 +01:00
|
|
|
ErrorLogger2 errorLogger;
|
2023-04-16 13:54:21 +02:00
|
|
|
CppCheck::getErrorMessages(errorLogger);
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
ASSERT(!errorLogger.ids.empty());
|
2010-05-16 07:38:29 +02:00
|
|
|
|
2015-11-06 21:58:49 +01:00
|
|
|
// Check if there are duplicate error ids in errorLogger.id
|
2011-01-16 18:45:05 +01:00
|
|
|
std::string duplicate;
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
for (std::list<std::string>::const_iterator it = errorLogger.ids.cbegin();
|
|
|
|
it != errorLogger.ids.cend();
|
2011-10-13 20:53:06 +02:00
|
|
|
++it) {
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
if (std::find(errorLogger.ids.cbegin(), it, *it) != it) {
|
2011-01-16 18:45:05 +01:00
|
|
|
duplicate = "Duplicate ID: " + *it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_EQUALS("", duplicate);
|
2018-02-10 22:30:49 +01:00
|
|
|
|
|
|
|
// Check for error ids from this class.
|
|
|
|
bool foundPurgedConfiguration = false;
|
|
|
|
bool foundTooManyConfigs = false;
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
for (const std::string & it : errorLogger.ids) {
|
2019-09-19 20:29:33 +02:00
|
|
|
if (it == "purgedConfiguration")
|
2018-02-10 22:30:49 +01:00
|
|
|
foundPurgedConfiguration = true;
|
2019-09-19 20:29:33 +02:00
|
|
|
else if (it == "toomanyconfigs")
|
2018-02-10 22:30:49 +01:00
|
|
|
foundTooManyConfigs = true;
|
|
|
|
}
|
|
|
|
ASSERT(foundPurgedConfiguration);
|
|
|
|
ASSERT(foundTooManyConfigs);
|
2011-01-16 17:06:07 +01:00
|
|
|
}
|
aligned and optimized unique error handling (#5280)
The handling in `CppCheck::reportErr()` and `Executor::hasToLog()` was
slightly different. I hope this can somehow be shared after the executor
reworking.
We were also using a very inappropriate container for the error list
which caused a lot of overhead.
`-D__GNUC__ --debug-warnings --template=daca2 --check-library -j2
../test/testsymboldatabase.cpp`
Clang 15
main process `284,218,587` -> `175,691,241`
worker process `9,123,697,183` -> `8,951,903,360`
2023-12-17 21:59:06 +01:00
|
|
|
|
|
|
|
void checkWithFile() const
|
|
|
|
{
|
|
|
|
ScopedFile file("test.cpp",
|
|
|
|
"int main()\n"
|
|
|
|
"{\n"
|
|
|
|
" int i = *((int*)0);\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
ErrorLogger2 errorLogger;
|
|
|
|
CppCheck cppcheck(errorLogger, false, {});
|
|
|
|
ASSERT_EQUALS(1, cppcheck.check(file.path()));
|
|
|
|
// TODO: how to properly disable these warnings?
|
|
|
|
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
|
|
|
|
return id == "logChecker";
|
|
|
|
}), errorLogger.ids.end());
|
|
|
|
ASSERT_EQUALS(1, errorLogger.ids.size());
|
|
|
|
ASSERT_EQUALS("nullPointer", *errorLogger.ids.cbegin());
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkWithFS() const
|
|
|
|
{
|
|
|
|
ScopedFile file("test.cpp",
|
|
|
|
"int main()\n"
|
|
|
|
"{\n"
|
|
|
|
" int i = *((int*)0);\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
ErrorLogger2 errorLogger;
|
|
|
|
CppCheck cppcheck(errorLogger, false, {});
|
|
|
|
FileSettings fs;
|
|
|
|
fs.filename = file.path();
|
|
|
|
ASSERT_EQUALS(1, cppcheck.check(fs));
|
|
|
|
// TODO: how to properly disable these warnings?
|
|
|
|
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
|
|
|
|
return id == "logChecker";
|
|
|
|
}), errorLogger.ids.end());
|
|
|
|
ASSERT_EQUALS(1, errorLogger.ids.size());
|
|
|
|
ASSERT_EQUALS("nullPointer", *errorLogger.ids.cbegin());
|
|
|
|
}
|
|
|
|
|
|
|
|
void suppress_error_library() const
|
|
|
|
{
|
|
|
|
ScopedFile file("test.cpp",
|
|
|
|
"int main()\n"
|
|
|
|
"{\n"
|
|
|
|
" int i = *((int*)0);\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}");
|
|
|
|
|
|
|
|
ErrorLogger2 errorLogger;
|
|
|
|
CppCheck cppcheck(errorLogger, false, {});
|
|
|
|
const char xmldata[] = R"(<def format="2"><markup ext=".cpp" reporterrors="false"/></def>)";
|
|
|
|
const Settings s = settingsBuilder().libraryxml(xmldata, sizeof(xmldata)).build();
|
|
|
|
cppcheck.settings() = s;
|
|
|
|
ASSERT_EQUALS(0, cppcheck.check(file.path()));
|
|
|
|
// TODO: how to properly disable these warnings?
|
|
|
|
errorLogger.ids.erase(std::remove_if(errorLogger.ids.begin(), errorLogger.ids.end(), [](const std::string& id) {
|
|
|
|
return id == "logChecker";
|
|
|
|
}), errorLogger.ids.end());
|
|
|
|
ASSERT_EQUALS(0, errorLogger.ids.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: hwo to actually get duplicated findings
|
|
|
|
void unique_errors() const
|
|
|
|
{
|
|
|
|
ScopedFile file("inc.h",
|
|
|
|
"inline void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" (void)*((int*)0);\n"
|
|
|
|
"}");
|
|
|
|
ScopedFile test_file_a("a.cpp",
|
|
|
|
"#include \"inc.h\"");
|
|
|
|
ScopedFile test_file_b("b.cpp",
|
|
|
|
"#include \"inc.h\"");
|
|
|
|
|
|
|
|
ErrorLogger2 errorLogger;
|
|
|
|
CppCheck cppcheck(errorLogger, false, {});
|
|
|
|
ASSERT_EQUALS(1, cppcheck.check(test_file_a.path()));
|
|
|
|
ASSERT_EQUALS(1, cppcheck.check(test_file_b.path()));
|
|
|
|
// TODO: how to properly disable these warnings?
|
|
|
|
errorLogger.errmsgs.erase(std::remove_if(errorLogger.errmsgs.begin(), errorLogger.errmsgs.end(), [](const ErrorMessage& errmsg) {
|
|
|
|
return errmsg.id == "logChecker";
|
|
|
|
}), errorLogger.errmsgs.end());
|
|
|
|
// the internal errorlist is cleared after each check() call
|
|
|
|
ASSERT_EQUALS(2, errorLogger.errmsgs.size());
|
|
|
|
auto it = errorLogger.errmsgs.cbegin();
|
|
|
|
ASSERT_EQUALS("nullPointer", it->id);
|
|
|
|
++it;
|
|
|
|
ASSERT_EQUALS("nullPointer", it->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: test suppressions
|
|
|
|
// TODO: test all with FS
|
2009-02-05 21:06:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestCppcheck)
|