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 | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame^] | 21 | //===----------------------------------------------------------------------===// |
| 22 | // MacroFormalArgs Implementation |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | MacroFormalArgs::MacroFormalArgs(const MacroInfo *MI) { |
| 26 | assert(MI->isFunctionLike() && |
| 27 | "Can't have formal args for an object-like macro!"); |
| 28 | // Reserve space for arguments to avoid reallocation. |
| 29 | unsigned NumArgs = MI->getNumArgs(); |
| 30 | if (MI->isC99Varargs() || MI->isGNUVarargs()) |
| 31 | NumArgs += 3; // Varargs can have more than this, just some guess. |
| 32 | |
| 33 | ArgTokens.reserve(NumArgs); |
| 34 | } |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | // MacroExpander Implementation |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | MacroExpander::MacroExpander(LexerToken &Tok, MacroFormalArgs *Formals, |
| 41 | Preprocessor &pp) |
| 42 | : Macro(*Tok.getIdentifierInfo()->getMacroInfo()), FormalArgs(Formals), |
| 43 | PP(pp), CurToken(0), |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 44 | InstantiateLoc(Tok.getLocation()), |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 45 | AtStartOfLine(Tok.isAtStartOfLine()), |
| 46 | HasLeadingSpace(Tok.hasLeadingSpace()) { |
| 47 | } |
| 48 | |
Chris Lattner | 67b07cb | 2006-06-26 02:03:42 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 50 | /// Lex - Lex and return a token from this macro stream. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 51 | /// |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 52 | void MacroExpander::Lex(LexerToken &Tok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 53 | // Lexing off the end of the macro, pop this macro off the expansion stack. |
| 54 | if (CurToken == Macro.getNumTokens()) |
| 55 | return PP.HandleEndOfMacro(Tok); |
| 56 | |
| 57 | // Get the next token to return. |
| 58 | Tok = Macro.getReplacementToken(CurToken++); |
| 59 | |
Chris Lattner | c673f90 | 2006-06-30 06:10:41 +0000 | [diff] [blame] | 60 | // The token's current location indicate where the token was lexed from. We |
| 61 | // need this information to compute the spelling of the token, but any |
| 62 | // diagnostics for the expanded token should appear as if they came from |
| 63 | // InstantiationLoc. Pull this information together into a new SourceLocation |
| 64 | // that captures all of this. |
| 65 | Tok.SetLocation(PP.getSourceManager().getInstantiationLoc(Tok.getLocation(), |
| 66 | InstantiateLoc)); |
Chris Lattner | 30709b03 | 2006-06-21 03:01:55 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 68 | // If this is the first token, set the lexical properties of the token to |
| 69 | // match the lexical properties of the macro identifier. |
| 70 | if (CurToken == 1) { |
| 71 | Tok.SetFlagValue(LexerToken::StartOfLine , AtStartOfLine); |
| 72 | Tok.SetFlagValue(LexerToken::LeadingSpace, HasLeadingSpace); |
| 73 | } |
| 74 | |
| 75 | // Handle recursive expansion! |
| 76 | if (Tok.getIdentifierInfo()) |
| 77 | return PP.HandleIdentifier(Tok); |
| 78 | |
| 79 | // Otherwise, return a normal token. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 80 | } |