Skip to content

Commit

Permalink
CppCheck: made AnalyzerInformation a local object
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Dec 15, 2024
1 parent 3a0f039 commit dc61b2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
55 changes: 28 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,15 +993,15 @@ 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();
Expand Down Expand Up @@ -1180,7 +1182,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 Down Expand Up @@ -1238,7 +1240,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
}

executeAddons(dumpFile, file);

} catch (const TerminateException &) {
// Analysis is terminated
return mLogger->exitcode();
Expand All @@ -1251,9 +1252,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 +1293,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 +1343,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 +1359,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 +1370,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

0 comments on commit dc61b2a

Please sign in to comment.