Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions lib/cppcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
&s_timerResults);
if (mSettings.debugnormal)
tokenizer.printDebugOutput(1, std::cout);
checkNormalTokens(tokenizer);
checkNormalTokens(tokenizer, nullptr); // TODO: provide analyzer information

// create dumpfile
std::ofstream fdump;
Expand Down Expand Up @@ -886,15 +886,18 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string

mLogger->closePlist();

std::unique_ptr<AnalyzerInformation> analyzerInformation;

try {
if (mSettings.library.markupFile(file.spath())) {
// TODO: if an exception occurs in this block it will continue in an unexpected code path
if (!mSettings.buildDir.empty())
{
mAnalyzerInformation.reset(new AnalyzerInformation);
mLogger->setAnalyzerInfo(mAnalyzerInformation.get());
analyzerInformation.reset(new AnalyzerInformation);
mLogger->setAnalyzerInfo(analyzerInformation.get());
}

if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || mAnalyzerInformation)) {
if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || analyzerInformation)) {
std::size_t hash = 0;
// 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.
Tokenizer tokenizer(mSettings, mErrorLogger);
Expand All @@ -903,7 +906,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
if (fileStream) {
std::vector<std::string> files{file.spath()};
simplecpp::TokenList tokens(*fileStream, files);
if (mAnalyzerInformation) {
if (analyzerInformation) {
const Preprocessor preprocessor(mSettings, mErrorLogger);
hash = calculateHash(preprocessor, tokens, mSettings);
}
Expand All @@ -912,22 +915,21 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
else {
std::vector<std::string> files{file.spath()};
simplecpp::TokenList tokens(file.spath(), files);
if (mAnalyzerInformation) {
if (analyzerInformation) {
const Preprocessor preprocessor(mSettings, mErrorLogger);
hash = calculateHash(preprocessor, tokens, mSettings);
}
tokenizer.list.createTokens(std::move(tokens));
}
mUnusedFunctionsCheck->parseTokens(tokenizer, mSettings);

if (mAnalyzerInformation) {
if (analyzerInformation) {
mLogger->setAnalyzerInfo(nullptr);

std::list<ErrorMessage> errors;
mAnalyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors);
mAnalyzerInformation->setFileInfo("CheckUnusedFunctions", mUnusedFunctionsCheck->analyzerInfo());
mAnalyzerInformation->close();
mAnalyzerInformation.reset();
analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors);
analyzerInformation->setFileInfo("CheckUnusedFunctions", mUnusedFunctionsCheck->analyzerInfo());
analyzerInformation->close();
}
}
return EXIT_SUCCESS;
Expand Down Expand Up @@ -991,19 +993,20 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
preprocessor.removeComments(tokens1);

if (!mSettings.buildDir.empty()) {
mAnalyzerInformation.reset(new AnalyzerInformation);
mLogger->setAnalyzerInfo(mAnalyzerInformation.get());
analyzerInformation.reset(new AnalyzerInformation);
mLogger->setAnalyzerInfo(analyzerInformation.get());
}

if (mAnalyzerInformation) {
if (analyzerInformation) {
// Calculate hash so it can be compared with old hash / future hashes
const std::size_t hash = calculateHash(preprocessor, tokens1, mSettings);
std::list<ErrorMessage> errors;
if (!mAnalyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors)) {
if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors)) {
while (!errors.empty()) {
mErrorLogger.reportErr(errors.front());
errors.pop_front();
}
mLogger->setAnalyzerInfo(nullptr);
return mLogger->exitcode(); // known results => no need to reanalyze file
}
}
Expand Down Expand Up @@ -1040,6 +1043,8 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
for (const std::string &config : configurations)
(void)preprocessor.getcode(tokens1, config, files, true);

if (analyzerInformation)
mLogger->setAnalyzerInfo(nullptr);
return 0;
}

Expand Down Expand Up @@ -1180,7 +1185,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
}

