Cppcheck
cppcheck.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 Cppcheck team.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "cppcheck.h"
20 
21 #include "addoninfo.h"
22 #include "check.h"
23 #include "checkunusedfunctions.h"
24 #include "clangimport.h"
25 #include "color.h"
26 #include "ctu.h"
27 #include "errortypes.h"
28 #include "filesettings.h"
29 #include "library.h"
30 #include "path.h"
31 #include "platform.h"
32 #include "preprocessor.h"
33 #include "standards.h"
34 #include "suppressions.h"
35 #include "timer.h"
36 #include "token.h"
37 #include "tokenize.h"
38 #include "tokenlist.h"
39 #include "utils.h"
40 #include "valueflow.h"
41 #include "version.h"
42 
43 #include <algorithm>
44 #include <cassert>
45 #include <cstdio>
46 #include <cstdint>
47 #include <cstring>
48 #include <cctype>
49 #include <cstdlib>
50 #include <ctime>
51 #include <exception> // IWYU pragma: keep
52 #include <fstream>
53 #include <iostream> // <- TEMPORARY
54 #include <new>
55 #include <set>
56 #include <sstream>
57 #include <stdexcept>
58 #include <string>
59 #include <unordered_set>
60 #include <utility>
61 #include <vector>
62 
63 #ifndef _WIN32
64 #include <unistd.h>
65 #else
66 #include <process.h>
67 #endif
68 
69 #include "json.h"
70 
71 #include <simplecpp.h>
72 
73 #include "xml.h"
74 
75 #ifdef HAVE_RULES
76 #ifdef _WIN32
77 #define PCRE_STATIC
78 #endif
79 #include <pcre.h>
80 #endif
81 
82 class SymbolDatabase;
83 
84 static constexpr char Version[] = CPPCHECK_VERSION_STRING;
85 static constexpr char ExtraVersion[] = "";
86 
87 static constexpr char FILELIST[] = "cppcheck-addon-ctu-file-list";
88 
90 
91 // CWE ids used
92 static const CWE CWE398(398U); // Indicator of Poor Code Quality
93 
94 // File deleter
95 namespace {
96  class FilesDeleter {
97  public:
98  FilesDeleter() = default;
99  ~FilesDeleter() {
100  for (const std::string& fileName: mFilenames)
101  std::remove(fileName.c_str());
102  }
103  void addFile(const std::string& fileName) {
104  mFilenames.push_back(fileName);
105  }
106  private:
107  std::vector<std::string> mFilenames;
108  };
109 }
110 
111 static std::string cmdFileName(std::string f)
112 {
113  f = Path::toNativeSeparators(std::move(f));
114  if (f.find(' ') != std::string::npos)
115  return "\"" + f + "\"";
116  return f;
117 }
118 
119 static std::vector<std::string> split(const std::string &str, const std::string &sep=" ")
120 {
121  std::vector<std::string> ret;
122  for (std::string::size_type startPos = 0U; startPos < str.size();) {
123  startPos = str.find_first_not_of(sep, startPos);
124  if (startPos == std::string::npos)
125  break;
126 
127  if (str[startPos] == '\"') {
128  const std::string::size_type endPos = str.find('\"', startPos + 1);
129  ret.push_back(str.substr(startPos + 1, endPos - startPos - 1));
130  startPos = (endPos < str.size()) ? (endPos + 1) : endPos;
131  continue;
132  }
133 
134  const std::string::size_type endPos = str.find(sep, startPos + 1);
135  ret.push_back(str.substr(startPos, endPos - startPos));
136  startPos = endPos;
137  }
138 
139  return ret;
140 }
141 
142 static int getPid()
143 {
144 #ifndef _WIN32
145  return getpid();
146 #else
147  return _getpid();
148 #endif
149 }
150 
151 static std::string getDumpFileName(const Settings& settings, const std::string& filename)
152 {
153  if (!settings.dumpFile.empty())
154  return settings.dumpFile;
155 
156  std::string extension;
157  if (settings.dump)
158  extension = ".dump";
159  else
160  extension = "." + std::to_string(getPid()) + ".dump";
161 
162  if (!settings.dump && !settings.buildDir.empty())
163  return AnalyzerInformation::getAnalyzerInfoFile(settings.buildDir, filename, emptyString) + extension;
164  return filename + extension;
165 }
166 
167 static std::string getCtuInfoFileName(const std::string &dumpFile)
168 {
169  return dumpFile.substr(0, dumpFile.size()-4) + "ctu-info";
170 }
171 
172 static void createDumpFile(const Settings& settings,
173  const std::string& filename,
174  std::ofstream& fdump,
175  std::string& dumpFile)
176 {
177  if (!settings.dump && settings.addons.empty())
178  return;
179  dumpFile = getDumpFileName(settings, filename);
180 
181  fdump.open(dumpFile);
182  if (!fdump.is_open())
183  return;
184 
185  {
186  std::ofstream fout(getCtuInfoFileName(dumpFile));
187  }
188 
189  std::string language;
190  switch (settings.enforcedLang) {
191  case Standards::Language::C:
192  language = " language=\"c\"";
193  break;
194  case Standards::Language::CPP:
195  language = " language=\"cpp\"";
196  break;
197  case Standards::Language::None:
198  {
199  // TODO: error out on unknown language?
200  const Standards::Language lang = Path::identify(filename);
201  if (lang == Standards::Language::CPP)
202  language = " language=\"cpp\"";
203  else if (lang == Standards::Language::C)
204  language = " language=\"c\"";
205  break;
206  }
207  }
208 
209  fdump << "<?xml version=\"1.0\"?>\n";
210  fdump << "<dumps" << language << ">\n";
211  fdump << " <platform"
212  << " name=\"" << settings.platform.toString() << '\"'
213  << " char_bit=\"" << settings.platform.char_bit << '\"'
214  << " short_bit=\"" << settings.platform.short_bit << '\"'
215  << " int_bit=\"" << settings.platform.int_bit << '\"'
216  << " long_bit=\"" << settings.platform.long_bit << '\"'
217  << " long_long_bit=\"" << settings.platform.long_long_bit << '\"'
218  << " pointer_bit=\"" << (settings.platform.sizeof_pointer * settings.platform.char_bit) << '\"'
219  << "/>" << '\n';
220 }
221 
222 static std::string detectPython(const CppCheck::ExecuteCmdFn &executeCommand)
223 {
224 #ifdef _WIN32
225  const char *py_exes[] = { "python3.exe", "python.exe" };
226 #else
227  const char *py_exes[] = { "python3", "python" };
228 #endif
229  for (const char* py_exe : py_exes) {
230  std::string out;
231 #ifdef _MSC_VER
232  // FIXME: hack to avoid debug assertion with _popen() in executeCommand() for non-existing commands
233  const std::string cmd = std::string(py_exe) + " --version >NUL 2>&1";
234  if (system(cmd.c_str()) != 0) {
235  // TODO: get more detailed error?
236  continue;
237  }
238 #endif
239  if (executeCommand(py_exe, split("--version"), "2>&1", out) == EXIT_SUCCESS && startsWith(out, "Python ") && std::isdigit(out[7])) {
240  return py_exe;
241  }
242  }
243  return "";
244 }
245 
246 static std::vector<picojson::value> executeAddon(const AddonInfo &addonInfo,
247  const std::string &defaultPythonExe,
248  const std::string &file,
249  const std::string &premiumArgs,
250  const CppCheck::ExecuteCmdFn &executeCommand)
251 {
252  const std::string redirect = "2>&1";
253 
254  std::string pythonExe;
255 
256  if (!addonInfo.executable.empty())
257  pythonExe = addonInfo.executable;
258  else if (!addonInfo.python.empty())
259  pythonExe = cmdFileName(addonInfo.python);
260  else if (!defaultPythonExe.empty())
261  pythonExe = cmdFileName(defaultPythonExe);
262  else {
263  // store in static variable so we only look this up once
264  static const std::string detectedPythonExe = detectPython(executeCommand);
265  if (detectedPythonExe.empty())
266  throw InternalError(nullptr, "Failed to auto detect python");
267  pythonExe = detectedPythonExe;
268  }
269 
270  std::string args;
271  if (addonInfo.executable.empty())
272  args = cmdFileName(addonInfo.runScript) + " " + cmdFileName(addonInfo.scriptFile);
273  args += std::string(args.empty() ? "" : " ") + "--cli" + addonInfo.args;
274  if (!premiumArgs.empty() && !addonInfo.executable.empty())
275  args += " " + premiumArgs;
276 
277  const bool is_file_list = (file.find(FILELIST) != std::string::npos);
278  const std::string fileArg = (is_file_list ? " --file-list " : " ") + cmdFileName(file);
279  args += fileArg;
280 
281  std::string result;
282  if (const int exitcode = executeCommand(pythonExe, split(args), redirect, result)) {
283  std::string message("Failed to execute addon '" + addonInfo.name + "' - exitcode is " + std::to_string(exitcode));
284  std::string details = pythonExe + " " + args;
285  if (result.size() > 2) {
286  details += "\nOutput:\n";
287  details += result;
288  const auto pos = details.find_last_not_of("\n\r");
289  if (pos != std::string::npos)
290  details.resize(pos + 1);
291  }
292  throw InternalError(nullptr, std::move(message), std::move(details));
293  }
294 
295  std::vector<picojson::value> addonResult;
296 
297  // Validate output..
298  std::istringstream istr(result);
299  std::string line;
300  while (std::getline(istr, line)) {
301  // TODO: also bail out?
302  if (line.empty()) {
303  //std::cout << "addon '" << addonInfo.name << "' result contains empty line" << std::endl;
304  continue;
305  }
306 
307  // TODO: get rid of this
308  if (startsWith(line,"Checking ")) {
309  //std::cout << "addon '" << addonInfo.name << "' result contains 'Checking ' line" << std::endl;
310  continue;
311  }
312 
313  if (line[0] != '{') {
314  //std::cout << "addon '" << addonInfo.name << "' result is not a JSON" << std::endl;
315 
316  result.erase(result.find_last_not_of('\n') + 1, std::string::npos); // Remove trailing newlines
317  throw InternalError(nullptr, "Failed to execute '" + pythonExe + " " + args + "'. " + result);
318  }
319 
320  //std::cout << "addon '" << addonInfo.name << "' result is " << line << std::endl;
321 
322  // TODO: make these failures?
323  picojson::value res;
324  const std::string err = picojson::parse(res, line);
325  if (!err.empty()) {
326  //std::cout << "addon '" << addonInfo.name << "' result is not a valid JSON (" << err << ")" << std::endl;
327  continue;
328  }
329  if (!res.is<picojson::object>()) {
330  //std::cout << "addon '" << addonInfo.name << "' result is not a JSON object" << std::endl;
331  continue;
332  }
333  addonResult.emplace_back(std::move(res));
334  }
335 
336  // Valid results
337  return addonResult;
338 }
339 
340 static std::string getDefinesFlags(const std::string &semicolonSeparatedString)
341 {
342  std::string flags;
343  for (const std::string &d: split(semicolonSeparatedString, ";"))
344  flags += "-D" + d + " ";
345  return flags;
346 }
347 
349  bool useGlobalSuppressions,
350  ExecuteCmdFn executeCommand)
351  : mErrorLogger(errorLogger)
352  , mUseGlobalSuppressions(useGlobalSuppressions)
353  , mExecuteCommand(std::move(executeCommand))
354 {}
355 
357 {
358  while (!mFileInfo.empty()) {
359  delete mFileInfo.back();
360  mFileInfo.pop_back();
361  }
362 
363  if (mPlistFile.is_open()) {
365  mPlistFile.close();
366  }
367 }
368 
369 const char * CppCheck::version()
370 {
371  return Version;
372 }
373 
375 {
376  return ExtraVersion;
377 }
378 
379 static bool reportClangErrors(std::istream &is, const std::function<void(const ErrorMessage&)>& reportErr, std::vector<ErrorMessage> &warnings)
380 {
381  std::string line;
382  while (std::getline(is, line)) {
383  if (line.empty() || line[0] == ' ' || line[0] == '`' || line[0] == '-')
384  continue;
385 
386  std::string::size_type pos3 = line.find(": error: ");
387  if (pos3 == std::string::npos)
388  pos3 = line.find(": fatal error:");
389  if (pos3 == std::string::npos)
390  pos3 = line.find(": warning:");
391  if (pos3 == std::string::npos)
392  continue;
393 
394  // file:line:column: error: ....
395  const std::string::size_type pos2 = line.rfind(':', pos3 - 1);
396  const std::string::size_type pos1 = line.rfind(':', pos2 - 1);
397 
398  if (pos1 >= pos2 || pos2 >= pos3)
399  continue;
400 
401  const std::string filename = line.substr(0, pos1);
402  const std::string linenr = line.substr(pos1+1, pos2-pos1-1);
403  const std::string colnr = line.substr(pos2+1, pos3-pos2-1);
404  const std::string msg = line.substr(line.find(':', pos3+1) + 2);
405 
406  const std::string locFile = Path::toNativeSeparators(filename);
407  const int line_i = strToInt<int>(linenr);
408  const int column = strToInt<unsigned int>(colnr);
409  ErrorMessage::FileLocation loc(locFile, line_i, column);
410  ErrorMessage errmsg({std::move(loc)},
411  locFile,
413  msg,
414  "syntaxError",
416 
417  if (line.compare(pos3, 10, ": warning:") == 0) {
418  warnings.push_back(std::move(errmsg));
419  continue;
420  }
421 
422  reportErr(errmsg);
423 
424  return true;
425  }
426  return false;
427 }
428 
429 unsigned int CppCheck::checkClang(const std::string &path)
430 {
433 
434  if (!mSettings.quiet)
435  mErrorLogger.reportOut(std::string("Checking ") + path + " ...", Color::FgGreen);
436 
437  // TODO: this ignores the configured language
438  const bool isCpp = Path::identify(path) == Standards::Language::CPP;
439  const std::string langOpt = isCpp ? "-x c++" : "-x c";
440  const std::string analyzerInfo = mSettings.buildDir.empty() ? std::string() : AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, path, emptyString);
441  const std::string clangcmd = analyzerInfo + ".clang-cmd";
442  const std::string clangStderr = analyzerInfo + ".clang-stderr";
443  const std::string clangAst = analyzerInfo + ".clang-ast";
444  std::string exe = mSettings.clangExecutable;
445 #ifdef _WIN32
446  // append .exe if it is not a path
447  if (Path::fromNativeSeparators(mSettings.clangExecutable).find('/') == std::string::npos) {
448  exe += ".exe";
449  }
450 #endif
451 
452  std::string flags(langOpt + " ");
453  // TODO: does not apply C standard
454  if (isCpp && !mSettings.standards.stdValue.empty())
455  flags += "-std=" + mSettings.standards.stdValue + " ";
456 
457  for (const std::string &i: mSettings.includePaths)
458  flags += "-I" + i + " ";
459 
461 
462  const std::string args2 = "-fsyntax-only -Xclang -ast-dump -fno-color-diagnostics " + flags + path;
463  const std::string redirect2 = analyzerInfo.empty() ? std::string("2>&1") : ("2> " + clangStderr);
464  if (!mSettings.buildDir.empty()) {
465  std::ofstream fout(clangcmd);
466  fout << exe << " " << args2 << " " << redirect2 << std::endl;
467  } else if (mSettings.verbose && !mSettings.quiet) {
468  mErrorLogger.reportOut(exe + " " + args2);
469  }
470 
471  std::string output2;
472  const int exitcode = mExecuteCommand(exe,split(args2),redirect2,output2);
473  if (exitcode != EXIT_SUCCESS) {
474  // TODO: report as proper error
475  std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (exitcode: " << exitcode << " / output: " << output2 << ")" << std::endl;
476  return 0; // TODO: report as failure?
477  }
478 
479  if (output2.find("TranslationUnitDecl") == std::string::npos) {
480  // TODO: report as proper error
481  std::cerr << "Failed to execute '" << exe << " " << args2 << " " << redirect2 << "' - (no TranslationUnitDecl in output)" << std::endl;
482  return 0; // TODO: report as failure?
483  }
484 
485  // Ensure there are not syntax errors...
486  std::vector<ErrorMessage> compilerWarnings;
487  if (!mSettings.buildDir.empty()) {
488  std::ifstream fin(clangStderr);
489  auto reportError = [this](const ErrorMessage& errorMessage) {
490  reportErr(errorMessage);
491  };
492  if (reportClangErrors(fin, reportError, compilerWarnings))
493  return 0;
494  } else {
495  std::istringstream istr(output2);
496  auto reportError = [this](const ErrorMessage& errorMessage) {
497  reportErr(errorMessage);
498  };
499  if (reportClangErrors(istr, reportError, compilerWarnings))
500  return 0;
501  }
502 
503  if (!mSettings.buildDir.empty()) {
504  std::ofstream fout(clangAst);
505  fout << output2 << std::endl;
506  }
507 
508  try {
509  Preprocessor preprocessor(mSettings, this);
510  Tokenizer tokenizer(mSettings, this, &preprocessor);
511  tokenizer.list.appendFileIfNew(path);
512  std::istringstream ast(output2);
513  clangimport::parseClangAstDump(tokenizer, ast);
514  ValueFlow::setValues(tokenizer.list,
515  const_cast<SymbolDatabase&>(*tokenizer.getSymbolDatabase()),
516  this,
517  mSettings,
518  &s_timerResults);
520  tokenizer.printDebugOutput(1);
521  checkNormalTokens(tokenizer);
522 
523  // create dumpfile
524  std::ofstream fdump;
525  std::string dumpFile;
526  createDumpFile(mSettings, path, fdump, dumpFile);
527  if (fdump.is_open()) {
528  // TODO: use tinyxml2 to create XML
529  fdump << "<dump cfg=\"\">\n";
530  for (const ErrorMessage& errmsg: compilerWarnings)
531  fdump << " <clang-warning file=\"" << toxml(errmsg.callStack.front().getfile()) << "\" line=\"" << errmsg.callStack.front().line << "\" column=\"" << errmsg.callStack.front().column << "\" message=\"" << toxml(errmsg.shortMessage()) << "\"/>\n";
532  fdump << " <standards>\n";
533  fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>\n";
534  fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>\n";
535  fdump << " </standards>\n";
536  tokenizer.dump(fdump);
537  fdump << "</dump>\n";
538  fdump << "</dumps>\n";
539  fdump.close();
540  }
541 
542  // run addons
543  executeAddons(dumpFile, path);
544 
545  } catch (const InternalError &e) {
546  const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, path, "Bailing out from analysis: Processing Clang AST dump failed");
547  reportErr(errmsg);
548  } catch (const TerminateException &) {
549  // Analysis is terminated
550  return mExitCode;
551  } catch (const std::exception &e) {
552  internalError(path, std::string("Processing Clang AST dump failed: ") + e.what());
553  }
554 
555  return mExitCode;
556 }
557 
558 unsigned int CppCheck::check(const std::string &path)
559 {
560  if (mSettings.clang)
561  return checkClang(Path::simplifyPath(path));
562 
564 }
565 
566 unsigned int CppCheck::check(const std::string &path, const std::string &content)
567 {
568  std::istringstream iss(content);
569  return checkFile(Path::simplifyPath(path), emptyString, &iss);
570 }
571 
572 unsigned int CppCheck::check(const FileSettings &fs)
573 {
574  // TODO: move to constructor when CppCheck no longer owns the settings
577 
579  temp.mSettings = mSettings;
580  if (!temp.mSettings.userDefines.empty())
581  temp.mSettings.userDefines += ';';
582  if (mSettings.clang)
583  temp.mSettings.userDefines += fs.defines;
584  else
587  temp.mSettings.userUndefs.insert(fs.undefs.cbegin(), fs.undefs.cend());
588  if (fs.standard.find("++") != std::string::npos)
590  else if (!fs.standard.empty())
591  temp.mSettings.standards.setC(fs.standard);
592  if (fs.platformType != Platform::Type::Unspecified)
594  if (mSettings.clang) {
595  temp.mSettings.includePaths.insert(temp.mSettings.includePaths.end(), fs.systemIncludePaths.cbegin(), fs.systemIncludePaths.cend());
596  // TODO: propagate back suppressions
597  const unsigned int returnValue = temp.check(Path::simplifyPath(fs.filename));
599  mUnusedFunctionsCheck->updateFunctionData(*temp.mUnusedFunctionsCheck);
600  return returnValue;
601  }
602  const unsigned int returnValue = temp.checkFile(Path::simplifyPath(fs.filename), fs.cfg);
605  mUnusedFunctionsCheck->updateFunctionData(*temp.mUnusedFunctionsCheck);
606  return returnValue;
607 }
608 
609 static simplecpp::TokenList createTokenList(const std::string& filename, std::vector<std::string>& files, simplecpp::OutputList* outputList, std::istream* fileStream)
610 {
611  if (fileStream)
612  return {*fileStream, files, filename, outputList};
613 
614  return {filename, files, outputList};
615 }
616 
617 unsigned int CppCheck::checkFile(const std::string& filename, const std::string &cfgname, std::istream* fileStream)
618 {
619  // TODO: move to constructor when CppCheck no longer owns the settings
622 
623  mExitCode = 0;
624 
625  if (Settings::terminated())
626  return mExitCode;
627 
628  const Timer fileTotalTimer(mSettings.showtime == SHOWTIME_MODES::SHOWTIME_FILE_TOTAL, filename);
629 
630  if (!mSettings.quiet) {
631  std::string fixedpath = Path::simplifyPath(filename);
632  fixedpath = Path::toNativeSeparators(std::move(fixedpath));
633  mErrorLogger.reportOut(std::string("Checking ") + fixedpath + ' ' + cfgname + std::string("..."), Color::FgGreen);
634 
635  if (mSettings.verbose) {
637  std::string undefs;
638  for (const std::string& U : mSettings.userUndefs) {
639  if (!undefs.empty())
640  undefs += ';';
641  undefs += ' ' + U;
642  }
643  mErrorLogger.reportOut("Undefines:" + undefs);
644  std::string includePaths;
645  for (const std::string &I : mSettings.includePaths)
646  includePaths += " -I" + I;
647  mErrorLogger.reportOut("Includes:" + includePaths);
648  mErrorLogger.reportOut(std::string("Platform:") + mSettings.platform.toString());
649  }
650  }
651 
652  if (mPlistFile.is_open()) {
654  mPlistFile.close();
655  }
656 
657  try {
658  if (mSettings.library.markupFile(filename)) {
660  // this is not a real source file - we just want to tokenize it. treat it as C anyways as the language needs to be determined.
661  Tokenizer tokenizer(mSettings, this);
662  tokenizer.list.setLang(Standards::Language::C);
663  if (fileStream) {
664  tokenizer.list.createTokens(*fileStream, filename);
665  }
666  else {
667  std::ifstream in(filename);
668  tokenizer.list.createTokens(in, filename);
669  }
670  mUnusedFunctionsCheck->parseTokens(tokenizer, mSettings);
671  }
672  return EXIT_SUCCESS;
673  }
674 
675  simplecpp::OutputList outputList;
676  std::vector<std::string> files;
677  simplecpp::TokenList tokens1 = createTokenList(filename, files, &outputList, fileStream);
678 
679  // If there is a syntax error, report it and stop
680  const auto output_it = std::find_if(outputList.cbegin(), outputList.cend(), [](const simplecpp::Output &output){
681  return Preprocessor::hasErrors(output);
682  });
683  if (output_it != outputList.cend()) {
684  const simplecpp::Output &output = *output_it;
685  std::string file = Path::fromNativeSeparators(output.location.file());
688 
689  ErrorMessage::FileLocation loc1(file, output.location.line, output.location.col);
690 
691  ErrorMessage errmsg({std::move(loc1)},
692  "",
694  output.msg,
695  "syntaxError",
697  reportErr(errmsg);
698  return mExitCode;
699  }
700 
701  Preprocessor preprocessor(mSettings, this);
702 
703  if (!preprocessor.loadFiles(tokens1, files))
704  return mExitCode;
705 
706  if (!mSettings.plistOutput.empty()) {
707  std::string filename2;
708  if (filename.find('/') != std::string::npos)
709  filename2 = filename.substr(filename.rfind('/') + 1);
710  else
711  filename2 = filename;
712  const std::size_t fileNameHash = std::hash<std::string> {}(filename);
713  filename2 = mSettings.plistOutput + filename2.substr(0, filename2.find('.')) + "_" + std::to_string(fileNameHash) + ".plist";
714  mPlistFile.open(filename2);
716  }
717 
718  std::string dumpProlog;
719  if (mSettings.dump || !mSettings.addons.empty()) {
720  dumpProlog += " <rawtokens>\n";
721  for (unsigned int i = 0; i < files.size(); ++i) {
722  dumpProlog += " <file index=\"";
723  dumpProlog += std::to_string(i);
724  dumpProlog += "\" name=\"";
725  dumpProlog += ErrorLogger::toxml(files[i]);
726  dumpProlog += "\"/>\n";
727  }
728  for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
729  dumpProlog += " <tok ";
730 
731  dumpProlog += "fileIndex=\"";
732  dumpProlog += std::to_string(tok->location.fileIndex);
733  dumpProlog += "\" ";
734 
735  dumpProlog += "linenr=\"";
736  dumpProlog += std::to_string(tok->location.line);
737  dumpProlog += "\" ";
738 
739  dumpProlog +="column=\"";
740  dumpProlog += std::to_string(tok->location.col);
741  dumpProlog += "\" ";
742 
743  dumpProlog += "str=\"";
744  dumpProlog += ErrorLogger::toxml(tok->str());
745  dumpProlog += "\"";
746 
747  dumpProlog += "/>\n";
748  }
749  dumpProlog += " </rawtokens>\n";
750  }
751 
752  // Parse comments and then remove them
753  preprocessor.inlineSuppressions(tokens1, mSettings.supprs.nomsg);
754  if (mSettings.dump || !mSettings.addons.empty()) {
755  std::ostringstream oss;
756  mSettings.supprs.nomsg.dump(oss);
757  dumpProlog += oss.str();
758  }
759  tokens1.removeComments();
760  preprocessor.removeComments();
761 
762  if (!mSettings.buildDir.empty()) {
763  // Get toolinfo
764  std::ostringstream toolinfo;
765  toolinfo << CPPCHECK_VERSION_STRING;
766  toolinfo << (mSettings.severity.isEnabled(Severity::warning) ? 'w' : ' ');
767  toolinfo << (mSettings.severity.isEnabled(Severity::style) ? 's' : ' ');
768  toolinfo << (mSettings.severity.isEnabled(Severity::performance) ? 'p' : ' ');
769  toolinfo << (mSettings.severity.isEnabled(Severity::portability) ? 'p' : ' ');
770  toolinfo << (mSettings.severity.isEnabled(Severity::information) ? 'i' : ' ');
771  toolinfo << mSettings.userDefines;
772  mSettings.supprs.nomsg.dump(toolinfo);
773 
774  // Calculate hash so it can be compared with old hash / future hashes
775  const std::size_t hash = preprocessor.calculateHash(tokens1, toolinfo.str());
776  std::list<ErrorMessage> errors;
777  if (!mAnalyzerInformation.analyzeFile(mSettings.buildDir, filename, cfgname, hash, errors)) {
778  while (!errors.empty()) {
779  reportErr(errors.front());
780  errors.pop_front();
781  }
782  return mExitCode; // known results => no need to reanalyze file
783  }
784  }
785 
786  FilesDeleter filesDeleter;
787 
788  // write dump file xml prolog
789  std::ofstream fdump;
790  std::string dumpFile;
791  createDumpFile(mSettings, filename, fdump, dumpFile);
792  if (fdump.is_open()) {
793  fdump << dumpProlog;
794  if (!mSettings.dump)
795  filesDeleter.addFile(dumpFile);
796  }
797 
798  // Get directives
799  preprocessor.setDirectives(tokens1);
800  preprocessor.simplifyPragmaAsm(&tokens1);
801 
802  preprocessor.setPlatformInfo(&tokens1);
803 
804  // Get configurations..
805  std::set<std::string> configurations;
807  Timer t("Preprocessor::getConfigs", mSettings.showtime, &s_timerResults);
808  configurations = preprocessor.getConfigs(tokens1);
809  } else {
810  configurations.insert(mSettings.userDefines);
811  }
812 
814  for (const std::string &config : configurations)
815  (void)preprocessor.getcode(tokens1, config, files, true);
816 
817  return 0;
818  }
819 
820 #ifdef HAVE_RULES
821  // Run define rules on raw code
822  const auto rules_it = std::find_if(mSettings.rules.cbegin(), mSettings.rules.cend(), [](const Settings::Rule& rule) {
823  return rule.tokenlist == "define";
824  });
825  if (rules_it != mSettings.rules.cend()) {
826  std::string code;
827  const std::list<Directive> &directives = preprocessor.getDirectives();
828  for (const Directive &dir : directives) {
829  if (startsWith(dir.str,"#define ") || startsWith(dir.str,"#include "))
830  code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
831  }
832  Tokenizer tokenizer2(mSettings, this);
833  std::istringstream istr2(code);
834  tokenizer2.list.createTokens(istr2, Path::identify(*files.begin()));
835  executeRules("define", tokenizer2);
836  }
837 #endif
838 
839  if (!mSettings.force && configurations.size() > mSettings.maxConfigs) {
841  tooManyConfigsError(Path::toNativeSeparators(filename),configurations.size());
842  } else {
843  mTooManyConfigs = true;
844  }
845  }
846 
847  std::set<unsigned long long> hashes;
848  int checkCount = 0;
849  bool hasValidConfig = false;
850  std::list<std::string> configurationError;
851  for (const std::string &currCfg : configurations) {
852  // bail out if terminated
853  if (Settings::terminated())
854  break;
855 
856  // Check only a few configurations (default 12), after that bail out, unless --force
857  // was used.
858  if (!mSettings.force && ++checkCount > mSettings.maxConfigs)
859  break;
860 
861  if (!mSettings.userDefines.empty()) {
863  const std::vector<std::string> v1(split(mSettings.userDefines, ";"));
864  for (const std::string &cfg: split(currCfg, ";")) {
865  if (std::find(v1.cbegin(), v1.cend(), cfg) == v1.cend()) {
866  mCurrentConfig += ";" + cfg;
867  }
868  }
869  } else {
870  mCurrentConfig = currCfg;
871  }
872 
874  Timer t("Preprocessor::getcode", mSettings.showtime, &s_timerResults);
875  std::string codeWithoutCfg = preprocessor.getcode(tokens1, mCurrentConfig, files, true);
876  t.stop();
877 
878  if (startsWith(codeWithoutCfg,"#file"))
879  codeWithoutCfg.insert(0U, "//");
880  std::string::size_type pos = 0;
881  while ((pos = codeWithoutCfg.find("\n#file",pos)) != std::string::npos)
882  codeWithoutCfg.insert(pos+1U, "//");
883  pos = 0;
884  while ((pos = codeWithoutCfg.find("\n#endfile",pos)) != std::string::npos)
885  codeWithoutCfg.insert(pos+1U, "//");
886  pos = 0;
887  while ((pos = codeWithoutCfg.find(Preprocessor::macroChar,pos)) != std::string::npos)
888  codeWithoutCfg[pos] = ' ';
889  reportOut(codeWithoutCfg);
890  continue;
891  }
892 
893  Tokenizer tokenizer(mSettings, this, &preprocessor);
895  tokenizer.setTimerResults(&s_timerResults);
896 
897  try {
898  // Create tokens, skip rest of iteration if failed
899  {
900  Timer timer("Tokenizer::createTokens", mSettings.showtime, &s_timerResults);
901  simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, mCurrentConfig, files, true);
902  tokenizer.list.createTokens(std::move(tokensP));
903  }
904  hasValidConfig = true;
905 
906  // locations macros
907  mLocationMacros.clear();
908  for (const Token* tok = tokenizer.tokens(); tok; tok = tok->next()) {
909  if (!tok->getMacroName().empty())
910  mLocationMacros[Location(files[tok->fileIndex()], tok->linenr())].emplace(tok->getMacroName());
911  }
912 
913  // If only errors are printed, print filename after the check
914  if (!mSettings.quiet && (!mCurrentConfig.empty() || checkCount > 1)) {
915  std::string fixedpath = Path::simplifyPath(filename);
916  fixedpath = Path::toNativeSeparators(std::move(fixedpath));
917  mErrorLogger.reportOut("Checking " + fixedpath + ": " + mCurrentConfig + "...", Color::FgGreen);
918  }
919 
920  if (!tokenizer.tokens())
921  continue;
922 
923  // skip rest of iteration if just checking configuration
925  continue;
926 
927  // Check raw tokens
928  checkRawTokens(tokenizer);
929 
930  // Simplify tokens into normal form, skip rest of iteration if failed
931  if (!tokenizer.simplifyTokens1(mCurrentConfig))
932  continue;
933 
934  // dump xml if --dump
935  if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) {
936  fdump << "<dump cfg=\"" << ErrorLogger::toxml(mCurrentConfig) << "\">" << std::endl;
937  fdump << " <standards>" << std::endl;
938  fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl;
939  fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl;
940  fdump << " </standards>" << std::endl;
941  preprocessor.dump(fdump);
942  tokenizer.dump(fdump);
943  fdump << "</dump>" << std::endl;
944  }
945 
946  // Need to call this even if the hash will skip this configuration
948 
949  // Skip if we already met the same simplified token list
950  if (mSettings.force || mSettings.maxConfigs > 1) {
951  const std::size_t hash = tokenizer.list.calculateHash();
952  if (hashes.find(hash) != hashes.end()) {
955  continue;
956  }
957  hashes.insert(hash);
958  }
959 
960  // Check normal tokens
961  checkNormalTokens(tokenizer);
962 
963 #ifdef HAVE_RULES
964  // handling of "simple" rules has been removed.
965  if (hasRule("simple"))
966  throw InternalError(nullptr, "Handling of \"simple\" rules has been removed in Cppcheck. Use --addon instead.");
967 #endif
968 
969  } catch (const simplecpp::Output &o) {
970  // #error etc during preprocessing
971  configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
972  --checkCount; // don't count invalid configurations
973 
974  if (!hasValidConfig && currCfg == *configurations.rbegin()) {
975  // If there is no valid configuration then report error..
976  std::string file = Path::fromNativeSeparators(o.location.file());
979 
980  ErrorMessage::FileLocation loc1(file, o.location.line, o.location.col);
981 
982  ErrorMessage errmsg({std::move(loc1)},
983  filename,
985  o.msg,
986  "preprocessorErrorDirective",
988  reportErr(errmsg);
989  }
990  continue;
991 
992  } catch (const TerminateException &) {
993  // Analysis is terminated
994  return mExitCode;
995 
996  } catch (const InternalError &e) {
997  ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, filename);
998  reportErr(errmsg);
999  }
1000  }
1001 
1002  if (!hasValidConfig && configurations.size() > 1 && mSettings.severity.isEnabled(Severity::information)) {
1003  std::string msg;
1004  msg = "This file is not analyzed. Cppcheck failed to extract a valid configuration. Use -v for more details.";
1005  msg += "\nThis file is not analyzed. Cppcheck failed to extract a valid configuration. The tested configurations have these preprocessor errors:";
1006  for (const std::string &s : configurationError)
1007  msg += '\n' + s;
1008 
1009  const std::string locFile = Path::toNativeSeparators(filename);
1010  ErrorMessage::FileLocation loc(locFile, 0, 0);
1011  ErrorMessage errmsg({std::move(loc)},
1012  locFile,
1014  msg,
1015  "noValidConfiguration",
1017  reportErr(errmsg);
1018  }
1019 
1020  // dumped all configs, close root </dumps> element now
1021  if (fdump.is_open()) {
1022  fdump << "</dumps>" << std::endl;
1023  fdump.close();
1024  }
1025 
1026  executeAddons(dumpFile, Path::simplifyPath(filename));
1027 
1028  } catch (const TerminateException &) {
1029  // Analysis is terminated
1030  return mExitCode;
1031  } catch (const std::runtime_error &e) {
1032  internalError(filename, std::string("Checking file failed: ") + e.what());
1033  } catch (const std::bad_alloc &) {
1034  internalError(filename, "Checking file failed: out of memory");
1035  } catch (const InternalError &e) {
1036  const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, filename, "Bailing out from analysis: Checking file failed");
1037  reportErr(errmsg);
1038  }
1039 
1040  if (!mSettings.buildDir.empty()) {
1042  }
1043 
1044  // In jointSuppressionReport mode, unmatched suppressions are
1045  // collected after all files are processed
1048  }
1049 
1050  mErrorList.clear();
1051 
1054 
1055  return mExitCode;
1056 }
1057 
1058 // TODO: replace with ErrorMessage::fromInternalError()
1059 void CppCheck::internalError(const std::string &filename, const std::string &msg)
1060 {
1061  const std::string fullmsg("Bailing out from analysis: " + msg);
1062 
1063  ErrorMessage::FileLocation loc1(filename, 0, 0);
1064 
1065  ErrorMessage errmsg({std::move(loc1)},
1066  emptyString,
1068  fullmsg,
1069  "internalError",
1071 
1072  mErrorLogger.reportErr(errmsg);
1073 }
1074 
1075 //---------------------------------------------------------------------------
1076 // CppCheck - A function that checks a raw token list
1077 //---------------------------------------------------------------------------
1078 void CppCheck::checkRawTokens(const Tokenizer &tokenizer)
1079 {
1080 #ifdef HAVE_RULES
1081  // Execute rules for "raw" code
1082  executeRules("raw", tokenizer);
1083 #else
1084  (void)tokenizer;
1085 #endif
1086 }
1087 
1088 //---------------------------------------------------------------------------
1089 // CppCheck - A function that checks a normal token list
1090 //---------------------------------------------------------------------------
1091 
1093 {
1094  CheckUnusedFunctions unusedFunctionsChecker;
1095 
1096  // TODO: this should actually be the behavior if only "--enable=unusedFunction" is specified - see #10648
1097  const char* unusedFunctionOnly = std::getenv("UNUSEDFUNCTION_ONLY");
1098  const bool doUnusedFunctionOnly = unusedFunctionOnly && (std::strcmp(unusedFunctionOnly, "1") == 0);
1099 
1100  if (!doUnusedFunctionOnly) {
1101  const std::time_t maxTime = mSettings.checksMaxTime > 0 ? std::time(nullptr) + mSettings.checksMaxTime : 0;
1102 
1103  // call all "runChecks" in all registered Check classes
1104  // cppcheck-suppress shadowFunction - TODO: fix this
1105  for (Check *check : Check::instances()) {
1106  if (Settings::terminated())
1107  return;
1108 
1109  if (maxTime > 0 && std::time(nullptr) > maxTime) {
1110  if (mSettings.debugwarnings) {
1111  ErrorMessage::FileLocation loc(tokenizer.list.getFiles()[0], 0, 0);
1112  ErrorMessage errmsg({std::move(loc)},
1113  emptyString,
1115  "Checks maximum time exceeded",
1116  "checksMaxTime",
1118  reportErr(errmsg);
1119  }
1120  return;
1121  }
1122 
1123  Timer timerRunChecks(check->name() + "::runChecks", mSettings.showtime, &s_timerResults);
1124  check->runChecks(tokenizer, this);
1125  }
1126  }
1127 
1129  unusedFunctionsChecker.parseTokens(tokenizer, mSettings);
1130  }
1132  mUnusedFunctionsCheck->parseTokens(tokenizer, mSettings);
1133  }
1134 
1135  if (mSettings.clang) {
1136  // TODO: Use CTU for Clang analysis
1137  return;
1138  }
1139 
1140  if (mSettings.useSingleJob() || !mSettings.buildDir.empty()) {
1141  // Analyse the tokens..
1142 
1143  if (CTU::FileInfo * const fi1 = CTU::getFileInfo(&tokenizer)) {
1144  if (!mSettings.buildDir.empty())
1145  mAnalyzerInformation.setFileInfo("ctu", fi1->toString());
1146  if (mSettings.useSingleJob())
1147  mFileInfo.push_back(fi1);
1148  else
1149  delete fi1;
1150  }
1151 
1152  if (!doUnusedFunctionOnly) {
1153  // cppcheck-suppress shadowFunction - TODO: fix this
1154  for (const Check *check : Check::instances()) {
1155  if (Check::FileInfo * const fi = check->getFileInfo(&tokenizer, &mSettings)) {
1156  if (!mSettings.buildDir.empty())
1157  mAnalyzerInformation.setFileInfo(check->name(), fi->toString());
1158  if (mSettings.useSingleJob())
1159  mFileInfo.push_back(fi);
1160  else
1161  delete fi;
1162  }
1163  }
1164  }
1165  }
1166 
1168  mAnalyzerInformation.setFileInfo("CheckUnusedFunctions", unusedFunctionsChecker.analyzerInfo());
1169  }
1170 
1171 #ifdef HAVE_RULES
1172  executeRules("normal", tokenizer);
1173 #endif
1174 }
1175 
1176 //---------------------------------------------------------------------------
1177 
1178 #ifdef HAVE_RULES
1179 bool CppCheck::hasRule(const std::string &tokenlist) const
1180 {
1181  return std::any_of(mSettings.rules.cbegin(), mSettings.rules.cend(), [&](const Settings::Rule& rule) {
1182  return rule.tokenlist == tokenlist;
1183  });
1184 }
1185 
1186 static const char * pcreErrorCodeToString(const int pcreExecRet)
1187 {
1188  switch (pcreExecRet) {
1189  case PCRE_ERROR_NULL:
1190  return "Either code or subject was passed as NULL, or ovector was NULL "
1191  "and ovecsize was not zero (PCRE_ERROR_NULL)";
1192  case PCRE_ERROR_BADOPTION:
1193  return "An unrecognized bit was set in the options argument (PCRE_ERROR_BADOPTION)";
1194  case PCRE_ERROR_BADMAGIC:
1195  return "PCRE stores a 4-byte \"magic number\" at the start of the compiled code, "
1196  "to catch the case when it is passed a junk pointer and to detect when a "
1197  "pattern that was compiled in an environment of one endianness is run in "
1198  "an environment with the other endianness. This is the error that PCRE "
1199  "gives when the magic number is not present (PCRE_ERROR_BADMAGIC)";
1200  case PCRE_ERROR_UNKNOWN_NODE:
1201  return "While running the pattern match, an unknown item was encountered in the "
1202  "compiled pattern. This error could be caused by a bug in PCRE or by "
1203  "overwriting of the compiled pattern (PCRE_ERROR_UNKNOWN_NODE)";
1204  case PCRE_ERROR_NOMEMORY:
1205  return "If a pattern contains back references, but the ovector that is passed "
1206  "to pcre_exec() is not big enough to remember the referenced substrings, "
1207  "PCRE gets a block of memory at the start of matching to use for this purpose. "
1208  "If the call via pcre_malloc() fails, this error is given. The memory is "
1209  "automatically freed at the end of matching. This error is also given if "
1210  "pcre_stack_malloc() fails in pcre_exec(). "
1211  "This can happen only when PCRE has been compiled with "
1212  "--disable-stack-for-recursion (PCRE_ERROR_NOMEMORY)";
1213  case PCRE_ERROR_NOSUBSTRING:
1214  return "This error is used by the pcre_copy_substring(), pcre_get_substring(), "
1215  "and pcre_get_substring_list() functions (see below). "
1216  "It is never returned by pcre_exec() (PCRE_ERROR_NOSUBSTRING)";
1217  case PCRE_ERROR_MATCHLIMIT:
1218  return "The backtracking limit, as specified by the match_limit field in a pcre_extra "
1219  "structure (or defaulted) was reached. "
1220  "See the description above (PCRE_ERROR_MATCHLIMIT)";
1221  case PCRE_ERROR_CALLOUT:
1222  return "This error is never generated by pcre_exec() itself. "
1223  "It is provided for use by callout functions that want to yield a distinctive "
1224  "error code. See the pcrecallout documentation for details (PCRE_ERROR_CALLOUT)";
1225  case PCRE_ERROR_BADUTF8:
1226  return "A string that contains an invalid UTF-8 byte sequence was passed as a subject, "
1227  "and the PCRE_NO_UTF8_CHECK option was not set. If the size of the output vector "
1228  "(ovecsize) is at least 2, the byte offset to the start of the the invalid UTF-8 "
1229  "character is placed in the first element, and a reason code is placed in the "
1230  "second element. The reason codes are listed in the following section. For "
1231  "backward compatibility, if PCRE_PARTIAL_HARD is set and the problem is a truncated "
1232  "UTF-8 character at the end of the subject (reason codes 1 to 5), "
1233  "PCRE_ERROR_SHORTUTF8 is returned instead of PCRE_ERROR_BADUTF8";
1234  case PCRE_ERROR_BADUTF8_OFFSET:
1235  return "The UTF-8 byte sequence that was passed as a subject was checked and found to "
1236  "be valid (the PCRE_NO_UTF8_CHECK option was not set), but the value of "
1237  "startoffset did not point to the beginning of a UTF-8 character or the end of "
1238  "the subject (PCRE_ERROR_BADUTF8_OFFSET)";
1239  case PCRE_ERROR_PARTIAL:
1240  return "The subject string did not match, but it did match partially. See the "
1241  "pcrepartial documentation for details of partial matching (PCRE_ERROR_PARTIAL)";
1242  case PCRE_ERROR_BADPARTIAL:
1243  return "This code is no longer in use. It was formerly returned when the PCRE_PARTIAL "
1244  "option was used with a compiled pattern containing items that were not supported "
1245  "for partial matching. From release 8.00 onwards, there are no restrictions on "
1246  "partial matching (PCRE_ERROR_BADPARTIAL)";
1247  case PCRE_ERROR_INTERNAL:
1248  return "An unexpected internal error has occurred. This error could be caused by a bug "
1249  "in PCRE or by overwriting of the compiled pattern (PCRE_ERROR_INTERNAL)";
1250  case PCRE_ERROR_BADCOUNT:
1251  return "This error is given if the value of the ovecsize argument is negative "
1252  "(PCRE_ERROR_BADCOUNT)";
1253  case PCRE_ERROR_RECURSIONLIMIT:
1254  return "The internal recursion limit, as specified by the match_limit_recursion "
1255  "field in a pcre_extra structure (or defaulted) was reached. "
1256  "See the description above (PCRE_ERROR_RECURSIONLIMIT)";
1257  case PCRE_ERROR_DFA_UITEM:
1258  return "PCRE_ERROR_DFA_UITEM";
1259  case PCRE_ERROR_DFA_UCOND:
1260  return "PCRE_ERROR_DFA_UCOND";
1261  case PCRE_ERROR_DFA_WSSIZE:
1262  return "PCRE_ERROR_DFA_WSSIZE";
1263  case PCRE_ERROR_DFA_RECURSE:
1264  return "PCRE_ERROR_DFA_RECURSE";
1265  case PCRE_ERROR_NULLWSLIMIT:
1266  return "PCRE_ERROR_NULLWSLIMIT";
1267  case PCRE_ERROR_BADNEWLINE:
1268  return "An invalid combination of PCRE_NEWLINE_xxx options was "
1269  "given (PCRE_ERROR_BADNEWLINE)";
1270  case PCRE_ERROR_BADOFFSET:
1271  return "The value of startoffset was negative or greater than the length "
1272  "of the subject, that is, the value in length (PCRE_ERROR_BADOFFSET)";
1273  case PCRE_ERROR_SHORTUTF8:
1274  return "This error is returned instead of PCRE_ERROR_BADUTF8 when the subject "
1275  "string ends with a truncated UTF-8 character and the PCRE_PARTIAL_HARD option is set. "
1276  "Information about the failure is returned as for PCRE_ERROR_BADUTF8. "
1277  "It is in fact sufficient to detect this case, but this special error code for "
1278  "PCRE_PARTIAL_HARD precedes the implementation of returned information; "
1279  "it is retained for backwards compatibility (PCRE_ERROR_SHORTUTF8)";
1280  case PCRE_ERROR_RECURSELOOP:
1281  return "This error is returned when pcre_exec() detects a recursion loop "
1282  "within the pattern. Specifically, it means that either the whole pattern "
1283  "or a subpattern has been called recursively for the second time at the same "
1284  "position in the subject string. Some simple patterns that might do this "
1285  "are detected and faulted at compile time, but more complicated cases, "
1286  "in particular mutual recursions between two different subpatterns, "
1287  "cannot be detected until run time (PCRE_ERROR_RECURSELOOP)";
1288  case PCRE_ERROR_JIT_STACKLIMIT:
1289  return "This error is returned when a pattern that was successfully studied "
1290  "using a JIT compile option is being matched, but the memory available "
1291  "for the just-in-time processing stack is not large enough. See the pcrejit "
1292  "documentation for more details (PCRE_ERROR_JIT_STACKLIMIT)";
1293  case PCRE_ERROR_BADMODE:
1294  return "This error is given if a pattern that was compiled by the 8-bit library "
1295  "is passed to a 16-bit or 32-bit library function, or vice versa (PCRE_ERROR_BADMODE)";
1296  case PCRE_ERROR_BADENDIANNESS:
1297  return "This error is given if a pattern that was compiled and saved is reloaded on a "
1298  "host with different endianness. The utility function pcre_pattern_to_host_byte_order() "
1299  "can be used to convert such a pattern so that it runs on the new host (PCRE_ERROR_BADENDIANNESS)";
1300  case PCRE_ERROR_DFA_BADRESTART:
1301  return "PCRE_ERROR_DFA_BADRESTART";
1302 #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
1303  case PCRE_ERROR_BADLENGTH:
1304  return "This error is given if pcre_exec() is called with a negative value for the length argument (PCRE_ERROR_BADLENGTH)";
1305  case PCRE_ERROR_JIT_BADOPTION:
1306  return "This error is returned when a pattern that was successfully studied using a JIT compile "
1307  "option is being matched, but the matching mode (partial or complete match) does not correspond "
1308  "to any JIT compilation mode. When the JIT fast path function is used, this error may be "
1309  "also given for invalid options. See the pcrejit documentation for more details (PCRE_ERROR_JIT_BADOPTION)";
1310 #endif
1311  }
1312  return "";
1313 }
1314 
1315 void CppCheck::executeRules(const std::string &tokenlist, const Tokenizer &tokenizer)
1316 {
1317  // There is no rule to execute
1318  if (!hasRule(tokenlist))
1319  return;
1320 
1321  // Write all tokens in a string that can be parsed by pcre
1322  std::ostringstream ostr;
1323  for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next())
1324  ostr << " " << tok->str();
1325  const std::string str(ostr.str());
1326 
1327  for (const Settings::Rule &rule : mSettings.rules) {
1328  if (rule.pattern.empty() || rule.id.empty() || rule.severity == Severity::none || rule.tokenlist != tokenlist)
1329  continue;
1330 
1331  if (!mSettings.quiet) {
1332  reportOut("Processing rule: " + rule.pattern, Color::FgGreen);
1333  }
1334 
1335  const char *pcreCompileErrorStr = nullptr;
1336  int erroffset = 0;
1337  pcre * const re = pcre_compile(rule.pattern.c_str(),0,&pcreCompileErrorStr,&erroffset,nullptr);
1338  if (!re) {
1339  if (pcreCompileErrorStr) {
1340  const std::string msg = "pcre_compile failed: " + std::string(pcreCompileErrorStr);
1341  const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1342  emptyString,
1344  msg,
1345  "pcre_compile",
1347 
1348  reportErr(errmsg);
1349  }
1350  continue;
1351  }
1352 
1353  // Optimize the regex, but only if PCRE_CONFIG_JIT is available
1354 #ifdef PCRE_CONFIG_JIT
1355  const char *pcreStudyErrorStr = nullptr;
1356  pcre_extra * const pcreExtra = pcre_study(re, PCRE_STUDY_JIT_COMPILE, &pcreStudyErrorStr);
1357  // pcre_study() returns NULL for both errors and when it can not optimize the regex.
1358  // The last argument is how one checks for errors.
1359  // It is NULL if everything works, and points to an error string otherwise.
1360  if (pcreStudyErrorStr) {
1361  const std::string msg = "pcre_study failed: " + std::string(pcreStudyErrorStr);
1362  const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1363  emptyString,
1365  msg,
1366  "pcre_study",
1368 
1369  reportErr(errmsg);
1370  // pcre_compile() worked, but pcre_study() returned an error. Free the resources allocated by pcre_compile().
1371  pcre_free(re);
1372  continue;
1373  }
1374 #else
1375  const pcre_extra * const pcreExtra = nullptr;
1376 #endif
1377 
1378  int pos = 0;
1379  int ovector[30]= {0};
1380  while (pos < (int)str.size()) {
1381  const int pcreExecRet = pcre_exec(re, pcreExtra, str.c_str(), (int)str.size(), pos, 0, ovector, 30);
1382  if (pcreExecRet < 0) {
1383  const std::string errorMessage = pcreErrorCodeToString(pcreExecRet);
1384  if (!errorMessage.empty()) {
1385  const ErrorMessage errmsg(std::list<ErrorMessage::FileLocation>(),
1386  emptyString,
1388  std::string("pcre_exec failed: ") + errorMessage,
1389  "pcre_exec",
1391 
1392  reportErr(errmsg);
1393  }
1394  break;
1395  }
1396  const auto pos1 = (unsigned int)ovector[0];
1397  const auto pos2 = (unsigned int)ovector[1];
1398 
1399  // jump to the end of the match for the next pcre_exec
1400  pos = (int)pos2;
1401 
1402  // determine location..
1403  std::string file = tokenizer.list.getSourceFilePath();
1404  int line = 0;
1405 
1406  std::size_t len = 0;
1407  for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
1408  len = len + 1U + tok->str().size();
1409  if (len > pos1) {
1410  file = tokenizer.list.getFiles().at(tok->fileIndex());
1411  line = tok->linenr();
1412  break;
1413  }
1414  }
1415 
1416  ErrorMessage::FileLocation loc(file, line, 0);
1417 
1418  // Create error message
1419  std::string summary;
1420  if (rule.summary.empty())
1421  summary = "found '" + str.substr(pos1, pos2 - pos1) + "'";
1422  else
1423  summary = rule.summary;
1424  const ErrorMessage errmsg({std::move(loc)}, tokenizer.list.getSourceFilePath(), rule.severity, summary, rule.id, Certainty::normal);
1425 
1426  // Report error
1427  reportErr(errmsg);
1428  }
1429 
1430  pcre_free(re);
1431 #ifdef PCRE_CONFIG_JIT
1432  // Free up the EXTRA PCRE value (may be NULL at this point)
1433  if (pcreExtra) {
1434  pcre_free_study(pcreExtra);
1435  }
1436 #endif
1437  }
1438 }
1439 #endif
1440 
1441 void CppCheck::executeAddons(const std::string& dumpFile, const std::string& file0)
1442 {
1443  if (!dumpFile.empty()) {
1444  std::vector<std::string> f{dumpFile};
1445  executeAddons(f, file0);
1446  }
1447 }
1448 
1449 void CppCheck::executeAddons(const std::vector<std::string>& files, const std::string& file0)
1450 {
1451  if (mSettings.addons.empty() || files.empty())
1452  return;
1453 
1454  FilesDeleter filesDeleter;
1455 
1456  std::string fileList;
1457 
1458  if (files.size() >= 2 || endsWith(files[0], ".ctu-info")) {
1459  fileList = Path::getPathFromFilename(files[0]) + FILELIST + std::to_string(getPid());
1460  filesDeleter.addFile(fileList);
1461  std::ofstream fout(fileList);
1462  for (const std::string& f: files)
1463  fout << f << std::endl;
1464  }
1465 
1466  // ensure all addons have already been resolved - TODO: remove when settings are const after creation
1467  assert(mSettings.addonInfos.size() == mSettings.addons.size());
1468 
1469  for (const AddonInfo &addonInfo : mSettings.addonInfos) {
1470  if (addonInfo.name != "misra" && !addonInfo.ctu && endsWith(files.back(), ".ctu-info"))
1471  continue;
1472 
1473  const std::vector<picojson::value> results =
1474  executeAddon(addonInfo, mSettings.addonPython, fileList.empty() ? files[0] : fileList, mSettings.premiumArgs, mExecuteCommand);
1475 
1476  const bool misraC2023 = mSettings.premiumArgs.find("--misra-c-2023") != std::string::npos;
1477 
1478  for (const picojson::value& res : results) {
1479  // TODO: get rid of copy?
1480  // this is a copy so we can access missing fields and get a default value
1481  picojson::object obj = res.get<picojson::object>();
1482 
1483  ErrorMessage errmsg;
1484 
1485  if (obj.count("file") > 0) {
1486  std::string fileName = obj["file"].get<std::string>();
1487  const int64_t lineNumber = obj["linenr"].get<int64_t>();
1488  const int64_t column = obj["column"].get<int64_t>();
1489  errmsg.callStack.emplace_back(std::move(fileName), lineNumber, column);
1490  } else if (obj.count("loc") > 0) {
1491  for (const picojson::value &locvalue: obj["loc"].get<picojson::array>()) {
1492  picojson::object loc = locvalue.get<picojson::object>();
1493  std::string fileName = loc["file"].get<std::string>();
1494  const int64_t lineNumber = loc["linenr"].get<int64_t>();
1495  const int64_t column = loc["column"].get<int64_t>();
1496  const std::string info = loc["info"].get<std::string>();
1497  errmsg.callStack.emplace_back(std::move(fileName), info, lineNumber, column);
1498  }
1499  }
1500 
1501  errmsg.id = obj["addon"].get<std::string>() + "-" + obj["errorId"].get<std::string>();
1502  if (misraC2023 && startsWith(errmsg.id, "misra-c2012-"))
1503  errmsg.id = "misra-c2023-" + errmsg.id.substr(12);
1504  errmsg.setmsg(mSettings.getMisraRuleText(errmsg.id, obj["message"].get<std::string>()));
1505  const std::string severity = obj["severity"].get<std::string>();
1506  errmsg.severity = severityFromString(severity);
1507  if (errmsg.severity == Severity::none || errmsg.severity == Severity::internal) {
1508  if (!endsWith(errmsg.id, "-logChecker"))
1509  continue;
1510  errmsg.severity = Severity::internal;
1511  }
1512  else if (!mSettings.severity.isEnabled(errmsg.severity)) {
1513  // Do not filter out premium misra/cert/autosar messages that has been
1514  // explicitly enabled with a --premium option
1515  if (!isPremiumCodingStandardId(errmsg.id))
1516  continue;
1517  }
1518  errmsg.file0 = file0;
1519 
1520  reportErr(errmsg);
1521  }
1522  }
1523 }
1524 
1525 void CppCheck::executeAddonsWholeProgram(const std::list<std::pair<std::string, std::size_t>> &files)
1526 {
1527  if (mSettings.addons.empty())
1528  return;
1529 
1530  std::vector<std::string> ctuInfoFiles;
1531  for (const auto &f: files) {
1532  const std::string &dumpFileName = getDumpFileName(mSettings, f.first);
1533  ctuInfoFiles.push_back(getCtuInfoFileName(dumpFileName));
1534  }
1535 
1536  try {
1537  executeAddons(ctuInfoFiles, "");
1538  } catch (const InternalError& e) {
1539  const ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, "", "Bailing out from analysis: Whole program analysis failed");
1540  reportErr(errmsg);
1541  }
1542 
1543  if (mSettings.buildDir.empty()) {
1544  for (const std::string &f: ctuInfoFiles)
1545  std::remove(f.c_str());
1546  }
1547 }
1548 
1550 {
1551  return mSettings;
1552 }
1553 
1554 void CppCheck::tooManyConfigsError(const std::string &file, const int numberOfConfigurations)
1555 {
1557  return;
1558 
1559  mTooManyConfigs = false;
1560 
1561  if (mSettings.severity.isEnabled(Severity::information) && file.empty())
1562  return;
1563 
1564  std::list<ErrorMessage::FileLocation> loclist;
1565  if (!file.empty()) {
1566  loclist.emplace_back(file, 0, 0);
1567  }
1568 
1569  std::ostringstream msg;
1570  msg << "Too many #ifdef configurations - cppcheck only checks " << mSettings.maxConfigs;
1571  if (numberOfConfigurations > mSettings.maxConfigs)
1572  msg << " of " << numberOfConfigurations << " configurations. Use --force to check all configurations.\n";
1573  if (file.empty())
1574  msg << " configurations. Use --force to check all configurations. For more details, use --enable=information.\n";
1575  msg << "The checking of the file will be interrupted because there are too many "
1576  "#ifdef configurations. Checking of all #ifdef configurations can be forced "
1577  "by --force command line option or from GUI preferences. However that may "
1578  "increase the checking time.";
1579  if (file.empty())
1580  msg << " For more details, use --enable=information.";
1581 
1582 
1583  ErrorMessage errmsg(std::move(loclist),
1584  emptyString,
1586  msg.str(),
1587  "toomanyconfigs", CWE398,
1589 
1590  reportErr(errmsg);
1591 }
1592 
1593 void CppCheck::purgedConfigurationMessage(const std::string &file, const std::string& configuration)
1594 {
1595  mTooManyConfigs = false;
1596 
1597  if (mSettings.severity.isEnabled(Severity::information) && file.empty())
1598  return;
1599 
1600  std::list<ErrorMessage::FileLocation> loclist;
1601  if (!file.empty()) {
1602  loclist.emplace_back(file, 0, 0);
1603  }
1604 
1605  ErrorMessage errmsg(std::move(loclist),
1606  emptyString,
1608  "The configuration '" + configuration + "' was not checked because its code equals another one.",
1609  "purgedConfiguration",
1611 
1612  reportErr(errmsg);
1613 }
1614 
1615 //---------------------------------------------------------------------------
1616 
1617 // TODO: part of this logic is duplicated in Executor::hasToLog()
1619 {
1620  if (msg.severity == Severity::internal) {
1621  mErrorLogger.reportErr(msg);
1622  return;
1623  }
1624 
1625  if (!mSettings.library.reportErrors(msg.file0))
1626  return;
1627 
1628  std::set<std::string> macroNames;
1629  if (!msg.callStack.empty()) {
1630  const std::string &file = msg.callStack.back().getfile(false);
1631  int lineNumber = msg.callStack.back().line;
1632  const auto it = mLocationMacros.find(Location(file, lineNumber));
1633  if (it != mLocationMacros.cend())
1634  macroNames = it->second;
1635  }
1636 
1637  // TODO: only convert if necessary
1638  const auto errorMessage = SuppressionList::ErrorMessage::fromErrorMessage(msg, macroNames);
1639 
1641  // Safety: Report critical errors to ErrorLogger
1643  mExitCode = 1;
1644 
1646  // Report with internal severity to signal that there is this critical error but
1647  // it is suppressed
1648  ErrorMessage temp(msg);
1650  mErrorLogger.reportErr(temp);
1651  } else {
1652  // Report critical error that is not explicitly suppressed
1653  mErrorLogger.reportErr(msg);
1654  }
1655  }
1656  return;
1657  }
1658 
1659  // TODO: there should be no need for the verbose and default messages here
1660  std::string errmsg = msg.toString(mSettings.verbose);
1661  if (errmsg.empty())
1662  return;
1663 
1664  // Alert only about unique errors.
1665  // This makes sure the errors of a single check() call are unique.
1666  // TODO: get rid of this? This is forwarded to another ErrorLogger which is also doing this
1667  if (!mErrorList.emplace(std::move(errmsg)).second)
1668  return;
1669 
1670  if (!mSettings.buildDir.empty())
1672 
1673  if (!mSettings.supprs.nofail.isSuppressed(errorMessage) && !mSettings.supprs.nomsg.isSuppressed(errorMessage)) {
1674  mExitCode = 1;
1675  }
1676 
1677  mErrorLogger.reportErr(msg);
1678  // check if plistOutput should be populated and the current output file is open and the error is not suppressed
1679  if (!mSettings.plistOutput.empty() && mPlistFile.is_open() && !mSettings.supprs.nomsg.isSuppressed(errorMessage)) {
1680  // add error to plist output file
1682  }
1683 }
1684 
1685 void CppCheck::reportOut(const std::string &outmsg, Color c)
1686 {
1687  mErrorLogger.reportOut(outmsg, c);
1688 }
1689 
1690 void CppCheck::reportProgress(const std::string &filename, const char stage[], const std::size_t value)
1691 {
1692  mErrorLogger.reportProgress(filename, stage, value);
1693 }
1694 
1696 {
1697  Settings s;
1703 
1704  CppCheck cppcheck(errorlogger, true, nullptr);
1705  cppcheck.purgedConfigurationMessage(emptyString,emptyString);
1706  cppcheck.mTooManyConfigs = true;
1707  cppcheck.tooManyConfigsError(emptyString,0U);
1708  // TODO: add functions to get remaining error messages
1709 
1710  // call all "getErrorMessages" in all registered Check classes
1711  for (std::list<Check *>::const_iterator it = Check::instances().cbegin(); it != Check::instances().cend(); ++it)
1712  (*it)->getErrorMessages(&errorlogger, &s);
1713 
1715  Preprocessor::getErrorMessages(&errorlogger, s);
1716 }
1717 
1718 void CppCheck::analyseClangTidy(const FileSettings &fileSettings)
1719 {
1720  std::string allIncludes;
1721  for (const std::string &inc : fileSettings.includePaths) {
1722  allIncludes = allIncludes + "-I\"" + inc + "\" ";
1723  }
1724 
1725  const std::string allDefines = getDefinesFlags(fileSettings.defines);
1726 
1727 #ifdef _WIN32
1728  constexpr char exe[] = "clang-tidy.exe";
1729 #else
1730  constexpr char exe[] = "clang-tidy";
1731 #endif
1732 
1733  const std::string args = "-quiet -checks=*,-clang-analyzer-*,-llvm* \"" + fileSettings.filename + "\" -- " + allIncludes + allDefines;
1734  std::string output;
1735  if (const int exitcode = mExecuteCommand(exe, split(args), emptyString, output)) {
1736  std::cerr << "Failed to execute '" << exe << "' (exitcode: " << std::to_string(exitcode) << ")" << std::endl;
1737  return;
1738  }
1739 
1740  // parse output and create error messages
1741  std::istringstream istr(output);
1742  std::string line;
1743 
1744  if (!mSettings.buildDir.empty()) {
1745  const std::string analyzerInfoFile = AnalyzerInformation::getAnalyzerInfoFile(mSettings.buildDir, fileSettings.filename, emptyString);
1746  std::ofstream fcmd(analyzerInfoFile + ".clang-tidy-cmd");
1747  fcmd << istr.str();
1748  }
1749 
1750  while (std::getline(istr, line)) {
1751  if (line.find("error") == std::string::npos && line.find("warning") == std::string::npos)
1752  continue;
1753 
1754  std::size_t endColumnPos = line.find(": error:");
1755  if (endColumnPos == std::string::npos) {
1756  endColumnPos = line.find(": warning:");
1757  }
1758 
1759  const std::size_t endLinePos = line.rfind(':', endColumnPos-1);
1760  const std::size_t endNamePos = line.rfind(':', endLinePos - 1);
1761  const std::size_t endMsgTypePos = line.find(':', endColumnPos + 2);
1762  const std::size_t endErrorPos = line.rfind('[', std::string::npos);
1763  if (endLinePos==std::string::npos || endNamePos==std::string::npos || endMsgTypePos==std::string::npos || endErrorPos==std::string::npos)
1764  continue;
1765 
1766  const std::string lineNumString = line.substr(endNamePos + 1, endLinePos - endNamePos - 1);
1767  const std::string columnNumString = line.substr(endLinePos + 1, endColumnPos - endLinePos - 1);
1768  const std::string messageString = line.substr(endMsgTypePos + 1, endErrorPos - endMsgTypePos - 1);
1769  const std::string errorString = line.substr(endErrorPos, line.length());
1770 
1771  std::string fixedpath = Path::simplifyPath(line.substr(0, endNamePos));
1772  const int64_t lineNumber = strToInt<int64_t>(lineNumString);
1773  const int64_t column = strToInt<int64_t>(columnNumString);
1774  fixedpath = Path::toNativeSeparators(std::move(fixedpath));
1775 
1776  ErrorMessage errmsg;
1777  errmsg.callStack.emplace_back(fixedpath, lineNumber, column);
1778 
1779  errmsg.id = "clang-tidy-" + errorString.substr(1, errorString.length() - 2);
1780  if (errmsg.id.find("performance") != std::string::npos)
1782  else if (errmsg.id.find("portability") != std::string::npos)
1784  else if (errmsg.id.find("cert") != std::string::npos || errmsg.id.find("misc") != std::string::npos || errmsg.id.find("unused") != std::string::npos)
1785  errmsg.severity = Severity::warning;
1786  else
1787  errmsg.severity = Severity::style;
1788 
1789  errmsg.file0 = std::move(fixedpath);
1790  errmsg.setmsg(messageString);
1791  reportErr(errmsg);
1792  }
1793 }
1794 
1796 {
1797  bool errors = false;
1798  // Init CTU
1800  // Analyse the tokens
1801  CTU::FileInfo ctu;
1802  for (const Check::FileInfo *fi : mFileInfo) {
1803  const auto *fi2 = dynamic_cast<const CTU::FileInfo *>(fi);
1804  if (fi2) {
1805  ctu.functionCalls.insert(ctu.functionCalls.end(), fi2->functionCalls.cbegin(), fi2->functionCalls.cend());
1806  ctu.nestedCalls.insert(ctu.nestedCalls.end(), fi2->nestedCalls.cbegin(), fi2->nestedCalls.cend());
1807  }
1808  }
1809 
1810  // cppcheck-suppress shadowFunction - TODO: fix this
1811  for (Check *check : Check::instances())
1812  errors |= check->analyseWholeProgram(&ctu, mFileInfo, mSettings, *this); // TODO: ctu
1813 
1815  errors |= mUnusedFunctionsCheck->check(mSettings, *this);
1816 
1817  return errors && (mExitCode > 0);
1818 }
1819 
1820 void CppCheck::analyseWholeProgram(const std::string &buildDir, const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
1821 {
1822  executeAddonsWholeProgram(files); // TODO: pass FileSettings
1823  if (buildDir.empty()) {
1824  removeCtuInfoFiles(files, fileSettings);
1825  return;
1826  }
1829  std::list<Check::FileInfo*> fileInfoList;
1830  CTU::FileInfo ctuFileInfo;
1831 
1832  // Load all analyzer info data..
1833  const std::string filesTxt(buildDir + "/files.txt");
1834  std::ifstream fin(filesTxt);
1835  std::string filesTxtLine;
1836  while (std::getline(fin, filesTxtLine)) {
1837  const std::string::size_type firstColon = filesTxtLine.find(':');
1838  if (firstColon == std::string::npos)
1839  continue;
1840  const std::string::size_type lastColon = filesTxtLine.rfind(':');
1841  if (firstColon == lastColon)
1842  continue;
1843  const std::string xmlfile = buildDir + '/' + filesTxtLine.substr(0,firstColon);
1844  //const std::string sourcefile = filesTxtLine.substr(lastColon+1);
1845 
1846  tinyxml2::XMLDocument doc;
1847  const tinyxml2::XMLError error = doc.LoadFile(xmlfile.c_str());
1848  if (error != tinyxml2::XML_SUCCESS)
1849  continue;
1850 
1851  const tinyxml2::XMLElement * const rootNode = doc.FirstChildElement();
1852  if (rootNode == nullptr)
1853  continue;
1854 
1855  for (const tinyxml2::XMLElement *e = rootNode->FirstChildElement(); e; e = e->NextSiblingElement()) {
1856  if (std::strcmp(e->Name(), "FileInfo") != 0)
1857  continue;
1858  const char *checkClassAttr = e->Attribute("check");
1859  if (!checkClassAttr)
1860  continue;
1861  if (std::strcmp(checkClassAttr, "ctu") == 0) {
1862  ctuFileInfo.loadFromXml(e);
1863  continue;
1864  }
1865  // cppcheck-suppress shadowFunction - TODO: fix this
1866  for (const Check *check : Check::instances()) {
1867  if (checkClassAttr == check->name())
1868  fileInfoList.push_back(check->loadFileInfoFromXml(e));
1869  }
1870  }
1871  }
1872 
1873  // Set CTU max depth
1875 
1876  // Analyse the tokens
1877  // cppcheck-suppress shadowFunction - TODO: fix this
1878  for (Check *check : Check::instances())
1879  check->analyseWholeProgram(&ctuFileInfo, fileInfoList, mSettings, *this);
1880 
1882  mUnusedFunctionsCheck->check(mSettings, *this);
1883 
1884  for (Check::FileInfo *fi : fileInfoList)
1885  delete fi;
1886 }
1887 
1888 void CppCheck::removeCtuInfoFiles(const std::list<std::pair<std::string, std::size_t>> &files, const std::list<FileSettings>& fileSettings)
1889 {
1890  if (mSettings.buildDir.empty()) {
1891  for (const auto& f: files) {
1892  const std::string &dumpFileName = getDumpFileName(mSettings, f.first);
1893  const std::string &ctuInfoFileName = getCtuInfoFileName(dumpFileName);
1894  std::remove(ctuInfoFileName.c_str());
1895  }
1896  for (const auto& fs: fileSettings) {
1897  const std::string &dumpFileName = getDumpFileName(mSettings, fs.filename);
1898  const std::string &ctuInfoFileName = getCtuInfoFileName(dumpFileName);
1899  std::remove(ctuInfoFileName.c_str());
1900  }
1901  }
1902 }
1903 
1904 // cppcheck-suppress unusedFunction - only used in tests
1906 {
1908 }
1909 
1911 {
1913 }
1914 
1915 bool CppCheck::isPremiumCodingStandardId(const std::string& id) const {
1916  if (mSettings.premiumArgs.find("--misra") != std::string::npos) {
1917  if (startsWith(id, "misra-") || startsWith(id, "premium-misra-"))
1918  return true;
1919  }
1920  if (mSettings.premiumArgs.find("--cert") != std::string::npos && startsWith(id, "premium-cert-"))
1921  return true;
1922  if (mSettings.premiumArgs.find("--autosar") != std::string::npos && startsWith(id, "premium-autosar-"))
1923  return true;
1924  return false;
1925 }
static std::string getAnalyzerInfoFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg)
void reportErr(const ErrorMessage &msg)
void setFileInfo(const std::string &check, const std::string &fileInfo)
void close()
Close current TU.analyzerinfo file.
bool analyzeFile(const std::string &buildDir, const std::string &sourcefile, const std::string &cfg, std::size_t hash, std::list< ErrorMessage > &errors)
std::list< NestedCall > nestedCalls
Definition: ctu.h:127
void loadFromXml(const tinyxml2::XMLElement *xmlElement)
Definition: ctu.cpp:232
std::list< FunctionCall > functionCalls
Definition: ctu.h:126
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.
Definition: check.h:103
Interface class that cppcheck uses to communicate with the checks.
Definition: check.h:59
static std::list< Check * > & instances()
List of registered check classes.
Definition: check.cpp:89
This is the base class which will use other classes to do static code analysis for C and C++ code to ...
Definition: cppcheck.h:56
bool analyseWholeProgram()
Analyse whole program, run this after all TUs has been scanned.
Definition: cppcheck.cpp:1795
ErrorLogger & mErrorLogger
Definition: cppcheck.h:228
std::unordered_set< std::string > mErrorList
Definition: cppcheck.h:223
static void resetTimerResults()
Definition: cppcheck.cpp:1905
unsigned int checkClang(const std::string &path)
Definition: cppcheck.cpp:429
void checkRawTokens(const Tokenizer &tokenizer)
Check raw tokens.
Definition: cppcheck.cpp:1078
std::unique_ptr< CheckUnusedFunctions > mUnusedFunctionsCheck
Definition: cppcheck.h:253
static void printTimerResults(SHOWTIME_MODES mode)
Definition: cppcheck.cpp:1910
void checkNormalTokens(const Tokenizer &tokenizer)
Check normal tokens.
Definition: cppcheck.cpp:1092
Settings & settings()
Get reference to current settings.
Definition: cppcheck.cpp:1549
AnalyzerInformation mAnalyzerInformation
Definition: cppcheck.h:246
Settings mSettings
Definition: cppcheck.h:224
void executeAddonsWholeProgram(const std::list< std::pair< std::string, std::size_t >> &files)
Execute addons.
Definition: cppcheck.cpp:1525
unsigned int checkFile(const std::string &filename, const std::string &cfgname, std::istream *fileStream=nullptr)
Check a file using stream.
Definition: cppcheck.cpp:617
std::list< Check::FileInfo * > mFileInfo
File info used for whole program analysis.
Definition: cppcheck.h:244
void removeCtuInfoFiles(const std::list< std::pair< std::string, std::size_t >> &files, const std::list< FileSettings > &fileSettings)
Remove *.ctu-info files.
Definition: cppcheck.cpp:1888
std::function< int(std::string, std::vector< std::string >, std::string, std::string &)> ExecuteCmdFn
Definition: cppcheck.h:58
static void getErrorMessages(ErrorLogger &errorlogger)
Call all "getErrorMessages" in all registered Check classes.
Definition: cppcheck.cpp:1695
std::pair< std::string, int > Location
Definition: cppcheck.h:233
bool mUseGlobalSuppressions
Definition: cppcheck.h:238
std::string mCurrentConfig
Current preprocessor configuration.
Definition: cppcheck.h:231
void internalError(const std::string &filename, const std::string &msg)
There has been an internal error => Report information message.
Definition: cppcheck.cpp:1059
static const char * version()
Returns current version number as a string.
Definition: cppcheck.cpp:369
unsigned int mExitCode
Definition: cppcheck.h:236
void reportOut(const std::string &outmsg, Color c=Color::Reset) override
Information about progress is directed here.
Definition: cppcheck.cpp:1685
unsigned int check(const std::string &path)
This starts the actual checking.
Definition: cppcheck.cpp:558
std::ofstream mPlistFile
Definition: cppcheck.h:251
CppCheck(ErrorLogger &errorLogger, bool useGlobalSuppressions, ExecuteCmdFn executeCommand)
Constructor.
Definition: cppcheck.cpp:348
void reportProgress(const std::string &filename, const char stage[], const std::size_t value) override
Report progress to client.
Definition: cppcheck.cpp:1690
~CppCheck() override
Destructor.
Definition: cppcheck.cpp:356
std::map< Location, std::set< std::string > > mLocationMacros
Definition: cppcheck.h:234
void analyseClangTidy(const FileSettings &fileSettings)
Analyze all files using clang-tidy.
Definition: cppcheck.cpp:1718
bool isPremiumCodingStandardId(const std::string &id) const
Definition: cppcheck.cpp:1915
void executeAddons(const std::vector< std::string > &files, const std::string &file0)
Execute addons.
Definition: cppcheck.cpp:1449
bool mTooManyConfigs
Are there too many configs?
Definition: cppcheck.h:241
ExecuteCmdFn mExecuteCommand
Callback for executing a shell command (exe, args, output)
Definition: cppcheck.h:249
void purgedConfigurationMessage(const std::string &file, const std::string &configuration)
Definition: cppcheck.cpp:1593
void tooManyConfigsError(const std::string &file, const int numberOfConfigurations)
Definition: cppcheck.cpp:1554
static const char * extraVersion()
Returns extra version info as a string.
Definition: cppcheck.cpp:374
void reportErr(const ErrorMessage &msg) override
Errors and warnings are directed here.
Definition: cppcheck.cpp:1618
A preprocessor directive Each preprocessor directive (#include, #define, #undef, #if,...
Definition: preprocessor.h:49
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:217
static bool isCriticalErrorId(const std::string &id)
Definition: errorlogger.h:267
static std::string plistData(const ErrorMessage &msg)
static const char * plistFooter()
Definition: errorlogger.h:261
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.
Definition: errorlogger.h:244
File name and line number.
Definition: errorlogger.h:55
Wrapper for error messages, provided by reportErr()
Definition: errorlogger.h:48
Severity severity
Definition: errorlogger.h:173
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)
Definition: errorlogger.h:171
std::list< FileLocation > callStack
Definition: errorlogger.h:167
std::string id
Definition: errorlogger.h:168
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
Definition: library.cpp:1587
bool markupFile(const std::string &path) const
Definition: library.cpp:1576
static std::string simplifyPath(std::string originalPath)
Simplify path "foo/bar/.." => "foo".
Definition: path.cpp:83
static std::string fromNativeSeparators(std::string path)
Convert path to use internal path separators.
Definition: path.cpp:75
static std::string toNativeSeparators(std::string path)
Convert path to use native separators.
Definition: path.cpp:62
static Standards::Language identify(const std::string &path, bool *header=nullptr)
Identify the language based on the file extension.
Definition: path.cpp:248
static std::string getPathFromFilename(const std::string &filename)
Lookup the path part from a filename (e.g., '/tmp/a.h' -> '/tmp/', 'a.h' -> '')
Definition: path.cpp:88
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.
Definition: path.cpp:181
nonneg int short_bit
bits in char
Definition: platform.h:85
nonneg int int_bit
bits in short
Definition: platform.h:86
nonneg int long_bit
bits in int
Definition: platform.h:87
nonneg int char_bit
Definition: platform.h:84
nonneg int sizeof_pointer
Definition: platform.h:101
const char * toString() const
Definition: platform.h:147
nonneg int long_long_bit
bits in long
Definition: platform.h:88
bool set(Type t)
set the platform type for predefined platforms - deprecated use set(const std::string&,...
Definition: platform.cpp:36
The cppcheck preprocessor.
Definition: preprocessor.h:72
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
void removeComments()
const std::list< Directive > & getDirectives() const
list of all directives met while preprocessing file
Definition: preprocessor.h:99
void setDirectives(const simplecpp::TokenList &tokens)
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)
void preprocess(std::istream &istr, std::map< std::string, std::string > &result, const std::string &filename, const std::list< std::string > &includePaths=std::list< std::string >())
Extract the code for each configuration.
static void getErrorMessages(ErrorLogger *errorLogger, const Settings &settings)
static char macroChar
character that is inserted in expanded macros
Definition: preprocessor.h:89
void dump(std::ostream &out) const
dump all directives present in source file
void inlineSuppressions(const simplecpp::TokenList &tokens, SuppressionList &suppressions)
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:96
std::set< std::string > userUndefs
undefines given by the user
Definition: settings.h:387
bool quiet
Is –quiet given?
Definition: settings.h:280
bool isUnusedFunctionCheckEnabled() const
Check if the user wants to check for unused functions and if it's possible at all.
Definition: settings.h:461
bool preprocessOnly
Using -E for debugging purposes.
Definition: settings.h:277
bool checkAllConfigurations
check all configurations (false if -D or –max-configs is used
Definition: settings.h:125
std::string getMisraRuleText(const std::string &id, const std::string &text) const
Definition: settings.cpp:592
std::vector< std::string > basePaths
Paths used as base for conversion to relative paths.
Definition: settings.h:119
int maxCtuDepth
–max-ctu-depth
Definition: settings.h:248
Suppressions supprs
suppressions
Definition: settings.h:367
std::string plistOutput
plist output (–plist-output=<dir>)
Definition: settings.h:268
std::string clangExecutable
Custom Clang executable.
Definition: settings.h:154
Standards::Language enforcedLang
Name of the language that is enforced.
Definition: settings.h:191
SimpleEnableGroup< Checks > checks
Definition: settings.h:358
bool checkConfiguration
Is the 'configuration checking' wanted?
Definition: settings.h:128
static bool terminated()
termination requested?
Definition: settings.h:447
bool relativePaths
Use relative paths in output.
Definition: settings.h:283
std::unordered_set< std::string > addons
addons, either filename of python/json file or json data
Definition: settings.h:110
Library library
Library.
Definition: settings.h:238
bool clang
Use Clang.
Definition: settings.h:151
bool safety
Safety certified behavior Show checkers report when Cppcheck finishes Make cppcheck checking more str...
Definition: settings.h:311
std::string buildDir
–cppcheck-build-dir.
Definition: settings.h:122
std::string userDefines
defines given by the user
Definition: settings.h:384
std::vector< AddonInfo > addonInfos
the loaded addons infos
Definition: settings.h:113
std::string premiumArgs
Extra arguments for Cppcheck Premium addon.
Definition: settings.h:271
Platform platform
Definition: settings.h:256
bool dump
Is –dump given?
Definition: settings.h:187
int checksMaxTime
The maximum time in seconds for the checks of a single file.
Definition: settings.h:139
bool useSingleJob() const
Definition: settings.h:455
bool debugnormal
Is –debug-normal given?
Definition: settings.h:175
bool force
Force checking the files with "too many" configurations (–force).
Definition: settings.h:221
bool verbose
Is –verbose given?
Definition: settings.h:396
std::list< std::string > includePaths
List of include paths, e.g.
Definition: settings.h:225
std::string dumpFile
Definition: settings.h:188
SHOWTIME_MODES showtime
show timing information (–showtime=file|summary|top5)
Definition: settings.h:361
int maxConfigs
Maximum number of configurations to check before bailing.
Definition: settings.h:245
SimpleEnableGroup< Severity > severity
Definition: settings.h:356
std::string addonPython
Path to the python interpreter to be used to run addons.
Definition: settings.h:116
bool debugwarnings
Is –debug-warnings given?
Definition: settings.h:184
Standards standards
Struct contains standards settings.
Definition: settings.h:364
bool isEnabled(T flag) const
Definition: settings.h:67
void enable(T flag)
Definition: settings.h:70
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
Definition: timer.cpp:41
void reset()
Definition: timer.cpp:99
Definition: timer.h:70
void stop()
Definition: timer.cpp:123
const std::string & getSourceFilePath() const
Definition: tokenlist.cpp:75
std::size_t calculateHash() const
Calculates a hash of the token list used to compare multiple token lists with each other as quickly a...
Definition: tokenlist.cpp:423
void setLang(Standards::Language lang)
const std::vector< std::string > & getFiles() const
Get filenames (the sourcefile + the files it include).
Definition: tokenlist.h:141
bool createTokens(std::istream &code, const std::string &file0)
Create tokens from code.
Definition: tokenlist.cpp:335
int appendFileIfNew(std::string fileName)
append file name if seen the first time; return its index in any case
Definition: tokenlist.cpp:109
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
Token * next()
Definition: token.h:830
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
void printDebugOutput(int simplification) const
print –debug output if debug flags match the simplification: 0=unknown/both simplifications 1=1st sim...
Definition: tokenize.cpp:5855
const Token * tokens() const
Definition: tokenize.h:609
bool simplifyTokens1(const std::string &configuration)
Definition: tokenize.cpp:3361
TokenList list
Token list: stores all tokens.
Definition: tokenize.h:607
const SymbolDatabase * getSymbolDatabase() const
Definition: tokenize.h:580
void dump(std::ostream &out) const
Definition: tokenize.cpp:5905
void setTimerResults(TimerResults *tr)
Definition: tokenize.h:60
Color
Definition: color.h:27
static const std::string emptyString
Definition: config.h:127
static constexpr char FILELIST[]
Definition: cppcheck.cpp:87
static std::string cmdFileName(std::string f)
Definition: cppcheck.cpp:111
static const CWE CWE398(398U)
static constexpr char ExtraVersion[]
Definition: cppcheck.cpp:85
static std::string detectPython(const CppCheck::ExecuteCmdFn &executeCommand)
Definition: cppcheck.cpp:222
static std::vector< std::string > split(const std::string &str, const std::string &sep=" ")
Definition: cppcheck.cpp:119
static std::string getDumpFileName(const Settings &settings, const std::string &filename)
Definition: cppcheck.cpp:151
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)
Definition: cppcheck.cpp:246
static constexpr char Version[]
Definition: cppcheck.cpp:84
static std::string getDefinesFlags(const std::string &semicolonSeparatedString)
Definition: cppcheck.cpp:340
static bool reportClangErrors(std::istream &is, const std::function< void(const ErrorMessage &)> &reportErr, std::vector< ErrorMessage > &warnings)
Definition: cppcheck.cpp:379
static TimerResults s_timerResults
Definition: cppcheck.cpp:89
static int getPid()
Definition: cppcheck.cpp:142
static void createDumpFile(const Settings &settings, const std::string &filename, std::ofstream &fdump, std::string &dumpFile)
Definition: cppcheck.cpp:172
static simplecpp::TokenList createTokenList(const std::string &filename, std::vector< std::string > &files, simplecpp::OutputList *outputList, std::istream *fileStream)
Definition: cppcheck.cpp:609
static std::string getCtuInfoFileName(const std::string &dumpFile)
Definition: cppcheck.cpp:167
Severity severityFromString(const std::string &severity)
Definition: errortypes.cpp:75
@ none
No severity (default value).
@ warning
Warning.
@ portability
Portability warning.
@ style
Style warning.
@ debug
Debug message.
@ information
Checking information.
@ performance
Performance warning.
@ error
Programming error.
@ internal
Internal message.
@ unusedFunction
int maxCtuDepth
Definition: ctu.cpp:58
CPPCHECKLIB FileInfo * getFileInfo(const Tokenizer *tokenizer)
Parse current TU and extract file info.
Definition: ctu.cpp:309
void setValues(TokenList &tokenlist, SymbolDatabase &symboldatabase, ErrorLogger *errorLogger, const Settings &settings, TimerResultsIntf *timerResults)
Perform valueflow analysis.
Definition: valueflow.cpp:9535
void CPPCHECKLIB parseClangAstDump(Tokenizer &tokenizer, std::istream &f)
static std::string cfg(const std::vector< std::string > &configs, const std::string &userDefines)
std::string args
Definition: addoninfo.h:30
std::string scriptFile
Definition: addoninfo.h:28
std::string name
Definition: addoninfo.h:27
bool ctu
Definition: addoninfo.h:32
std::string python
Definition: addoninfo.h:31
std::string runScript
Definition: addoninfo.h:33
std::string executable
Definition: addoninfo.h:29
File settings.
Definition: filesettings.h:30
std::string cppcheckDefines() const
Definition: filesettings.h:35
std::string filename
Definition: filesettings.h:32
std::string defines
Definition: filesettings.h:33
std::set< std::string > undefs
Definition: filesettings.h:38
std::string cfg
Definition: filesettings.h:31
Platform::Type platformType
Definition: filesettings.h:43
std::string standard
Definition: filesettings.h:42
std::list< std::string > includePaths
Definition: filesettings.h:39
std::list< std::string > systemIncludePaths
Definition: filesettings.h:41
Simple container to be thrown when internal error is detected.
Definition: errortypes.h:36
bool setC(const std::string &str)
Definition: standards.h:48
std::string getC() const
Definition: standards.h:64
std::string stdValue
–std value given on command line
Definition: standards.h:46
bool setCPP(std::string str)
Definition: standards.h:87
std::string getCPP() const
Definition: standards.h:93
static SuppressionList::ErrorMessage fromErrorMessage(const ::ErrorMessage &msg, const std::set< std::string > &macroNames)
SuppressionList nofail
suppress exitcode
Definition: suppressions.h:266
SuppressionList nomsg
suppress message (–suppressions)
Definition: suppressions.h:264
SHOWTIME_MODES
Definition: timer.h:30
bool startsWith(const std::string &str, const char start[], std::size_t startlen)
Definition: utils.h:94
bool endsWith(const std::string &str, char c)
Definition: utils.h:110
#define CPPCHECK_VERSION_STRING
Definition: version.h:18