Skip to content

Commit fb1b3be

Browse files
committed
Fix #769 (PORTABILITY_NO_EOF_NEWLINE is reported for C++ headers because included files are tokenized with an empty DUI)
1 parent e5cb618 commit fb1b3be

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎simplecpp.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3926,7 +3926,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
39263926
return {id_it->second, false};
39273927
}
39283928

3929-
auto *const data = new FileData {path, TokenList(path, filenames, {}, outputList)};
3929+
auto *const data = new FileData {path, TokenList(path, filenames, dui, outputList)};
39303930

39313931
if (dui.removeComments)
39323932
data->tokens.removeComments();

‎test.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4504,6 +4504,45 @@ static void include11() // #669 - -include with preprocess()
45044504
ASSERT_EQUALS("Can not open include file 'missing.h' that is explicitly included.", outputList.begin()->msg);
45054505
}
45064506

4507+
static void include12() // #769 - No "No newline at end of file" warning in C++ header
4508+
{
4509+
const std::string header = "testrunner_769.h";
4510+
{
4511+
std::ofstream f(header, std::ios::binary);
4512+
f << "int x;"; // <- no newline at end of file
4513+
}
4514+
4515+
const char code_c[] = "#include \"testrunner_769.h\"\n";
4516+
4517+
{
4518+
std::vector<std::string> files;
4519+
const simplecpp::TokenList rawtokens = makeTokenList(code_c, files, "769.cpp");
4520+
simplecpp::FileDataCache cache;
4521+
simplecpp::TokenList out(files);
4522+
simplecpp::OutputList outputList;
4523+
simplecpp::DUI dui;
4524+
dui.std = "c++11";
4525+
simplecpp::preprocess(out, rawtokens, files, cache, dui, &outputList);
4526+
simplecpp::cleanup(cache);
4527+
ASSERT_EQUALS("", toString(outputList));
4528+
}
4529+
4530+
{
4531+
std::vector<std::string> files;
4532+
const simplecpp::TokenList rawtokens = makeTokenList(code_c, files, "769.c");
4533+
simplecpp::FileDataCache cache;
4534+
simplecpp::TokenList out(files);
4535+
simplecpp::OutputList outputList;
4536+
simplecpp::DUI dui;
4537+
dui.std = "c11";
4538+
simplecpp::preprocess(out, rawtokens, files, cache, dui, &outputList);
4539+
simplecpp::cleanup(cache);
4540+
ASSERT_EQUALS("file1,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
4541+
}
4542+
4543+
std::remove(header.c_str());
4544+
}
4545+
45074546
static void readfile_nullbyte()
45084547
{
45094548
const char code[] = "ab\0cd\n";
@@ -6104,6 +6143,7 @@ static void runTests(int argc, char **argv, Input input)
61046143
TEST_CASE(include9); // #include MACRO
61056144
TEST_CASE(include10); // -include with load()
61066145
TEST_CASE(include11); // -include with preprocess()
6146+
TEST_CASE(include12); // #769
61076147

61086148
TEST_CASE(multiline1);
61096149
TEST_CASE(multiline2);

0 commit comments

Comments
 (0)