Skip to content

Commit

Permalink
do not add empty filename to files in preprocess()
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave committed Apr 23, 2024
1 parent ad9b49d commit b58cc36
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
19 changes: 11 additions & 8 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3274,6 +3274,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
sizeOfType.insert(std::make_pair("double *", sizeof(double *)));
sizeOfType.insert(std::make_pair("long double *", sizeof(long double *)));

// use a dummy vector for the macros because as this is not part of the file and would add an empty entry - e.g. /usr/include/poll.h
std::vector<std::string> dummy;

const bool hasInclude = (dui.std.size() == 5 && dui.std.compare(0,3,"c++") == 0 && dui.std >= "c++17");
MacroMap macros;
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
Expand All @@ -3285,26 +3288,26 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
continue;
const std::string lhs(macrostr.substr(0,eq));
const std::string rhs(eq==std::string::npos ? std::string("1") : macrostr.substr(eq+1));
const Macro macro(lhs, rhs, files);
const Macro macro(lhs, rhs, dummy);
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
}

macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", files)));
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", files)));
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", files)));
macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", dummy)));
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", dummy)));
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", dummy)));
struct tm ltime = {};
getLocaltime(ltime);
macros.insert(std::make_pair("__DATE__", Macro("__DATE__", getDateDefine(&ltime), files)));
macros.insert(std::make_pair("__TIME__", Macro("__TIME__", getTimeDefine(&ltime), files)));
macros.insert(std::make_pair("__DATE__", Macro("__DATE__", getDateDefine(&ltime), dummy)));
macros.insert(std::make_pair("__TIME__", Macro("__TIME__", getTimeDefine(&ltime), dummy)));

if (!dui.std.empty()) {
std::string std_def = simplecpp::getCStdString(dui.std);
if (!std_def.empty()) {
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, files)));
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, dummy)));
} else {
std_def = simplecpp::getCppStdString(dui.std);
if (!std_def.empty())
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, files)));
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
}
}

Expand Down
40 changes: 40 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2714,6 +2714,44 @@ static void token()
ASSERT_TOKEN("+22", false, true, false);
}

static void preprocess_files()
{
{
const char code[] = "#define A";
std::vector<std::string> files;

simplecpp::TokenList tokens = makeTokenList(code, files);
ASSERT_EQUALS(1, files.size());
ASSERT_EQUALS("", *files.cbegin());

simplecpp::TokenList tokens2(files);
ASSERT_EQUALS(1, files.size());
ASSERT_EQUALS("", *files.cbegin());

std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens, files, filedata, simplecpp::DUI(), nullptr);
ASSERT_EQUALS(1, files.size());
ASSERT_EQUALS("", *files.cbegin());
}
{
const char code[] = "#define A";
std::vector<std::string> files;

simplecpp::TokenList tokens = makeTokenList(code, files, "test.cpp");
ASSERT_EQUALS(1, files.size());
ASSERT_EQUALS("test.cpp", *files.cbegin());

simplecpp::TokenList tokens2(files);
ASSERT_EQUALS(1, files.size());
ASSERT_EQUALS("test.cpp", *files.cbegin());

std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens, files, filedata, simplecpp::DUI(), nullptr);
ASSERT_EQUALS(1, files.size());
ASSERT_EQUALS("test.cpp", *files.cbegin());
}
}

static void fuzz_crash()
{
{
Expand Down Expand Up @@ -2949,6 +2987,8 @@ int main(int argc, char **argv)

TEST_CASE(token);

TEST_CASE(preprocess_files);

TEST_CASE(fuzz_crash);

return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
Expand Down

0 comments on commit b58cc36

Please sign in to comment.