diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp index a9905f339160..81ccbca7b835 100644 --- a/clang/Lex/Lexer.cpp +++ b/clang/Lex/Lexer.cpp @@ -359,6 +359,10 @@ FinishIdentifier: FormTokenWithChars(Result, CurPtr); Result.SetKind(tok::identifier); + // If we are in raw mode, return this identifier raw. There is no need to + // look up identifier information or attempt to macro expand it. + if (LexingRawMode) return; + // Fill in Result.IdentifierInfo, looking up the identifier in the // identifier table. PP.LookUpIdentifierInfo(Result, IdStart); diff --git a/clang/Lex/MacroExpander.cpp b/clang/Lex/MacroExpander.cpp index d68e36f45e12..d65448759b63 100644 --- a/clang/Lex/MacroExpander.cpp +++ b/clang/Lex/MacroExpander.cpp @@ -508,6 +508,15 @@ void MacroExpander::PasteTokens(LexerToken &Tok) { ++CurToken; Tok = Result; } while (!isAtEnd() && (*MacroTokens)[CurToken].getKind() == tok::hashhash); + + // Now that we got the result token, it will be subject to expansion. Since + // token pasting re-lexes the result token in raw mode, identifier information + // isn't looked up. As such, if the result is an identifier, look up id info. + if (Tok.getKind() == tok::identifier) { + // Look up the identifier info for the token. We disabled identifier lookup + // by saying we're skipping contents, so we need to do this manually. + Tok.SetIdentifierInfo(PP.LookUpIdentifierInfo(Tok)); + } } /// isNextTokenLParen - If the next token lexed will pop this macro off the diff --git a/clang/Lex/Preprocessor.cpp b/clang/Lex/Preprocessor.cpp index f302b6770977..1a6a620dd562 100644 --- a/clang/Lex/Preprocessor.cpp +++ b/clang/Lex/Preprocessor.cpp @@ -971,12 +971,9 @@ IdentifierInfo *Preprocessor::LookUpIdentifierInfo(LexerToken &Identifier, /// identifier. This callback looks up the identifier in the map and/or /// potentially macro expands it or turns it into a named token (like 'for'). void Preprocessor::HandleIdentifier(LexerToken &Identifier) { - if (Identifier.getIdentifierInfo() == 0) { - // If we are skipping tokens (because we are in a #if 0 block), there will - // be no identifier info, just return the token. - assert(isSkipping() && "Token isn't an identifier?"); - return; - } + assert(Identifier.getIdentifierInfo() && + "Can't handle identifiers without identifier info!"); + IdentifierInfo &II = *Identifier.getIdentifierInfo(); // If this identifier was poisoned, and if it was not produced from a macro diff --git a/clang/test/Preprocessor/macro_fn_lparen_scan2.c b/clang/test/Preprocessor/macro_fn_lparen_scan2.c new file mode 100644 index 000000000000..38c0b5c35d07 --- /dev/null +++ b/clang/test/Preprocessor/macro_fn_lparen_scan2.c @@ -0,0 +1,7 @@ +// RUN: clang -E %s | grep 'FUNC (3+1);' + +#define F(a) a +#define FUNC(a) (a+1) + +F(FUNC) FUNC (3); /* final token sequence is FUNC(3+1) */ +