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

Clang-format breaks stringize as wide string macro #17518

Closed
BillyONeal opened this issue Sep 7, 2013 · 4 comments
Closed

Clang-format breaks stringize as wide string macro #17518

BillyONeal opened this issue Sep 7, 2013 · 4 comments
Labels
bugzilla Issues migrated from bugzilla clang-format

Comments

@BillyONeal
Copy link
Contributor

Bugzilla Link 17144
Resolution FIXED
Resolved on Sep 13, 2013 10:10
Version trunk
OS Windows NT
CC @zygoloid

Extended Description

This bug is similar to bug 17122 ( http://llvm.org/bugs/show_bug.cgi?id=17122 ), except the bug happens in the macro, not in the use of the macro.

Input:

#define STRINGIZE_LITERAL(x) L#x
#define STRINGIZE(x) STRINGIZE_LITERAL(x)

Output:

#define STRINGIZE_LITERAL(x) L #x
#define STRINGIZE(x) STRINGIZE_LITERAL(x)

(Note the space inserted after the L)

Unfortunately I don't know anything about the preprocessor, so I don't know if this STRINGIZE macro is just bogus.

Example:

C:\Users\billy_000\Desktop>type example.cpp
#include

#define HELLO_VERSION 1.0.0
#define STRINGIZE_LITERAL(x) L#x
#define STRINGIZE(x) STRINGIZE_LITERAL(x)

int main()
{
wchar_t const* message = L"Hello, my version is " STRINGIZE(HELLO_VERSION) L"\n";
fwprintf(stdout, message);
}

C:\Users\billy_000\Desktop>cl /EHsc /W4 /nologo example.cpp && example.exe
example.cpp
Hello, my version is 1.0.0

C:\Users\billy_000\Desktop>clang++ -o example_clang.exe example.cpp
example.cpp:9:54: error: expected ';' at end of declaration
wchar_t const* message = L"Hello, my version is " STRINGIZE(HELLO_VERSION) L"\n";
^
;
1 error generated.

C:\Users\billy_000\Desktop>clang-format -style=LLVM -i example.cpp

C:\Users\billy_000\Desktop>type example.cpp
#include

#define HELLO_VERSION 1.0.0
#define STRINGIZE_LITERAL(x) L #x
#define STRINGIZE(x) STRINGIZE_LITERAL(x)

int main() {
wchar_t const *message =
L"Hello, my version is " STRINGIZE(HELLO_VERSION)L"\n";
fwprintf(stdout, message);
}

C:\Users\billy_000\Desktop>cl /EHsc /W4 /nologo example.cpp && example.exe
example.cpp
example.cpp(9) : error C2146: syntax error : missing ';' before identifier 'L'
example.cpp(9) : error C2065: 'L' : undeclared identifier
example.cpp(9) : error C2143: syntax error : missing ';' before 'string'
example.cpp(9) : error C2308: concatenating mismatched strings
Concatenating narrow "1.0.0" with wide "
"

C:\Users\billy_000\Desktop>clang++ -o example_clang.exe example.cpp
example.cpp:9:31: error: expected ';' at end of declaration
L"Hello, my version is " STRINGIZE(HELLO_VERSION)L"\n";
^
;
1 error generated.

Thanks!

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 10, 2013

Fixed in r190408.

@zygoloid
Copy link
Mannequin

zygoloid mannequin commented Sep 13, 2013

#define STRINGIZE_LITERAL(x) L#x
#define STRINGIZE(x) STRINGIZE_LITERAL(x)
[...]
Unfortunately I don't know anything about the preprocessor, so I don't know
if this STRINGIZE macro is just bogus.

It is. With a correct preprocessor, this will always produce two separate tokens: an 'L' token and a string literal.

@llvmbot
Copy link
Collaborator

llvmbot commented Sep 13, 2013

Yep, the correct implementation is:
#define STRINGIZE_LITERAL(x) L ## #x

But L#x implementation seems to be relatively wide-spread due to Visual C++ allowing it. I'd keep the fix so that we don't break existing code.

@BillyONeal
Copy link
Contributor Author

Thanks very much guys :) I'll fix the STRINGIZE_LITERAL macro too.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 9, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla clang-format
Projects
None yet
Development

No branches or pull requests

2 participants