diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2013-11-23 04:06:09 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2013-11-23 04:06:09 +0000 |
commit | 34f30516aa7f32cbff61dc9066f0b332dfd88c2b (patch) | |
tree | 3761579a8a9c29f366a7f5a6c805567dfecef066 /clang/lib/Lex | |
parent | 5c6eab21ebaf93c68b5d1df8b845518ea6f54b63 (diff) | |
download | bcm5719-llvm-34f30516aa7f32cbff61dc9066f0b332dfd88c2b.tar.gz bcm5719-llvm-34f30516aa7f32cbff61dc9066f0b332dfd88c2b.zip |
Generate a marker token when entering or leaving a submodule when building a
module. Use the marker to diagnose cases where we try to transition between
submodules when not at the top level (most likely because a closing brace was
missing at the end of a header file, but is also possible if submodule headers
attempt to do something fundamentally non-modular, like our .def files).
llvm-svn: 195543
Diffstat (limited to 'clang/lib/Lex')
-rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 43 | ||||
-rw-r--r-- | clang/lib/Lex/PPLexerChange.cpp | 74 | ||||
-rw-r--r-- | clang/lib/Lex/Preprocessor.cpp | 4 |
3 files changed, 85 insertions, 36 deletions
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 86c508fe9b1..1e3602e84f4 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -1389,6 +1389,19 @@ bool Preprocessor::ConcatenateIncludeName( return true; } +/// \brief Push a token onto the token stream containing an annotation. +static void EnterAnnotationToken(Preprocessor &PP, + SourceLocation Begin, SourceLocation End, + tok::TokenKind Kind, void *AnnotationVal) { + Token *Tok = new Token[1]; + Tok[0].startToken(); + Tok[0].setKind(Kind); + Tok[0].setLocation(Begin); + Tok[0].setAnnotationEndLoc(End); + Tok[0].setAnnotationValue(AnnotationVal); + PP.EnterTokenStream(Tok, 1, true, true); +} + /// HandleIncludeDirective - The "\#include" tokens have just been read, read /// the file to be included from the lexer, then include it! This is a common /// routine with functionality shared between \#include, \#include_next and @@ -1590,7 +1603,7 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, // include directive maps to. bool BuildingImportedModule = Path[0].first->getName() == getLangOpts().CurrentModule; - + if (!BuildingImportedModule && getLangOpts().ObjC2) { // If we're not building the imported module, warn that we're going // to automatically turn this inclusion directive into a module import. @@ -1639,13 +1652,8 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, // make the module visible. // FIXME: Produce this as the current token directly, rather than // allocating a new token for it. - Token *Tok = new Token[1]; - Tok[0].startToken(); - Tok[0].setKind(tok::annot_module_include); - Tok[0].setLocation(HashLoc); - Tok[0].setAnnotationEndLoc(End); - Tok[0].setAnnotationValue(Imported); - EnterTokenStream(Tok, 1, true, true); + EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_include, + Imported); } return; } @@ -1692,8 +1700,23 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, FileID FID = SourceMgr.createFileID(File, IncludePos, FileCharacter); assert(!FID.isInvalid() && "Expected valid file ID"); - // Finally, if all is good, enter the new file! - EnterSourceFile(FID, CurDir, FilenameTok.getLocation()); + // Determine if we're switching to building a new submodule, and which one. + ModuleMap::KnownHeader BuildingModule; + if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty()) { + Module *RequestingModule = getModuleForLocation(FilenameLoc); + BuildingModule = + HeaderInfo.getModuleMap().findModuleForHeader(File, RequestingModule); + } + + // If all is good, enter the new file! + EnterSourceFile(FID, CurDir, FilenameTok.getLocation(), + static_cast<bool>(BuildingModule)); + + // 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) + EnterAnnotationToken(*this, HashLoc, End, tok::annot_module_begin, + BuildingModule.getModule()); } /// HandleIncludeNextDirective - Implements \#include_next. diff --git a/clang/lib/Lex/PPLexerChange.cpp b/clang/lib/Lex/PPLexerChange.cpp index 1f970a4450d..3b3c5a91920 100644 --- a/clang/lib/Lex/PPLexerChange.cpp +++ b/clang/lib/Lex/PPLexerChange.cpp @@ -69,7 +69,7 @@ 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) { + SourceLocation Loc, bool IsSubmodule) { assert(!CurTokenLexer && "Cannot #include a file inside a macro!"); ++NumEnteredSourceFiles; @@ -78,7 +78,7 @@ void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, if (PTH) { if (PTHLexer *PL = PTH->CreateLexer(FID)) { - EnterSourceFileWithPTH(PL, CurDir); + EnterSourceFileWithPTH(PL, CurDir, IsSubmodule); return; } } @@ -101,14 +101,16 @@ void Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset); } - EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir); + EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir, + IsSubmodule); return; } /// 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) { + const DirectoryLookup *CurDir, + bool IsSubmodule) { // Add the current lexer to the include stack. if (CurPPLexer || CurTokenLexer) @@ -117,6 +119,7 @@ void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, CurLexer.reset(TheLexer); CurPPLexer = TheLexer; CurDirLookup = CurDir; + CurIsSubmodule = IsSubmodule; if (CurLexerKind != CLK_LexAfterModuleImport) CurLexerKind = CLK_Lexer; @@ -133,7 +136,8 @@ 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) { + const DirectoryLookup *CurDir, + bool IsSubmodule) { if (CurPPLexer || CurTokenLexer) PushIncludeMacroStack(); @@ -141,6 +145,7 @@ void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, CurDirLookup = CurDir; CurPTHLexer.reset(PL); CurPPLexer = CurPTHLexer.get(); + CurIsSubmodule = IsSubmodule; if (CurLexerKind != CLK_LexAfterModuleImport) CurLexerKind = CLK_PTHLexer; @@ -244,6 +249,29 @@ void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) { // but it might if they're empty? } +/// \brief Determine the location to use as the end of the buffer for a lexer. +/// +/// If the file ends with a newline, form the EOF token on the newline itself, +/// rather than "on the line following it", which doesn't exist. This makes +/// diagnostics relating to the end of file include the last file that the user +/// actually typed, which is goodness. +const char *Preprocessor::getCurLexerEndPos() { + const char *EndPos = CurLexer->BufferEnd; + if (EndPos != CurLexer->BufferStart && + (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { + --EndPos; + + // Handle \n\r and \r\n: + if (EndPos != CurLexer->BufferStart && + (EndPos[-1] == '\n' || EndPos[-1] == '\r') && + EndPos[-1] != EndPos[0]) + --EndPos; + } + + return EndPos; +} + + /// HandleEndOfFile - This callback is invoked when the lexer hits the end of /// the current file. This either returns the EOF token or pops a level off /// the include stack and keeps going. @@ -342,7 +370,19 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { FileID ExitedFID; 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; + if (LeavingSubmodule) { + const char *EndPos = getCurLexerEndPos(); + Result.startToken(); + CurLexer->BufferPtr = EndPos; + CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); + Result.setAnnotationEndLoc(Result.getLocation()); + Result.setAnnotationValue(0); + } + // We're done with the #included file. RemoveTopOfLexerStack(); @@ -357,27 +397,13 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { PPCallbacks::ExitFile, FileType, ExitedFID); } - // Client should lex another token. - return false; + // Client should lex another token unless we generated an EOM. + return LeavingSubmodule; } - // If the file ends with a newline, form the EOF token on the newline itself, - // rather than "on the line following it", which doesn't exist. This makes - // diagnostics relating to the end of file include the last file that the user - // actually typed, which is goodness. + // If this is the end of the main file, form an EOF token. if (CurLexer) { - const char *EndPos = CurLexer->BufferEnd; - if (EndPos != CurLexer->BufferStart && - (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { - --EndPos; - - // Handle \n\r and \r\n: - if (EndPos != CurLexer->BufferStart && - (EndPos[-1] == '\n' || EndPos[-1] == '\r') && - EndPos[-1] != EndPos[0]) - --EndPos; - } - + const char *EndPos = getCurLexerEndPos(); Result.startToken(); CurLexer->BufferPtr = EndPos; CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index b500efee4e6..92aeb5c3e5f 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -67,8 +67,8 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts, CodeComplete(0), CodeCompletionFile(0), CodeCompletionOffset(0), LastTokenWasAt(false), ModuleImportExpectsIdentifier(false), 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), CurIsSubmodule(false), + Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0), MICache(0), DeserialMIChainHead(0) { OwnsHeaderSearch = OwnsHeaders; |