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" |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace clang; |
| 20 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 21 | MacroExpander::MacroExpander(LexerToken &Tok, Preprocessor &pp) |
| 22 | : Macro(*Tok.getIdentifierInfo()->getMacroInfo()), PP(pp), CurToken(0), |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 23 | InstantiateLoc(Tok.getLocation()), |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 24 | AtStartOfLine(Tok.isAtStartOfLine()), |
| 25 | HasLeadingSpace(Tok.hasLeadingSpace()) { |
| 26 | } |
| 27 | |
Chris Lattner | 67b07cb | 2006-06-26 02:03:42 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 29 | /// Lex - Lex and return a token from this macro stream. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 30 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 31 | void MacroExpander::Lex(LexerToken &Tok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 32 | // Lexing off the end of the macro, pop this macro off the expansion stack. |
| 33 | if (CurToken == Macro.getNumTokens()) |
| 34 | return PP.HandleEndOfMacro(Tok); |
| 35 | |
| 36 | // Get the next token to return. |
| 37 | Tok = Macro.getReplacementToken(CurToken++); |
| 38 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 39 | // The token's current location indicate where the token was lexed from. We |
| 40 | // need this information to compute the spelling of the token, but any |
| 41 | // diagnostics for the expanded token should appear as if they came from |
| 42 | // InstantiationLoc. Pull this information together into a new SourceLocation |
| 43 | // that captures all of this. |
| 44 | Tok.SetLocation(PP.getSourceManager().getInstantiationLoc(Tok.getLocation(), |
| 45 | InstantiateLoc)); |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 47 | // If this is the first token, set the lexical properties of the token to |
| 48 | // match the lexical properties of the macro identifier. |
| 49 | if (CurToken == 1) { |
| 50 | Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine); |
| 51 | Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace); |
| 52 | } |
| 53 | |
| 54 | // Handle recursive expansion! |
| 55 | if (Tok.getIdentifierInfo()) |
| 56 | return PP.HandleIdentifier(Tok); |
| 57 | |
| 58 | // Otherwise, return a normal token. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 59 | } |