Skip to content

Commit 61a4ae9

Browse files
authored
CppCheck: removed duplicated exception handling from checkInternal() (danmar#8019)
1 parent 92b7624 commit 61a4ae9

File tree

1 file changed

+79
-89
lines changed

1 file changed

+79
-89
lines changed

lib/cppcheck.cpp

Lines changed: 79 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,109 +1143,99 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11431143
continue;
11441144
}
11451145

1146-
try {
1147-
TokenList tokenlist{mSettings, file.lang()};
1148-
1149-
{
1150-
bool skipCfg = false;
1151-
// Create tokens, skip rest of iteration if failed
1152-
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1153-
simplecpp::OutputList outputList_cfg;
1154-
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, outputList_cfg);
1155-
const simplecpp::Output* o = preprocessor.handleErrors(outputList_cfg);
1156-
if (!o) {
1157-
tokenlist.createTokens(std::move(tokensP));
1158-
}
1159-
else {
1160-
// #error etc during preprocessing
1161-
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + tokensP.file(o->location) + ':' + std::to_string(o->location.line) + "] " + o->msg);
1162-
--checkCount; // don't count invalid configurations
1163-
1164-
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
1165-
// If there is no valid configuration then report error..
1166-
preprocessor.error(tokensP.file(o->location), o->location.line, o->location.col, o->msg, o->type);
1167-
}
1168-
skipCfg = true;
1169-
}
1170-
});
1171-
if (skipCfg)
1172-
continue;
1173-
}
1174-
hasValidConfig = true;
1146+
TokenList tokenlist{mSettings, file.lang()};
11751147

1176-
Tokenizer tokenizer(std::move(tokenlist), mErrorLogger);
1177-
try {
1178-
if (mSettings.showtime != ShowTime::NONE)
1179-
tokenizer.setTimerResults(&s_timerResults);
1180-
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?
1181-
1182-
// locations macros
1183-
mLogger->setLocationMacros(tokenizer.tokens(), files);
1184-
1185-
// If only errors are printed, print filename after the check
1186-
if (!mSettings.quiet && (!currentConfig.empty() || checkCount > 1)) {
1187-
std::string fixedpath = Path::toNativeSeparators(file.spath());
1188-
mErrorLogger.reportOut("Checking " + fixedpath + ": " + currentConfig + "...", Color::FgGreen);
1148+
{
1149+
bool skipCfg = false;
1150+
// Create tokens, skip rest of iteration if failed
1151+
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1152+
simplecpp::OutputList outputList_cfg;
1153+
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, outputList_cfg);
1154+
const simplecpp::Output* o = preprocessor.handleErrors(outputList_cfg);
1155+
if (!o) {
1156+
tokenlist.createTokens(std::move(tokensP));
1157+
}
1158+
else {
1159+
// #error etc during preprocessing
1160+
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + tokensP.file(o->location) + ':' + std::to_string(o->location.line) + "] " + o->msg);
1161+
--checkCount; // don't count invalid configurations
1162+
1163+
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
1164+
// If there is no valid configuration then report error..
1165+
preprocessor.error(tokensP.file(o->location), o->location.line, o->location.col, o->msg, o->type);
1166+
}
1167+
skipCfg = true;
11891168
}
1169+
});
1170+
if (skipCfg)
1171+
continue;
1172+
}
1173+
hasValidConfig = true;
11901174

