Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 1 | //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements pieces of the Preprocessor interface that manage the |
| 11 | // current lexer stack. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | ebf0049 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Lex/HeaderSearch.h" |
| 19 | #include "clang/Lex/LexDiagnostic.h" |
| 20 | #include "clang/Lex/MacroInfo.h" |
| 21 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Ted Kremenek | 85b48c6 | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 552c169 | 2013-06-11 22:15:02 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Ted Kremenek | 3d9740d | 2008-11-18 01:33:13 +0000 | [diff] [blame] | 27 | PPCallbacks::~PPCallbacks() {} |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 28 | |
| 29 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e46832 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 30 | // Miscellaneous Methods. |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 33 | /// isInPrimaryFile - Return true if we're in the top-level file, not in a |
James Dennett | 1244a0d | 2012-06-22 05:36:05 +0000 | [diff] [blame] | 34 | /// \#include. This looks through macro expansions and active _Pragma lexers. |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 35 | bool Preprocessor::isInPrimaryFile() const { |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 36 | if (IsFileLexer()) |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 37 | return IncludeMacroStack.empty(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 39 | // If there are any stacked lexers, we're in a #include. |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 40 | assert(IsFileLexer(IncludeMacroStack[0]) && |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 41 | "Top level include stack isn't our primary lexer?"); |
| 42 | for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 43 | if (IsFileLexer(IncludeMacroStack[i])) |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 44 | return false; |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | /// getCurrentLexer - Return the current file lexer being lexed from. Note |
| 49 | /// that this ignores any potentially active macro expansions and _Pragma |
| 50 | /// expansions going on at the time. |
Ted Kremenek | b33ce32 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 51 | PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 52 | if (IsFileLexer()) |
Ted Kremenek | b33ce32 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 53 | return CurPPLexer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 55 | // Look for a stacked lexer. |
| 56 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
Ted Kremenek | b33ce32 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 57 | const IncludeStackInfo& ISI = IncludeMacroStack[i-1]; |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 58 | if (IsFileLexer(ISI)) |
Ted Kremenek | b33ce32 | 2008-11-20 01:49:44 +0000 | [diff] [blame] | 59 | return ISI.ThePPLexer; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 60 | } |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 61 | return nullptr; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | 3e46832 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 64 | |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | // Methods for Entering and Callbacks for leaving various contexts |
| 67 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 68 | |
| 69 | /// EnterSourceFile - Add a source file to the top of the include stack and |
Nuno Lopes | 0e5d13e | 2009-11-29 17:07:16 +0000 | [diff] [blame] | 70 | /// start lexing tokens from it instead of the current buffer. |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 71 | bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, |
| 72 | SourceLocation Loc) { |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 73 | assert(!CurTokenLexer && "Cannot #include a file inside a macro!"); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 74 | ++NumEnteredSourceFiles; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 76 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 77 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 78 | |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 79 | if (PTH) { |
Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 80 | if (PTHLexer *PL = PTH->CreateLexer(FID)) { |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 81 | EnterSourceFileWithPTH(PL, CurDir); |
| 82 | return false; |
Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 83 | } |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 85 | |
| 86 | // Get the MemoryBuffer for this FID, if it fails, we fail. |
Douglas Gregor | 4fb7fbe | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 87 | bool Invalid = false; |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 88 | const llvm::MemoryBuffer *InputFile = |
| 89 | getSourceManager().getBuffer(FID, Loc, &Invalid); |
| 90 | if (Invalid) { |
| 91 | SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID); |
| 92 | Diag(Loc, diag::err_pp_error_opening_file) |
| 93 | << std::string(SourceMgr.getBufferName(FileStart)) << ""; |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 94 | return true; |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 95 | } |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 96 | |
| 97 | if (isCodeCompletionEnabled() && |
| 98 | SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) { |
| 99 | CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID); |
| 100 | CodeCompletionLoc = |
Argyrios Kyrtzidis | e6e67de | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 101 | CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 104 | EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir); |
| 105 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | } |
Chris Lattner | c88a23e | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 108 | /// EnterSourceFileWithLexer - Add a source file to the top of the include stack |
| 109 | /// and start lexing tokens from it instead of the current buffer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 110 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 111 | const DirectoryLookup *CurDir) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 113 | // Add the current lexer to the include stack. |
Ted Kremenek | 4524521 | 2008-11-19 21:57:25 +0000 | [diff] [blame] | 114 | if (CurPPLexer || CurTokenLexer) |
Ted Kremenek | 7c1e61d | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 115 | PushIncludeMacroStack(); |
| 116 | |
Ted Kremenek | a0d2a16 | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 117 | CurLexer.reset(TheLexer); |
Ted Kremenek | 68ef9fc | 2008-11-18 00:12:49 +0000 | [diff] [blame] | 118 | CurPPLexer = TheLexer; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 119 | CurDirLookup = CurDir; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 120 | CurSubmodule = nullptr; |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 121 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 122 | CurLexerKind = CLK_Lexer; |
| 123 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 124 | // Notify the client, if desired, that we are in a new source file. |
| 125 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
Chris Lattner | 66a740e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 126 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | b03dc76 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 127 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 129 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 130 | PPCallbacks::EnterFile, FileType); |
| 131 | } |
| 132 | } |
| 133 | |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 134 | /// EnterSourceFileWithPTH - Add a source file to the top of the include stack |
| 135 | /// and start getting tokens from it using the PTH cache. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL, |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 137 | const DirectoryLookup *CurDir) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 139 | if (CurPPLexer || CurTokenLexer) |
| 140 | PushIncludeMacroStack(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 142 | CurDirLookup = CurDir; |
| 143 | CurPTHLexer.reset(PL); |
| 144 | CurPPLexer = CurPTHLexer.get(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 145 | CurSubmodule = nullptr; |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 146 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 147 | CurLexerKind = CLK_PTHLexer; |
| 148 | |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 149 | // Notify the client, if desired, that we are in a new source file. |
| 150 | if (Callbacks) { |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 151 | FileID FID = CurPPLexer->getFileID(); |
Chris Lattner | 4fd8b95 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 152 | SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID); |
| 153 | SrcMgr::CharacteristicKind FileType = |
| 154 | SourceMgr.getFileCharacteristic(EnterLoc); |
| 155 | Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType); |
Ted Kremenek | af058b5 | 2008-12-02 19:46:31 +0000 | [diff] [blame] | 156 | } |
| 157 | } |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 158 | |
| 159 | /// EnterMacro - Add a Macro to the top of the include stack and start lexing |
| 160 | /// tokens from it instead of the current buffer. |
Chris Lattner | 9dc9c20 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 161 | void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, |
Richard Smith | 5edd583 | 2012-08-30 13:38:46 +0000 | [diff] [blame] | 162 | MacroInfo *Macro, MacroArgs *Args) { |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 163 | std::unique_ptr<TokenLexer> TokLexer; |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 164 | if (NumCachedTokenLexers == 0) { |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 165 | TokLexer = llvm::make_unique<TokenLexer>(Tok, ILEnd, Macro, Args, *this); |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 166 | } else { |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 167 | TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]); |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 168 | TokLexer->Init(Tok, ILEnd, Macro, Args); |
| 169 | } |
| 170 | |
Ted Kremenek | 7c1e61d | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 171 | PushIncludeMacroStack(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 172 | CurDirLookup = nullptr; |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 173 | CurTokenLexer = std::move(TokLexer); |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 174 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 175 | CurLexerKind = CLK_TokenLexer; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /// EnterTokenStream - Add a "macro" context to the top of the include stack, |
Chris Lattner | 3e46832 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 179 | /// which will cause the lexer to start returning the specified tokens. |
| 180 | /// |
| 181 | /// If DisableMacroExpansion is true, tokens lexed from the token stream will |
| 182 | /// not be subject to further macro expansion. Otherwise, these tokens will |
| 183 | /// be re-macro-expanded when/if expansion is enabled. |
| 184 | /// |
| 185 | /// If OwnsTokens is false, this method assumes that the specified stream of |
| 186 | /// tokens has a permanent owner somewhere, so they do not need to be copied. |
| 187 | /// If it is true, it assumes the array of tokens is allocated with new[] and |
| 188 | /// must be freed. |
| 189 | /// |
| 190 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 191 | bool DisableMacroExpansion, |
| 192 | bool OwnsTokens) { |
Richard Smith | bdf54a21 | 2014-09-23 21:05:52 +0000 | [diff] [blame] | 193 | if (CurLexerKind == CLK_CachingLexer) { |
| 194 | if (CachedLexPos < CachedTokens.size()) { |
| 195 | // We're entering tokens into the middle of our cached token stream. We |
| 196 | // can't represent that, so just insert the tokens into the buffer. |
| 197 | CachedTokens.insert(CachedTokens.begin() + CachedLexPos, |
| 198 | Toks, Toks + NumToks); |
| 199 | if (OwnsTokens) |
| 200 | delete [] Toks; |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | // New tokens are at the end of the cached token sequnece; insert the |
| 205 | // token stream underneath the caching lexer. |
| 206 | ExitCachingLexMode(); |
| 207 | EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
| 208 | EnterCachingLexMode(); |
| 209 | return; |
| 210 | } |
| 211 | |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 212 | // Create a macro expander to expand from the specified token stream. |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 213 | std::unique_ptr<TokenLexer> TokLexer; |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 214 | if (NumCachedTokenLexers == 0) { |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 215 | TokLexer = llvm::make_unique<TokenLexer>( |
| 216 | Toks, NumToks, DisableMacroExpansion, OwnsTokens, *this); |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 217 | } else { |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 218 | TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]); |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 219 | TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
| 220 | } |
| 221 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 222 | // Save our current state. |
Ted Kremenek | 7c1e61d | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 223 | PushIncludeMacroStack(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 224 | CurDirLookup = nullptr; |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 225 | CurTokenLexer = std::move(TokLexer); |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 226 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 227 | CurLexerKind = CLK_TokenLexer; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 230 | /// \brief Compute the relative path that names the given file relative to |
| 231 | /// the given directory. |
| 232 | static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, |
| 233 | const FileEntry *File, |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 234 | SmallString<128> &Result) { |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 235 | Result.clear(); |
| 236 | |
| 237 | StringRef FilePath = File->getDir()->getName(); |
| 238 | StringRef Path = FilePath; |
| 239 | while (!Path.empty()) { |
| 240 | if (const DirectoryEntry *CurDir = FM.getDirectory(Path)) { |
| 241 | if (CurDir == Dir) { |
| 242 | Result = FilePath.substr(Path.size()); |
| 243 | llvm::sys::path::append(Result, |
| 244 | llvm::sys::path::filename(File->getName())); |
| 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | Path = llvm::sys::path::parent_path(Path); |
| 250 | } |
| 251 | |
| 252 | Result = File->getName(); |
| 253 | } |
| 254 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 255 | void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) { |
| 256 | if (CurTokenLexer) { |
| 257 | CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result); |
| 258 | return; |
| 259 | } |
| 260 | if (CurLexer) { |
| 261 | CurLexer->PropagateLineStartLeadingSpaceInfo(Result); |
| 262 | return; |
| 263 | } |
| 264 | // FIXME: Handle other kinds of lexers? It generally shouldn't matter, |
| 265 | // but it might if they're empty? |
| 266 | } |
| 267 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 268 | /// \brief Determine the location to use as the end of the buffer for a lexer. |
| 269 | /// |
| 270 | /// If the file ends with a newline, form the EOF token on the newline itself, |
| 271 | /// rather than "on the line following it", which doesn't exist. This makes |
| 272 | /// diagnostics relating to the end of file include the last file that the user |
| 273 | /// actually typed, which is goodness. |
| 274 | const char *Preprocessor::getCurLexerEndPos() { |
| 275 | const char *EndPos = CurLexer->BufferEnd; |
| 276 | if (EndPos != CurLexer->BufferStart && |
| 277 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
| 278 | --EndPos; |
| 279 | |
| 280 | // Handle \n\r and \r\n: |
| 281 | if (EndPos != CurLexer->BufferStart && |
| 282 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 283 | EndPos[-1] != EndPos[0]) |
| 284 | --EndPos; |
| 285 | } |
| 286 | |
| 287 | return EndPos; |
| 288 | } |
| 289 | |
| 290 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 291 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 292 | /// the current file. This either returns the EOF token or pops a level off |
| 293 | /// the include stack and keeps going. |
| 294 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 295 | assert(!CurTokenLexer && |
| 296 | "Ending a file when currently in a macro!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 298 | // See if this file had a controlling macro. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 299 | if (CurPPLexer) { // Not ending a macro, ignore it. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | if (const IdentifierInfo *ControllingMacro = |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 301 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 302 | // Okay, this has a controlling macro, remember in HeaderFileInfo. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | if (const FileEntry *FE = |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 304 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) { |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 305 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 306 | if (MacroInfo *MI = |
| 307 | getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) { |
| 308 | MI->UsedForHeaderGuard = true; |
| 309 | } |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 310 | if (const IdentifierInfo *DefinedMacro = |
| 311 | CurPPLexer->MIOpt.GetDefinedMacro()) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 312 | if (!isMacroDefined(ControllingMacro) && |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 313 | DefinedMacro != ControllingMacro && |
| 314 | HeaderInfo.FirstTimeLexingFile(FE)) { |
Ismail Pazarbasi | 8d0f2f3 | 2013-10-12 23:17:37 +0000 | [diff] [blame] | 315 | |
| 316 | // If the edit distance between the two macros is more than 50%, |
| 317 | // DefinedMacro may not be header guard, or can be header guard of |
| 318 | // another header file. Therefore, it maybe defining something |
| 319 | // completely different. This can be observed in the wild when |
| 320 | // handling feature macros or header guards in different files. |
| 321 | |
| 322 | const StringRef ControllingMacroName = ControllingMacro->getName(); |
| 323 | const StringRef DefinedMacroName = DefinedMacro->getName(); |
| 324 | const size_t MaxHalfLength = std::max(ControllingMacroName.size(), |
| 325 | DefinedMacroName.size()) / 2; |
| 326 | const unsigned ED = ControllingMacroName.edit_distance( |
| 327 | DefinedMacroName, true, MaxHalfLength); |
| 328 | if (ED <= MaxHalfLength) { |
| 329 | // Emit a warning for a bad header guard. |
| 330 | Diag(CurPPLexer->MIOpt.GetMacroLocation(), |
| 331 | diag::warn_header_guard) |
| 332 | << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro; |
| 333 | Diag(CurPPLexer->MIOpt.GetDefinedLocation(), |
| 334 | diag::note_header_guard) |
| 335 | << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro |
| 336 | << ControllingMacro |
| 337 | << FixItHint::CreateReplacement( |
| 338 | CurPPLexer->MIOpt.GetDefinedLocation(), |
| 339 | ControllingMacro->getName()); |
| 340 | } |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
John McCall | 95ff270 | 2011-10-18 00:44:04 +0000 | [diff] [blame] | 347 | // Complain about reaching a true EOF within arc_cf_code_audited. |
| 348 | // We don't want to complain about reaching the end of a macro |
| 349 | // instantiation or a _Pragma. |
| 350 | if (PragmaARCCFCodeAuditedLoc.isValid() && |
John McCall | 43d4dd4 | 2011-10-18 01:36:41 +0000 | [diff] [blame] | 351 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 352 | Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited); |
| 353 | |
| 354 | // Recover by leaving immediately. |
| 355 | PragmaARCCFCodeAuditedLoc = SourceLocation(); |
| 356 | } |
| 357 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 358 | // If this is a #include'd file, pop it off the include stack and continue |
| 359 | // lexing the #includer file. |
| 360 | if (!IncludeMacroStack.empty()) { |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 361 | |
| 362 | // If we lexed the code-completion file, act as if we reached EOF. |
| 363 | if (isCodeCompletionEnabled() && CurPPLexer && |
| 364 | SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) == |
| 365 | CodeCompletionFileLoc) { |
| 366 | if (CurLexer) { |
| 367 | Result.startToken(); |
| 368 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
| 369 | CurLexer.reset(); |
| 370 | } else { |
| 371 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
| 372 | CurPTHLexer->getEOF(Result); |
| 373 | CurPTHLexer.reset(); |
| 374 | } |
| 375 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 376 | CurPPLexer = nullptr; |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 380 | if (!isEndOfMacro && CurPPLexer && |
| 381 | SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) { |
| 382 | // Notify SourceManager to record the number of FileIDs that were created |
| 383 | // during lexing of the #include'd file. |
| 384 | unsigned NumFIDs = |
| 385 | SourceMgr.local_sloc_entry_size() - |
| 386 | CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/; |
| 387 | SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs); |
| 388 | } |
| 389 | |
Argyrios Kyrtzidis | 7a70d2f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 390 | FileID ExitedFID; |
| 391 | if (Callbacks && !isEndOfMacro && CurPPLexer) |
| 392 | ExitedFID = CurPPLexer->getFileID(); |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 393 | |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 394 | bool LeavingSubmodule = CurSubmodule && CurLexer; |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 395 | if (LeavingSubmodule) { |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 396 | // Notify the parser that we've left the module. |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 397 | const char *EndPos = getCurLexerEndPos(); |
| 398 | Result.startToken(); |
| 399 | CurLexer->BufferPtr = EndPos; |
| 400 | CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); |
| 401 | Result.setAnnotationEndLoc(Result.getLocation()); |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 402 | Result.setAnnotationValue(CurSubmodule); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 403 | |
| 404 | // We're done with this submodule. |
| 405 | LeaveSubmodule(); |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 408 | // We're done with the #included file. |
| 409 | RemoveTopOfLexerStack(); |
| 410 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 411 | // Propagate info about start-of-line/leading white-space/etc. |
| 412 | PropagateLineStartLeadingSpaceInfo(Result); |
| 413 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 414 | // Notify the client, if desired, that we are in a new source file. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 415 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
Chris Lattner | 66a740e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 416 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 4fd8b95 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 417 | SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); |
| 418 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
Argyrios Kyrtzidis | 7a70d2f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 419 | PPCallbacks::ExitFile, FileType, ExitedFID); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 422 | // Client should lex another token unless we generated an EOM. |
| 423 | return LeavingSubmodule; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 426 | // If this is the end of the main file, form an EOF token. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 427 | if (CurLexer) { |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 428 | const char *EndPos = getCurLexerEndPos(); |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 429 | Result.startToken(); |
| 430 | CurLexer->BufferPtr = EndPos; |
| 431 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 433 | if (isCodeCompletionEnabled()) { |
| 434 | // Inserting the code-completion point increases the source buffer by 1, |
| 435 | // but the main FileID was created before inserting the point. |
| 436 | // Compensate by reducing the EOF location by 1, otherwise the location |
| 437 | // will point to the next FileID. |
| 438 | // FIXME: This is hacky, the code-completion point should probably be |
| 439 | // inserted before the main FileID is created. |
| 440 | if (CurLexer->getFileLoc() == CodeCompletionFileLoc) |
| 441 | Result.setLocation(Result.getLocation().getLocWithOffset(-1)); |
| 442 | } |
| 443 | |
Axel Naumann | 2eb1d90 | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 444 | if (!isIncrementalProcessingEnabled()) |
| 445 | // We're done with lexing. |
| 446 | CurLexer.reset(); |
Chris Lattner | 190f64e | 2009-02-13 23:06:48 +0000 | [diff] [blame] | 447 | } else { |
| 448 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 449 | CurPTHLexer->getEOF(Result); |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 450 | CurPTHLexer.reset(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 451 | } |
Axel Naumann | 2eb1d90 | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 452 | |
| 453 | if (!isIncrementalProcessingEnabled()) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 454 | CurPPLexer = nullptr; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 455 | |
Argyrios Kyrtzidis | 8ed7414 | 2014-03-08 21:18:26 +0000 | [diff] [blame] | 456 | if (TUKind == TU_Complete) { |
Argyrios Kyrtzidis | e1974dc | 2014-03-07 07:47:58 +0000 | [diff] [blame] | 457 | // This is the end of the top-level file. 'WarnUnusedMacroLocs' has |
| 458 | // collected all macro locations that we need to warn because they are not |
| 459 | // used. |
| 460 | for (WarnUnusedMacroLocsTy::iterator |
| 461 | I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); |
| 462 | I!=E; ++I) |
| 463 | Diag(*I, diag::pp_macro_not_used); |
| 464 | } |
Daniel Dunbar | cb9eaf5 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 466 | // If we are building a module that has an umbrella header, make sure that |
| 467 | // each of the headers within the directory covered by the umbrella header |
| 468 | // was actually included by the umbrella header. |
| 469 | if (Module *Mod = getCurrentModule()) { |
| 470 | if (Mod->getUmbrellaHeader()) { |
| 471 | SourceLocation StartLoc |
| 472 | = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 473 | |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 474 | if (!getDiagnostics().isIgnored(diag::warn_uncovered_module_header, |
| 475 | StartLoc)) { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 476 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 477 | const DirectoryEntry *Dir = Mod->getUmbrellaDir().Entry; |
Ben Langmuir | 54cbc70 | 2014-06-25 23:53:43 +0000 | [diff] [blame] | 478 | vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 479 | std::error_code EC; |
Ben Langmuir | 54cbc70 | 2014-06-25 23:53:43 +0000 | [diff] [blame] | 480 | for (vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), End; |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 481 | Entry != End && !EC; Entry.increment(EC)) { |
| 482 | using llvm::StringSwitch; |
| 483 | |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 484 | // Check whether this entry has an extension typically associated with |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 485 | // headers. |
Ben Langmuir | 54cbc70 | 2014-06-25 23:53:43 +0000 | [diff] [blame] | 486 | if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->getName())) |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 487 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 488 | .Default(false)) |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 489 | continue; |
| 490 | |
Ben Langmuir | 54cbc70 | 2014-06-25 23:53:43 +0000 | [diff] [blame] | 491 | if (const FileEntry *Header = |
| 492 | getFileManager().getFile(Entry->getName())) |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 493 | if (!getSourceManager().hasFileInfo(Header)) { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 494 | if (!ModMap.isHeaderInUnavailableModule(Header)) { |
| 495 | // Find the relative path that would access this header. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 496 | SmallString<128> RelativePath; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 497 | computeRelativePath(FileMgr, Dir, Header, RelativePath); |
| 498 | Diag(StartLoc, diag::warn_uncovered_module_header) |
Douglas Gregor | 8f1f333 | 2013-01-04 18:58:28 +0000 | [diff] [blame] | 499 | << Mod->getFullModuleName() << RelativePath; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 500 | } |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
Douglas Gregor | f4e76b8 | 2013-05-20 13:49:41 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 507 | return true; |
| 508 | } |
| 509 | |
| 510 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 511 | /// hits the end of its token stream. |
| 512 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 513 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 514 | "Ending a macro when currently in a #include file!"); |
| 515 | |
Argyrios Kyrtzidis | 8cc0459 | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 516 | if (!MacroExpandingLexersStack.empty() && |
| 517 | MacroExpandingLexersStack.back().first == CurTokenLexer.get()) |
| 518 | removeCachedMacroExpandedTokensOfLastLexer(); |
| 519 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 520 | // Delete or cache the now-dead macro expander. |
| 521 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | a0d2a16 | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 522 | CurTokenLexer.reset(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 523 | else |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 524 | TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 525 | |
| 526 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 527 | return HandleEndOfFile(Result, true); |
| 528 | } |
| 529 | |
| 530 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 531 | /// lexer stack. This should only be used in situations where the current |
| 532 | /// state of the top-of-stack lexer is unknown. |
| 533 | void Preprocessor::RemoveTopOfLexerStack() { |
| 534 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 536 | if (CurTokenLexer) { |
| 537 | // Delete or cache the now-dead macro expander. |
| 538 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | a0d2a16 | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 539 | CurTokenLexer.reset(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 540 | else |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 541 | TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Ted Kremenek | 7c1e61d | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 544 | PopIncludeMacroStack(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 548 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 549 | /// state, returning the token on the next source line. |
| 550 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 551 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 552 | "Pasted comment can only be formed from macro"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 554 | // We handle this by scanning for the closest real lexer, switching it to |
| 555 | // raw mode and preprocessor mode. This will cause it to return \n as an |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 556 | // explicit EOD token. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 557 | PreprocessorLexer *FoundLexer = nullptr; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 558 | bool LexerWasInPPMode = false; |
| 559 | for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { |
| 560 | IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 561 | if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 563 | // Once we find a real lexer, mark it as raw mode (disabling macro |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 564 | // expansions) and preprocessor mode (return EOD). We know that the lexer |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 565 | // was *not* in raw mode before, because the macro that the comment came |
| 566 | // from was expanded. However, it could have already been in preprocessor |
| 567 | // mode (#if COMMENT) in which case we have to return it to that mode and |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 568 | // return EOD. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 569 | FoundLexer = ISI.ThePPLexer; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 570 | FoundLexer->LexingRawMode = true; |
| 571 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 572 | FoundLexer->ParsingPreprocessorDirective = true; |
| 573 | break; |
| 574 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 576 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 577 | // lexer. In either case, finish off the macro the comment came from, getting |
| 578 | // the next token. |
| 579 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 580 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 581 | // Discarding comments as long as we don't have EOF or EOD. This 'comments |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 582 | // out' the rest of the line, including any tokens that came from other macros |
| 583 | // that were active, as in: |
| 584 | // #define submacro a COMMENT b |
| 585 | // submacro c |
| 586 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 587 | while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 588 | Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 589 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 590 | // If we got an eod token, then we successfully found the end of the line. |
| 591 | if (Tok.is(tok::eod)) { |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 592 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 593 | // Restore the lexer back to normal mode instead of raw mode. |
| 594 | FoundLexer->LexingRawMode = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 596 | // If the lexer was already in preprocessor mode, just return the EOD token |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 597 | // to finish the preprocessor line. |
| 598 | if (LexerWasInPPMode) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 600 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 601 | FoundLexer->ParsingPreprocessorDirective = false; |
| 602 | return Lex(Tok); |
| 603 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 605 | // If we got an EOF token, then we reached the end of the token stream but |
| 606 | // didn't find an explicit \n. This can only happen if there was no lexer |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 607 | // active (an active lexer would return EOD at EOF if there was no \n in |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 608 | // preprocessor directive mode), so just return EOF as our token. |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 609 | assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode"); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 610 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 611 | |
Richard Smith | 50474bf | 2015-04-23 23:29:05 +0000 | [diff] [blame] | 612 | void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc) { |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 613 | if (!getLangOpts().ModulesLocalVisibility) { |
| 614 | // Just track that we entered this submodule. |
| 615 | BuildingSubmoduleStack.push_back( |
| 616 | BuildingSubmoduleInfo(M, ImportLoc, CurSubmoduleState)); |
| 617 | return; |
| 618 | } |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 619 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 620 | // Resolve as much of the module definition as we can now, before we enter |
| 621 | // one of its headers. |
| 622 | // FIXME: Can we enable Complain here? |
| 623 | // FIXME: Can we do this when local visibility is disabled? |
| 624 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
| 625 | ModMap.resolveExports(M, /*Complain=*/false); |
| 626 | ModMap.resolveUses(M, /*Complain=*/false); |
| 627 | ModMap.resolveConflicts(M, /*Complain=*/false); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 628 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 629 | // If this is the first time we've entered this module, set up its state. |
Richard Smith | e520293 | 2015-05-21 01:26:53 +0000 | [diff] [blame^] | 630 | auto R = Submodules.insert(std::make_pair(M, SubmoduleState())); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 631 | auto &State = R.first->second; |
| 632 | bool FirstTime = R.second; |
| 633 | if (FirstTime) { |
| 634 | // Determine the set of starting macros for this submodule; take these |
| 635 | // from the "null" module (the predefines buffer). |
| 636 | auto &StartingMacros = NullSubmoduleState.Macros; |
| 637 | |
| 638 | // Restore to the starting state. |
| 639 | // FIXME: Do this lazily, when each macro name is first referenced. |
| 640 | for (auto &Macro : StartingMacros) { |
| 641 | MacroState MS(Macro.second.getLatest()); |
| 642 | MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros()); |
| 643 | State.Macros.insert(std::make_pair(Macro.first, std::move(MS))); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | // Track that we entered this module. |
| 648 | BuildingSubmoduleStack.push_back( |
| 649 | BuildingSubmoduleInfo(M, ImportLoc, CurSubmoduleState)); |
| 650 | |
| 651 | // Switch to this submodule as the current submodule. |
| 652 | CurSubmoduleState = &State; |
| 653 | |
| 654 | // This module is visible to itself. |
| 655 | if (FirstTime) |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 656 | makeModuleVisible(M, ImportLoc); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | void Preprocessor::LeaveSubmodule() { |
| 660 | auto &Info = BuildingSubmoduleStack.back(); |
| 661 | |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 662 | Module *LeavingMod = Info.M; |
| 663 | SourceLocation ImportLoc = Info.ImportLoc; |
| 664 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 665 | // Create ModuleMacros for any macros defined in this submodule. |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 666 | for (auto &Macro : CurSubmoduleState->Macros) { |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 667 | auto *II = const_cast<IdentifierInfo*>(Macro.first); |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 668 | |
| 669 | // Find the starting point for the MacroDirective chain in this submodule. |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 670 | MacroDirective *OldMD = nullptr; |
| 671 | if (getLangOpts().ModulesLocalVisibility) { |
| 672 | // FIXME: It'd be better to start at the state from when we most recently |
| 673 | // entered this submodule, but it doesn't really matter. |
| 674 | auto &PredefMacros = NullSubmoduleState.Macros; |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 675 | auto PredefMacroIt = PredefMacros.find(Macro.first); |
| 676 | if (PredefMacroIt == PredefMacros.end()) |
| 677 | OldMD = nullptr; |
| 678 | else |
| 679 | OldMD = PredefMacroIt->second.getLatest(); |
| 680 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 681 | |
| 682 | // This module may have exported a new macro. If so, create a ModuleMacro |
| 683 | // representing that fact. |
| 684 | bool ExplicitlyPublic = false; |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 685 | for (auto *MD = Macro.second.getLatest(); MD != OldMD; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 686 | MD = MD->getPrevious()) { |
Richard Smith | 1e17285 | 2015-04-28 21:05:07 +0000 | [diff] [blame] | 687 | assert(MD && "broken macro directive chain"); |
| 688 | |
Richard Smith | 38477db | 2015-05-02 00:45:56 +0000 | [diff] [blame] | 689 | // Stop on macros defined in other submodules we #included along the way. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 690 | // There's no point doing this if we're tracking local submodule |
Richard Smith | 38477db | 2015-05-02 00:45:56 +0000 | [diff] [blame] | 691 | // visibility, since there can be no such directives in our list. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 692 | if (!getLangOpts().ModulesLocalVisibility) { |
| 693 | Module *Mod = getModuleContainingLocation(MD->getLocation()); |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 694 | if (Mod != LeavingMod) |
Richard Smith | 38477db | 2015-05-02 00:45:56 +0000 | [diff] [blame] | 695 | break; |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 696 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 697 | |
| 698 | if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) { |
| 699 | // The latest visibility directive for a name in a submodule affects |
| 700 | // all the directives that come before it. |
| 701 | if (VisMD->isPublic()) |
| 702 | ExplicitlyPublic = true; |
| 703 | else if (!ExplicitlyPublic) |
| 704 | // Private with no following public directive: not exported. |
| 705 | break; |
| 706 | } else { |
| 707 | MacroInfo *Def = nullptr; |
| 708 | if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) |
| 709 | Def = DefMD->getInfo(); |
| 710 | |
| 711 | // FIXME: Issue a warning if multiple headers for the same submodule |
| 712 | // define a macro, rather than silently ignoring all but the first. |
| 713 | bool IsNew; |
Richard Smith | 32dbd69 | 2015-05-02 01:14:40 +0000 | [diff] [blame] | 714 | // Don't bother creating a module macro if it would represent a #undef |
| 715 | // that doesn't override anything. |
| 716 | if (Def || !Macro.second.getOverriddenMacros().empty()) |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 717 | addModuleMacro(LeavingMod, II, Def, |
| 718 | Macro.second.getOverriddenMacros(), IsNew); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 719 | break; |
| 720 | } |
| 721 | } |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 722 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 723 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 724 | // Put back the outer module's state, if we're tracking it. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 725 | if (getLangOpts().ModulesLocalVisibility) |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 726 | CurSubmoduleState = Info.OuterSubmoduleState; |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 727 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 728 | BuildingSubmoduleStack.pop_back(); |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 729 | |
| 730 | // A nested #include makes the included submodule visible. |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 731 | if (!BuildingSubmoduleStack.empty() || !getLangOpts().ModulesLocalVisibility) |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 732 | makeModuleVisible(LeavingMod, ImportLoc); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 733 | } |