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