rui314 / chibicc

A small C compiler
MIT License
9.68k stars 881 forks source link

Non-directives incorrectly recognized as directives #43

Open kukrimate opened 3 years ago

kukrimate commented 3 years ago

The following code:

#define x
x#define y z
y

Should expand to:

#define y z
y

But instead chibicc expands it to:

z

Rationale: ISO/IEC 9899:1999 6.10:

A preprocessing directive consists of a sequence of preprocessing tokens that satisfies the following constraints: The first token in the sequence is a # preprocessing token that (at the start of translation phase 4) is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character.

orogenic commented 3 years ago

EDIT: misunderstood the original comment sorry. Leaving for additional clarifying reference to standard.

The first token in the sequence is a # preprocessing token ... that follows white space containing at least one new-line character.

x is not white space. Maybe you are assuming x is expanded. It is not. The second line x#define y z is not a preprocessing directive.

Rationale: [ISO/IEC 9899:2017, 6.10, §8]

the sequence of preprocessing tokens on the second line is not a preprocessing directive, because it does not begin with a # at the start of translation phase 4, even though it will do so after the macro EMPTY has been replaced.

orogenic commented 3 years ago

Wording of the issue title "non-directives" is possibly misleading.

[ISO/IEC 9899:2017, 6.10, §11n174]

Despite the name, a non-directive is a preprocessing directive.