Skip to content

Commit 95696ea

Browse files
authored
Catch preprocessor errors possibly issued during loading files (#2430)
* Also catch preprocessor errors possibly issued during loading files Currently only errors that are issued during preprocessing are caught. * Bump simplecpp, implement suggestions Use return value checking instead of catching an exception for calling Preprocessor::loadFiles(). Handle new enum value simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND where the corresponding enum is used in Cppcheck. * Use "noloc" location if an explicit include can not be opened
1 parent d538315 commit 95696ea

5 files changed

Lines changed: 48 additions & 23 deletions

File tree

externals/simplecpp/simplecpp.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2426,8 +2426,16 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
24262426
continue;
24272427

24282428
std::ifstream fin(filename.c_str());
2429-
if (!fin.is_open())
2429+
if (!fin.is_open()) {
2430+
if (outputList) {
2431+
simplecpp::Output err(fileNumbers);
2432+
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
2433+
err.location = Location(fileNumbers);
2434+
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
2435+
outputList->push_back(err);
2436+
}
24302437
continue;
2438+
}
24312439

24322440
TokenList *tokenlist = new TokenList(fin, fileNumbers, filename, outputList);
24332441
if (!tokenlist->front()) {

externals/simplecpp/simplecpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ namespace simplecpp {
167167
INCLUDE_NESTED_TOO_DEEPLY,
168168
SYNTAX_ERROR,
169169
PORTABILITY_BACKSLASH,
170-
UNHANDLED_CHAR_ERROR
170+
UNHANDLED_CHAR_ERROR,
171+
EXPLICIT_INCLUDE_NOT_FOUND
171172
} type;
172173
Location location;
173174
std::string msg;

lib/cppcheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
275275
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
276276
case simplecpp::Output::SYNTAX_ERROR:
277277
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
278+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
278279
err = true;
279280
break;
280281
case simplecpp::Output::WARNING:
@@ -299,7 +300,8 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
299300
}
300301
}
301302

302-
preprocessor.loadFiles(tokens1, files);
303+
if (!preprocessor.loadFiles(tokens1, files))
304+
return mExitCode;
303305

304306
if (!mSettings.plistOutput.empty()) {
305307
std::string filename2;

lib/preprocessor.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ static bool hasErrors(const simplecpp::OutputList &outputList)
559559
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
560560
case simplecpp::Output::SYNTAX_ERROR:
561561
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
562+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
562563
return true;
563564
case simplecpp::Output::WARNING:
564565
case simplecpp::Output::MISSING_HEADER:
@@ -569,12 +570,36 @@ static bool hasErrors(const simplecpp::OutputList &outputList)
569570
return false;
570571
}
571572

573+
void Preprocessor::handleErrors(const simplecpp::OutputList& outputList, bool throwError)
574+
{
575+
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
576+
reportOutput(outputList, showerror);
577+
if (throwError) {
578+
for (const simplecpp::Output& output : outputList) {
579+
switch (output.type) {
580+
case simplecpp::Output::ERROR:
581+
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
582+
case simplecpp::Output::SYNTAX_ERROR:
583+
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
584+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
585+
throw output;
586+
case simplecpp::Output::WARNING:
587+
case simplecpp::Output::MISSING_HEADER:
588+
case simplecpp::Output::PORTABILITY_BACKSLASH:
589+
break;
590+
};
591+
}
592+
}
593+
}
572594

573-
void Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files)
595+
bool Preprocessor::loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files)
574596
{
575597
const simplecpp::DUI dui = createDUI(mSettings, emptyString, files[0]);
576598

577-
mTokenLists = simplecpp::load(rawtokens, files, dui, nullptr);
599+
simplecpp::OutputList outputList;
600+
mTokenLists = simplecpp::load(rawtokens, files, dui, &outputList);
601+
handleErrors(outputList, false);
602+
return !hasErrors(outputList);
578603
}
579604

580605
void Preprocessor::removeComments()
@@ -614,23 +639,7 @@ simplecpp::TokenList Preprocessor::preprocess(const simplecpp::TokenList &tokens
614639
simplecpp::TokenList tokens2(files);
615640
simplecpp::preprocess(tokens2, tokens1, files, mTokenLists, dui, &outputList, &macroUsage);
616641

617-
const bool showerror = (!mSettings.userDefines.empty() && !mSettings.force);
618-
reportOutput(outputList, showerror);
619-
if (throwError && hasErrors(outputList)) {
620-
for (const simplecpp::Output &output : outputList) {
621-
switch (output.type) {
622-
case simplecpp::Output::ERROR:
623-
case simplecpp::Output::INCLUDE_NESTED_TOO_DEEPLY:
624-
case simplecpp::Output::SYNTAX_ERROR:
625-
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
626-
throw output;
627-
case simplecpp::Output::WARNING:
628-
case simplecpp::Output::MISSING_HEADER:
629-
case simplecpp::Output::PORTABILITY_BACKSLASH:
630-
break;
631-
};
632-
}
633-
}
642+
handleErrors(outputList, throwError);
634643

635644
tokens2.removeComments();
636645

@@ -717,6 +726,9 @@ void Preprocessor::reportOutput(const simplecpp::OutputList &outputList, bool sh
717726
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
718727
error(out.location.file(), out.location.line, out.msg);
719728
break;
729+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
730+
error(emptyString, 0, out.msg);
731+
break;
720732
};
721733
}
722734
}

lib/preprocessor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ class CPPCHECKLIB Preprocessor {
9898

9999
std::set<std::string> getConfigs(const simplecpp::TokenList &tokens) const;
100100

101-
void loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files);
101+
void handleErrors(const simplecpp::OutputList &outputList, bool throwError);
102+
103+
bool loadFiles(const simplecpp::TokenList &rawtokens, std::vector<std::string> &files);
102104

103105
void removeComments();
104106

0 commit comments

Comments
 (0)