Skip to content

Commit fcb91c0

Browse files
committed
CppCheck: adjusted scope and errorhandling in checkFile()
1 parent 6e02524 commit fcb91c0

File tree

1 file changed

+57
-52
lines changed

1 file changed

+57
-52
lines changed

lib/cppcheck.cpp

+57-52
Original file line numberDiff line numberDiff line change
@@ -1125,9 +1125,9 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11251125
continue;
11261126
}
11271127

1128-
TokenList tokenlist{&mSettings};
1129-
11301128
try {
1129+
TokenList tokenlist{&mSettings};
1130+
11311131
// Create tokens, skip rest of iteration if failed
11321132
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
11331133
simplecpp::TokenList tokensP = preprocessor.preprocess(tokens1, mCurrentConfig, files, true);
@@ -1136,66 +1136,71 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
11361136
hasValidConfig = true;
11371137

11381138
Tokenizer tokenizer(std::move(tokenlist), mSettings, mErrorLogger);
1139-
if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_NONE)
1140-
tokenizer.setTimerResults(&s_timerResults);
1141-
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?
1142-
1143-
// locations macros
1144-
mLogger->setLocationMacros(tokenizer.tokens(), files);
1145-
1146-
// If only errors are printed, print filename after the check
1147-
if (!mSettings.quiet && (!mCurrentConfig.empty() || checkCount > 1)) {
1148-
std::string fixedpath = Path::toNativeSeparators(file.spath());
1149-
mErrorLogger.reportOut("Checking " + fixedpath + ": " + mCurrentConfig + "...", Color::FgGreen);
1150-
}
1139+
try {
1140+
if (mSettings.showtime != SHOWTIME_MODES::SHOWTIME_NONE)
1141+
tokenizer.setTimerResults(&s_timerResults);
1142+
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?
1143+
1144+
// locations macros
1145+
mLogger->setLocationMacros(tokenizer.tokens(), files);
1146+
1147+
// If only errors are printed, print filename after the check
1148+
if (!mSettings.quiet && (!mCurrentConfig.empty() || checkCount > 1)) {
1149+
std::string fixedpath = Path::toNativeSeparators(file.spath());
1150+
mErrorLogger.reportOut("Checking " + fixedpath + ": " + mCurrentConfig + "...", Color::FgGreen);
1151+
}
11511152

1152-
if (!tokenizer.tokens())
1153-
continue;
1153+
if (!tokenizer.tokens())
1154+
continue;
11541155

1155-
// skip rest of iteration if just checking configuration
1156-
if (mSettings.checkConfiguration)
1157-
continue;
1156+
// skip rest of iteration if just checking configuration
1157+
if (mSettings.checkConfiguration)
1158+
continue;
11581159

11591160
#ifdef HAVE_RULES
1160-
// Execute rules for "raw" code
1161-
executeRules("raw", tokenizer.list);
1161+
// Execute rules for "raw" code
1162+
executeRules("raw", tokenizer.list);
11621163
#endif
11631164

1164-
// Simplify tokens into normal form, skip rest of iteration if failed
1165-
if (!tokenizer.simplifyTokens1(mCurrentConfig))
1166-
continue;
1165+
// Simplify tokens into normal form, skip rest of iteration if failed
1166+
if (!tokenizer.simplifyTokens1(mCurrentConfig))
1167+
continue;
11671168

1168-
// dump xml if --dump
1169-
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) {
1170-
fdump << "<dump cfg=\"" << ErrorLogger::toxml(mCurrentConfig) << "\">" << std::endl;
1171-
fdump << " <standards>" << std::endl;
1172-
fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl;
1173-
fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl;
1174-
fdump << " </standards>" << std::endl;
1175-
fdump << getLibraryDumpData();
1176-
preprocessor.dump(fdump);
1177-
tokenizer.dump(fdump);
1178-
fdump << "</dump>" << std::endl;
1179-
}
1169+
// dump xml if --dump
1170+
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) {
1171+
fdump << "<dump cfg=\"" << ErrorLogger::toxml(mCurrentConfig) << "\">" << std::endl;
1172+
fdump << " <standards>" << std::endl;
1173+
fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl;
1174+
fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl;
1175+
fdump << " </standards>" << std::endl;
1176+
fdump << getLibraryDumpData();
1177+
preprocessor.dump(fdump);
1178+
tokenizer.dump(fdump);
1179+
fdump << "</dump>" << std::endl;
1180+
}
11801181

1181-
if (mSettings.inlineSuppressions) {
1182-
// Need to call this even if the hash will skip this configuration
1183-
mSuppressions.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer);
1184-
}
1182+
if (mSettings.inlineSuppressions) {
1183+
// Need to call this even if the hash will skip this configuration
1184+
mSuppressions.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer);
1185+
}
11851186

1186-
// Skip if we already met the same simplified token list
1187-
if (mSettings.force || mSettings.maxConfigs > 1) {
1188-
const std::size_t hash = tokenizer.list.calculateHash();
1189-
if (hashes.find(hash) != hashes.end()) {
1190-
if (mSettings.debugwarnings)
1191-
purgedConfigurationMessage(file.spath(), mCurrentConfig);
1192-
continue;
1187+
// Skip if we already met the same simplified token list
1188+
if (mSettings.force || mSettings.maxConfigs > 1) {
1189+
const std::size_t hash = tokenizer.list.calculateHash();
1190+
if (hashes.find(hash) != hashes.end()) {
1191+
if (mSettings.debugwarnings)
1192+
purgedConfigurationMessage(file.spath(), mCurrentConfig);
1193+
continue;
1194+
}
1195+
hashes.insert(hash);
11931196
}
1194-
hashes.insert(hash);
1195-
}
11961197

1197-
// Check normal tokens
1198-
checkNormalTokens(tokenizer, analyzerInformation.get());
1198+
// Check normal tokens
1199+
checkNormalTokens(tokenizer, analyzerInformation.get());
1200+
} catch (const InternalError &e) {
1201+
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath());
1202+
mErrorLogger.reportErr(errmsg);
1203+
}
11991204
} catch (const simplecpp::Output &o) {
12001205
// #error etc during preprocessing
12011206
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
@@ -1225,7 +1230,7 @@ unsigned int CppCheck::checkFile(const FileWithDetails& file, const std::string
12251230
mLogger->setAnalyzerInfo(nullptr);
12261231
return mLogger->exitcode();
12271232
} catch (const InternalError &e) {
1228-
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenlist, file.spath());
1233+
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, file.spath());
12291234
mErrorLogger.reportErr(errmsg);
12301235
}
12311236
}

0 commit comments

Comments
 (0)