From b0a3c5a48b5a1f10c47a24eee985d3032ef9a1d5 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Fri, 31 Jan 2014 20:47:44 +0000 Subject: [PATCH] Track the currently-being-built submodule inside the preprocessor (rather than just storing a flag indicating if there was one), and include it in the 'end of module' annotation. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@200573 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Lex/Preprocessor.h | 31 +++++++++++++++---------------- lib/Lex/PPDirectives.cpp | 12 ++++++++---- lib/Lex/PPLexerChange.cpp | 32 ++++++++++++++------------------ lib/Lex/Preprocessor.cpp | 2 +- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 7862ed3109..2134067693 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -286,25 +286,26 @@ class Preprocessor : public RefCountedBase { CLK_LexAfterModuleImport } CurLexerKind; - /// \brief True if the current lexer is for a submodule. - bool CurIsSubmodule; + /// \brief If the current lexer is for a submodule that is being built, this + /// is that submodule. + Module *CurSubmodule; /// \brief Keeps track of the stack of files currently /// \#included, and macros currently being expanded from, not counting /// CurLexer/CurTokenLexer. struct IncludeStackInfo { enum CurLexerKind CurLexerKind; - bool IsSubmodule; + Module *TheSubmodule; Lexer *TheLexer; PTHLexer *ThePTHLexer; PreprocessorLexer *ThePPLexer; TokenLexer *TheTokenLexer; const DirectoryLookup *TheDirLookup; - IncludeStackInfo(enum CurLexerKind K, bool SM, Lexer *L, PTHLexer *P, + IncludeStackInfo(enum CurLexerKind K, Module *M, Lexer *L, PTHLexer *P, PreprocessorLexer *PPL, TokenLexer *TL, const DirectoryLookup *D) - : CurLexerKind(K), IsSubmodule(SM), TheLexer(L), ThePTHLexer(P), + : CurLexerKind(K), TheSubmodule(M), TheLexer(L), ThePTHLexer(P), ThePPLexer(PPL), TheTokenLexer(TL), TheDirLookup(D) {} }; std::vector IncludeMacroStack; @@ -686,14 +687,14 @@ public: void EndSourceFile(); /// \brief Add a source file to the top of the include stack and - /// start lexing tokens from it instead of the current buffer. + /// start lexing tokens from it instead of the current buffer. /// - /// Emit an error and don't enter the file on error. - void EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir, - SourceLocation Loc, bool IsSubmodule = false); + /// Emits a diagnostic, doesn't enter the file, and returns true on error. + bool EnterSourceFile(FileID CurFileID, const DirectoryLookup *Dir, + SourceLocation Loc); /// \brief Add a Macro to the top of the include stack and start lexing - /// tokens from it instead of the current buffer. + /// tokens from it instead of the current buffer. /// /// \param Args specifies the tokens input to a function-like macro. /// \param ILEnd specifies the location of the ')' for a function-like macro @@ -1317,7 +1318,7 @@ private: void PushIncludeMacroStack() { IncludeMacroStack.push_back(IncludeStackInfo(CurLexerKind, - CurIsSubmodule, + CurSubmodule, CurLexer.take(), CurPTHLexer.take(), CurPPLexer, @@ -1332,8 +1333,8 @@ private: CurPPLexer = IncludeMacroStack.back().ThePPLexer; CurTokenLexer.reset(IncludeMacroStack.back().TheTokenLexer); CurDirLookup = IncludeMacroStack.back().TheDirLookup; + CurSubmodule = IncludeMacroStack.back().TheSubmodule; CurLexerKind = IncludeMacroStack.back().CurLexerKind; - CurIsSubmodule = IncludeMacroStack.back().IsSubmodule; IncludeMacroStack.pop_back(); } @@ -1435,13 +1436,11 @@ private: /// \brief Add a lexer to the top of the include stack and /// start lexing tokens from it instead of the current buffer. - void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir, - bool IsSubmodule = false); + void EnterSourceFileWithLexer(Lexer *TheLexer, const DirectoryLookup *Dir); /// \brief Add a lexer to the top of the include stack and /// start getting tokens from it using the PTH cache. - void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir, - bool IsSubmodule = false); + void EnterSourceFileWithPTH(PTHLexer *PL, const DirectoryLookup *Dir); /// \brief Set the FileID for the preprocessor predefines. void setPredefinesFileID(FileID FID) { diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index c16dfb23e9..4a9e09f798 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -1660,14 +1660,18 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, } // If all is good, enter the new file! - EnterSourceFile(FID, CurDir, FilenameTok.getLocation(), - static_cast(BuildingModule)); + if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation())) + return; // If we're walking into another part of the same module, let the parser // know that any future declarations are within that other submodule. - if (BuildingModule) + if (BuildingModule) { + assert(!CurSubmodule && "should not have marked this as a module yet"); + CurSubmodule = BuildingModule.getModule(); + EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, - BuildingModule.getModule()); + CurSubmodule); + } } /// HandleIncludeNextDirective - Implements \#include_next. diff --git a/lib/Lex/PPLexerChange.cpp b/lib/Lex/PPLexerChange.cpp index 3b3c5a9192..4170fc3316 100644 --- a/lib/Lex/PPLexerChange.cpp +++ b/lib/Lex/PPLexerChange.cpp @@ -68,8 +68,8 @@ PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { /// EnterSourceFile - Add a source file to the top of the include stack and /// start lexing tokens from it instead of the current buffer. -void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, - SourceLocation Loc, bool IsSubmodule) { +bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, + SourceLocation Loc) { assert(!CurTokenLexer && "Cannot #include a file inside a macro!"); ++NumEnteredSourceFiles; @@ -78,8 +78,8 @@ void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, if (PTH) { if (PTHLexer *PL = PTH->CreateLexer(FID)) { - EnterSourceFileWithPTH(PL, CurDir, IsSubmodule); - return; + EnterSourceFileWithPTH(PL, CurDir); + return false; } } @@ -91,7 +91,7 @@ void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID); Diag(Loc, diag::err_pp_error_opening_file) << std::string(SourceMgr.getBufferName(FileStart)) << ""; - return; + return true; } if (isCodeCompletionEnabled() && @@ -101,16 +101,14 @@ void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset); } - EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir, - IsSubmodule); - return; + EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir); + return false; } /// EnterSourceFileWithLexer - Add a source file to the top of the include stack /// and start lexing tokens from it instead of the current buffer. void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, - const DirectoryLookup *CurDir, - bool IsSubmodule) { + const DirectoryLookup *CurDir) { // Add the current lexer to the include stack. if (CurPPLexer || CurTokenLexer) @@ -119,7 +117,7 @@ void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, CurLexer.reset(TheLexer); CurPPLexer = TheLexer; CurDirLookup = CurDir; - CurIsSubmodule = IsSubmodule; + CurSubmodule = 0; if (CurLexerKind != CLK_LexAfterModuleImport) CurLexerKind = CLK_Lexer; @@ -136,8 +134,7 @@ void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, /// EnterSourceFileWithPTH - Add a source file to the top of the include stack /// and start getting tokens from it using the PTH cache. void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, - const DirectoryLookup *CurDir, - bool IsSubmodule) { + const DirectoryLookup *CurDir) { if (CurPPLexer || CurTokenLexer) PushIncludeMacroStack(); @@ -145,7 +142,7 @@ void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, CurDirLookup = CurDir; CurPTHLexer.reset(PL); CurPPLexer = CurPTHLexer.get(); - CurIsSubmodule = IsSubmodule; + CurSubmodule = 0; if (CurLexerKind != CLK_LexAfterModuleImport) CurLexerKind = CLK_PTHLexer; @@ -371,16 +368,15 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { if (Callbacks && !isEndOfMacro && CurPPLexer) ExitedFID = CurPPLexer->getFileID(); - // If this file corresponded to a submodule, notify the parser that we've - // left that submodule. - bool LeavingSubmodule = CurIsSubmodule && CurLexer; + bool LeavingSubmodule = CurSubmodule && CurLexer; if (LeavingSubmodule) { + // Notify the parser that we've left the module. const char *EndPos = getCurLexerEndPos(); Result.startToken(); CurLexer->BufferPtr = EndPos; CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); Result.setAnnotationEndLoc(Result.getLocation()); - Result.setAnnotationValue(0); + Result.setAnnotationValue(CurSubmodule); } // We're done with the #included file. diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index aa3926304e..68201b39f6 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -67,7 +67,7 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr PPOpts, CodeComplete(0), CodeCompletionFile(0), CodeCompletionOffset(0), LastTokenWasAt(false), ModuleImportExpectsIdentifier(false), CodeCompletionReached(0), SkipMainFilePreamble(0, true), CurPPLexer(0), - CurDirLookup(0), CurLexerKind(CLK_Lexer), CurIsSubmodule(false), + CurDirLookup(0), CurLexerKind(CLK_Lexer), CurSubmodule(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0), MICache(0), DeserialMIChainHead(0) { OwnsHeaderSearch = OwnsHeaders; -- 2.49.0