llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.16k stars 12.03k forks source link

Incorrect paste of placeholder with '##'. #116720

Open mrolle45 opened 3 hours ago

mrolle45 commented 3 hours ago

If a macro definition contains x ## ## and x is a parameter and the macro is called with an empty argument, then clang ignores the x ## when expanding the macro, leaving just ##. It then tries to paste ## after the preceding token! To avoid confusion, let paste be a ## token in an odd numbered position in a sequence of ## tokens, and let hashhash be a ## token in an even numbered position in a sequence of ## tokens. In most cases, clang treats hashhash as an ordinary token, not as a paste operator. But if x is a parameter for an empty argument, foo x ## bar foo x ## ## bar foo x ## ## ## bar is expanded to foo bar foo ## bar foo ## ## bar. clang interprets this as foo bar foopastebar foohashhashbar. The result output is foo bar foobar foo ## bar.

llvmbot commented 3 hours ago

@llvm/issue-subscribers-clang-frontend

Author: None (mrolle45)

If a macro definition contains `x ## ##` and `x` is a parameter and the macro is called with an empty argument, then clang ignores the `x ##` when expanding the macro, leaving just `##`. It then tries to paste `##` after the preceding token! To avoid confusion, let **paste** be a ## token in an odd numbered position in a sequence of ## tokens, and let **hashhash** be a ## token in an even numbered position in a sequence of ## tokens. In most cases, clang treats **hashhash** as an ordinary token, not as a paste operator. But if `x` is a parameter for an empty argument, `foo x ## bar foo x ## ## bar foo x ## ## ## bar ` is expanded to `foo bar foo ## bar foo ## ## bar`. clang interprets this as `foo bar foo `**paste**` bar foo `**hashhash**` bar`. The result output is `foo bar foobar foo ## bar`.