59 #include <unordered_set>
65 #include <simplecpp.h>
81 static constexpr
char FILELIST[] =
"cppcheck-addon-ctu-file-list";
92 FilesDeleter() =
default;
94 for (
const std::string& fileName: mFilenames)
95 std::remove(fileName.c_str());
97 void addFile(
const std::string& fileName) {
98 mFilenames.push_back(fileName);
101 std::vector<std::string> mFilenames;
108 if (f.find(
' ') != std::string::npos)
109 return "\"" + f +
"\"";
113 static std::vector<std::string>
split(
const std::string &str,
const std::string &sep=
" ")
115 std::vector<std::string> ret;
116 for (std::string::size_type startPos = 0U; startPos < str.size();) {
117 startPos = str.find_first_not_of(sep, startPos);
118 if (startPos == std::string::npos)
121 if (str[startPos] ==
'\"') {
122 const std::string::size_type endPos = str.find(
'\"', startPos + 1);
123 ret.push_back(str.substr(startPos + 1, endPos - startPos - 1));
124 startPos = (endPos < str.size()) ? (endPos + 1) : endPos;
128 const std::string::size_type endPos = str.find(sep, startPos + 1);
129 ret.push_back(str.substr(startPos, endPos - startPos));
141 std::string extension;
145 extension =
"." + std::to_string(settings.
pid) +
".dump";
149 return filename + extension;
154 return dumpFile.substr(0, dumpFile.size()-4) +
"ctu-info";
158 const std::string& filename,
159 std::ofstream& fdump,
160 std::string& dumpFile)
162 if (!settings.
dump && settings.
addons.empty())
166 fdump.open(dumpFile);
167 if (!fdump.is_open())
174 std::string language;
176 case Standards::Language::C:
177 language =
" language=\"c\"";
179 case Standards::Language::CPP:
180 language =
" language=\"cpp\"";
182 case Standards::Language::None:
186 if (lang == Standards::Language::CPP)
187 language =
" language=\"cpp\"";
188 else if (lang == Standards::Language::C)
189 language =
" language=\"c\"";
194 fdump <<
"<?xml version=\"1.0\"?>\n";
195 fdump <<
"<dumps" << language <<
">\n";
196 fdump <<
" <platform"
210 const char *py_exes[] = {
"python3.exe",
"python.exe" };
212 const char *py_exes[] = {
"python3",
"python" };
214 for (
const char* py_exe : py_exes) {
218 const std::string cmd = std::string(py_exe) +
" --version >NUL 2>&1";
219 if (system(cmd.c_str()) != 0) {
224 if (executeCommand(py_exe,
split(
"--version"),
"2>&1", out) == EXIT_SUCCESS &&
startsWith(out,
"Python ") && std::isdigit(out[7])) {
232 const std::string &defaultPythonExe,
233 const std::string &file,
234 const std::string &premiumArgs,
237 const std::string redirect =
"2>&1";
239 std::string pythonExe;
243 else if (!addonInfo.
python.empty())
245 else if (!defaultPythonExe.empty())
249 static const std::string detectedPythonExe =
detectPython(executeCommand);
250 if (detectedPythonExe.empty())
251 throw InternalError(
nullptr,
"Failed to auto detect python");
252 pythonExe = detectedPythonExe;
258 args += std::string(args.empty() ?
"" :
" ") +
"--cli" + addonInfo.
args;
259 if (!premiumArgs.empty() && !addonInfo.
executable.empty())
260 args +=
" " + premiumArgs;
262 const bool is_file_list = (file.find(
FILELIST) != std::string::npos);
263 const std::string fileArg = (is_file_list ?
" --file-list " :
" ") +
cmdFileName(file);
267 if (
const int exitcode = executeCommand(pythonExe,
split(args), redirect, result)) {
268 std::string message(
"Failed to execute addon '" + addonInfo.
name +
"' - exitcode is " + std::to_string(exitcode));
269 std::string details = pythonExe +
" " + args;
270 if (result.size() > 2) {
271 details +=
"\nOutput:\n";
273 const auto pos = details.find_last_not_of(
"\n\r");
274 if (pos != std::string::npos)
275 details.resize(pos + 1);
277 throw InternalError(
nullptr, std::move(message), std::move(details));
280 std::vector<picojson::value> addonResult;
283 std::istringstream istr(result);
285 while (std::getline(istr, line)) {
298 if (line[0] !=
'{') {
301 result.erase(result.find_last_not_of(
'\n') + 1, std::string::npos);
302 throw InternalError(
nullptr,
"Failed to execute '" + pythonExe +
" " + args +
"'. " + result);
309 const std::string err = picojson::parse(res, line);
314 if (!res.is<picojson::object>()) {
318 addonResult.emplace_back(std::move(res));
328 for (
const std::string &d:
split(semicolonSeparatedString,
";"))
329 flags +=
"-D" + d +
" ";
334 bool useGlobalSuppressions,
336 : mErrorLogger(errorLogger)
337 , mUseGlobalSuppressions(useGlobalSuppressions)
338 , mExecuteCommand(std::move(executeCommand))
367 while (std::getline(is, line)) {
368 if (line.empty() || line[0] ==
' ' || line[0] ==
'`' || line[0] ==
'-')
371 std::string::size_type pos3 = line.find(
": error: ");
372 if (pos3 == std::string::npos)
373 pos3 = line.find(
": fatal error:");
374 if (pos3 == std::string::npos)
375 pos3 = line.find(
": warning:");
376 if (pos3 == std::string::npos)
380 const std::string::size_type pos2 = line.rfind(
':', pos3 - 1);
381 const std::string::size_type pos1 = line.rfind(
':', pos2 - 1);
383 if (pos1 >= pos2 || pos2 >= pos3)
386 const std::string filename = line.substr(0, pos1);
387 const std::string linenr = line.substr(pos1+1, pos2-pos1-1);
388 const std::string colnr = line.substr(pos2+1, pos3-pos2-1);
389 const std::string msg = line.substr(line.find(
':', pos3+1) + 2);
392 const int line_i = strToInt<int>(linenr);
393 const int column = strToInt<unsigned int>(colnr);
402 if (line.compare(pos3, 10,
": warning:") == 0) {
403 warnings.push_back(std::move(errmsg));
423 const bool isCpp =
Path::identify(path) == Standards::Language::CPP;
424 const std::string langOpt = isCpp ?
"-x c++" :
"-x c";
426 const std::string clangcmd = analyzerInfo +
".clang-cmd";
427 const std::string clangStderr = analyzerInfo +
".clang-stderr";
428 const std::string clangAst = analyzerInfo +
".clang-ast";
437 std::string flags(langOpt +
" ");
443 flags +=
"-I" + i +
" ";
447 const std::string args2 =
"-fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " + flags + path;
448 const std::string redirect2 = analyzerInfo.empty() ? std::string(
"2>&1") : (
"2> " + clangStderr);
450 std::ofstream fout(clangcmd);
451 fout << exe <<
" " << args2 <<
" " << redirect2 << std::endl;
458 if (exitcode != EXIT_SUCCESS) {
460 std::cerr <<
"Failed to execute '" << exe <<
" " << args2 <<
" " << redirect2 <<
"' - (exitcode: " << exitcode <<
" / output: " << output2 <<
")" << std::endl;
464 if (output2.find(
"TranslationUnitDecl") == std::string::npos) {
466 std::cerr <<
"Failed to execute '" << exe <<
" " << args2 <<
" " << redirect2 <<
"' - (no TranslationUnitDecl in output)" << std::endl;
471 std::vector<ErrorMessage> compilerWarnings;
473 std::ifstream fin(clangStderr);
474 auto reportError = [
this](
const ErrorMessage& errorMessage) {
480 std::istringstream istr(output2);
481 auto reportError = [
this](
const ErrorMessage& errorMessage) {
489 std::ofstream fout(clangAst);
490 fout << output2 << std::endl;
496 std::istringstream ast(output2);
509 std::string dumpFile;
511 if (fdump.is_open()) {
513 fdump <<
"<dump cfg=\"\">\n";
515 fdump <<
" <clang-warning file=\"" <<
toxml(errmsg.callStack.front().getfile()) <<
"\" line=\"" << errmsg.callStack.front().line <<
"\" column=\"" << errmsg.callStack.front().column <<
"\" message=\"" <<
toxml(errmsg.shortMessage()) <<
"\"/>\n";
516 fdump <<
" <standards>\n";
519 fdump <<
" </standards>\n";
520 tokenizer.
dump(fdump);
521 fdump <<
"</dump>\n";
522 fdump <<
"</dumps>\n";
535 }
catch (
const std::exception &e) {
536 internalError(path, std::string(
"Processing Clang AST dump failed: ") + e.what());
552 std::istringstream iss(content);
572 if (fs.
standard.find(
"++") != std::string::npos)
593 static simplecpp::TokenList
createTokenList(
const std::string& filename, std::vector<std::string>& files, simplecpp::OutputList* outputList, std::istream* fileStream)
596 return {*fileStream, files, filename, outputList};
598 return {filename, files, outputList};
601 unsigned int CppCheck::checkFile(
const std::string& filename,
const std::string &cfgname, std::istream* fileStream)
628 std::string includePaths;
630 includePaths +=
" -I" + I;
651 std::ifstream in(filename);
659 simplecpp::OutputList outputList;
660 std::vector<std::string> files;
661 simplecpp::TokenList tokens1 =
createTokenList(filename, files, &outputList, fileStream);
664 const auto output_it = std::find_if(outputList.cbegin(), outputList.cend(), [](
const simplecpp::Output &output){
665 return Preprocessor::hasErrors(output);
667 if (output_it != outputList.cend()) {
668 const simplecpp::Output &output = *output_it;
687 if (!preprocessor.
loadFiles(tokens1, files))
691 std::string filename2;
692 if (filename.find(
'/') != std::string::npos)
693 filename2 = filename.substr(filename.rfind(
'/') + 1);
695 filename2 = filename;
696 const std::size_t fileNameHash = std::hash<std::string> {}(filename);
697 filename2 =
mSettings.
plistOutput + filename2.substr(0, filename2.find(
'.')) +
"_" + std::to_string(fileNameHash) +
".plist";
702 std::string dumpProlog;
710 std::ostringstream oss;
712 dumpProlog += oss.str();
714 tokens1.removeComments();
719 std::ostringstream toolinfo;
730 const std::size_t hash = preprocessor.
calculateHash(tokens1, toolinfo.str());
731 std::list<ErrorMessage> errors;
733 while (!errors.empty()) {
741 FilesDeleter filesDeleter;
745 std::string dumpFile;
747 if (fdump.is_open()) {
750 filesDeleter.addFile(dumpFile);
760 std::set<std::string> configurations;
763 configurations = preprocessor.
getConfigs(tokens1);
769 for (
const std::string &config : configurations)
770 (void)preprocessor.
getcode(tokens1, config, files,
true);
777 if (hasRule(
"define")) {
779 for (
const Directive &dir : directives) {
781 code +=
"#line " + std::to_string(dir.linenr) +
" \"" + dir.file +
"\"\n" + dir.str +
'\n';
784 std::istringstream istr2(code);
787 executeRules(
"define", tokenlist);
799 std::set<unsigned long long> hashes;
801 bool hasValidConfig =
false;
802 std::list<std::string> configurationError;
803 for (
const std::string &currCfg : configurations) {
816 for (
const std::string &
cfg:
split(currCfg,
";")) {
817 if (std::find(v1.cbegin(), v1.cend(),
cfg) == v1.cend()) {
831 codeWithoutCfg.insert(0U,
"//");
832 std::string::size_type pos = 0;
833 while ((pos = codeWithoutCfg.find(
"\n#file",pos)) != std::string::npos)
834 codeWithoutCfg.insert(pos+1U,
"//");
836 while ((pos = codeWithoutCfg.find(
"\n#endfile",pos)) != std::string::npos)
837 codeWithoutCfg.insert(pos+1U,
"//");
840 codeWithoutCfg[pos] =
' ';
857 hasValidConfig =
true;
862 if (!tok->getMacroName().empty())
882 executeRules(
"raw", tokenizer.
list);
892 fdump <<
" <standards>" << std::endl;
895 fdump <<
" </standards>" << std::endl;
896 preprocessor.
dump(fdump);
897 tokenizer.
dump(fdump);
898 fdump <<
"</dump>" << std::endl;
907 if (hashes.find(hash) != hashes.end()) {
917 }
catch (
const simplecpp::Output &o) {
919 configurationError.push_back((
mCurrentConfig.empty() ?
"\'\'" :
mCurrentConfig) +
" : [" + o.location.file() +
':' + std::to_string(o.location.line) +
"] " + o.msg);
922 if (!hasValidConfig && currCfg == *configurations.rbegin()) {
934 "preprocessorErrorDirective",
952 msg =
"This file is not analyzed. Cppcheck failed to extract a valid configuration. Use -v for more details.";
953 msg +=
"\nThis file is not analyzed. Cppcheck failed to extract a valid configuration. The tested configurations have these preprocessor errors:";
954 for (
const std::string &s : configurationError)
963 "noValidConfiguration",
969 if (fdump.is_open()) {
970 fdump <<
"</dumps>" << std::endl;
979 }
catch (
const std::runtime_error &e) {
980 internalError(filename, std::string(
"Checking file failed: ") + e.what());
981 }
catch (
const std::bad_alloc &) {
982 internalError(filename,
"Checking file failed: out of memory");
1009 const std::string fullmsg(
"Bailing out from analysis: " + msg);
1032 const char* unusedFunctionOnly = std::getenv(
"UNUSEDFUNCTION_ONLY");
1033 const bool doUnusedFunctionOnly = unusedFunctionOnly && (std::strcmp(unusedFunctionOnly,
"1") == 0);
1035 if (!doUnusedFunctionOnly) {
1044 if (maxTime > 0 && std::time(
nullptr) > maxTime) {
1050 "Checks maximum time exceeded",
1059 check->runChecks(tokenizer,
this);
1087 if (!doUnusedFunctionOnly) {
1107 executeRules(
"normal", tokenizer.
list);
1114 bool CppCheck::hasRule(
const std::string &tokenlist)
const
1116 return std::any_of(
mSettings.rules.cbegin(),
mSettings.rules.cend(), [&](
const Settings::Rule& rule) {
1117 return rule.tokenlist == tokenlist;
1121 static const char * pcreErrorCodeToString(
const int pcreExecRet)
1123 switch (pcreExecRet) {
1124 case PCRE_ERROR_NULL:
1125 return "Either code or subject was passed as NULL, or ovector was NULL "
1126 "and ovecsize was not zero (PCRE_ERROR_NULL)";
1127 case PCRE_ERROR_BADOPTION:
1128 return "An unrecognized bit was set in the options argument (PCRE_ERROR_BADOPTION)";
1129 case PCRE_ERROR_BADMAGIC:
1130 return "PCRE stores a 4-byte \"magic number\" at the start of the compiled code, "
1131 "to catch the case when it is passed a junk pointer and to detect when a "
1132 "pattern that was compiled in an environment of one endianness is run in "
1133 "an environment with the other endianness. This is the error that PCRE "
1134 "gives when the magic number is not present (PCRE_ERROR_BADMAGIC)";
1135 case PCRE_ERROR_UNKNOWN_NODE:
1136 return "While running the pattern match, an unknown item was encountered in the "
1137 "compiled pattern. This error could be caused by a bug in PCRE or by "
1138 "overwriting of the compiled pattern (PCRE_ERROR_UNKNOWN_NODE)";
1139 case PCRE_ERROR_NOMEMORY:
1140 return "If a pattern contains back references, but the ovector that is passed "
1141 "to pcre_exec() is not big enough to remember the referenced substrings, "
1142 "PCRE gets a block of memory at the start of matching to use for this purpose. "
1143 "If the call via pcre_malloc() fails, this error is given. The memory is "
1144 "automatically freed at the end of matching. This error is also given if "
1145 "pcre_stack_malloc() fails in pcre_exec(). "
1146 "This can happen only when PCRE has been compiled with "
1147 "--disable-stack-for-recursion (PCRE_ERROR_NOMEMORY)";
1148 case PCRE_ERROR_NOSUBSTRING:
1149 return "This error is used by the pcre_copy_substring(), pcre_get_substring(), "
1150 "and pcre_get_substring_list() functions (see below). "
1151 "It is never returned by pcre_exec() (PCRE_ERROR_NOSUBSTRING)";
1152 case PCRE_ERROR_MATCHLIMIT:
1153 return "The backtracking limit, as specified by the match_limit field in a pcre_extra "
1154 "structure (or defaulted) was reached. "
1155 "See the description above (PCRE_ERROR_MATCHLIMIT)";
1156 case PCRE_ERROR_CALLOUT:
1157 return "This error is never generated by pcre_exec() itself. "
1158 "It is provided for use by callout functions that want to yield a distinctive "
1159 "error code. See the pcrecallout documentation for details (PCRE_ERROR_CALLOUT)";
1160 case PCRE_ERROR_BADUTF8:
1161 return "A string that contains an invalid UTF-8 byte sequence was passed as a subject, "
1162 "and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector "
1163 "(ovecsize) is at least 2, the byte offset to the start of the the invalid UTF-8 "
1164 "character is placed in the first element, and a reason code is placed in the "
1165 "second element. The reason codes are listed in the following section. For "
1166 "backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a truncated "
1167 "UTF-8 character at the end of the subject (reason codes 1 to 5), "
1168 "PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8";
1169 case PCRE_ERROR_BADUTF8_OFFSET:
1170 return "The UTF-8 byte sequence that was passed as a subject was checked and found to "
1171 "be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of "
1172 "startoffset did not point to the beginning of a UTF-8 character or the end of "
1173 "the subject (PCRE_ERROR_BADUTF8_OFFSET)";
1174 case PCRE_ERROR_PARTIAL:
1175 return "The subject string did not match, but it did match partially. See the "
1176 "pcrepartial documentation for details of partial matching (PCRE_ERROR_PARTIAL)";
1177 case PCRE_ERROR_BADPARTIAL:
1178 return "This code is no longer in use. It was formerly returned when the PCRE_PARTIAL "
1179 "option was used with a compiled pattern containing items that were not supported "
1180 "for partial matching. From release 8.00 onwards, there are no restrictions on "
1181 "partial matching (PCRE_ERROR_BADPARTIAL)";
1182 case PCRE_ERROR_INTERNAL:
1183 return "An unexpected internal error has occurred. This error could be caused by a bug "
1184 "in PCRE or by overwriting of the compiled pattern (PCRE_ERROR_INTERNAL)";
1185 case PCRE_ERROR_BADCOUNT:
1186 return "This error is given if the value of the ovecsize argument is negative "
1187 "(PCRE_ERROR_BADCOUNT)";
1188 case PCRE_ERROR_RECURSIONLIMIT:
1189 return "The internal recursion limit, as specified by the match_limit_recursion "
1190 "field in a pcre_extra structure (or defaulted) was reached. "
1191 "See the description above (PCRE_ERROR_RECURSIONLIMIT)";
1192 case PCRE_ERROR_DFA_UITEM:
1193 return "PCRE_ERROR_DFA_UITEM";
1194 case PCRE_ERROR_DFA_UCOND:
1195 return "PCRE_ERROR_DFA_UCOND";
1196 case PCRE_ERROR_DFA_WSSIZE:
1197 return "PCRE_ERROR_DFA_WSSIZE";
1198 case PCRE_ERROR_DFA_RECURSE:
1199 return "PCRE_ERROR_DFA_RECURSE";
1200 case PCRE_ERROR_NULLWSLIMIT:
1201 return "PCRE_ERROR_NULLWSLIMIT";
1202 case PCRE_ERROR_BADNEWLINE:
1203 return "An invalid combination of PCRE_NEWLINE_xxx options was "
1204 "given (PCRE_ERROR_BADNEWLINE)";
1205 case PCRE_ERROR_BADOFFSET:
1206 return "The value of startoffset was negative or greater than the length "
1207 "of the subject, that is, the value in length (PCRE_ERROR_BADOFFSET)";
1208 case PCRE_ERROR_SHORTUTF8:
1209 return "This error is returned instead of PCRE_ERROR_BADUTF8 when the subject "
1210 "string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set. "
1211 "Information about the failure is returned as for PCRE_ERROR_BADUTF8. "
1212 "It is in fact sufficient to detect this case, but this special error code for "
1213 "PCRE_PARTIAL_HARD precedes the implementation of returned information; "
1214 "it is retained for backwards compatibility (PCRE_ERROR_SHORTUTF8)";
1215 case PCRE_ERROR_RECURSELOOP:
1216 return "This error is returned when pcre_exec() detects a recursion loop "
1217 "within the pattern. Specifically, it means that either the whole pattern "
1218 "or a subpattern has been called recursively for the second time at the same "
1219 "position in the subject string. Some simple patterns that might do this "
1220 "are detected and faulted at compile time, but more complicated cases, "
1221 "in particular mutual recursions between two different subpatterns, "
1222 "cannot be detected until run time (PCRE_ERROR_RECURSELOOP)";
1223 case PCRE_ERROR_JIT_STACKLIMIT:
1224 return "This error is returned when a pattern that was successfully studied "
1225 "using a JIT compile option is being matched, but the memory available "
1226 "for the just-in-time processing stack is not large enough. See the pcrejit "
1227 "documentation for more details (PCRE_ERROR_JIT_STACKLIMIT)";
1228 case PCRE_ERROR_BADMODE:
1229 return "This error is given if a pattern that was compiled by the 8-bit library "
1230 "is passed to a 16-bit or 32-bit library function, or vice versa (PCRE_ERROR_BADMODE)";
1231 case PCRE_ERROR_BADENDIANNESS:
1232 return "This error is given if a pattern that was compiled and saved is reloaded on a "
1233 "host with different endianness. The utility function pcre_pattern_to_host_byte_order() "
1234 "can be used to convert such a pattern so that it runs on the new host (PCRE_ERROR_BADENDIANNESS)";
1235 case PCRE_ERROR_DFA_BADRESTART:
1236 return "PCRE_ERROR_DFA_BADRESTART";
1237 #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
1238 case PCRE_ERROR_BADLENGTH:
1239 return "This error is given if pcre_exec() is called with a negative value for the length argument (PCRE_ERROR_BADLENGTH)";
1240 case PCRE_ERROR_JIT_BADOPTION:
1241 return "This error is returned when a pattern that was successfully studied using a JIT compile "
1242 "option is being matched, but the matching mode (partial or complete match) does not correspond "
1243 "to any JIT compilation mode. When the JIT fast path function is used, this error may be "
1244 "also given for invalid options. See the pcrejit documentation for more details (PCRE_ERROR_JIT_BADOPTION)";
1250 void CppCheck::executeRules(
const std::string &tokenlist,
const TokenList &list)
1253 if (!hasRule(tokenlist))
1263 for (
const Settings::Rule &rule :
mSettings.rules) {
1264 if (rule.tokenlist != tokenlist)
1271 const char *pcreCompileErrorStr =
nullptr;
1273 pcre *
const re = pcre_compile(rule.pattern.c_str(),0,&pcreCompileErrorStr,&erroffset,
nullptr);
1275 if (pcreCompileErrorStr) {
1276 const std::string msg =
"pcre_compile failed: " + std::string(pcreCompileErrorStr);
1277 const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1290 #ifdef PCRE_CONFIG_JIT
1291 const char *pcreStudyErrorStr =
nullptr;
1292 pcre_extra *
const pcreExtra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
1296 if (pcreStudyErrorStr) {
1297 const std::string msg =
"pcre_study failed: " + std::string(pcreStudyErrorStr);
1298 const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1311 const pcre_extra *
const pcreExtra =
nullptr;
1315 int ovector[30]= {0};
1316 while (pos < (
int)str.size()) {
1317 const int pcreExecRet = pcre_exec(re, pcreExtra, str.c_str(), (
int)str.size(), pos, 0, ovector, 30);
1318 if (pcreExecRet < 0) {
1319 const std::string errorMessage = pcreErrorCodeToString(pcreExecRet);
1320 if (!errorMessage.empty()) {
1321 const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1324 std::string(
"pcre_exec failed: ") + errorMessage,
1332 const auto pos1 = (
unsigned int)ovector[0];
1333 const auto pos2 = (
unsigned int)ovector[1];
1342 std::size_t len = 0;
1344 len = len + 1U + tok->str().size();
1346 fileIndex = tok->fileIndex();
1347 line = tok->linenr();
1352 const std::string& file = list.
getFiles()[fileIndex];
1360 !rule.summary.empty() ? rule.summary :
"found '" + str.substr(pos1, pos2 - pos1) +
"'",
1369 #ifdef PCRE_CONFIG_JIT
1372 pcre_free_study(pcreExtra);
1381 if (!dumpFile.empty()) {
1382 std::vector<std::string> f{dumpFile};
1392 FilesDeleter filesDeleter;
1394 std::string fileList;
1396 if (files.size() >= 2 ||
endsWith(files[0],
".ctu-info")) {
1398 filesDeleter.addFile(fileList);
1399 std::ofstream fout(fileList);
1400 for (
const std::string& f: files)
1401 fout << f << std::endl;
1408 if (addonInfo.
name !=
"misra" && !addonInfo.
ctu &&
endsWith(files.back(),
".ctu-info"))
1411 const std::vector<picojson::value> results =
1416 for (
const picojson::value& res : results) {
1419 picojson::object obj = res.get<picojson::object>();
1423 if (obj.count(
"file") > 0) {
1424 std::string fileName = obj[
"file"].get<std::string>();
1425 const int64_t lineNumber = obj[
"linenr"].get<int64_t>();
1426 const int64_t column = obj[
"column"].get<int64_t>();
1427 errmsg.
callStack.emplace_back(std::move(fileName), lineNumber, column);
1428 }
else if (obj.count(
"loc") > 0) {
1429 for (
const picojson::value &locvalue: obj[
"loc"].get<picojson::array>()) {
1430 picojson::object loc = locvalue.get<picojson::object>();
1431 std::string fileName = loc[
"file"].get<std::string>();
1432 const int64_t lineNumber = loc[
"linenr"].get<int64_t>();
1433 const int64_t column = loc[
"column"].get<int64_t>();
1434 std::string info = loc[
"info"].get<std::string>();
1435 errmsg.callStack.emplace_back(std::move(fileName), std::move(info), lineNumber, column);
1439 errmsg.id = obj[
"addon"].get<std::string>() +
"-" + obj[
"errorId"].get<std::string>();
1440 if (misraC2023 &&
startsWith(errmsg.id,
"misra-c2012-"))
1441 errmsg.id =
"misra-c2023-" + errmsg.id.substr(12);
1443 const std::string severity = obj[
"severity"].get<std::string>();
1446 if (!
endsWith(errmsg.id,
"-logChecker"))
1456 errmsg.file0 = file0;
1468 std::vector<std::string> ctuInfoFiles;
1469 for (
const auto &f: files) {
1482 for (
const std::string &f: ctuInfoFiles)
1483 std::remove(f.c_str());
1502 std::list<ErrorMessage::FileLocation> loclist;
1503 if (!file.empty()) {
1504 loclist.emplace_back(file, 0, 0);
1507 std::ostringstream msg;
1510 msg <<
" of " << numberOfConfigurations <<
" configurations. Use --force to check all configurations.\n";
1512 msg <<
" configurations. Use --force to check all configurations. For more details, use --enable=information.\n";
1513 msg <<
"The checking of the file will be interrupted because there are too many "
1514 "#ifdef configurations. Checking of all #ifdef configurations can be forced "
1515 "by --force command line option or from GUI preferences. However that may "
1516 "increase the checking time.";
1518 msg <<
" For more details, use --enable=information.";
1525 "toomanyconfigs",
CWE398,
1538 std::list<ErrorMessage::FileLocation> loclist;
1539 if (!file.empty()) {
1540 loclist.emplace_back(file, 0, 0);
1546 "The configuration '" + configuration +
"' was not checked because its code equals another one.",
1547 "purgedConfiguration",
1566 std::set<std::string> macroNames;
1568 const std::string &file = msg.
callStack.back().getfile(
false);
1569 int lineNumber = msg.
callStack.back().line;
1572 macroNames = it->second;
1605 if (!
mErrorList.emplace(std::move(errmsg)).second)
1650 (*it)->getErrorMessages(&errorlogger, &s);
1658 std::string allIncludes;
1659 for (
const std::string &inc : fileSettings.
includePaths) {
1660 allIncludes = allIncludes +
"-I\"" + inc +
"\" ";
1666 constexpr
char exe[] =
"clang-tidy.exe";
1668 constexpr
char exe[] =
"clang-tidy";
1671 const std::string args =
"-quiet -checks=*,-clang-analyzer-*,-llvm* \"" + fileSettings.
filename() +
"\" -- " + allIncludes + allDefines;
1674 std::cerr <<
"Failed to execute '" << exe <<
"' (exitcode: " << std::to_string(exitcode) <<
")" << std::endl;
1679 std::istringstream istr(output);
1684 std::ofstream fcmd(analyzerInfoFile +
".clang-tidy-cmd");
1688 while (std::getline(istr, line)) {
1689 if (line.find(
"error") == std::string::npos && line.find(
"warning") == std::string::npos)
1692 std::size_t endColumnPos = line.find(
": error:");
1693 if (endColumnPos == std::string::npos) {
1694 endColumnPos = line.find(
": warning:");
1697 const std::size_t endLinePos = line.rfind(
':', endColumnPos-1);
1698 const std::size_t endNamePos = line.rfind(
':', endLinePos - 1);
1699 const std::size_t endMsgTypePos = line.find(
':', endColumnPos + 2);
1700 const std::size_t endErrorPos = line.rfind(
'[', std::string::npos);
1701 if (endLinePos==std::string::npos || endNamePos==std::string::npos || endMsgTypePos==std::string::npos || endErrorPos==std::string::npos)
1704 const std::string lineNumString = line.substr(endNamePos + 1, endLinePos - endNamePos - 1);
1705 const std::string columnNumString = line.substr(endLinePos + 1, endColumnPos - endLinePos - 1);
1706 const std::string messageString = line.substr(endMsgTypePos + 1, endErrorPos - endMsgTypePos - 1);
1707 const std::string errorString = line.substr(endErrorPos, line.length());
1710 const int64_t lineNumber = strToInt<int64_t>(lineNumString);
1711 const int64_t column = strToInt<int64_t>(columnNumString);
1715 errmsg.
callStack.emplace_back(fixedpath, lineNumber, column);
1717 errmsg.
id =
"clang-tidy-" + errorString.substr(1, errorString.length() - 2);
1718 if (errmsg.
id.find(
"performance") != std::string::npos)
1720 else if (errmsg.
id.find(
"portability") != std::string::npos)
1722 else if (errmsg.
id.find(
"cert") != std::string::npos || errmsg.
id.find(
"misc") != std::string::npos || errmsg.
id.find(
"unused") != std::string::npos)
1727 errmsg.
file0 = std::move(fixedpath);
1728 errmsg.
setmsg(messageString);
1735 bool errors =
false;
1761 if (buildDir.empty()) {
1767 std::list<Check::FileInfo*> fileInfoList;
1771 const std::string filesTxt(buildDir +
"/files.txt");
1772 std::ifstream fin(filesTxt);
1773 std::string filesTxtLine;
1774 while (std::getline(fin, filesTxtLine)) {
1775 const std::string::size_type firstColon = filesTxtLine.find(
':');
1776 if (firstColon == std::string::npos)
1778 const std::string::size_type lastColon = filesTxtLine.rfind(
':');
1779 if (firstColon == lastColon)
1781 const std::string xmlfile = buildDir +
'/' + filesTxtLine.substr(0,firstColon);
1784 tinyxml2::XMLDocument doc;
1785 const tinyxml2::XMLError
error = doc.LoadFile(xmlfile.c_str());
1786 if (
error != tinyxml2::XML_SUCCESS)
1789 const tinyxml2::XMLElement *
const rootNode = doc.FirstChildElement();
1790 if (rootNode ==
nullptr)
1793 for (
const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
1794 if (std::strcmp(e->Name(),
"FileInfo") != 0)
1796 const char *checkClassAttr = e->Attribute(
"check");
1797 if (!checkClassAttr)
1799 if (std::strcmp(checkClassAttr,
"ctu") == 0) {
1805 if (checkClassAttr ==
check->name())
1806 fileInfoList.push_back(
check->loadFileInfoFromXml(e));
1817 check->analyseWholeProgram(&ctuFileInfo, fileInfoList,
mSettings, *
this);
1831 for (
const auto& f: files) {
1834 std::remove(ctuInfoFileName.c_str());
1836 for (
const auto& fs: fileSettings) {
1839 std::remove(ctuInfoFileName.c_str());
1868 std::string dumpProlog;
1869 dumpProlog +=
" <rawtokens>\n";
1870 for (
unsigned int i = 0; i < files.size(); ++i) {
1871 dumpProlog +=
" <file index=\"";
1872 dumpProlog += std::to_string(i);
1873 dumpProlog +=
"\" name=\"";
1875 dumpProlog +=
"\"/>\n";
1877 for (
const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
1878 dumpProlog +=
" <tok ";
1880 dumpProlog +=
"fileIndex=\"";
1881 dumpProlog += std::to_string(tok->location.fileIndex);
1882 dumpProlog +=
"\" ";
1884 dumpProlog +=
"linenr=\"";
1885 dumpProlog += std::to_string(tok->location.line);
1886 dumpProlog +=
"\" ";
1888 dumpProlog +=
"column=\"";
1889 dumpProlog += std::to_string(tok->location.col);
1890 dumpProlog +=
"\" ";
1892 dumpProlog +=
"str=\"";
1896 dumpProlog +=
"/>\n";
1898 dumpProlog +=
" </rawtokens>\n";
std::list< NestedCall > nestedCalls
void loadFromXml(const tinyxml2::XMLElement *xmlElement)
std::list< FunctionCall > functionCalls
Check for functions never called.
void parseTokens(const Tokenizer &tokenizer, const Settings &settings)
static void getErrorMessages(ErrorLogger &errorLogger)
static void analyseWholeProgram(const Settings &settings, ErrorLogger &errorLogger, const std::string &buildDir)
std::string analyzerInfo() const
Base class used for whole-program analysis.
Interface class that cppcheck uses to communicate with the checks.
static std::list< Check * > & instances()
List of registered check classes.
This is the base class which will use other classes to do static code analysis for C and C++ code to ...
bool analyseWholeProgram()
Analyse whole program, run this after all TUs has been scanned.
ErrorLogger & mErrorLogger
std::unordered_set< std::string > mErrorList
static void resetTimerResults()
unsigned int checkClang(const std::string &path)
std::unique_ptr< CheckUnusedFunctions > mUnusedFunctionsCheck
static void printTimerResults(SHOWTIME_MODES mode)
void checkNormalTokens(const Tokenizer &tokenizer)
Check normal tokens.
Settings & settings()
Get reference to current settings.
void removeCtuInfoFiles(const std::list< FileWithDetails > &files, const std::list< FileSettings > &fileSettings)
Remove *.ctu-info files.
AnalyzerInformation mAnalyzerInformation
unsigned int checkFile(const std::string &filename, const std::string &cfgname, std::istream *fileStream=nullptr)
Check a file using stream.
std::list< Check::FileInfo * > mFileInfo
File info used for whole program analysis.
std::function< int(std::string, std::vector< std::string >, std::string, std::string &)> ExecuteCmdFn
static void getErrorMessages(ErrorLogger &errorlogger)
Call all "getErrorMessages" in all registered Check classes.
void executeAddonsWholeProgram(const std::list< FileWithDetails > &files)
Execute addons.
std::pair< std::string, int > Location
bool mUseGlobalSuppressions
std::string mCurrentConfig
Current preprocessor configuration.
void internalError(const std::string &filename, const std::string &msg)
There has been an internal error => Report information message.
static const char * version()
Returns current version number as a string.
void reportOut(const std::string &outmsg, Color c=Color::Reset) override
Information about progress is directed here.
std::string getDumpFileContentsRawTokens(const std::vector< std::string > &files, const simplecpp::TokenList &tokens1) const
Get dumpfile <rawtokens> contents, this is only public for testing purposes.
unsigned int check(const std::string &path)
This starts the actual checking.
CppCheck(ErrorLogger &errorLogger, bool useGlobalSuppressions, ExecuteCmdFn executeCommand)
Constructor.
void reportProgress(const std::string &filename, const char stage[], const std::size_t value) override
Report progress to client.
~CppCheck() override
Destructor.
std::map< Location, std::set< std::string > > mLocationMacros
void analyseClangTidy(const FileSettings &fileSettings)
Analyze all files using clang-tidy.
bool isPremiumCodingStandardId(const std::string &id) const
void executeAddons(const std::vector< std::string > &files, const std::string &file0)
Execute addons.
bool mTooManyConfigs
Are there too many configs?
ExecuteCmdFn mExecuteCommand
Callback for executing a shell command (exe, args, output)
void purgedConfigurationMessage(const std::string &file, const std::string &configuration)
void tooManyConfigsError(const std::string &file, const int numberOfConfigurations)
static const char * extraVersion()
Returns extra version info as a string.
void reportErr(const ErrorMessage &msg) override
Errors and warnings are directed here.
This is an interface, which the class responsible of error logging should implement.
static bool isCriticalErrorId(const std::string &id)
static std::string plistData(const ErrorMessage &msg)
static const char * plistFooter()
virtual void reportErr(const ErrorMessage &msg)=0
Information about found errors and warnings is directed here.
static std::string toxml(const std::string &str)
Convert XML-sensitive characters into XML entities.
virtual void reportOut(const std::string &outmsg, Color c=Color::Reset)=0
Information about progress is directed here.
static std::string plistHeader(const std::string &version, const std::vector< std::string > &files)
virtual void reportProgress(const std::string &filename, const char stage[], const std::size_t value)
Report progress to client.
File name and line number.
Wrapper for error messages, provided by reportErr()
std::string toString(bool verbose, const std::string &templateFormat=emptyString, const std::string &templateLocation=emptyString) const
Format the error message into a string.
std::string file0
For GUI rechecking; source file (not header)
std::list< FileLocation > callStack
static ErrorMessage fromInternalError(const InternalError &internalError, const TokenList *tokenList, const std::string &filename, const std::string &msg=emptyString)
void setmsg(const std::string &msg)
set short and verbose messages
bool reportErrors(const std::string &path) const
bool markupFile(const std::string &path) const
static std::string simplifyPath(std::string originalPath)
Simplify path "foo/bar/.." => "foo".
static std::string fromNativeSeparators(std::string path)
Convert path to use internal path separators.
static std::string toNativeSeparators(std::string path)
Convert path to use native separators.
static Standards::Language identify(const std::string &path, bool *header=nullptr)
Identify the language based on the file extension.
static std::string getPathFromFilename(const std::string &filename)
Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
static std::string getRelativePath(const std::string &absolutePath, const std::vector< std::string > &basePaths)
Create a relative path from an absolute one, if absolute path is inside the basePaths.
The cppcheck preprocessor.
void setPlatformInfo(simplecpp::TokenList *tokens) const
void simplifyPragmaAsm(simplecpp::TokenList *tokenList) const
bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector< std::string > &files)
std::set< std::string > getConfigs(const simplecpp::TokenList &tokens) const
static void getErrorMessages(ErrorLogger &errorLogger, const Settings &settings)
std::size_t calculateHash(const simplecpp::TokenList &tokens1, const std::string &toolinfo) const
Calculate HASH.
std::string getcode(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector< std::string > &files, const bool writeLocations)
simplecpp::TokenList preprocess(const simplecpp::TokenList &tokens1, const std::string &cfg, std::vector< std::string > &files, bool throwError=false)
static char macroChar
character that is inserted in expanded macros
void dump(std::ostream &out) const
dump all directives present in source file
void inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions)
std::list< Directive > createDirectives(const simplecpp::TokenList &tokens) const
This is just a container for general settings so that we don't need to pass individual values to func...
std::set< std::string > userUndefs
undefines given by the user
bool quiet
Is –quiet given?
bool preprocessOnly
Using -E for debugging purposes.
bool checkAllConfigurations
check all configurations (false if -D or –max-configs is used
std::string getMisraRuleText(const std::string &id, const std::string &text) const
std::vector< std::string > basePaths
Paths used as base for conversion to relative paths.
int maxCtuDepth
–max-ctu-depth
Suppressions supprs
suppressions
std::string plistOutput
plist output (–plist-output=<dir>)
std::string clangExecutable
Custom Clang executable.
Standards::Language enforcedLang
Name of the language that is enforced.
SimpleEnableGroup< Checks > checks
bool checkConfiguration
Is the 'configuration checking' wanted?
static bool terminated()
termination requested?
bool relativePaths
Use relative paths in output.
std::unordered_set< std::string > addons
addons, either filename of python/json file or json data
bool safety
Safety certified behavior Show checkers report when Cppcheck finishes Make cppcheck checking more str...
std::string buildDir
–cppcheck-build-dir.
std::string userDefines
defines given by the user
std::vector< AddonInfo > addonInfos
the loaded addons infos
std::string premiumArgs
Extra arguments for Cppcheck Premium addon.
int checksMaxTime
The maximum time in seconds for the checks of a single file.
bool useSingleJob() const
bool debugnormal
Is –debug-normal given?
bool force
Force checking the files with "too many" configurations (–force).
bool verbose
Is –verbose given?
std::list< std::string > includePaths
List of include paths, e.g.
SHOWTIME_MODES showtime
show timing information (–showtime=file|summary|top5)
int maxConfigs
Maximum number of configurations to check before bailing.
SimpleEnableGroup< Severity > severity
std::string addonPython
Path to the python interpreter to be used to run addons.
bool debugwarnings
Is –debug-warnings given?
Standards standards
Struct contains standards settings.
bool isEnabled(T flag) const
void dump(std::ostream &out) const
Create an xml dump of suppressions.
void markUnmatchedInlineSuppressionsAsChecked(const Tokenizer &tokenizer)
Marks Inline Suppressions as checked if source line is in the token stream.
const std::list< Suppression > & getSuppressions() const
Returns list of all suppressions.
std::string addSuppressions(std::list< Suppression > suppressions)
Combine list of suppressions into the current suppressions.
bool isSuppressed(const ErrorMessage &errmsg, bool global=true)
Returns true if this message should not be shown to the user.
bool isSuppressedExplicitly(const ErrorMessage &errmsg, bool global=true)
Returns true if this message is "explicitly" suppressed.
static bool reportUnmatchedSuppressions(const std::list< SuppressionList::Suppression > &unmatched, ErrorLogger &errorLogger)
Report unmatched suppressions.
std::list< Suppression > getUnmatchedLocalSuppressions(const std::string &file, const bool unusedFunctionChecking) const
Returns list of unmatched local (per-file) suppressions.
void showResults(SHOWTIME_MODES mode) const
const std::string & getSourceFilePath() const
std::size_t calculateHash() const
Calculates a hash of the token list used to compare multiple token lists with each other as quickly a...
void setLang(Standards::Language lang)
const std::vector< std::string > & getFiles() const
Get filenames (the sourcefile + the files it include).
const Token * front() const
get first token of list
bool createTokens(std::istream &code, const std::string &file0)
Create tokens from code.
int appendFileIfNew(std::string fileName)
append file name if seen the first time; return its index in any case
The token list that the TokenList generates is a linked-list of this class.
The main purpose is to tokenize the source code.
void printDebugOutput(int simplification) const
print –debug output if debug flags match the simplification: 0=unknown/both simplifications 1=1st sim...
const Token * tokens() const
bool simplifyTokens1(const std::string &configuration)
void setDirectives(std::list< Directive > directives)
TokenList list
Token list: stores all tokens.
const SymbolDatabase * getSymbolDatabase() const
void dump(std::ostream &out) const
void setTimerResults(TimerResults *tr)
static const std::string emptyString
static constexpr char FILELIST[]
static std::string cmdFileName(std::string f)
static const CWE CWE398(398U)
static constexpr char ExtraVersion[]
static std::string detectPython(const CppCheck::ExecuteCmdFn &executeCommand)
static std::vector< std::string > split(const std::string &str, const std::string &sep=" ")
static std::string getDumpFileName(const Settings &settings, const std::string &filename)
static std::vector< picojson::value > executeAddon(const AddonInfo &addonInfo, const std::string &defaultPythonExe, const std::string &file, const std::string &premiumArgs, const CppCheck::ExecuteCmdFn &executeCommand)
static constexpr char Version[]
static std::string getDefinesFlags(const std::string &semicolonSeparatedString)
static bool reportClangErrors(std::istream &is, const std::function< void(const ErrorMessage &)> &reportErr, std::vector< ErrorMessage > &warnings)
static TimerResults s_timerResults
static void createDumpFile(const Settings &settings, const std::string &filename, std::ofstream &fdump, std::string &dumpFile)
static simplecpp::TokenList createTokenList(const std::string &filename, std::vector< std::string > &files, simplecpp::OutputList *outputList, std::istream *fileStream)
static std::string getCtuInfoFileName(const std::string &dumpFile)
Severity severityFromString(const std::string &severity)
@ none
No severity (default value).
@ portability
Portability warning.
@ information
Checking information.
@ performance
Performance warning.
@ error
Programming error.
@ internal
Internal message.
CPPCHECKLIB FileInfo * getFileInfo(const Tokenizer &tokenizer)
Parse current TU and extract file info.
void setValues(TokenList &tokenlist, SymbolDatabase &symboldatabase, ErrorLogger &errorLogger, const Settings &settings, TimerResultsIntf *timerResults)
Perform valueflow analysis.
void CPPCHECKLIB parseClangAstDump(Tokenizer &tokenizer, std::istream &f)
static std::string cfg(const std::vector< std::string > &configs, const std::string &userDefines)
A preprocessor directive Each preprocessor directive (#include, #define, #undef, #if,...
std::string cppcheckDefines() const
const std::string & filename() const
std::set< std::string > undefs
Platform::Type platformType
std::list< std::string > includePaths
std::list< std::string > systemIncludePaths
Simple container to be thrown when internal error is detected.
bool setC(const std::string &str)
std::string stdValue
–std value given on command line
bool setCPP(std::string str)
std::string getCPP() const
static SuppressionList::ErrorMessage fromErrorMessage(const ::ErrorMessage &msg, const std::set< std::string > ¯oNames)
SuppressionList nofail
suppress exitcode
SuppressionList nomsg
suppress message (–suppressions)
bool startsWith(const std::string &str, const char start[], std::size_t startlen)
bool endsWith(const std::string &str, char c)
#define CPPCHECK_VERSION_STRING