1191-
if (!tokenizer.tokens())
1192-
continue;
1175+
Tokenizer tokenizer(std::move(tokenlist), mErrorLogger);
1176+
try {
1177+
if (mSettings.showtime != ShowTime::NONE)
1178+
tokenizer.setTimerResults(&s_timerResults);
1179+
tokenizer.setDirectives(directives); // TODO: how to avoid repeated copies?
11931180

1194-
// skip rest of iteration if just checking configuration
1195-
if (mSettings.checkConfiguration)
1196-
continue;
1181+
// locations macros
1182+
mLogger->setLocationMacros(tokenizer.tokens(), files);
1183+
1184+
// If only errors are printed, print filename after the check
1185+
if (!mSettings.quiet && (!currentConfig.empty() || checkCount > 1)) {
1186+
std::string fixedpath = Path::toNativeSeparators(file.spath());
1187+
mErrorLogger.reportOut("Checking " + fixedpath + ": " + currentConfig + "...", Color::FgGreen);
1188+
}
1189+
1190+
if (!tokenizer.tokens())
1191+
continue;
1192+
1193+
// skip rest of iteration if just checking configuration
1194+
if (mSettings.checkConfiguration)
1195+
continue;
11971196

11981197
#ifdef HAVE_RULES
1199-
// Execute rules for "raw" code
1200-
executeRules("raw", tokenizer.list);
1198+
// Execute rules for "raw" code
1199+
executeRules("raw", tokenizer.list);
12011200
#endif
12021201

1203-
// Simplify tokens into normal form, skip rest of iteration if failed
1204-
if (!tokenizer.simplifyTokens1(currentConfig, fileIndex))
1205-
continue;
1202+
// Simplify tokens into normal form, skip rest of iteration if failed
1203+
if (!tokenizer.simplifyTokens1(currentConfig, fileIndex))
1204+
continue;
12061205

1207-
// dump xml if --dump
1208-
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) {
1209-
fdump << "<dump cfg=\"" << ErrorLogger::toxml(currentConfig) << "\">" << std::endl;
1210-
fdump << " <standards>" << std::endl;
1211-
fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl;
1212-
fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl;
1213-
fdump << " </standards>" << std::endl;
1214-
fdump << getLibraryDumpData();
1215-
preprocessor.dump(fdump);
1216-
tokenizer.dump(fdump);
1217-
fdump << "</dump>" << std::endl;
1218-
}
1206+
// dump xml if --dump
1207+
if ((mSettings.dump || !mSettings.addons.empty()) && fdump.is_open()) {
1208+
fdump << "<dump cfg=\"" << ErrorLogger::toxml(currentConfig) << "\">" << std::endl;
1209+
fdump << " <standards>" << std::endl;
1210+
fdump << " <c version=\"" << mSettings.standards.getC() << "\"/>" << std::endl;
1211+
fdump << " <cpp version=\"" << mSettings.standards.getCPP() << "\"/>" << std::endl;
1212+
fdump << " </standards>" << std::endl;
1213+
fdump << getLibraryDumpData();
1214+
preprocessor.dump(fdump);
1215+
tokenizer.dump(fdump);
1216+
fdump << "</dump>" << std::endl;
1217+
}
12191218

1220-
if (mSettings.inlineSuppressions) {
1221-
// Need to call this even if the hash will skip this configuration
1222-
mSuppressions.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer);
1223-
}
1219+
if (mSettings.inlineSuppressions) {
1220+
// Need to call this even if the hash will skip this configuration
1221+
mSuppressions.nomsg.markUnmatchedInlineSuppressionsAsChecked(tokenizer);
1222+
}
12241223

1225-
// Skip if we already met the same simplified token list
1226-
if (maxConfigs > 1) {
1227-
const std::size_t hash = tokenizer.list.calculateHash();
1228-
if (hashes.find(hash) != hashes.end()) {
1229-
if (mSettings.debugwarnings)
1230-
purgedConfigurationMessage(file.spath(), currentConfig);
1231-
continue;
1232-
}
1233-
hashes.insert(hash);
1224+
// Skip if we already met the same simplified token list
1225+
if (maxConfigs > 1) {
1226+
const std::size_t hash = tokenizer.list.calculateHash();
1227+
if (hashes.find(hash) != hashes.end()) {
1228+
if (mSettings.debugwarnings)
1229+
purgedConfigurationMessage(file.spath(), currentConfig);
1230+
continue;
12341231
}
1235-
1236-
// Check normal tokens
1237-
checkNormalTokens(tokenizer, analyzerInformation.get(), currentConfig);
1238-
} catch (const InternalError &e) {
1239-
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath());
1240-
mErrorLogger.reportErr(errmsg);
1232+
hashes.insert(hash);
12411233
}
1242-
} catch (const TerminateException &) {
1243-
// Analysis is terminated
1244-
if (analyzerInformation)
1245-
mLogger->setAnalyzerInfo(nullptr);
1246-
return mLogger->exitcode();
1234+
1235+
// Check normal tokens
1236+
checkNormalTokens(tokenizer, analyzerInformation.get(), currentConfig);
12471237
} catch (const InternalError &e) {
1248-
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, nullptr, file.spath());
1238+
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath());
12491239
mErrorLogger.reportErr(errmsg);
12501240
}
12511241
}

0 commit comments

Comments
 (0)