diff options
Diffstat (limited to 'lib/Lex/Preprocessor.cpp')
| -rw-r--r-- | lib/Lex/Preprocessor.cpp | 66 | 
1 files changed, 54 insertions, 12 deletions
| diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 614530cf387f..3b070ce049db 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -26,6 +26,7 @@  //===----------------------------------------------------------------------===//  #include "clang/Lex/Preprocessor.h" +#include "clang/Lex/PreprocessorOptions.h"  #include "MacroArgs.h"  #include "clang/Lex/ExternalPreprocessorSource.h"  #include "clang/Lex/HeaderSearch.h" @@ -49,21 +50,25 @@ using namespace clang;  //===----------------------------------------------------------------------===//  ExternalPreprocessorSource::~ExternalPreprocessorSource() { } -Preprocessor::Preprocessor(DiagnosticsEngine &diags, LangOptions &opts, +PPMutationListener::~PPMutationListener() { } + +Preprocessor::Preprocessor(llvm::IntrusiveRefCntPtr<PreprocessorOptions> PPOpts, +                           DiagnosticsEngine &diags, LangOptions &opts,                             const TargetInfo *target, SourceManager &SM,                             HeaderSearch &Headers, ModuleLoader &TheModuleLoader,                             IdentifierInfoLookup* IILookup,                             bool OwnsHeaders,                             bool DelayInitialization,                             bool IncrProcessing) -  : Diags(&diags), LangOpts(opts), Target(target),FileMgr(Headers.getFileMgr()), +  : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), Target(target), +    FileMgr(Headers.getFileMgr()),      SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader),      ExternalSource(0), Identifiers(opts, IILookup),       IncrementalProcessing(IncrProcessing), CodeComplete(0),       CodeCompletionFile(0), CodeCompletionOffset(0), CodeCompletionReached(0),      SkipMainFilePreamble(0, true), CurPPLexer(0),  -    CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), MacroArgCache(0),  -    Record(0), MIChainHead(0), MICache(0)  +    CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), Listener(0), +    MacroArgCache(0), Record(0), MIChainHead(0), MICache(0)   {    OwnsHeaderSearch = OwnsHeaders; @@ -285,6 +290,39 @@ Preprocessor::macro_end(bool IncludeExternalMacros) const {    return Macros.end();  } +/// \brief Compares macro tokens with a specified token value sequence. +static bool MacroDefinitionEquals(const MacroInfo *MI, +                                  llvm::ArrayRef<TokenValue> Tokens) { +  return Tokens.size() == MI->getNumTokens() && +      std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin()); +} + +StringRef Preprocessor::getLastMacroWithSpelling( +                                    SourceLocation Loc, +                                    ArrayRef<TokenValue> Tokens) const { +  SourceLocation BestLocation; +  StringRef BestSpelling; +  for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end(); +       I != E; ++I) { +    if (!I->second->isObjectLike()) +      continue; +    const MacroInfo *MI = I->second->findDefinitionAtLoc(Loc, SourceMgr); +    if (!MI) +      continue; +    if (!MacroDefinitionEquals(MI, Tokens)) +      continue; +    SourceLocation Location = I->second->getDefinitionLoc(); +    // Choose the macro defined latest. +    if (BestLocation.isInvalid() || +        (Location.isValid() && +         SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) { +      BestLocation = Location; +      BestSpelling = I->first->getName(); +    } +  } +  return BestSpelling; +} +  void Preprocessor::recomputeCurLexerKind() {    if (CurLexer)      CurLexerKind = CLK_Lexer; @@ -378,17 +416,17 @@ StringRef Preprocessor::getSpelling(const Token &Tok,  /// CreateString - Plop the specified string into a scratch buffer and return a  /// location for it.  If specified, the source location provides a source  /// location for the token. -void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok, +void Preprocessor::CreateString(StringRef Str, Token &Tok,                                  SourceLocation ExpansionLocStart,                                  SourceLocation ExpansionLocEnd) { -  Tok.setLength(Len); +  Tok.setLength(Str.size());    const char *DestPtr; -  SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr); +  SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);    if (ExpansionLocStart.isValid())      Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart, -                                       ExpansionLocEnd, Len); +                                       ExpansionLocEnd, Str.size());    Tok.setLocation(Loc);    // If this is a raw identifier or a literal token, set the pointer data. @@ -641,10 +679,14 @@ void Preprocessor::LexAfterModuleImport(Token &Result) {    }    // If we have a non-empty module path, load the named module. -  if (!ModuleImportPath.empty()) -    (void)TheModuleLoader.loadModule(ModuleImportLoc, ModuleImportPath, -                                     Module::MacrosVisible, -                                     /*IsIncludeDirective=*/false); +  if (!ModuleImportPath.empty()) { +    Module *Imported = TheModuleLoader.loadModule(ModuleImportLoc, +                                                  ModuleImportPath, +                                                  Module::MacrosVisible, +                                                  /*IsIncludeDirective=*/false); +    if (Callbacks) +      Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported); +  }  }  void Preprocessor::addCommentHandler(CommentHandler *Handler) { | 
