Skip to content

Commit dc61b2a

Browse files
committed
CppCheck: made AnalyzerInformation a local object
1 parent 3a0f039 commit dc61b2a

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
@@ -731,7 +731,7 @@ unsigned int CppCheck::checkClang(const FileWithDetails &file)
731731
&s_timerResults);
732732
if (mSettings.debugnormal)
733733
tokenizer.printDebugOutput(1, std::cout);
734-
checkNormalTokens(tokenizer);
734+
checkNormalTokens(tokenizer, nullptr); // TODO: provide analyzer information
735735

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

887887
mLogger->closePlist();
888888

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

897-
if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || mAnalyzerInformation)) {
900+
if (mUnusedFunctionsCheck && (mSettings.useSingleJob() || analyzerInformation)) {
898901
std::size_t hash = 0;
899902
// 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.
900903
Tokenizer tokenizer(mSettings, mErrorLogger);
@@ -903,7 +906,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
903906
if (fileStream) {
904907
std::vector<std::string> files{file.spath()};
905908
simplecpp::TokenList tokens(*fileStream, files);
906-
if (mAnalyzerInformation) {
909+
if (analyzerInformation) {
907910
const Preprocessor preprocessor(mSettings, mErrorLogger);
908911
hash = calculateHash(preprocessor, tokens, mSettings);
909912
}
@@ -912,22 +915,21 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
912915
else {
913916
std::vector<std::string> files{file.spath()};
914917
simplecpp::TokenList tokens(file.spath(), files);
915-
if (mAnalyzerInformation) {
918+
if (analyzerInformation) {
916919
const Preprocessor preprocessor(mSettings, mErrorLogger);
917920
hash = calculateHash(preprocessor, tokens, mSettings);
918921
}
919922
tokenizer.list.createTokens(std::move(tokens));
920923
}
921924
mUnusedFunctionsCheck->parseTokens(tokenizer, mSettings);
922925

923-
if (mAnalyzerInformation) {
926+
if (analyzerInformation) {
924927
mLogger->setAnalyzerInfo(nullptr);
925928

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

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

998-
if (mAnalyzerInformation) {
1000+
if (analyzerInformation) {
9991001
// Calculate hash so it can be compared with old hash / future hashes
10001002
const std::size_t hash = calculateHash(preprocessor, tokens1, mSettings);
10011003
std::list<ErrorMessage> errors;
1002-
if (!mAnalyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors)) {
1004+
if (!analyzerInformation->analyzeFile(mSettings.buildDir, file.spath(), cfgname, hash, errors)) {
10031005
while (!errors.empty()) {
10041006
mErrorLogger.reportErr(errors.front());
10051007
errors.pop_front();
@@ -1180,7 +1182,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11801182
}
11811183

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

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

1254-
if (mAnalyzerInformation) {
1255+
if (analyzerInformation) {
12551256
mLogger->setAnalyzerInfo(nullptr);
1256-
mAnalyzerInformation.reset();
1257+
analyzerInformation.reset();
12571258
}
12581259

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

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

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

1345-
if (mSettings.useSingleJob() || mAnalyzerInformation) {
1346+
if (mSettings.useSingleJob() || analyzerInformation) {
13461347
// Analyse the tokens..
13471348
{
13481349
CTU::FileInfo * const fi1 = CTU::getFileInfo(tokenizer);
1349-
if (mAnalyzerInformation)
1350-
mAnalyzerInformation->setFileInfo("ctu", fi1->toString());
1350+
if (analyzerInformation)
1351+
analyzerInformation->setFileInfo("ctu", fi1->toString());
13511352
if (mSettings.useSingleJob())
13521353
mFileInfo.push_back(fi1);
13531354
else
@@ -1358,8 +1359,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
13581359
// cppcheck-suppress shadowFunction - TODO: fix this
13591360
for (const Check *check : Check::instances()) {
13601361
if (Check::FileInfo * const fi = check->getFileInfo(tokenizer, mSettings)) {
1361-
if (mAnalyzerInformation)
1362-
mAnalyzerInformation->setFileInfo(check->name(), fi->toString());
1362+
if (analyzerInformation)
1363+
analyzerInformation->setFileInfo(check->name(), fi->toString());
13631364
if (mSettings.useSingleJob())
13641365
mFileInfo.push_back(fi);
13651366
else
@@ -1369,8 +1370,8 @@ void CppCheck::checkNormalTokens(const Tokenizer &tokenizer)
13691370
}
13701371
}
13711372

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

13761377
#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)