From bd89fdcbe6ad28d5052ec7295d4afefeaf4a4135 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 9 Jul 2013 01:00:29 +0000 Subject: [PATCH] Don't give # and ## special treatment when in -traditional-cpp mode. Patch by Austin Seipp! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185896 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Lex/PPDirectives.cpp | 12 ++++++++++++ test/Preprocessor/traditional-cpp.c | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index 026a7e7228..cb56615ddc 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -1905,6 +1905,18 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok, continue; } + // If we're in -traditional mode, then we should ignore stringification + // and token pasting. Mark the tokens as unknown so as not to confuse + // things. + if (getLangOpts().TraditionalCPP) { + Tok.setKind(tok::unknown); + MI->AddTokenToBody(Tok); + + // Get the next token of the macro. + LexUnexpandedToken(Tok); + continue; + } + if (Tok.is(tok::hashhash)) { // If we see token pasting, check if it looks like the gcc comma diff --git a/test/Preprocessor/traditional-cpp.c b/test/Preprocessor/traditional-cpp.c index 4c4633e039..b8bb99d93c 100644 --- a/test/Preprocessor/traditional-cpp.c +++ b/test/Preprocessor/traditional-cpp.c @@ -88,3 +88,20 @@ a b c in skipped block Preserve URLs: http://clang.llvm.org /* CHECK: {{^}}Preserve URLs: http://clang.llvm.org{{$}} */ + +/* The following tests ensure we ignore # and ## in macro bodies */ + +#define FOO_NO_STRINGIFY(a) test(# a) +FOO_NO_STRINGIFY(foobar) +/* CHECK: {{^}}test(# foobar){{$}} + */ + +#define FOO_NO_PASTE(a, b) test(b##a) +FOO_NO_PASTE(foo,bar) +/* CHECK {{^}}test(bar##foo){{$}} + */ + +#define BAR_NO_STRINGIFY(a) test(#a) +BAR_NO_STRINGIFY(foobar) +/* CHECK: {{^}}test(#foobar){{$}} + */ -- 2.40.0