1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2025 Cppcheck team.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "color.h"
#include "cppcheck.h"
#include "errorlogger.h"
#include "filesettings.h"
#include "fixture.h"
#include "helpers.h"
#include "settings.h"
#include "standards.h"
#include "suppressions.h"
#include "simplecpp.h"
#include <algorithm>
#include <list>
#include <string>
#include <vector>
class TestCppcheck : public TestFixture {
public:
TestCppcheck() : TestFixture("TestCppcheck") {}
private:
class ErrorLogger2 : public ErrorLogger {
public:
std::list<std::string> ids;
std::list<ErrorMessage> errmsgs;
private:
void reportOut(const std::string & /*outmsg*/, Color /*c*/ = Color::Reset) override {}
void reportErr(const ErrorMessage &msg) override {
ids.push_back(msg.id);
errmsgs.push_back(msg);
}
};
void run() override {
TEST_CASE(getErrorMessages);
TEST_CASE(checkWithFile);
TEST_CASE(checkWithFS);
TEST_CASE(suppress_error_library);
TEST_CASE(unique_errors);
TEST_CASE(isPremiumCodingStandardId);
TEST_CASE(getDumpFileContentsRawTokens);
TEST_CASE(getDumpFileContentsLibrary);
TEST_CASE(getClangFlagsIncludeFile);
}
void getErrorMessages() const {
ErrorLogger2 errorLogger;
CppCheck::getErrorMessages(errorLogger);
ASSERT(!errorLogger.ids.empty());
// Check if there are duplicate error ids in errorLogger.id
std::string duplicate;
for (auto it = errorLogger.ids.cbegin();
it != errorLogger.ids.cend();
++it) {
if (std::find(errorLogger.ids.cbegin(), it, *it) != it) {
duplicate = "Duplicate ID: " + *it;
break;
}
}
ASSERT_EQUALS("", duplicate);
// Check for error ids from this class.
bool foundPurgedConfiguration = false;
bool foundTooManyConfigs = false;
bool foundMissingInclude = false; // #11984
bool foundMissingIncludeSystem = false; // #11984
for (const std::string & it : errorLogger.ids) {
if (it == "purgedConfiguration")
foundPurgedConfiguration = true;
else if (it == "toomanyconfigs")
foundTooManyConfigs = true;
else if (it == "missingInclude")
foundMissingInclude = true;
else if (it == "missingIncludeSystem")
foundMissingIncludeSystem = true;
}
ASSERT(foundPurgedConfiguration);
ASSERT(foundTooManyConfigs);
ASSERT(foundMissingInclude);
ASSERT(foundMissingIncludeSystem);
}
void checkWithFile() const
{
ScopedFile file("test.cpp",
"int main()\n"
"{\n"
" int i = *((int*)0);\n"
" return 0;\n"
"}");
const Settings s;
Suppressions supprs;
ErrorLogger2 errorLogger;
CppCheck cppcheck(s, supprs, errorLogger, false, {});
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(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"
"}");
const Settings s;
Suppressions supprs;
ErrorLogger2 errorLogger;
CppCheck cppcheck(s, supprs, errorLogger, false, {});
FileSettings fs{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"
"}");
const char xmldata[] = R"(<def format="2"><markup ext=".cpp" reporterrors="false"/></def>)";
const Settings s = settingsBuilder().libraryxml(xmldata).build();
Suppressions supprs;
ErrorLogger2 errorLogger;
CppCheck cppcheck(s, supprs, errorLogger, false, {});
ASSERT_EQUALS(0, cppcheck.check(FileWithDetails(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\"");
const Settings s;
Suppressions supprs;
ErrorLogger2 errorLogger;
CppCheck cppcheck(s, supprs, errorLogger, false, {});
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(test_file_a.path())));
ASSERT_EQUALS(1, cppcheck.check(FileWithDetails(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& msg) {
return msg.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);
}
void isPremiumCodingStandardId() const {
Suppressions supprs;
ErrorLogger2 errorLogger;
{
Settings s;
s.premiumArgs = "";
CppCheck cppcheck(s, supprs, errorLogger, false, {});
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2012-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("misra-c2023-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c2012-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c2023-0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c++2008-0.0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-misra-c++2023-0.0.0"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-cert-int50-cpp"));
ASSERT_EQUALS(false, cppcheck.isPremiumCodingStandardId("premium-autosar-0-0-0"));
}
{
Settings s;
s.premiumArgs = "--misra-c-2012 --cert-c++-2016 --autosar";
CppCheck cppcheck(s, supprs, errorLogger, false, {});
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("misra-c2012-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("misra-c2023-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c2012-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c2023-0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c++2008-0.0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-misra-c++2023-0.0.0"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-cert-int50-cpp"));
ASSERT_EQUALS(true, cppcheck.isPremiumCodingStandardId("premium-autosar-0-0-0"));
}
}
void getDumpFileContentsRawTokens() const {
Settings s = settingsBuilder().build();
s.relativePaths = true;
s.basePaths.emplace_back("/some/path");
Suppressions supprs;
ErrorLogger2 errorLogger;
CppCheck cppcheck(s, supprs, errorLogger, false, {});
std::vector<std::string> files{"/some/path/test.cpp"};
simplecpp::TokenList tokens1(files);
const std::string expected = " <rawtokens>\n"
" <file index=\"0\" name=\"test.cpp\"/>\n"
" </rawtokens>\n";
ASSERT_EQUALS(expected, cppcheck.getDumpFileContentsRawTokens(files, tokens1));
}
void getDumpFileContentsLibrary() const {
Suppressions supprs;
ErrorLogger2 errorLogger;
{
Settings s;
s.libraries.emplace_back("std.cfg");
CppCheck cppcheck(s, supprs, errorLogger, false, {});
//std::vector<std::string> files{ "/some/path/test.cpp" };
const std::string expected = " <library lib=\"std.cfg\"/>\n";
ASSERT_EQUALS(expected, cppcheck.getLibraryDumpData());
}
{
Settings s;
s.libraries.emplace_back("std.cfg");
s.libraries.emplace_back("posix.cfg");
CppCheck cppcheck(s, supprs, errorLogger, false, {});
const std::string expected = " <library lib=\"std.cfg\"/>\n <library lib=\"posix.cfg\"/>\n";
ASSERT_EQUALS(expected, cppcheck.getLibraryDumpData());
}
}
void getClangFlagsIncludeFile() const {
Settings s;
s.userIncludes.emplace_back("1.h");
Suppressions supprs;
ErrorLogger2 errorLogger;
CppCheck cppcheck(s, supprs, errorLogger, false, {});
ASSERT_EQUALS("-x c --include 1.h ", cppcheck.getClangFlags(Standards::Language::C));
}
// TODO: test suppressions
// TODO: test all with FS
};
REGISTER_TEST(TestCppcheck)
|