LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 17144 - Clang-format breaks stringize as wide string macro
Summary: Clang-format breaks stringize as wide string macro
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Formatter (show other bugs)
Version: trunk
Hardware: PC Windows NT
: P normal
Assignee: Alexander Kornienko
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-09-07 02:13 PDT by Billy O'Neal
Modified: 2013-09-13 10:10 PDT (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Billy O'Neal 2013-09-07 02:13:44 PDT
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 <cstdio>

#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 <cstdio>

#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!
Comment 1 Alexander Kornienko 2013-09-10 08:42:43 PDT
Fixed in r190408.
Comment 2 Richard Smith 2013-09-12 22:41:24 PDT
(In reply to comment #0)
> #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.
Comment 3 Alexander Kornienko 2013-09-13 06:21:16 PDT
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.
Comment 4 Billy O'Neal 2013-09-13 10:10:40 PDT
Thanks very much guys :) I'll fix the STRINGIZE_LITERAL macro too.