blob: e98125dde460a0ce73f124a5980b790b15277c8d [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"
Chris Lattner30709b032006-06-21 03:01:55 +000017#include "clang/Basic/SourceManager.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000018using namespace llvm;
19using namespace clang;
20
Chris Lattnerd01e2912006-06-18 16:22:51 +000021MacroExpander::MacroExpander(LexerToken &Tok, Preprocessor &pp)
22 : Macro(*Tok.getIdentifierInfo()->getMacroInfo()), PP(pp), CurToken(0),
Chris Lattner50b497e2006-06-18 16:32:35 +000023 InstantiateLoc(Tok.getLocation()),
Chris Lattnerd01e2912006-06-18 16:22:51 +000024 AtStartOfLine(Tok.isAtStartOfLine()),
25 HasLeadingSpace(Tok.hasLeadingSpace()) {
26}
27
Chris Lattner67b07cb2006-06-26 02:03:42 +000028
Chris Lattner22eb9722006-06-18 05:43:12 +000029/// Lex - Lex and return a token from this macro stream.
Chris Lattnerd01e2912006-06-18 16:22:51 +000030///
Chris Lattnercb283342006-06-18 06:48:37 +000031void MacroExpander::Lex(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +000032 // 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 Lattnerc673f902006-06-30 06:10:41 +000039 // 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 Lattner30709b032006-06-21 03:01:55 +000046
Chris Lattner22eb9722006-06-18 05:43:12 +000047 // 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 Lattner22eb9722006-06-18 05:43:12 +000059}