Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- MacroExpander.cpp - Lex from a macro expansion -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the MacroExpander interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Lex/MacroExpander.h" |
| 15 | #include "clang/Lex/MacroInfo.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | using namespace llvm; |
| 18 | using namespace clang; |
| 19 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 20 | MacroExpander::MacroExpander(LexerToken &Tok, Preprocessor &pp) |
| 21 | : Macro(*Tok.getIdentifierInfo()->getMacroInfo()), PP(pp), CurToken(0), |
| 22 | InstantiateLoc(Tok.getSourceLocation()), |
| 23 | AtStartOfLine(Tok.isAtStartOfLine()), |
| 24 | HasLeadingSpace(Tok.hasLeadingSpace()) { |
| 25 | } |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 30 | /// Lex - Lex and return a token from this macro stream. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 31 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 32 | void MacroExpander::Lex(LexerToken &Tok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 33 | // Lexing off the end of the macro, pop this macro off the expansion stack. |
| 34 | if (CurToken == Macro.getNumTokens()) |
| 35 | return PP.HandleEndOfMacro(Tok); |
| 36 | |
| 37 | // Get the next token to return. |
| 38 | Tok = Macro.getReplacementToken(CurToken++); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 39 | //Tok.SetLocation(InstantiateLoc); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 40 | |
| 41 | // If this is the first token, set the lexical properties of the token to |
| 42 | // match the lexical properties of the macro identifier. |
| 43 | if (CurToken == 1) { |
| 44 | Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine); |
| 45 | Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace); |
| 46 | } |
| 47 | |
| 48 | // Handle recursive expansion! |
| 49 | if (Tok.getIdentifierInfo()) |
| 50 | return PP.HandleIdentifier(Tok); |
| 51 | |
| 52 | // Otherwise, return a normal token. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 53 | } |