Skip to content

Commit

Permalink
do not treat directories like regular files in existence checks
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Feb 16, 2024
1 parent 6fc3ad7 commit 3ead71c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ int main(int argc, char **argv)
std::cout << "error: could not open file '" << filename << "'" << std::endl;
std::exit(1);
}
if (!simplecpp::isFile(filename)) {
std::cout << "error: could not open file '" << filename << "' - not a regular file" << std::endl;
std::exit(1);
}
rawtokens = new simplecpp::TokenList(f, files,filename,&outputList);
}
else {
Expand Down
29 changes: 16 additions & 13 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3026,9 +3026,11 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
if (nonExistingFilesCache.contains(simplePath))
return ""; // file is known not to exist, skip expensive file open call
#endif
f.open(simplePath.c_str());
if (f.is_open())
return simplePath;
if (simplecpp::isFile(simplePath)) {
f.open(simplePath.c_str());
if (f.is_open())
return simplePath;
}
#ifdef SIMPLECPP_WINDOWS
nonExistingFilesCache.add(simplePath);
#endif
Expand Down Expand Up @@ -3131,18 +3133,19 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
if (ret.find(filename) != ret.end())
continue;

std::ifstream fin(filename.c_str());
if (!fin.is_open()) {
if (outputList) {
simplecpp::Output err(filenames);
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
err.location = Location(filenames);
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
outputList->push_back(err);
{
std::ifstream fin(filename.c_str());
if (!fin.is_open() || !isFile(filename)) {
if (outputList) {
simplecpp::Output err(filenames);
err.type = simplecpp::Output::EXPLICIT_INCLUDE_NOT_FOUND;
err.location = Location(filenames);
err.msg = "Can not open include file '" + filename + "' that is explicitly included.";
outputList->push_back(err);
}
continue;
}
continue;
}
fin.close();

TokenList *tokenlist = new TokenList(filename, filenames, outputList);
if (!tokenlist->front()) {
Expand Down
24 changes: 24 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,26 @@ static void missingHeader3()
ASSERT_EQUALS("", toString(outputList));
}

#ifndef _WIN32
static void missingHeader4()
{
// this is a directory
const char code[] = "#include \"/\"\n";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/\"\n", toString(outputList));
}

static void missingHeader5()
{
// this is a directory
const char code[] = "#include \"/usr\"\n";
simplecpp::OutputList outputList;
ASSERT_EQUALS("", preprocess(code, &outputList));
ASSERT_EQUALS("file0,1,missing_header,Header not found: \"/usr\"\n", toString(outputList));
}
#endif

static void nestedInclude()
{
const char code[] = "#include \"test.h\"\n";
Expand Down Expand Up @@ -2867,6 +2887,10 @@ int main(int argc, char **argv)
TEST_CASE(missingHeader1);
TEST_CASE(missingHeader2);
TEST_CASE(missingHeader3);
#ifndef _WIN32
TEST_CASE(missingHeader4);
TEST_CASE(missingHeader5);
#endif
TEST_CASE(nestedInclude);
TEST_CASE(systemInclude);

Expand Down

0 comments on commit 3ead71c

Please sign in to comment.