On 05/02/14 00:42, Richard Smith wrote:
> Here's an easy test:
> 
> #define foo() bar() b
> #define bar()
> a
> foo()
> c
> 
> This should produce:
> 
> # 1 "..."
> 
> 
> a
>  b
> c
> 
> ... but currently produces:
> 
> #1 "..."
> 
> 
> a b
> 
> c

Thanks Richard, I don't know how I managed to miss that.

Setting Token::StartOfLine as mentioned gets this right in the lexer.
However, even though the b token in the output has both
Token::StartOfLine and Token::LeadingSpace set,
PrintPPOutputPPCallbacks::HandleFirstTokOnLine uses the column number to
determine whether to print spaces, not the flag. As a result, the output
becomes not

a
 b
c

but

a
b
c

Given that the lexer behaviour is now right, would it be okay to leave
this as it is, and only add a test to check that b is on a separate
line, like attached? (I used first/second/third instead of a/b/c in an
attempt to get FileCheck to print a better suggestion when the test
fails, but it neither helps nor hurts.)

Cheers,
Harald van Dijk
Set Token::StartOfLine after empty macro expansion

Token::StartOfLine did not get set after an empty macro expansion where
the expansion takes place at the start of a line, but not setting that
can cause the tokens to be incorrectly put on the previous line.
---
 lib/Lex/TokenLexer.cpp                 | 1 +
 test/Preprocessor/macro_expand_empty.c | 9 ++++++++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/Lex/TokenLexer.cpp b/lib/Lex/TokenLexer.cpp
index 9913f30..543ca92 100644
--- a/lib/Lex/TokenLexer.cpp
+++ b/lib/Lex/TokenLexer.cpp
@@ -474,6 +474,7 @@ bool TokenLexer::Lex(Token &Tok) {
   } else {
     // If this is not the first token, we may still need to pass through
     // leading whitespace if we've expanded a macro.
+    if (AtStartOfLine) Tok.setFlag(Token::StartOfLine);
     if (HasLeadingSpace) Tok.setFlag(Token::LeadingSpace);
   }
   AtStartOfLine = false;
diff --git a/test/Preprocessor/macro_expand_empty.c b/test/Preprocessor/macro_expand_empty.c
index 3fb6394..c3fc4f2 100644
--- a/test/Preprocessor/macro_expand_empty.c
+++ b/test/Preprocessor/macro_expand_empty.c
@@ -1,4 +1,5 @@
-// RUN: %clang_cc1 -fsyntax-only %s
+// RUN: %clang_cc1 -E %s | FileCheck --strict-whitespace %s
+
 // Check that this doesn't crash
 
 #define IDENTITY1(x) x
@@ -12,3 +13,9 @@
 #define IDENTITY9(x) IDENTITY8(x) IDENTITY8(x) IDENTITY8(x) IDENTITY8(x)
 #define IDENTITY0(x) IDENTITY9(x) IDENTITY9(x) IDENTITY9(x) IDENTITY9(x)
 IDENTITY0()
+
+#define FOO() BAR() second
+#define BAR()
+first // CHECK: {{^}}first{{$}}
+FOO() // CHECK: second
+third // CHECK: {{^}}third{{$}}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to