PATCH: In -traditional mode, ignore token pasting and stringification (PR16371)

Austin Seipp aseipp at
Fri Jul 5 17:57:54 PDT 2013

Well, just to be clear, there's absolutely no intention of completely
emulating -traditional's behavior. We don't need full emulation, just
the behavior of ignoring # and ## inside function-like macro bodies,
which is provided by -traditional (Haskell has liberal identifier
syntax, so # can absolutely occur in a macro body in many positions.)

The example I brought up here about expansions in literal quotations
was to point out that, this patch 'implements' behavior found in GCC's
-traditional mode, but with an exception. Of course, that's really all
Clang's -traditional mode is anyway: a small collection of *some* of
GCCs behaviors, with caveats even at that. So, that considered I think
this is fine: the patch has relatively small impact/scope, and is
pretty simple on top of that.

On Fri, Jul 5, 2013 at 6:50 PM, Eli Friedman <eli.friedman at> wrote:
> On Fri, Jul 5, 2013 at 3:44 PM, Austin Seipp <aseipp at> wrote:
>> Hello,
>> Attached is a patch that makes the preprocessor ignore token pasting
>> (##) and stringification (#) when in -traditional mode. This makes it
>> behave more like GCC[1].
>> This change fixes PR16371, and is needed for Clang to function
>> properly as a preprocessor for Haskell (in the Glasgow Haskell
>> Compiler.) If you're curious and look at the bug, I made some
>> incorrect assumptions about the behavior of -traditional for GCC (and
>> attached a bad patch,) but this fixes the problem in the principled
>> way. And the patch is simpler, which is good too.
>> There is something this patch does not handle compared to GCC. For
>> traditional macros, GCC will expand parameter identifiers *inside*
>> string bodies. This allows you to imitate these operators. So this
>> code:
>> ---
>> #define foo(a) test(#a)
>> foo(123)
>> ---
>> would become the following under -traditional:
>> ---
>> #define foo(a) test("a")
>> foo(123)
>> ---
>> and 'a' would be expanded to '123' inside the string. This would even
>> occur in a case like:
>> ---
>> #define foo(a) test("this is a test")
>> ---
>> under -traditional mode.
>> This patch does not attempt to implement this behavior; we don't need
>> it in GHC, and Clang's -traditional is already a subset of what GCC
>> supports anyway, and as a result it's pretty simple.
> Completely emulating -traditional would be crazy with our current lexer and
> parser implementation; I think the only implementation we would accept would
> be implementing it from scratch, independent from the current Lexer.  (There
> wouldn't be much code duplication given how different the semantics of
> traditional preprocessing are, and we could simplify the implementation by
> assuming it's only used for preprocessed output.)
> Richard, do you have an opinion on this patch?  You've expressed some
> concerns about -traditional-cpp before.
> -Eli

Austin - PGP: 4096R/0x91384671

More information about the cfe-commits mailing list