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" |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/PreprocessorOptions.h" |
Douglas Gregor | ebf0049 | 2011-10-17 15:32:29 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/LexDiagnostic.h" |
| 21 | #include "clang/Lex/MacroInfo.h" |
Reid Kleckner | 738d48d | 2015-11-02 17:53:55 +0000 | [diff] [blame] | 22 | #include "clang/Lex/PTHManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
Ted Kremenek | 85b48c6 | 2008-11-20 07:56:33 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 552c169 | 2013-06-11 22:15:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Path.h" |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 29 | PPCallbacks::~PPCallbacks() {} |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 30 | |
| 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3e46832 | 2008-03-10 06:06:04 +0000 | [diff] [blame] | 32 | // Miscellaneous Methods. |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 35 | /// 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] | 36 | /// \#include. This looks through macro expansions and active _Pragma lexers. |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 37 | bool Preprocessor::isInPrimaryFile() const { |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 38 | if (IsFileLexer()) |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 39 | return IncludeMacroStack.empty(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 41 | // If there are any stacked lexers, we're in a #include. |
Ted Kremenek | 6bc5f3e | 2008-11-20 16:19:53 +0000 | [diff] [blame] | 42 | assert(IsFileLexer(IncludeMacroStack[0]) && |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 43 | "Top level include stack isn't our primary lexer?"); |
NAKAMURA Takumi | a5b348be | 2017-09-18 04:55:31 +0000 | [diff] [blame] | 44 | return std::none_of( |
| 45 | IncludeMacroStack.begin() + 1, IncludeMacroStack.end(), |
Vitaly Buka | 55a27d3 | 2017-09-18 08:26:01 +0000 | [diff] [blame] | 46 | [&](const IncludeStackInfo &ISI) -> bool { return IsFileLexer(ISI); }); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 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. |
Erik Verbruggen | e4fd652 | 2016-10-26 13:06:13 +0000 | [diff] [blame] | 57 | for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) { |
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 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 710bb87 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 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; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 88 | const llvm::MemoryBuffer *InputFile = |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 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; |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 120 | CurLexerSubmodule = nullptr; |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 121 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 122 | CurLexerKind = CLK_Lexer; |
Yaron Keren | e02bcdc | 2015-11-07 16:35:07 +0000 | [diff] [blame] | 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(); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 145 | CurLexerSubmodule = nullptr; |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 146 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 147 | CurLexerKind = CLK_PTHLexer; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 230 | /// Compute the relative path that names the given file relative to |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 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()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 243 | llvm::sys::path::append(Result, |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 244 | llvm::sys::path::filename(File->getName())); |
| 245 | return; |
| 246 | } |
| 247 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 249 | Path = llvm::sys::path::parent_path(Path); |
| 250 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 251 | |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 268 | /// Determine the location to use as the end of the buffer for a lexer. |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 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 | |
Bruno Cardoso Lopes | b907563 | 2017-04-27 22:29:14 +0000 | [diff] [blame] | 290 | static void collectAllSubModulesWithUmbrellaHeader( |
| 291 | const Module &Mod, SmallVectorImpl<const Module *> &SubMods) { |
| 292 | if (Mod.getUmbrellaHeader()) |
| 293 | SubMods.push_back(&Mod); |
| 294 | for (auto *M : Mod.submodules()) |
| 295 | collectAllSubModulesWithUmbrellaHeader(*M, SubMods); |
| 296 | } |
| 297 | |
Bruno Cardoso Lopes | ce9a810 | 2017-04-27 22:29:10 +0000 | [diff] [blame] | 298 | void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) { |
| 299 | assert(Mod.getUmbrellaHeader() && "Module must use umbrella header"); |
| 300 | SourceLocation StartLoc = |
| 301 | SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 302 | if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header, StartLoc)) |
| 303 | return; |
| 304 | |
| 305 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
| 306 | const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 307 | llvm::vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem(); |
Bruno Cardoso Lopes | ce9a810 | 2017-04-27 22:29:10 +0000 | [diff] [blame] | 308 | std::error_code EC; |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 309 | for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), |
| 310 | End; |
Bruno Cardoso Lopes | ce9a810 | 2017-04-27 22:29:10 +0000 | [diff] [blame] | 311 | Entry != End && !EC; Entry.increment(EC)) { |
| 312 | using llvm::StringSwitch; |
| 313 | |
| 314 | // Check whether this entry has an extension typically associated with |
| 315 | // headers. |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 316 | if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path())) |
Bruno Cardoso Lopes | ce9a810 | 2017-04-27 22:29:10 +0000 | [diff] [blame] | 317 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 318 | .Default(false)) |
| 319 | continue; |
| 320 | |
Sam McCall | 0ae0056 | 2018-09-14 12:47:38 +0000 | [diff] [blame] | 321 | if (const FileEntry *Header = getFileManager().getFile(Entry->path())) |
Bruno Cardoso Lopes | ce9a810 | 2017-04-27 22:29:10 +0000 | [diff] [blame] | 322 | if (!getSourceManager().hasFileInfo(Header)) { |
| 323 | if (!ModMap.isHeaderInUnavailableModule(Header)) { |
| 324 | // Find the relative path that would access this header. |
| 325 | SmallString<128> RelativePath; |
| 326 | computeRelativePath(FileMgr, Dir, Header, RelativePath); |
| 327 | Diag(StartLoc, diag::warn_uncovered_module_header) |
| 328 | << Mod.getFullModuleName() << RelativePath; |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 334 | /// HandleEndOfFile - This callback is invoked when the lexer hits the end of |
| 335 | /// the current file. This either returns the EOF token or pops a level off |
| 336 | /// the include stack and keeps going. |
| 337 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 338 | assert(!CurTokenLexer && |
| 339 | "Ending a file when currently in a macro!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 341 | // If we have an unclosed module region from a pragma at the end of a |
| 342 | // module, complain and close it now. |
| 343 | // FIXME: This is not correct if we are building a module from PTH. |
| 344 | const bool LeavingSubmodule = CurLexer && CurLexerSubmodule; |
| 345 | if ((LeavingSubmodule || IncludeMacroStack.empty()) && |
| 346 | !BuildingSubmoduleStack.empty() && |
| 347 | BuildingSubmoduleStack.back().IsPragma) { |
| 348 | Diag(BuildingSubmoduleStack.back().ImportLoc, |
| 349 | diag::err_pp_module_begin_without_module_end); |
| 350 | Module *M = LeaveSubmodule(/*ForPragma*/true); |
| 351 | |
| 352 | Result.startToken(); |
| 353 | const char *EndPos = getCurLexerEndPos(); |
| 354 | CurLexer->BufferPtr = EndPos; |
| 355 | CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); |
| 356 | Result.setAnnotationEndLoc(Result.getLocation()); |
| 357 | Result.setAnnotationValue(M); |
| 358 | return true; |
| 359 | } |
| 360 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 361 | // See if this file had a controlling macro. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 362 | if (CurPPLexer) { // Not ending a macro, ignore it. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 363 | if (const IdentifierInfo *ControllingMacro = |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 364 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
Steve Naroff | 3fa455a | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 365 | // Okay, this has a controlling macro, remember in HeaderFileInfo. |
Yaron Keren | 6522461 | 2015-12-18 10:30:12 +0000 | [diff] [blame] | 366 | if (const FileEntry *FE = CurPPLexer->getFileEntry()) { |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 367 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
Argyrios Kyrtzidis | 9ef53ce | 2014-04-09 18:21:23 +0000 | [diff] [blame] | 368 | if (MacroInfo *MI = |
Yaron Keren | 9370ea2 | 2017-04-16 15:53:19 +0000 | [diff] [blame] | 369 | getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) |
| 370 | MI->setUsedForHeaderGuard(true); |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 371 | if (const IdentifierInfo *DefinedMacro = |
| 372 | CurPPLexer->MIOpt.GetDefinedMacro()) { |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 373 | if (!isMacroDefined(ControllingMacro) && |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 374 | DefinedMacro != ControllingMacro && |
| 375 | HeaderInfo.FirstTimeLexingFile(FE)) { |
Ismail Pazarbasi | 8d0f2f3 | 2013-10-12 23:17:37 +0000 | [diff] [blame] | 376 | |
| 377 | // If the edit distance between the two macros is more than 50%, |
| 378 | // DefinedMacro may not be header guard, or can be header guard of |
| 379 | // another header file. Therefore, it maybe defining something |
| 380 | // completely different. This can be observed in the wild when |
| 381 | // handling feature macros or header guards in different files. |
| 382 | |
| 383 | const StringRef ControllingMacroName = ControllingMacro->getName(); |
| 384 | const StringRef DefinedMacroName = DefinedMacro->getName(); |
| 385 | const size_t MaxHalfLength = std::max(ControllingMacroName.size(), |
| 386 | DefinedMacroName.size()) / 2; |
| 387 | const unsigned ED = ControllingMacroName.edit_distance( |
| 388 | DefinedMacroName, true, MaxHalfLength); |
| 389 | if (ED <= MaxHalfLength) { |
| 390 | // Emit a warning for a bad header guard. |
| 391 | Diag(CurPPLexer->MIOpt.GetMacroLocation(), |
| 392 | diag::warn_header_guard) |
| 393 | << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro; |
| 394 | Diag(CurPPLexer->MIOpt.GetDefinedLocation(), |
| 395 | diag::note_header_guard) |
| 396 | << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro |
| 397 | << ControllingMacro |
| 398 | << FixItHint::CreateReplacement( |
| 399 | CurPPLexer->MIOpt.GetDefinedLocation(), |
| 400 | ControllingMacro->getName()); |
| 401 | } |
Richard Trieu | 33a4b3d | 2013-06-12 21:20:57 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 405 | } |
| 406 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
John McCall | 95ff270 | 2011-10-18 00:44:04 +0000 | [diff] [blame] | 408 | // Complain about reaching a true EOF within arc_cf_code_audited. |
| 409 | // We don't want to complain about reaching the end of a macro |
| 410 | // instantiation or a _Pragma. |
| 411 | if (PragmaARCCFCodeAuditedLoc.isValid() && |
John McCall | 43d4dd4 | 2011-10-18 01:36:41 +0000 | [diff] [blame] | 412 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 413 | Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited); |
| 414 | |
| 415 | // Recover by leaving immediately. |
| 416 | PragmaARCCFCodeAuditedLoc = SourceLocation(); |
| 417 | } |
| 418 | |
Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 419 | // Complain about reaching a true EOF within assume_nonnull. |
| 420 | // We don't want to complain about reaching the end of a macro |
| 421 | // instantiation or a _Pragma. |
| 422 | if (PragmaAssumeNonNullLoc.isValid() && |
| 423 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { |
| 424 | Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull); |
| 425 | |
| 426 | // Recover by leaving immediately. |
| 427 | PragmaAssumeNonNullLoc = SourceLocation(); |
| 428 | } |
| 429 | |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 430 | bool LeavingPCHThroughHeader = false; |
| 431 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 432 | // If this is a #include'd file, pop it off the include stack and continue |
| 433 | // lexing the #includer file. |
| 434 | if (!IncludeMacroStack.empty()) { |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 435 | |
| 436 | // If we lexed the code-completion file, act as if we reached EOF. |
| 437 | if (isCodeCompletionEnabled() && CurPPLexer && |
| 438 | SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) == |
| 439 | CodeCompletionFileLoc) { |
| 440 | if (CurLexer) { |
| 441 | Result.startToken(); |
| 442 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
| 443 | CurLexer.reset(); |
| 444 | } else { |
| 445 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
| 446 | CurPTHLexer->getEOF(Result); |
| 447 | CurPTHLexer.reset(); |
| 448 | } |
| 449 | |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 450 | CurPPLexer = nullptr; |
Volodymyr Sapsai | 9d540f1 | 2018-01-19 23:41:47 +0000 | [diff] [blame] | 451 | recomputeCurLexerKind(); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 452 | return true; |
| 453 | } |
| 454 | |
Argyrios Kyrtzidis | 61ef3db | 2011-08-21 23:33:04 +0000 | [diff] [blame] | 455 | if (!isEndOfMacro && CurPPLexer && |
| 456 | SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) { |
| 457 | // Notify SourceManager to record the number of FileIDs that were created |
| 458 | // during lexing of the #include'd file. |
| 459 | unsigned NumFIDs = |
| 460 | SourceMgr.local_sloc_entry_size() - |
| 461 | CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/; |
| 462 | SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs); |
| 463 | } |
| 464 | |
Ilya Biryukov | f315000 | 2017-08-21 12:03:08 +0000 | [diff] [blame] | 465 | bool ExitedFromPredefinesFile = false; |
Argyrios Kyrtzidis | 7a70d2f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 466 | FileID ExitedFID; |
Ilya Biryukov | f315000 | 2017-08-21 12:03:08 +0000 | [diff] [blame] | 467 | if (!isEndOfMacro && CurPPLexer) { |
Argyrios Kyrtzidis | 7a70d2f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 468 | ExitedFID = CurPPLexer->getFileID(); |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 469 | |
Ilya Biryukov | f315000 | 2017-08-21 12:03:08 +0000 | [diff] [blame] | 470 | assert(PredefinesFileID.isValid() && |
| 471 | "HandleEndOfFile is called before PredefinesFileId is set"); |
| 472 | ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID); |
| 473 | } |
| 474 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 475 | if (LeavingSubmodule) { |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 476 | // We're done with this submodule. |
| 477 | Module *M = LeaveSubmodule(/*ForPragma*/false); |
| 478 | |
Richard Smith | 67294e2 | 2014-01-31 20:47:44 +0000 | [diff] [blame] | 479 | // Notify the parser that we've left the module. |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 480 | const char *EndPos = getCurLexerEndPos(); |
| 481 | Result.startToken(); |
| 482 | CurLexer->BufferPtr = EndPos; |
| 483 | CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); |
| 484 | Result.setAnnotationEndLoc(Result.getLocation()); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 485 | Result.setAnnotationValue(M); |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 488 | bool FoundPCHThroughHeader = false; |
| 489 | if (CurPPLexer && creatingPCHWithThroughHeader() && |
| 490 | isPCHThroughHeader( |
| 491 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
| 492 | FoundPCHThroughHeader = true; |
| 493 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 494 | // We're done with the #included file. |
| 495 | RemoveTopOfLexerStack(); |
| 496 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 497 | // Propagate info about start-of-line/leading white-space/etc. |
| 498 | PropagateLineStartLeadingSpaceInfo(Result); |
| 499 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 500 | // 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] | 501 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
Chris Lattner | 66a740e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 502 | SrcMgr::CharacteristicKind FileType = |
Chris Lattner | 4fd8b95 | 2009-01-19 08:01:53 +0000 | [diff] [blame] | 503 | SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); |
| 504 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
Argyrios Kyrtzidis | 7a70d2f | 2011-10-11 17:29:44 +0000 | [diff] [blame] | 505 | PPCallbacks::ExitFile, FileType, ExitedFID); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Ilya Biryukov | f315000 | 2017-08-21 12:03:08 +0000 | [diff] [blame] | 508 | // Restore conditional stack from the preamble right after exiting from the |
| 509 | // predefines file. |
| 510 | if (ExitedFromPredefinesFile) |
| 511 | replayPreambleConditionalStack(); |
| 512 | |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 513 | if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader && |
| 514 | (isInPrimaryFile() || |
| 515 | CurPPLexer->getFileID() == getPredefinesFileID())) { |
| 516 | // Leaving the through header. Continue directly to end of main file |
| 517 | // processing. |
| 518 | LeavingPCHThroughHeader = true; |
| 519 | } else { |
| 520 | // Client should lex another token unless we generated an EOM. |
| 521 | return LeavingSubmodule; |
| 522 | } |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 525 | // 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] | 526 | if (CurLexer) { |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 527 | const char *EndPos = getCurLexerEndPos(); |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 528 | Result.startToken(); |
| 529 | CurLexer->BufferPtr = EndPos; |
| 530 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | |
Argyrios Kyrtzidis | 9fd1571 | 2012-12-22 04:48:10 +0000 | [diff] [blame] | 532 | if (isCodeCompletionEnabled()) { |
| 533 | // Inserting the code-completion point increases the source buffer by 1, |
| 534 | // but the main FileID was created before inserting the point. |
| 535 | // Compensate by reducing the EOF location by 1, otherwise the location |
| 536 | // will point to the next FileID. |
| 537 | // FIXME: This is hacky, the code-completion point should probably be |
| 538 | // inserted before the main FileID is created. |
| 539 | if (CurLexer->getFileLoc() == CodeCompletionFileLoc) |
| 540 | Result.setLocation(Result.getLocation().getLocWithOffset(-1)); |
| 541 | } |
| 542 | |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 543 | if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader) { |
| 544 | // Reached the end of the compilation without finding the through header. |
| 545 | Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen) |
| 546 | << PPOpts->PCHThroughHeader << 0; |
| 547 | } |
| 548 | |
Axel Naumann | 2eb1d90 | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 549 | if (!isIncrementalProcessingEnabled()) |
| 550 | // We're done with lexing. |
| 551 | CurLexer.reset(); |
Chris Lattner | 190f64e | 2009-02-13 23:06:48 +0000 | [diff] [blame] | 552 | } else { |
| 553 | assert(CurPTHLexer && "Got EOF but no current lexer set!"); |
Ted Kremenek | 78cc247 | 2008-12-23 19:24:24 +0000 | [diff] [blame] | 554 | CurPTHLexer->getEOF(Result); |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 555 | CurPTHLexer.reset(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 556 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 557 | |
Axel Naumann | 2eb1d90 | 2012-03-16 10:40:17 +0000 | [diff] [blame] | 558 | if (!isIncrementalProcessingEnabled()) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 559 | CurPPLexer = nullptr; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 560 | |
Argyrios Kyrtzidis | 8ed7414 | 2014-03-08 21:18:26 +0000 | [diff] [blame] | 561 | if (TUKind == TU_Complete) { |
Argyrios Kyrtzidis | e1974dc | 2014-03-07 07:47:58 +0000 | [diff] [blame] | 562 | // This is the end of the top-level file. 'WarnUnusedMacroLocs' has |
| 563 | // collected all macro locations that we need to warn because they are not |
| 564 | // used. |
| 565 | for (WarnUnusedMacroLocsTy::iterator |
| 566 | I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); |
| 567 | I!=E; ++I) |
| 568 | Diag(*I, diag::pp_macro_not_used); |
| 569 | } |
Daniel Dunbar | cb9eaf5 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 570 | |
Douglas Gregor | fe76cfd | 2011-12-23 00:23:59 +0000 | [diff] [blame] | 571 | // If we are building a module that has an umbrella header, make sure that |
Bruno Cardoso Lopes | b907563 | 2017-04-27 22:29:14 +0000 | [diff] [blame] | 572 | // each of the headers within the directory, including all submodules, is |
| 573 | // covered by the umbrella header was actually included by the umbrella |
| 574 | // header. |
| 575 | if (Module *Mod = getCurrentModule()) { |
| 576 | llvm::SmallVector<const Module *, 4> AllMods; |
| 577 | collectAllSubModulesWithUmbrellaHeader(*Mod, AllMods); |
| 578 | for (auto *M : AllMods) |
| 579 | diagnoseMissingHeaderInUmbrellaDir(*M); |
| 580 | } |
Douglas Gregor | f4e76b8 | 2013-05-20 13:49:41 +0000 | [diff] [blame] | 581 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 582 | return true; |
| 583 | } |
| 584 | |
| 585 | /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer |
| 586 | /// hits the end of its token stream. |
| 587 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 588 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 589 | "Ending a macro when currently in a #include file!"); |
| 590 | |
Argyrios Kyrtzidis | 8cc0459 | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 591 | if (!MacroExpandingLexersStack.empty() && |
| 592 | MacroExpandingLexersStack.back().first == CurTokenLexer.get()) |
| 593 | removeCachedMacroExpandedTokensOfLastLexer(); |
| 594 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 595 | // Delete or cache the now-dead macro expander. |
| 596 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | a0d2a16 | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 597 | CurTokenLexer.reset(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 598 | else |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 599 | TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 600 | |
| 601 | // Handle this like a #include file being popped off the stack. |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 602 | return HandleEndOfFile(Result, true); |
| 603 | } |
| 604 | |
| 605 | /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the |
| 606 | /// lexer stack. This should only be used in situations where the current |
| 607 | /// state of the top-of-stack lexer is unknown. |
| 608 | void Preprocessor::RemoveTopOfLexerStack() { |
| 609 | assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 611 | if (CurTokenLexer) { |
| 612 | // Delete or cache the now-dead macro expander. |
| 613 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
Ted Kremenek | a0d2a16 | 2008-11-13 17:11:24 +0000 | [diff] [blame] | 614 | CurTokenLexer.reset(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 615 | else |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 616 | TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Ted Kremenek | 7c1e61d | 2008-11-13 16:51:03 +0000 | [diff] [blame] | 619 | PopIncludeMacroStack(); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /// HandleMicrosoftCommentPaste - When the macro expander pastes together a |
| 623 | /// comment (/##/) in microsoft mode, this method handles updating the current |
| 624 | /// state, returning the token on the next source line. |
| 625 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 626 | assert(CurTokenLexer && !CurPPLexer && |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 627 | "Pasted comment can only be formed from macro"); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 628 | // We handle this by scanning for the closest real lexer, switching it to |
| 629 | // 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] | 630 | // explicit EOD token. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 631 | PreprocessorLexer *FoundLexer = nullptr; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 632 | bool LexerWasInPPMode = false; |
Erik Verbruggen | e4fd652 | 2016-10-26 13:06:13 +0000 | [diff] [blame] | 633 | for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 634 | if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 636 | // 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] | 637 | // expansions) and preprocessor mode (return EOD). We know that the lexer |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 638 | // was *not* in raw mode before, because the macro that the comment came |
| 639 | // from was expanded. However, it could have already been in preprocessor |
| 640 | // 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] | 641 | // return EOD. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 642 | FoundLexer = ISI.ThePPLexer; |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 643 | FoundLexer->LexingRawMode = true; |
| 644 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 645 | FoundLexer->ParsingPreprocessorDirective = true; |
| 646 | break; |
| 647 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 649 | // Okay, we either found and switched over the lexer, or we didn't find a |
| 650 | // lexer. In either case, finish off the macro the comment came from, getting |
| 651 | // the next token. |
| 652 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 654 | // 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] | 655 | // out' the rest of the line, including any tokens that came from other macros |
| 656 | // that were active, as in: |
| 657 | // #define submacro a COMMENT b |
| 658 | // submacro c |
| 659 | // which should lex to 'a' only: 'b' and 'c' should be removed. |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 660 | while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 661 | Lex(Tok); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 663 | // If we got an eod token, then we successfully found the end of the line. |
| 664 | if (Tok.is(tok::eod)) { |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 665 | assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 666 | // Restore the lexer back to normal mode instead of raw mode. |
| 667 | FoundLexer->LexingRawMode = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 669 | // 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] | 670 | // to finish the preprocessor line. |
| 671 | if (LexerWasInPPMode) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 672 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 673 | // Otherwise, switch out of PP mode and return the next lexed token. |
| 674 | FoundLexer->ParsingPreprocessorDirective = false; |
| 675 | return Lex(Tok); |
| 676 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 678 | // If we got an EOF token, then we reached the end of the token stream but |
| 679 | // 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] | 680 | // 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] | 681 | // preprocessor directive mode), so just return EOF as our token. |
Peter Collingbourne | 2f1e36b | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 682 | assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode"); |
Chris Lattner | 1eed734 | 2008-03-09 04:10:46 +0000 | [diff] [blame] | 683 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 684 | |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 685 | void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc, |
| 686 | bool ForPragma) { |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 687 | if (!getLangOpts().ModulesLocalVisibility) { |
| 688 | // Just track that we entered this submodule. |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 689 | BuildingSubmoduleStack.push_back( |
| 690 | BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState, |
| 691 | PendingModuleMacroNames.size())); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 694 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 695 | // Resolve as much of the module definition as we can now, before we enter |
| 696 | // one of its headers. |
| 697 | // FIXME: Can we enable Complain here? |
| 698 | // FIXME: Can we do this when local visibility is disabled? |
| 699 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
| 700 | ModMap.resolveExports(M, /*Complain=*/false); |
| 701 | ModMap.resolveUses(M, /*Complain=*/false); |
| 702 | ModMap.resolveConflicts(M, /*Complain=*/false); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 703 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 704 | // 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] | 705 | auto R = Submodules.insert(std::make_pair(M, SubmoduleState())); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 706 | auto &State = R.first->second; |
| 707 | bool FirstTime = R.second; |
| 708 | if (FirstTime) { |
| 709 | // Determine the set of starting macros for this submodule; take these |
| 710 | // from the "null" module (the predefines buffer). |
Richard Smith | 4df6093 | 2015-06-30 21:29:55 +0000 | [diff] [blame] | 711 | // |
| 712 | // FIXME: If we have local visibility but not modules enabled, the |
| 713 | // NullSubmoduleState is polluted by #defines in the top-level source |
| 714 | // file. |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 715 | auto &StartingMacros = NullSubmoduleState.Macros; |
| 716 | |
| 717 | // Restore to the starting state. |
| 718 | // FIXME: Do this lazily, when each macro name is first referenced. |
| 719 | for (auto &Macro : StartingMacros) { |
Richard Smith | 4df6093 | 2015-06-30 21:29:55 +0000 | [diff] [blame] | 720 | // Skip uninteresting macros. |
| 721 | if (!Macro.second.getLatest() && |
| 722 | Macro.second.getOverriddenMacros().empty()) |
| 723 | continue; |
| 724 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 725 | MacroState MS(Macro.second.getLatest()); |
| 726 | MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros()); |
| 727 | State.Macros.insert(std::make_pair(Macro.first, std::move(MS))); |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | // Track that we entered this module. |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 732 | BuildingSubmoduleStack.push_back( |
| 733 | BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState, |
| 734 | PendingModuleMacroNames.size())); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 735 | |
| 736 | // Switch to this submodule as the current submodule. |
| 737 | CurSubmoduleState = &State; |
| 738 | |
| 739 | // This module is visible to itself. |
| 740 | if (FirstTime) |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 741 | makeModuleVisible(M, ImportLoc); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 744 | bool Preprocessor::needModuleMacros() const { |
| 745 | // If we're not within a submodule, we never need to create ModuleMacros. |
| 746 | if (BuildingSubmoduleStack.empty()) |
| 747 | return false; |
| 748 | // If we are tracking module macro visibility even for textually-included |
| 749 | // headers, we need ModuleMacros. |
| 750 | if (getLangOpts().ModulesLocalVisibility) |
| 751 | return true; |
| 752 | // Otherwise, we only need module macros if we're actually compiling a module |
| 753 | // interface. |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 754 | return getLangOpts().isCompilingModule(); |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 757 | Module *Preprocessor::LeaveSubmodule(bool ForPragma) { |
| 758 | if (BuildingSubmoduleStack.empty() || |
| 759 | BuildingSubmoduleStack.back().IsPragma != ForPragma) { |
| 760 | assert(ForPragma && "non-pragma module enter/leave mismatch"); |
| 761 | return nullptr; |
| 762 | } |
| 763 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 764 | auto &Info = BuildingSubmoduleStack.back(); |
| 765 | |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 766 | Module *LeavingMod = Info.M; |
| 767 | SourceLocation ImportLoc = Info.ImportLoc; |
| 768 | |
Richard Smith | 4971ed0 | 2017-05-19 23:32:38 +0000 | [diff] [blame] | 769 | if (!needModuleMacros() || |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 770 | (!getLangOpts().ModulesLocalVisibility && |
| 771 | LeavingMod->getTopLevelModuleName() != getLangOpts().CurrentModule)) { |
| 772 | // If we don't need module macros, or this is not a module for which we |
| 773 | // are tracking macro visibility, don't build any, and preserve the list |
| 774 | // of pending names for the surrounding submodule. |
Richard Smith | e5b5350 | 2016-02-19 22:43:58 +0000 | [diff] [blame] | 775 | BuildingSubmoduleStack.pop_back(); |
| 776 | makeModuleVisible(LeavingMod, ImportLoc); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 777 | return LeavingMod; |
Richard Smith | e5b5350 | 2016-02-19 22:43:58 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 780 | // Create ModuleMacros for any macros defined in this submodule. |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 781 | llvm::SmallPtrSet<const IdentifierInfo*, 8> VisitedMacros; |
| 782 | for (unsigned I = Info.OuterPendingModuleMacroNames; |
| 783 | I != PendingModuleMacroNames.size(); ++I) { |
| 784 | auto *II = const_cast<IdentifierInfo*>(PendingModuleMacroNames[I]); |
| 785 | if (!VisitedMacros.insert(II).second) |
| 786 | continue; |
| 787 | |
| 788 | auto MacroIt = CurSubmoduleState->Macros.find(II); |
| 789 | if (MacroIt == CurSubmoduleState->Macros.end()) |
| 790 | continue; |
| 791 | auto &Macro = MacroIt->second; |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 792 | |
| 793 | // Find the starting point for the MacroDirective chain in this submodule. |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 794 | MacroDirective *OldMD = nullptr; |
Richard Smith | e5b5350 | 2016-02-19 22:43:58 +0000 | [diff] [blame] | 795 | auto *OldState = Info.OuterSubmoduleState; |
| 796 | if (getLangOpts().ModulesLocalVisibility) |
| 797 | OldState = &NullSubmoduleState; |
| 798 | if (OldState && OldState != CurSubmoduleState) { |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 799 | // FIXME: It'd be better to start at the state from when we most recently |
| 800 | // entered this submodule, but it doesn't really matter. |
Richard Smith | e5b5350 | 2016-02-19 22:43:58 +0000 | [diff] [blame] | 801 | auto &OldMacros = OldState->Macros; |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 802 | auto OldMacroIt = OldMacros.find(II); |
Richard Smith | e5b5350 | 2016-02-19 22:43:58 +0000 | [diff] [blame] | 803 | if (OldMacroIt == OldMacros.end()) |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 804 | OldMD = nullptr; |
| 805 | else |
Richard Smith | e5b5350 | 2016-02-19 22:43:58 +0000 | [diff] [blame] | 806 | OldMD = OldMacroIt->second.getLatest(); |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 807 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 808 | |
| 809 | // This module may have exported a new macro. If so, create a ModuleMacro |
| 810 | // representing that fact. |
| 811 | bool ExplicitlyPublic = false; |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 812 | for (auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->getPrevious()) { |
Richard Smith | 1e17285 | 2015-04-28 21:05:07 +0000 | [diff] [blame] | 813 | assert(MD && "broken macro directive chain"); |
| 814 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 815 | if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) { |
| 816 | // The latest visibility directive for a name in a submodule affects |
| 817 | // all the directives that come before it. |
| 818 | if (VisMD->isPublic()) |
| 819 | ExplicitlyPublic = true; |
| 820 | else if (!ExplicitlyPublic) |
| 821 | // Private with no following public directive: not exported. |
| 822 | break; |
| 823 | } else { |
| 824 | MacroInfo *Def = nullptr; |
| 825 | if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) |
| 826 | Def = DefMD->getInfo(); |
| 827 | |
| 828 | // FIXME: Issue a warning if multiple headers for the same submodule |
| 829 | // define a macro, rather than silently ignoring all but the first. |
| 830 | bool IsNew; |
Richard Smith | 32dbd69 | 2015-05-02 01:14:40 +0000 | [diff] [blame] | 831 | // Don't bother creating a module macro if it would represent a #undef |
| 832 | // that doesn't override anything. |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 833 | if (Def || !Macro.getOverriddenMacros().empty()) |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 834 | addModuleMacro(LeavingMod, II, Def, |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 835 | Macro.getOverriddenMacros(), IsNew); |
Richard Smith | 4971ed0 | 2017-05-19 23:32:38 +0000 | [diff] [blame] | 836 | |
| 837 | if (!getLangOpts().ModulesLocalVisibility) { |
| 838 | // This macro is exposed to the rest of this compilation as a |
| 839 | // ModuleMacro; we don't need to track its MacroDirective any more. |
| 840 | Macro.setLatest(nullptr); |
| 841 | Macro.setOverriddenMacros(*this, {}); |
| 842 | } |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 843 | break; |
| 844 | } |
| 845 | } |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 846 | } |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 847 | PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 848 | |
Richard Smith | 4df6093 | 2015-06-30 21:29:55 +0000 | [diff] [blame] | 849 | // FIXME: Before we leave this submodule, we should parse all the other |
| 850 | // headers within it. Otherwise, we're left with an inconsistent state |
| 851 | // where we've made the module visible but don't yet have its complete |
| 852 | // contents. |
| 853 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 854 | // Put back the outer module's state, if we're tracking it. |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 855 | if (getLangOpts().ModulesLocalVisibility) |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 856 | CurSubmoduleState = Info.OuterSubmoduleState; |
Richard Smith | ee97793 | 2015-05-01 21:22:17 +0000 | [diff] [blame] | 857 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 858 | BuildingSubmoduleStack.pop_back(); |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 859 | |
| 860 | // A nested #include makes the included submodule visible. |
Richard Smith | 4df6093 | 2015-06-30 21:29:55 +0000 | [diff] [blame] | 861 | makeModuleVisible(LeavingMod, ImportLoc); |
Richard Smith | d138630 | 2017-05-04 00:29:54 +0000 | [diff] [blame] | 862 | return LeavingMod; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 863 | } |