unsigned int CppCheck::processFile() { exitcode = 0; // TODO: Should this be moved out to its own function so all the files can be // analysed before any files are checked? if (_settings.test_2_pass && _settings._jobs == 1) { const std::string printname = Path::toNativeSeparators(_filename); reportOut("Analysing " + printname + "..."); std::ifstream f(_filename.c_str()); analyseFile(f, _filename); } _errout.str(""); if (_settings.terminated()) return exitcode; if (_settings._errorsOnly == false) { std::string fixedpath(_filename); fixedpath = Path::simplifyPath(fixedpath.c_str()); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut(std::string("Checking ") + fixedpath + std::string("...")); } try { Preprocessor preprocessor(&_settings, this); std::list<std::string> configurations; std::string filedata = ""; if (!_fileContent.empty()) { // File content was given as a string std::istringstream iss(_fileContent); preprocessor.preprocess(iss, filedata, configurations, _filename, _settings._includePaths); } else { // Only file name was given, read the content from file std::ifstream fin(_filename.c_str()); Timer t("Preprocessor::preprocess", _settings._showtime, &S_timerResults); preprocessor.preprocess(fin, filedata, configurations, _filename, _settings._includePaths); } if (_settings.checkConfiguration) { return 0; } _settings.ifcfg = bool(configurations.size() > 1); if (!_settings.userDefines.empty()) { configurations.clear(); configurations.push_back(_settings.userDefines); } int checkCount = 0; for (std::list<std::string>::const_iterator it = configurations.begin(); it != configurations.end(); ++it) { // Check only 12 first configurations, after that bail out, unless --force // was used. if (!_settings._force && checkCount > 11) { const std::string fixedpath = Path::toNativeSeparators(_filename); ErrorLogger::ErrorMessage::FileLocation location; location.setfile(fixedpath); std::list<ErrorLogger::ErrorMessage::FileLocation> loclist; loclist.push_back(location); const std::string msg("Interrupted checking because of too many #ifdef configurations.\n" "The checking of the file was interrupted because there were too many " "#ifdef configurations. Checking of all #ifdef configurations can be forced " "by --force command line option or from GUI preferences. However that may " "increase the checking time."); ErrorLogger::ErrorMessage errmsg(loclist, Severity::information, msg, "toomanyconfigs", false); _errorLogger.reportErr(errmsg); break; } cfg = *it; Timer t("Preprocessor::getcode", _settings._showtime, &S_timerResults); const std::string codeWithoutCfg = Preprocessor::getcode(filedata, *it, _filename, &_settings, &_errorLogger); t.Stop(); // If only errors are printed, print filename after the check if (_settings._errorsOnly == false && it != configurations.begin()) { std::string fixedpath = Path::simplifyPath(_filename.c_str()); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("...")); } std::string appendCode = _settings.append(); if (!appendCode.empty()) Preprocessor::preprocessWhitespaces(appendCode); checkFile(codeWithoutCfg + appendCode, _filename.c_str()); ++checkCount; } } catch (std::runtime_error &e) { // Exception was thrown when checking this file.. const std::string fixedpath = Path::toNativeSeparators(_filename); _errorLogger.reportOut("Bailing out from checking " + fixedpath + ": " + e.what()); } reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions(_filename)); // This generates false positives - especially for libraries const bool verbose_orig = _settings._verbose; _settings._verbose = false; if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1) { _errout.str(""); if (_settings._errorsOnly == false) _errorLogger.reportOut("Checking usage of global functions.."); _checkUnusedFunctions.check(this); } _settings._verbose = verbose_orig; _errorList.clear(); return exitcode; }
unsigned int CppCheck::processFile() { exitcode = 0; // only show debug warnings for C/C++ source files (don't fix // debug warnings for java/c#/etc files) if (!Path::acceptFile(_filename)) _settings.debugwarnings = false; // TODO: Should this be moved out to its own function so all the files can be // analysed before any files are checked? if (_settings.test_2_pass && _settings._jobs == 1) { const std::string printname = Path::toNativeSeparators(_filename); reportOut("Analysing " + printname + "..."); std::ifstream f(_filename.c_str()); analyseFile(f, _filename); } _errout.str(""); if (_settings.terminated()) return exitcode; if (_settings._errorsOnly == false) { std::string fixedpath(_filename); fixedpath = Path::simplifyPath(fixedpath.c_str()); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut(std::string("Checking ") + fixedpath + std::string("...")); } try { Preprocessor preprocessor(&_settings, this); std::list<std::string> configurations; std::string filedata = ""; if (!_fileContent.empty()) { // File content was given as a string std::istringstream iss(_fileContent); preprocessor.preprocess(iss, filedata, configurations, _filename, _settings._includePaths); } else { // Only file name was given, read the content from file std::ifstream fin(_filename.c_str()); Timer t("Preprocessor::preprocess", _settings._showtime, &S_timerResults); preprocessor.preprocess(fin, filedata, configurations, _filename, _settings._includePaths); } if (_settings.checkConfiguration) { return 0; } _settings.ifcfg = bool(configurations.size() > 1); if (!_settings.userDefines.empty()) { configurations.clear(); configurations.push_back(_settings.userDefines); } int checkCount = 0; for (std::list<std::string>::const_iterator it = configurations.begin(); it != configurations.end(); ++it) { // Check only a few configurations (default 12), after that bail out, unless --force // was used. if (!_settings._force && checkCount >= _settings._maxConfigs) { const std::string fixedpath = Path::toNativeSeparators(_filename); ErrorLogger::ErrorMessage::FileLocation location; location.setfile(fixedpath); std::list<ErrorLogger::ErrorMessage::FileLocation> loclist; loclist.push_back(location); const std::string msg("Interrupted checking because of too many #ifdef configurations.\n" "The checking of the file was interrupted because there were too many " "#ifdef configurations. Checking of all #ifdef configurations can be forced " "by --force command line option or from GUI preferences. However that may " "increase the checking time."); ErrorLogger::ErrorMessage errmsg(loclist, Severity::information, msg, "toomanyconfigs", false); if (!_settings.nomsg.isSuppressedLocal(errmsg._id, fixedpath, location.line)) { reportErr(errmsg); } break; } cfg = *it; Timer t("Preprocessor::getcode", _settings._showtime, &S_timerResults); const std::string codeWithoutCfg = preprocessor.getcode(filedata, *it, _filename); t.Stop(); // If only errors are printed, print filename after the check if (_settings._errorsOnly == false && it != configurations.begin()) { std::string fixedpath = Path::simplifyPath(_filename.c_str()); fixedpath = Path::toNativeSeparators(fixedpath); _errorLogger.reportOut(std::string("Checking ") + fixedpath + ": " + cfg + std::string("...")); } const std::string &appendCode = _settings.append(); if (_settings.debugFalsePositive) { if (findError(codeWithoutCfg + appendCode, _filename.c_str())) { return exitcode; } } else { checkFile(codeWithoutCfg + appendCode, _filename.c_str()); } ++checkCount; } } catch (std::runtime_error &e) { // Exception was thrown when checking this file.. const std::string fixedpath = Path::toNativeSeparators(_filename); _errorLogger.reportOut("Bailing out from checking " + fixedpath + ": " + e.what()); } if (!_settings._errorsOnly) reportUnmatchedSuppressions(_settings.nomsg.getUnmatchedLocalSuppressions(_filename)); _errorList.clear(); return exitcode; }