Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

\n is a valid preprocessing token #395

Open
jyn514 opened this issue May 1, 2020 · 2 comments
Open

\n is a valid preprocessing token #395

jyn514 opened this issue May 1, 2020 · 2 comments
Labels
bug Something isn't working preprocessor Issue in the preprocessor (probably cycle detection)
Milestone

Comments

@jyn514
Copy link
Owner

jyn514 commented May 1, 2020

Expected behavior

\n should be a valid preprocessor token so that it can be used in token pasting.
Found in /usr/include/sys/sysmacros.h. glibc is cursed.

Code

#define __SYSMACROS_DM(symbol) __SYSMACROS_DM1 (a b \n c)
#define __SYSMACROS_DM1(...) __glibc_macro_warning (#__VA_ARGS__)
__SYSMACROS_DM(a)
<stdin>:1:53 error: invalid token: unknown token: '\'
#define __SYSMACROS_DM(symbol) __SYSMACROS_DM1 (a b \n c)
                                                    ^

Output from clang -E:

__glibc_macro_warning ("a b \n c")

http://port70.net/~nsz/c/c11/n1570.html#6.4p3:

The categories of preprocessing tokens are: header names, identifiers, preprocessing numbers, character constants, string literals, punctuators, and single non-white-space characters that do not lexically match the other preprocessing token categories.

"If it's not whitespace, it's valid!" 🤦

@jyn514 jyn514 added bug Something isn't working preprocessor Issue in the preprocessor (probably cycle detection) labels May 1, 2020
@jyn514 jyn514 added this to the Run glibc milestone May 1, 2020
@jyn514
Copy link
Owner Author

jyn514 commented May 1, 2020

Fixing this long-term will be difficult, since \n is not a valid lexer token. I came up with an idea to split apart the lexer and preprocessor: preprocessor tokens are a superset of lexer tokens, but should be turned into lexer tokens by the time preprocessing finishes. Then there would be two options for a lexer: either the preprocessor or the lexer, but not both.

@hdamron17
Copy link
Collaborator

So is '

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working preprocessor Issue in the preprocessor (probably cycle detection)
Projects
None yet
Development

No branches or pull requests

2 participants