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

Wrong token pasting/concatenation when token contains spaces #124

Open
versat opened this issue Apr 12, 2018 · 5 comments
Open

Wrong token pasting/concatenation when token contains spaces #124

versat opened this issue Apr 12, 2018 · 5 comments
Assignees

Comments

@versat
Copy link
Contributor

versat commented Apr 12, 2018

Stumbled over this issue when i added a define for the Qt macro Q_D(), see Cppcheck ticket https://trac.cppcheck.net/ticket/8479.
For this C code:

#define CONCAT(tok) tok##suffix

CONCAT(Test);
CONCAT(const Test);

gcc outputs:

$ gcc -E token_pasting_with_space.c
# 1 "token_pasting_with_space.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "token_pasting_with_space.c"


Testsuffix;
const Testsuffix;

simplecpp outputs:

$ ./simplecpp token_pasting_with_space.c


Testsuffix ;
constTestsuffix ;

The space between "const" and "Test" is removed.
Not sure what the standard says about such a case but since Qt and gcc are widely used i guess it should work like Qt expects it and gcc handles it to not break things.

versat added a commit to versat/cppcheck that referenced this issue Dec 7, 2018
Reference:
https://wiki.qt.io/D-Pointer#Q_DECLARE_PRIVATE_and_Q_DECLARE_PUBLIC
As long as trac ticket 8479 is not fixed i guess it is better to disable this macro.
See also danmar/simplecpp#124
versat pushed a commit to versat/simplecpp that referenced this issue Dec 7, 2018
@danmar
Copy link
Owner

danmar commented Aug 3, 2019

I have the feeling we merge "const Test suffix" into 1 token by intention.

You did not say if gcc preprocessor outputs 1 single token or 2.. you only showed the text output.

@versat
Copy link
Contributor Author

versat commented Aug 3, 2019

I have not found a way to get gcc printing the tokens.
But clang has an option to output tokens:

$ clang test.c -Xclang -dump-tokens
identifier 'Testsuffix'  [StartOfLine]  Loc=<test.c:3:1 <Spelling=<scratch space>:2:1>>
semi ';'                Loc=<test.c:3:13>
const 'const'    [StartOfLine]  Loc=<test.c:4:1 <Spelling=test.c:4:8>>
identifier 'Testsuffix'  [LeadingSpace] Loc=<test.c:4:1 <Spelling=<scratch space>:3:1>>
semi ';'                Loc=<test.c:4:19>
eof ''          Loc=<test.c:4:20>

clang seems to see two tokens.

@danmar
Copy link
Owner

danmar commented Aug 4, 2019

Nice! ok I agree we need to output 2 separate tokens. Well.. I still have the feeling we merge them by intention.

@danmar
Copy link
Owner

danmar commented Aug 4, 2019

I did not see any hashhash test that required merging. Only a hash test. So maybe it won't break anything to fix this.

@danmar danmar self-assigned this Sep 4, 2020
@orbitcowboy
Copy link
Contributor

This has been tested with ad9b49d and prints now the expected result:

$ ./simplecpp test.c


Testsuffix ;
const Testsuffix ;

@danmar Do you have a test case in place already?

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

3 participants