// Check normal tokens
checkNormalTokens(tokenizer);
checkNormalTokens(tokenizer, analyzerInformation.get());
} catch (const simplecpp::Output &o) {
// #error etc during preprocessing
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
Expand All @@ -1206,6 +1211,8 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string

} catch (const TerminateException &) {
// Analysis is terminated
if (analyzerInformation)
mLogger->setAnalyzerInfo(nullptr);
return mLogger->exitcode();
} catch (const InternalError &e) {
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath());
Expand Down Expand Up @@ -1238,9 +1245,10 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
}

executeAddons(dumpFile, file);

} catch (const TerminateException &) {
// Analysis is terminated
if (analyzerInformation)
mLogger->setAnalyzerInfo(nullptr);
return mLogger->exitcode();
} catch (const std::runtime_error &e) {
internalError(file.spath(), std::string("Checking file failed: ") + e.what());
Expand All @@ -1251,9 +1259,9 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
mErrorLogger.reportErr(errmsg);
}

if (mAnalyzerInformation) {
if (analyzerInformation) {
mLogger->setAnalyzerInfo(nullptr);
mAnalyzerInformation.reset();
analyzerInformation.reset();
}

// In jointSuppressionReport mode, unmatched suppressions are
Expand Down Expand Up @@ -1292,7 +1300,7 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
// CppCheck - A function that checks a normal token list
//---------------------------------------------------------------------------

void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation* analyzerInformation)
{
CheckUnusedFunctions unusedFunctionsChecker;

Expand Down Expand Up @@ -1342,12 +1350,12 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
return;
}

if (mSettings.useSingleJob() || mAnalyzerInformation) {
if (mSettings.useSingleJob() || analyzerInformation) {
// Analyse the tokens..
{
CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer);
if (mAnalyzerInformation)
mAnalyzerInformation->setFileInfo("ctu", fi1->toString());
if (analyzerInformation)
analyzerInformation->setFileInfo("ctu", fi1->toString());
if (mSettings.useSingleJob())
mFileInfo.push_back(fi1);
else
Expand All @@ -1358,8 +1366,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
// cppcheck-suppress shadowFunction - TODO: fix this
for (const Check *check : Check::instances()) {
if (Check::FileInfo * const fi = check->getFileInfo(tokenizer, mSettings)) {
if (mAnalyzerInformation)
mAnalyzerInformation->setFileInfo(check->name(), fi->toString());
if (analyzerInformation)
analyzerInformation->setFileInfo(check->name(), fi->toString());
if (mSettings.useSingleJob())
mFileInfo.push_back(fi);
else
Expand All @@ -1369,8 +1377,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
}
}

if (mSettings.checks.isEnabled(Checks::unusedFunction) && mAnalyzerInformation) {
mAnalyzerInformation->setFileInfo("CheckUnusedFunctions", unusedFunctionsChecker.analyzerInfo());
if (mSettings.checks.isEnabled(Checks::unusedFunction) && analyzerInformation) {
analyzerInformation->setFileInfo("CheckUnusedFunctions", unusedFunctionsChecker.analyzerInfo());
}

#ifdef HAVE_RULES
Expand Down
5 changes: 2 additions & 3 deletions lib/cppcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ class CPPCHECKLIB CppCheck {
/**
* @brief Check normal tokens
* @param tokenizer tokenizer instance
* @param analyzerInformation the analyzer infomation
*/
void checkNormalTokens(const Tokenizer &tokenizer);
void checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation* analyzerInformation);

/**
* Execute addons
Expand Down Expand Up @@ -226,8 +227,6 @@ class CPPCHECKLIB CppCheck {
/** File info used for whole program analysis */
std::list<Check::FileInfo*> mFileInfo;

std::unique_ptr<AnalyzerInformation> mAnalyzerInformation;

/** Callback for executing a shell command (exe, args, output) */
ExecuteCmdFn mExecuteCommand;

Expand Down