blob: 4bf867a848bc65e45454b75159a17a2e7ae990e2 [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 Lattner78186052006-07-09 00:45:31 +000021//===----------------------------------------------------------------------===//
22// MacroFormalArgs Implementation
23//===----------------------------------------------------------------------===//
24
25MacroFormalArgs::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
40MacroExpander::MacroExpander(LexerToken &Tok, MacroFormalArgs *Formals,
41 Preprocessor &pp)
42 : Macro(*Tok.getIdentifierInfo()->getMacroInfo()), FormalArgs(Formals),
43 PP(pp), CurToken(0),
Chris Lattner50b497e2006-06-18 16:32:35 +000044 InstantiateLoc(Tok.getLocation()),
Chris Lattnerd01e2912006-06-18 16:22:51 +000045 AtStartOfLine(Tok.isAtStartOfLine()),
46 HasLeadingSpace(Tok.hasLeadingSpace()) {
47}
48
Chris Lattner67b07cb2006-06-26 02:03:42 +000049
Chris Lattner22eb9722006-06-18 05:43:12 +000050/// Lex - Lex and return a token from this macro stream.
Chris Lattnerd01e2912006-06-18 16:22:51 +000051///
Chris Lattnercb283342006-06-18 06:48:37 +000052void MacroExpander::Lex(LexerToken &Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +000053 // 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 Lattnerc673f902006-06-30 06:10:41 +000060 // 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 Lattner30709b032006-06-21 03:01:55 +000067
Chris Lattner22eb9722006-06-18 05:43:12 +000068 // 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 Lattner22eb9722006-06-18 05:43:12 +000080}