void CppCheck::checkNormalTokens(const Tokenizer &tokenizer) { // call all "runChecks" in all registered Check classes for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { if (_settings.terminated()) return; if (tokenizer.isMaxTime()) return; Timer timerRunChecks((*it)->name() + "::runChecks", _settings.showtime, &S_timerResults); (*it)->runChecks(&tokenizer, &_settings, this); } // Analyse the tokens.. for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { Check::FileInfo *fi = (*it)->getFileInfo(&tokenizer, &_settings); if (fi != nullptr) { fileInfo.push_back(fi); analyzerInformation.setFileInfo((*it)->name(), fi->toString()); } } executeRules("normal", tokenizer); }
void CppCheck::checkFile(const std::string &code, const char FileName[]) { if (_settings.terminated() || _settings.checkConfiguration) return; Tokenizer _tokenizer(&_settings, this); bool result; // Tokenize the file std::istringstream istr(code); Timer timer("Tokenizer::tokenize", _settings._showtime, &S_timerResults); result = _tokenizer.tokenize(istr, FileName, cfg); timer.Stop(); if (!result) { // File had syntax errors, abort return; } Timer timer2("Tokenizer::fillFunctionList", _settings._showtime, &S_timerResults); _tokenizer.fillFunctionList(); timer2.Stop(); // call all "runChecks" in all registered Check classes for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { if (_settings.terminated()) return; Timer timerRunChecks((*it)->name() + "::runChecks", _settings._showtime, &S_timerResults); (*it)->runChecks(&_tokenizer, &_settings, this); } Timer timer3("Tokenizer::simplifyTokenList", _settings._showtime, &S_timerResults); result = _tokenizer.simplifyTokenList(); timer3.Stop(); if (!result) return; Timer timer4("Tokenizer::fillFunctionList", _settings._showtime, &S_timerResults); _tokenizer.fillFunctionList(); timer4.Stop(); if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1) _checkUnusedFunctions.parseTokens(_tokenizer); // call all "runSimplifiedChecks" in all registered Check classes for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { if (_settings.terminated()) return; Timer timerSimpleChecks((*it)->name() + "::runSimplifiedChecks", _settings._showtime, &S_timerResults); (*it)->runSimplifiedChecks(&_tokenizer, &_settings, this); } #ifdef HAVE_RULES // Are there extra rules? if (!_settings.rules.empty()) { std::ostringstream ostr; for (const Token *tok = _tokenizer.tokens(); tok; tok = tok->next()) ostr << " " << tok->str(); const std::string str(ostr.str()); for (std::list<Settings::Rule>::const_iterator it = _settings.rules.begin(); it != _settings.rules.end(); ++it) { const Settings::Rule &rule = *it; if (rule.pattern.empty() || rule.id.empty() || rule.severity.empty()) continue; const char *error = 0; int erroffset = 0; pcre *re = pcre_compile(rule.pattern.c_str(),0,&error,&erroffset,NULL); if (!re && error) { ErrorLogger::ErrorMessage errmsg(std::list<ErrorLogger::ErrorMessage::FileLocation>(), Severity::error, error, "pcre_compile", false); reportErr(errmsg); } if (!re) continue; int pos = 0; int ovector[30]; while (0 <= pcre_exec(re, NULL, str.c_str(), (int)str.size(), pos, 0, ovector, 30)) { unsigned int pos1 = (unsigned int)ovector[0]; unsigned int pos2 = (unsigned int)ovector[1]; // jump to the end of the match for the next pcre_exec pos = (int)pos2; // determine location.. ErrorLogger::ErrorMessage::FileLocation loc; loc.setfile(_tokenizer.getFiles()->front()); loc.line = 0; unsigned int len = 0; for (const Token *tok = _tokenizer.tokens(); tok; tok = tok->next()) { len = len + 1 + tok->str().size(); if (len > pos1) { loc.setfile(_tokenizer.getFiles()->at(tok->fileIndex())); loc.line = tok->linenr(); break; } } const std::list<ErrorLogger::ErrorMessage::FileLocation> callStack(1, loc); // Create error message std::string summary; if (rule.summary.empty()) summary = "found '" + str.substr(pos1, pos2 - pos1) + "'"; else summary = rule.summary; const ErrorLogger::ErrorMessage errmsg(callStack, Severity::fromString(rule.severity), summary, rule.id, false); // Report error reportErr(errmsg); } pcre_free(re); } } #endif }
void CppCheck::checkFile(const std::string &code, const char FileName[]) { if (_settings.terminated() || _settings.checkConfiguration) return; Tokenizer _tokenizer(&_settings, this); if (_settings._showtime != SHOWTIME_NONE) _tokenizer.setTimerResults(&S_timerResults); try { bool result; // Execute rules for "raw" code for (std::list<Settings::Rule>::const_iterator it = _settings.rules.begin(); it != _settings.rules.end(); ++it) { if (it->tokenlist == "raw") { Tokenizer tokenizer2(&_settings, this); std::istringstream istr(code); tokenizer2.list.createTokens(istr, FileName); executeRules("raw", tokenizer2); break; } } // Tokenize the file std::istringstream istr(code); Timer timer("Tokenizer::tokenize", _settings._showtime, &S_timerResults); result = _tokenizer.tokenize(istr, FileName, cfg); timer.Stop(); if (!result) { // File had syntax errors, abort return; } // dump if (_settings.dump) { std::string dumpfile = std::string(FileName) + ".dump"; std::ofstream fdump(dumpfile.c_str()); if (fdump.is_open()) { fdump << "<?xml version=\"1.0\"?>" << std::endl; fdump << "<dump cfg=\"" << cfg << "\">" << std::endl; _tokenizer.dump(fdump); fdump << "</dump>" << std::endl; } return; } // call all "runChecks" in all registered Check classes for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { if (_settings.terminated()) return; Timer timerRunChecks((*it)->name() + "::runChecks", _settings._showtime, &S_timerResults); (*it)->runChecks(&_tokenizer, &_settings, this); } if (_settings.isEnabled("unusedFunction") && _settings._jobs == 1) CheckUnusedFunctions::instance.parseTokens(_tokenizer, FileName, &_settings); executeRules("normal", _tokenizer); if (!_simplify) return; Timer timer3("Tokenizer::simplifyTokenList2", _settings._showtime, &S_timerResults); result = _tokenizer.simplifyTokenList2(); timer3.Stop(); if (!result) return; // call all "runSimplifiedChecks" in all registered Check classes for (std::list<Check *>::const_iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) { if (_settings.terminated()) return; Timer timerSimpleChecks((*it)->name() + "::runSimplifiedChecks", _settings._showtime, &S_timerResults); (*it)->runSimplifiedChecks(&_tokenizer, &_settings, this); } if (_settings.terminated()) return; executeRules("simple", _tokenizer); if (_settings.terminated()) return; } catch (const InternalError &e) { std::list<ErrorLogger::ErrorMessage::FileLocation> locationList; ErrorLogger::ErrorMessage::FileLocation loc; if (e.token) { loc.line = e.token->linenr(); const std::string fixedpath = Path::toNativeSeparators(_tokenizer.list.file(e.token)); loc.setfile(fixedpath); } else { ErrorLogger::ErrorMessage::FileLocation loc2; loc2.setfile(Path::toNativeSeparators(FileName)); locationList.push_back(loc2); loc.setfile(_tokenizer.list.getSourceFilePath()); } locationList.push_back(loc); const ErrorLogger::ErrorMessage errmsg(locationList, Severity::error, e.errorMessage, e.id, false); _errorLogger.reportErr(errmsg); } }