blob: 6da819e9b9c549b1a8688e7c97eb9b1d9534a0fe [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- 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"
17using namespace llvm;
18using namespace clang;
19
Chris Lattnerd01e2912006-06-18 16:22:51 +000020MacroExpander::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 Lattner22eb9722006-06-18 05:43:12 +000030/// Lex - Lex and return a token from this macro stream.
Chris Lattnerd01e2912006-06-18 16:22:51 +000031///
Chris Lattnercb283342006-06-18 06:48:37 +000032void MacroExpander::Lex(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +000033 // 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 Lattnerd01e2912006-06-18 16:22:51 +000039 //Tok.SetLocation(InstantiateLoc);
Chris Lattner22eb9722006-06-18 05:43:12 +000040
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 Lattner22eb9722006-06-18 05:43:12 +000053}