Skip to content

Commit 6790f27

Browse files
committed
CppCheck: made AnalyzerInformation a local object [skip ci]
1 parent 235bd66 commit 6790f27

2 files changed

Lines changed: 30 additions & 30 deletions

File tree

lib/cppcheck.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
734734
&s_timerResults);
735735
if (mSettings.debugnormal)
736736
tokenizer.printDebugOutput(1, std::cout);
737-
checkNormalTokens(tokenizer);
737+
checkNormalTokens(tokenizer, nullptr); // TODO: provide analyzer information
738738

739739
// create dumpfile
740740
std::ofstream fdump;
@@ -887,15 +887,18 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
887887

888888
mLogger->closePlist();
889889

890+
std::unique_ptr<AnalyzerInformation> analyzerInformation;
891+
890892
try {
891893
if (mSettings.library.markupFile(file.spath())) {
894+
// TODO: if an exception occurs in this block it will continue in an unexpected code path
892895
if (!mSettings.buildDir.empty())
893896
{
894-
mAnalyzerInformation.reset(new AnalyzerInformation);
895-
mLogger->setAnalyzerInfo(mAnalyzerInformation.get());
897+
analyzerInformation.reset(new AnalyzerInformation);
898+
mLogger->setAnalyzerInfo(analyzerInformation.get());
896899
}
897900

898-
if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || mAnalyzerInformation)) {
901+
if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || analyzerInformation)) {
899902
std::size_t hash = 0;
900903
// 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.
901904
Tokenizer tokenizer(mSettings, mErrorLogger);
@@ -904,7 +907,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
904907
if (fileStream) {
905908
std::vector<std::string> files{file.spath()};
906909
simplecpp::TokenList tokens(*fileStream, files);
907-
if (mAnalyzerInformation) {
910+
if (analyzerInformation) {
908911
const Preprocessor preprocessor(mSettings, mErrorLogger);
909912
hash = calculateHash(preprocessor, tokens, mSettings);
910913
}
@@ -913,22 +916,21 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
913916
else {
914917
std::vector<std::string> files{file.spath()};
915918
simplecpp::TokenList tokens(file.spath(), files);
916-
if (mAnalyzerInformation) {
919+
if (analyzerInformation) {
917920
const Preprocessor preprocessor(mSettings, mErrorLogger);
918921
hash = calculateHash(preprocessor, tokens, mSettings);
919922
}
920923
tokenizer.list.createTokens(std::move(tokens));
921924
}
922925
mUnusedFunctionsCheck->parseTokens(tokenizer, mSettings);
923926

924-
if (mAnalyzerInformation) {
927+
if (analyzerInformation) {
925928
mLogger->setAnalyzerInfo(nullptr);
926929

927930
std::list<ErrorMessage> errors;
928-
mAnalyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors);
929-
mAnalyzerInformation->setFileInfo("CheckUnusedFunctions", mUnusedFunctionsCheck->analyzerInfo());
930-
mAnalyzerInformation->close();
931-
mAnalyzerInformation.reset();
931+
analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors);
932+
analyzerInformation->setFileInfo("CheckUnusedFunctions", mUnusedFunctionsCheck->analyzerInfo());
933+
analyzerInformation->close();
932934
}
933935
}
934936
return EXIT_SUCCESS;
@@ -992,15 +994,15 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
992994
preprocessor.removeComments(tokens1);
993995

994996
if (!mSettings.buildDir.empty()) {
995-
mAnalyzerInformation.reset(new AnalyzerInformation);
996-
mLogger->setAnalyzerInfo(mAnalyzerInformation.get());
997+
analyzerInformation.reset(new AnalyzerInformation);
998+
mLogger->setAnalyzerInfo(analyzerInformation.get());
997999
}
9981000

999-
if (mAnalyzerInformation) {
1001+
if (analyzerInformation) {
10001002
// Calculate hash so it can be compared with old hash / future hashes
10011003
const std::size_t hash = calculateHash(preprocessor, tokens1, mSettings);
10021004
std::list<ErrorMessage> errors;
1003-
if (!mAnalyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors)) {
1005+
if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors)) {
10041006
while (!errors.empty()) {
10051007
mErrorLogger.reportErr(errors.front());
10061008
errors.pop_front();
@@ -1181,7 +1183,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11811183
}
11821184

11831185
// Check normal tokens
1184-
checkNormalTokens(tokenizer);
1186+
checkNormalTokens(tokenizer, analyzerInformation.get());
11851187
} catch (const simplecpp::Output &o) {
11861188
// #error etc during preprocessing
11871189
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
@@ -1239,7 +1241,6 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
12391241
}
12401242

12411243
executeAddons(dumpFile, file);
1242-
12431244
} catch (const TerminateException &) {
12441245
// Analysis is terminated
12451246
return mLogger->exitcode();
@@ -1252,9 +1253,9 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
12521253
mErrorLogger.reportErr(errmsg);
12531254
}
12541255

1255-
if (mAnalyzerInformation) {
1256+
if (analyzerInformation) {
12561257
mLogger->setAnalyzerInfo(nullptr);
1257-
mAnalyzerInformation.reset();
1258+
analyzerInformation.reset();
12581259
}
12591260

12601261
// In jointSuppressionReport mode, unmatched suppressions are
@@ -1293,7 +1294,7 @@ void CppCheck::internalError(const std::string &filename, const std::string &msg
12931294
// CppCheck - A function that checks a normal token list
12941295
//---------------------------------------------------------------------------
12951296

1296-
void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
1297+
void CppCheck::checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation* analyzerInformation)
12971298
{
12981299
CheckUnusedFunctions unusedFunctionsChecker;
12991300

@@ -1343,12 +1344,12 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
13431344
return;
13441345
}
13451346

1346-
if (mSettings.useSingleJob() || mAnalyzerInformation) {
1347+
if (mSettings.useSingleJob() || analyzerInformation) {
13471348
// Analyse the tokens..
13481349
{
13491350
CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer);
1350-
if (mAnalyzerInformation)
1351-
mAnalyzerInformation->setFileInfo("ctu", fi1->toString());
1351+
if (analyzerInformation)
1352+
analyzerInformation->setFileInfo("ctu", fi1->toString());
13521353
if (mSettings.useSingleJob())
13531354
mFileInfo.push_back(fi1);
13541355
else
@@ -1359,8 +1360,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
13591360
// cppcheck-suppress shadowFunction - TODO: fix this
13601361
for (const Check *check : Check::instances()) {
13611362
if (Check::FileInfo * const fi = check->getFileInfo(tokenizer, mSettings)) {
1362-
if (mAnalyzerInformation)
1363-
mAnalyzerInformation->setFileInfo(check->name(), fi->toString());
1363+
if (analyzerInformation)
1364+
analyzerInformation->setFileInfo(check->name(), fi->toString());
13641365
if (mSettings.useSingleJob())
13651366
mFileInfo.push_back(fi);
13661367
else
@@ -1370,8 +1371,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
13701371
}
13711372
}
13721373

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

13771378
#ifdef HAVE_RULES

lib/cppcheck.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ class CPPCHECKLIB CppCheck {
181181
/**
182182
* @brief Check normal tokens
183183
* @param tokenizer tokenizer instance
184+
* @param analyzerInformation the analyzer infomation
184185
*/
185-
void checkNormalTokens(const Tokenizer &tokenizer);
186+
void checkNormalTokens(const Tokenizer &tokenizer, AnalyzerInformation* analyzerInformation);
186187

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

229-
std::unique_ptr<AnalyzerInformation> mAnalyzerInformation;
230-
231230
/** Callback for executing a shell command (exe, args, output) */
232231
ExecuteCmdFn mExecuteCommand;
233232

0 commit comments

Comments
 (0)