Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c27e5d4

Browse files
versatdanmar
authored andcommitted
simplecpp.cpp: Issue error when an explicitly included file is not found (cppcheck-opensource#184)
* simplecpp.cpp: Issue error when an explicitly included file is not found If a file is explicitly included for example via command line option `-include=` but can not be opened this error message is issued now. Example: $ ./simplecpp -include=blah.h foo.c int main ( ) { return 0 ; } foo.c:1: missing header: Can not open include file 'blah.h' that is explicitly included for all files. Fixes cppcheck-opensource#183 * Change `error.type` from `MISSING_HEADER` header to `ERROR` `MISSING_HEADER` is only meant as an information to the user, nothing really relevant while this issue reveals a problem with the command line options that should get fixed. * simplecpp.cpp: Fix error message * Add new output error type EXPLICIT_INCLUDE_NOT_FOUND
1 parent 71adfce commit c27e5d4

4 files changed

Lines changed: 17 additions & 2 deletions

File tree

main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ int main(int argc, char **argv)
8686
case simplecpp::Output::UNHANDLED_CHAR_ERROR:
8787
std::cerr << "unhandled char error: ";
8888
break;
89+
case simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND:
90+
std::cerr << "explicit include not found: ";
91+
break;
8992
}
9093
std::cerr << output.msg << std::endl;
9194
}

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()) {

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;

test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ static std::string toString(const simplecpp::OutputList &outputList)
9292
break;
9393
case simplecpp::Output::Type::UNHANDLED_CHAR_ERROR:
9494
ostr << "unhandled_char_error,";
95+
break;
96+
case simplecpp::Output::Type::EXPLICIT_INCLUDE_NOT_FOUND:
97+
ostr << "explicit_include_not_found,";
9598
}
9699

97100
ostr << output.msg << '\n';

0 commit comments

Comments
 (0)