Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing to emit line directive if included file has a #if or #define on the first line #222

Open
njnobles opened this issue Sep 20, 2024 · 2 comments

Comments

@njnobles
Copy link
Contributor

Wave is not emitting the line directive if the first line in the included file is a #if or #ifdef that evaluates true and has content in its block. Likewise, it won't emit the line directive if the first line is a #define. This issue only occurs if there are not multiple #define or #if occurring on subsequent lines.
When using default_preprocessing_hooks, the above applies if the #if/#ifdef/#define is the first non-empty line or non-comment line. Essentially, if the directive immediately precedes the first content emitted from the included file. With eat_whitespace, the directive must be on the very first line of the include file to exhibit the issue.

Not sure if I've explained it very well, but I think an example will help better demonstrate the issue.

Example:
a.cpp

#include "if.h"
#include "def.h"
test

if.h

#if 1 //This must be on the first line for eat_whitespace, can be on 2nd, 3rd etc, for default_preprocessing_hooks as long as it's whitespace before it
if_h
#endif

def.h

#define A
def_h

Resulting preprocessed file:
a.preproc


if_h

def_h

Expected output:

#line 2 "if.h"
if_h
#line 2 "def.h"
def_h

As mentioned, the line directives do get emitted correctly if the include file is something like this:

#define A
#define B
content

So it's only occurring when there's a single directive at the beginning.

@jefftrull
Copy link
Collaborator

Thank you for finding this excellent corner case!

I'm still exploring the issue but I find it's actually more general: any blank line (or in your case, a directive that becomes a blank line) will cause the line directive to be suppressed. For example:

a.cpp

#include "b.h"

b.h


B

Running Wave on a.cpp produces:


B

i.e., no line directive showing that B came from b.h appears.

I think this is because the logic that determines whether to emit a line directive checks only whether the number of newlines (as perceived by the reader of the preprocessed code) is correct, and not whether the filename has changed.

@jefftrull
Copy link
Collaborator

Confirmed! Thanks for the report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants