Skip to content

Commit 04882c0

Browse files
committed
Cppcheck: reduced scope of try-catch for simplecpp::Output in checkInternal()
1 parent a9f88d4 commit 04882c0

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

lib/cppcheck.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,11 +1142,23 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
11421142
try {
11431143
TokenList tokenlist{mSettings, file.lang()};
11441144

1145-
// Create tokens, skip rest of iteration if failed
1146-
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1147-
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, true);
1148-
tokenlist.createTokens(std::move(tokensP));
1149-
});
1145+
try {
1146+
// Create tokens, skip rest of iteration if failed
1147+
Timer::run("Tokenizer::createTokens", mSettings.showtime, &s_timerResults, [&]() {
1148+
simplecpp::TokenList tokensP = preprocessor.preprocess(currentConfig, files, true);
1149+
tokenlist.createTokens(std::move(tokensP));
1150+
});
1151+
} catch (const simplecpp::Output &o) {
1152+
// #error etc during preprocessing
1153+
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
1154+
--checkCount; // don't count invalid configurations
1155+
1156+
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
1157+
// If there is no valid configuration then report error..
1158+
preprocessor.error(o.location.file(), o.location.line, o.location.col, o.msg, o.type);
1159+
}
1160+
continue;
1161+
}
11501162
hasValidConfig = true;
11511163

11521164
Tokenizer tokenizer(std::move(tokenlist), mErrorLogger);
@@ -1215,17 +1227,6 @@ unsigned int CppCheck::checkInternal(const FileWithDetails& file, const std::str
12151227
ErrorMessage errmsg = ErrorMessage::fromInternalError(e, &tokenizer.list, file.spath());
12161228
mErrorLogger.reportErr(errmsg);
12171229
}
1218-
} catch (const simplecpp::Output &o) {
1219-
// #error etc during preprocessing
1220-
configurationError.push_back((currentConfig.empty() ? "\'\'" : currentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
1221-
--checkCount; // don't count invalid configurations
1222-
1223-
if (!hasValidConfig && currCfg == *configurations.rbegin()) {
1224-
// If there is no valid configuration then report error..
1225-
preprocessor.error(o.location.file(), o.location.line, o.location.col, o.msg, o.type);
1226-
}
1227-
continue;
1228-
12291230
} catch (const TerminateException &) {
12301231
// Analysis is terminated
12311232
if (analyzerInformation)

lib/cppcheck.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ class CPPCHECKLIB CppCheck {
203203
* @brief Check a file using stream
204204
* @param file the file
205205
* @param cfgname cfg name
206-
* @param createTokenList a function to create the simplecpp::TokenList with - throws simplecpp::Output
206+
* @param createTokenList a function to create the simplecpp::TokenList with
207207
* @return number of errors found
208208
*/
209209
unsigned int checkInternal(const FileWithDetails& file, const std::string &cfgname, int fileIndex, const CreateTokenListFn& createTokenList);

lib/preprocessor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
118118

119119
void setPlatformInfo();
120120

121+
/**
122+
* @throws simplecpp::Output thrown in case of preprocessing error if throwError is true
123+
*/
121124
simplecpp::TokenList preprocess(const std::string &cfg, std::vector<std::string> &files, bool throwError = false);
122125

123126
std::string getcode(const std::string &cfg, std::vector<std::string> &files, bool writeLocations);
@@ -146,6 +149,9 @@ class CPPCHECKLIB WARN_UNUSED Preprocessor {
146149
private:
147150
static bool hasErrors(const simplecpp::Output &output);
148151

152+
/**
153+
* @throws simplecpp::Output thrown in case of preprocessing error if throwError is true
154+
*/
149155
bool handleErrors(const simplecpp::OutputList &outputList, bool throwError);
150156

151157
static void simplifyPragmaAsmPrivate(simplecpp::TokenList &tokenList);

0 commit comments

Comments
 (0)