Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ int main(int argc, char **argv)
found = true;
}
break;
case 'P':
if (std::strcmp(arg, "-P")==0) {
dui.ignoreLineDirectives = true;
found = true;
}
break;
}
if (!found) {
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
Expand Down Expand Up @@ -187,6 +193,7 @@ int main(int argc, char **argv)
std::cout << " -e Output errors only." << std::endl;
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
std::cout << " -l Print lines numbers." << std::endl;
std::cout << " -P Ignore #line / # <num> \"file\" directives (do not let them affect location tracking)." << std::endl;
return 0;
}

Expand Down
5 changes: 3 additions & 2 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,8 @@ void simplecpp::TokenList::readfile(Stream &stream, const std::string &filename,
else
fileindex = location.fileIndex;

lineDirective(fileindex, line, location);
if (!dui.ignoreLineDirectives)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve

I don't think this implements what #174 asks for. gcc -E -P suppresses the linemarkers in the output; it does not ignore #line directives in the input. In simplecpp the output markers come from TokenList::stringify(), which writes #line N "file" whenever the file changes or the line goes backwards. Those are untouched by this PR, so includes still produce markers:

$ cat a.h
int h;
$ cat m.c
#include "a.h"
int x;
#line 10 "foo.c"
int y;
$ ./simplecpp -P m.c

#line 1 "a.h"
int h ;
#line 2 "m.c"
int x ;

int y ;

Ignoring input #line also changes semantics in a way gcc's -P does not: __LINE__/__FILE__ and the locations in error messages no longer follow the #line directive (with gcc -E -P, #line 10 "foo.c" still affects __LINE__/__FILE__).

Suggestion: make this an output option instead, e.g. a parameter to stringify() (or a separate print loop in main.cpp) that skips writing the #line markers, and leave the tokenizer/DUI unchanged.

lineDirective(fileindex, line, location);
}

continue;
Expand Down Expand Up @@ -3223,7 +3224,7 @@ std::pair<simplecpp::FileData *, bool> simplecpp::FileDataCache::tryload(FileDat
return {id_it->second, false};
}

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve

Passing dui here instead of {} changes behaviour for all library users, not just -P: readfile() uses dui.std for the #line portability diagnostics, so headers will now get std-specific messages they didn't get before. That may be a good change, but it is unrelated to -P. If the option becomes output-only (see the other comment), this change isn't needed. If it is kept, it would be good to mention it in the PR description and add a test for it.


if (dui.removeComments)
data->tokens.removeComments();
Expand Down
3 changes: 2 additions & 1 deletion simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ namespace simplecpp {

/**
* Command line preprocessor settings.
* On the command line these are configured by -D, -U, -I, --include, -std
* On the command line these are configured by -D, -U, -I, --include, -std, -P
*/
struct SIMPLECPP_LIB DUI {
DUI() = default;
Expand All @@ -276,6 +276,7 @@ namespace simplecpp {
std::string std;
bool clearIncludeCache{};
bool removeComments{}; /** remove comment tokens from included files */
bool ignoreLineDirectives{}; /** -P: do not let #line directives affect location tracking */
};

/** List of tokens. */
Expand Down
14 changes: 14 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,19 @@ static void location12()
}


static void location13()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve

This test only covers a #line directive in the main file. The more common case for -P (e.g. creduce input) is output that crosses #include boundaries, and that currently still prints #line markers. A test that uses an include, and checks that the stringified output has no #line lines, would catch that.

{
simplecpp::DUI dui;
dui.ignoreLineDirectives = true;

const char code[] =
"#line 3 \"file.c\"\n"
"__LINE__ __FILE__\n";
ASSERT_EQUALS("\n"
"2 \"\"",
preprocess(code, dui));
}

static void missingHeader1()
{
const char code[] = "#include \"notexist.h\"\n";
Expand Down Expand Up @@ -4750,6 +4763,7 @@ static void runTests(int argc, char **argv, Input input)
TEST_CASE(location10);
TEST_CASE(location11);
TEST_CASE(location12);
TEST_CASE(location13);

TEST_CASE(missingHeader1);
TEST_CASE(missingHeader2);
Expand Down
Loading