-//===--- MacroExpander.cpp - Lex from a macro expansion -------------------===//
+//===--- TokenLexer.cpp - Lex from a token stream -------------------------===//
//
// The LLVM Compiler Infrastructure
//
//
//===----------------------------------------------------------------------===//
//
-// This file implements the MacroExpander interface.
+// This file implements the TokenLexer interface.
//
//===----------------------------------------------------------------------===//
unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
// Otherwise, we have to pre-expand this argument, populating Result. To do
- // this, we set up a fake MacroExpander to lex from the unexpanded argument
+ // this, we set up a fake TokenLexer to lex from the unexpanded argument
// list. With this installed, we lex expanded tokens until we hit the EOF
// token at the end of the unexp list.
PP.EnterTokenStream(AT, NumToks);
}
//===----------------------------------------------------------------------===//
-// MacroExpander Implementation
+// TokenLexer Implementation
//===----------------------------------------------------------------------===//
-/// Create a macro expander for the specified macro with the specified actual
+/// Create a TokenLexer for the specified macro with the specified actual
/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
-void MacroExpander::Init(Token &Tok, MacroArgs *Actuals) {
- // If the client is reusing a macro expander, make sure to free any memory
+void TokenLexer::Init(Token &Tok, MacroArgs *Actuals) {
+ // If the client is reusing a TokenLexer, make sure to free any memory
// associated with it.
destroy();
-/// Create a macro expander for the specified token stream. This does not
+/// Create a TokenLexer for the specified token stream. This does not
/// take ownership of the specified token vector.
-void MacroExpander::Init(const Token *TokArray, unsigned NumToks) {
- // If the client is reusing a macro expander, make sure to free any memory
+void TokenLexer::Init(const Token *TokArray, unsigned NumToks) {
+ // If the client is reusing a TokenLexer, make sure to free any memory
// associated with it.
destroy();
}
-void MacroExpander::destroy() {
+void TokenLexer::destroy() {
// If this was a function-like macro that actually uses its arguments, delete
// the expanded tokens.
if (OwnsTokens) {
Tokens = 0;
}
- // MacroExpander owns its formal arguments.
+ // TokenLexer owns its formal arguments.
if (ActualArgs) ActualArgs->destroy();
}
/// Expand the arguments of a function-like macro so that we can quickly
/// return preexpanded tokens from Tokens.
-void MacroExpander::ExpandFunctionArguments() {
+void TokenLexer::ExpandFunctionArguments() {
llvm::SmallVector<Token, 128> ResultToks;
// Loop through 'Tokens', expanding them into ResultToks. Keep
/// Lex - Lex and return a token from this macro stream.
///
-void MacroExpander::Lex(Token &Tok) {
+void TokenLexer::Lex(Token &Tok) {
// Lexing off the end of the macro, pop this macro off the expansion stack.
if (isAtEnd()) {
// If this is a macro (not a token stream), mark the macro enabled now
/// operator. Read the ## and RHS, and paste the LHS/RHS together. If there
/// are is another ## after it, chomp it iteratively. Return the result as Tok.
/// If this returns true, the caller should immediately return the token.
-bool MacroExpander::PasteTokens(Token &Tok) {
+bool TokenLexer::PasteTokens(Token &Tok) {
llvm::SmallVector<char, 128> Buffer;
do {
// Consume the ## operator.
}
}
- // Turn ## into 'other' to avoid # ## # from looking like a paste operator.
+ // Turn ## into 'unknown' to avoid # ## # from looking like a paste
+ // operator.
if (Result.is(tok::hashhash))
Result.setKind(tok::unknown);
// FIXME: Turn __VA_ARGS__ into "not a token"?
/// isNextTokenLParen - If the next token lexed will pop this macro off the
/// expansion stack, return 2. If the next unexpanded token is a '(', return
/// 1, otherwise return 0.
-unsigned MacroExpander::isNextTokenLParen() const {
+unsigned TokenLexer::isNextTokenLParen() const {
// Out of tokens?
if (isAtEnd())
return 2;
/// macro, other active macros, and anything left on the current physical
/// source line of the instantiated buffer. Handle this by returning the
/// first token on the next line.
-void MacroExpander::HandleMicrosoftCommentPaste(Token &Tok) {
+void TokenLexer::HandleMicrosoftCommentPaste(Token &Tok) {
// We 'comment out' the rest of this macro by just ignoring the rest of the
// tokens that have not been lexed yet, if any.
CurDirLookup = 0;
if (NumCachedMacroExpanders == 0) {
- CurMacroExpander = new MacroExpander(Tok, Args, *this);
+ CurMacroExpander = new TokenLexer(Tok, Args, *this);
} else {
CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
CurMacroExpander->Init(Tok, Args);
// Create a macro expander to expand from the specified token stream.
if (NumCachedMacroExpanders == 0) {
- CurMacroExpander = new MacroExpander(Toks, NumToks, *this);
+ CurMacroExpander = new TokenLexer(Toks, NumToks, *this);
} else {
CurMacroExpander = MacroExpanderCache[--NumCachedMacroExpanders];
CurMacroExpander->Init(Toks, NumToks);
-//===--- MacroExpander.h - Lex from a macro expansion -----------*- C++ -*-===//
+//===--- TokenLexer.h - Lex from a token buffer -----------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
//
//===----------------------------------------------------------------------===//
//
-// This file defines the MacroExpander and MacroArgs interfaces.
+// This file defines the TokenLexer and MacroArgs interfaces.
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_CLANG_MACROEXPANDER_H
-#define LLVM_CLANG_MACROEXPANDER_H
+#ifndef LLVM_CLANG_TOKENLEXER_H
+#define LLVM_CLANG_TOKENLEXER_H
#include "clang/Basic/SourceLocation.h"
#include <vector>
};
-/// MacroExpander - This implements a lexer that returns token from a macro body
+/// TokenLexer - This implements a lexer that returns token from a macro body
/// or token stream instead of lexing from a character buffer.
///
-class MacroExpander {
+class TokenLexer {
/// Macro - The macro we are expanding from. This is null if expanding a
/// token stream.
///
MacroInfo *Macro;
/// ActualArgs - The actual arguments specified for a function-like macro, or
- /// null. The MacroExpander owns the pointed-to object.
+ /// null. The TokenLexer owns the pointed-to object.
MacroArgs *ActualArgs;
/// PP - The current preprocessor object we are expanding for.
bool AtStartOfLine : 1;
bool HasLeadingSpace : 1;
- /// OwnsTokens - This is true if this MacroExpander allocated the Tokens
+ /// OwnsTokens - This is true if this TokenLexer allocated the Tokens
/// array, and thus needs to free it when destroyed. For simple object-like
/// macros (for example) we just point into the token buffer of the macro
/// definition, we don't make a copy of it.
bool OwnsTokens : 1;
- MacroExpander(const MacroExpander&); // DO NOT IMPLEMENT
- void operator=(const MacroExpander&); // DO NOT IMPLEMENT
+ TokenLexer(const TokenLexer&); // DO NOT IMPLEMENT
+ void operator=(const TokenLexer&); // DO NOT IMPLEMENT
public:
- /// Create a macro expander for the specified macro with the specified actual
+ /// Create a TokenLexer for the specified macro with the specified actual
/// arguments. Note that this ctor takes ownership of the ActualArgs pointer.
- MacroExpander(Token &Tok, MacroArgs *ActualArgs, Preprocessor &pp)
+ TokenLexer(Token &Tok, MacroArgs *ActualArgs, Preprocessor &pp)
: Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
Init(Tok, ActualArgs);
}
- /// Init - Initialize this macro expander to expand from the specified macro
+ /// Init - Initialize this TokenLexer to expand from the specified macro
/// with the specified argument information. Note that this ctor takes
/// ownership of the ActualArgs pointer.
void Init(Token &Tok, MacroArgs *ActualArgs);
- /// Create a macro expander for the specified token stream. This does not
+ /// Create a TokenLexer for the specified token stream. This does not
/// take ownership of the specified token vector.
- MacroExpander(const Token *TokArray, unsigned NumToks, Preprocessor &pp)
+ TokenLexer(const Token *TokArray, unsigned NumToks, Preprocessor &pp)
: Macro(0), ActualArgs(0), PP(pp), OwnsTokens(false) {
Init(TokArray, NumToks);
}
- /// Init - Initialize this macro expander with the specified token stream.
+ /// Init - Initialize this TokenLexer with the specified token stream.
/// This does not take ownership of the specified token vector.
void Init(const Token *TokArray, unsigned NumToks);
- ~MacroExpander() { destroy(); }
+ ~TokenLexer() { destroy(); }
/// isNextTokenLParen - If the next token lexed will pop this macro off the
/// expansion stack, return 2. If the next unexpanded token is a '(', return
/// CurMacroExpander - This is the current macro we are expanding, if we are
/// expanding a macro. One of CurLexer and CurMacroExpander must be null.
- MacroExpander *CurMacroExpander;
+ TokenLexer *CurMacroExpander;
/// IncludeMacroStack - This keeps track of the stack of files currently
/// #included, and macros currently being expanded from, not counting
struct IncludeStackInfo {
Lexer *TheLexer;
const DirectoryLookup *TheDirLookup;
- MacroExpander *TheMacroExpander;
- IncludeStackInfo(Lexer *L, const DirectoryLookup *D, MacroExpander *M)
- : TheLexer(L), TheDirLookup(D), TheMacroExpander(M) {
+ TokenLexer *TheMacroExpander;
+ IncludeStackInfo(Lexer *L, const DirectoryLookup *D, TokenLexer *TL)
+ : TheLexer(L), TheDirLookup(D), TheMacroExpander(TL) {
}
};
std::vector<IncludeStackInfo> IncludeMacroStack;
/// MacroExpanderCache - Cache macro expanders to reduce malloc traffic.
enum { MacroExpanderCacheSize = 8 };
unsigned NumCachedMacroExpanders;
- MacroExpander *MacroExpanderCache[MacroExpanderCacheSize];
+ TokenLexer *MacroExpanderCache[MacroExpanderCacheSize];
public:
Preprocessor(Diagnostic &diags, const LangOptions &opts, TargetInfo &target,
SourceManager &SM, HeaderSearch &Headers);