blob: e68fb7df8e88a8ba84dd47d76714676c0c10161b [file] [log] [blame]
Chris Lattner1eed7342008-03-09 04:10:46 +00001//===--- 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 Gregorebf00492011-10-17 15:32:29 +000016#include "clang/Basic/FileManager.h"
Chris Lattner1eed7342008-03-09 04:10:46 +000017#include "clang/Basic/SourceManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Lex/HeaderSearch.h"
19#include "clang/Lex/LexDiagnostic.h"
20#include "clang/Lex/MacroInfo.h"
21#include "llvm/ADT/StringSwitch.h"
Douglas Gregorfe76cfd2011-12-23 00:23:59 +000022#include "llvm/Support/FileSystem.h"
Ted Kremenek85b48c62008-11-20 07:56:33 +000023#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000024#include "llvm/Support/Path.h"
Chris Lattner1eed7342008-03-09 04:10:46 +000025using namespace clang;
26
Ted Kremenek3d9740d2008-11-18 01:33:13 +000027PPCallbacks::~PPCallbacks() {}
Chris Lattner1eed7342008-03-09 04:10:46 +000028
29//===----------------------------------------------------------------------===//
Chris Lattner3e468322008-03-10 06:06:04 +000030// Miscellaneous Methods.
Chris Lattner1eed7342008-03-09 04:10:46 +000031//===----------------------------------------------------------------------===//
32
Chris Lattner1eed7342008-03-09 04:10:46 +000033/// isInPrimaryFile - Return true if we're in the top-level file, not in a
James Dennett1244a0d2012-06-22 05:36:05 +000034/// \#include. This looks through macro expansions and active _Pragma lexers.
Chris Lattner1eed7342008-03-09 04:10:46 +000035bool Preprocessor::isInPrimaryFile() const {
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +000036 if (IsFileLexer())
Chris Lattner1eed7342008-03-09 04:10:46 +000037 return IncludeMacroStack.empty();
Mike Stump11289f42009-09-09 15:08:12 +000038
Chris Lattner1eed7342008-03-09 04:10:46 +000039 // If there are any stacked lexers, we're in a #include.
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +000040 assert(IsFileLexer(IncludeMacroStack[0]) &&
Chris Lattner1eed7342008-03-09 04:10:46 +000041 "Top level include stack isn't our primary lexer?");
42 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +000043 if (IsFileLexer(IncludeMacroStack[i]))
Chris Lattner1eed7342008-03-09 04:10:46 +000044 return false;
45 return true;
46}
47
48/// getCurrentLexer - Return the current file lexer being lexed from. Note
49/// that this ignores any potentially active macro expansions and _Pragma
50/// expansions going on at the time.
Ted Kremenekb33ce322008-11-20 01:49:44 +000051PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +000052 if (IsFileLexer())
Ted Kremenekb33ce322008-11-20 01:49:44 +000053 return CurPPLexer;
Mike Stump11289f42009-09-09 15:08:12 +000054
Chris Lattner1eed7342008-03-09 04:10:46 +000055 // Look for a stacked lexer.
56 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) {
Ted Kremenekb33ce322008-11-20 01:49:44 +000057 const IncludeStackInfo& ISI = IncludeMacroStack[i-1];
Ted Kremenek6bc5f3e2008-11-20 16:19:53 +000058 if (IsFileLexer(ISI))
Ted Kremenekb33ce322008-11-20 01:49:44 +000059 return ISI.ThePPLexer;
Chris Lattner1eed7342008-03-09 04:10:46 +000060 }
Craig Topperd2d442c2014-05-17 23:10:59 +000061 return nullptr;
Chris Lattner1eed7342008-03-09 04:10:46 +000062}
63
Chris Lattner3e468322008-03-10 06:06:04 +000064
65//===----------------------------------------------------------------------===//
66// Methods for Entering and Callbacks for leaving various contexts
67//===----------------------------------------------------------------------===//
Chris Lattner1eed7342008-03-09 04:10:46 +000068
69/// EnterSourceFile - Add a source file to the top of the include stack and
Nuno Lopes0e5d13e2009-11-29 17:07:16 +000070/// start lexing tokens from it instead of the current buffer.
Richard Smith67294e22014-01-31 20:47:44 +000071bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir,
72 SourceLocation Loc) {
David Blaikie7d170102013-05-15 07:37:26 +000073 assert(!CurTokenLexer && "Cannot #include a file inside a macro!");
Chris Lattner1eed7342008-03-09 04:10:46 +000074 ++NumEnteredSourceFiles;
Mike Stump11289f42009-09-09 15:08:12 +000075
Chris Lattner1eed7342008-03-09 04:10:46 +000076 if (MaxIncludeStackDepth < IncludeMacroStack.size())
77 MaxIncludeStackDepth = IncludeMacroStack.size();
78
Ted Kremenekaf058b52008-12-02 19:46:31 +000079 if (PTH) {
Chris Lattner710bb872009-11-30 04:18:44 +000080 if (PTHLexer *PL = PTH->CreateLexer(FID)) {
Richard Smith67294e22014-01-31 20:47:44 +000081 EnterSourceFileWithPTH(PL, CurDir);
82 return false;
Chris Lattner710bb872009-11-30 04:18:44 +000083 }
Ted Kremenekaf058b52008-12-02 19:46:31 +000084 }
Chris Lattner710bb872009-11-30 04:18:44 +000085
86 // Get the MemoryBuffer for this FID, if it fails, we fail.
Douglas Gregor4fb7fbe2010-03-16 20:01:30 +000087 bool Invalid = false;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +000088 const llvm::MemoryBuffer *InputFile =
89 getSourceManager().getBuffer(FID, Loc, &Invalid);
90 if (Invalid) {
91 SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID);
92 Diag(Loc, diag::err_pp_error_opening_file)
93 << std::string(SourceMgr.getBufferName(FileStart)) << "";
Richard Smith67294e22014-01-31 20:47:44 +000094 return true;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +000095 }
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +000096
97 if (isCodeCompletionEnabled() &&
98 SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
99 CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
100 CodeCompletionLoc =
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +0000101 CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000102 }
103
Richard Smith67294e22014-01-31 20:47:44 +0000104 EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir);
105 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000106}
Chris Lattnerc88a23e2008-09-26 20:12:23 +0000107
Ted Kremenekaf058b52008-12-02 19:46:31 +0000108/// 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 Stump11289f42009-09-09 15:08:12 +0000110void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
Richard Smith67294e22014-01-31 20:47:44 +0000111 const DirectoryLookup *CurDir) {
Mike Stump11289f42009-09-09 15:08:12 +0000112
Chris Lattner1eed7342008-03-09 04:10:46 +0000113 // Add the current lexer to the include stack.
Ted Kremenek45245212008-11-19 21:57:25 +0000114 if (CurPPLexer || CurTokenLexer)
Ted Kremenek7c1e61d2008-11-13 16:51:03 +0000115 PushIncludeMacroStack();
116
Ted Kremeneka0d2a162008-11-13 17:11:24 +0000117 CurLexer.reset(TheLexer);
Ted Kremenek68ef9fc2008-11-18 00:12:49 +0000118 CurPPLexer = TheLexer;
Chris Lattner1eed7342008-03-09 04:10:46 +0000119 CurDirLookup = CurDir;
Craig Topperd2d442c2014-05-17 23:10:59 +0000120 CurSubmodule = nullptr;
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000121 if (CurLexerKind != CLK_LexAfterModuleImport)
122 CurLexerKind = CLK_Lexer;
123
Chris Lattner1eed7342008-03-09 04:10:46 +0000124 // Notify the client, if desired, that we are in a new source file.
125 if (Callbacks && !CurLexer->Is_PragmaLexer) {
Chris Lattner66a740e2008-10-27 01:19:25 +0000126 SrcMgr::CharacteristicKind FileType =
Chris Lattnerb03dc762008-09-26 21:18:42 +0000127 SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
Mike Stump11289f42009-09-09 15:08:12 +0000128
Chris Lattner1eed7342008-03-09 04:10:46 +0000129 Callbacks->FileChanged(CurLexer->getFileLoc(),
130 PPCallbacks::EnterFile, FileType);
131 }
132}
133
Ted Kremenekaf058b52008-12-02 19:46:31 +0000134/// EnterSourceFileWithPTH - Add a source file to the top of the include stack
135/// and start getting tokens from it using the PTH cache.
Mike Stump11289f42009-09-09 15:08:12 +0000136void Preprocessor::EnterSourceFileWithPTH(PTHLexer *PL,
Richard Smith67294e22014-01-31 20:47:44 +0000137 const DirectoryLookup *CurDir) {
Mike Stump11289f42009-09-09 15:08:12 +0000138
Ted Kremenekaf058b52008-12-02 19:46:31 +0000139 if (CurPPLexer || CurTokenLexer)
140 PushIncludeMacroStack();
Chris Lattner1eed7342008-03-09 04:10:46 +0000141
Ted Kremenekaf058b52008-12-02 19:46:31 +0000142 CurDirLookup = CurDir;
143 CurPTHLexer.reset(PL);
144 CurPPLexer = CurPTHLexer.get();
Craig Topperd2d442c2014-05-17 23:10:59 +0000145 CurSubmodule = nullptr;
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000146 if (CurLexerKind != CLK_LexAfterModuleImport)
147 CurLexerKind = CLK_PTHLexer;
148
Ted Kremenekaf058b52008-12-02 19:46:31 +0000149 // Notify the client, if desired, that we are in a new source file.
150 if (Callbacks) {
Chris Lattnerd32480d2009-01-17 06:22:33 +0000151 FileID FID = CurPPLexer->getFileID();
Chris Lattner4fd8b952009-01-19 08:01:53 +0000152 SourceLocation EnterLoc = SourceMgr.getLocForStartOfFile(FID);
153 SrcMgr::CharacteristicKind FileType =
154 SourceMgr.getFileCharacteristic(EnterLoc);
155 Callbacks->FileChanged(EnterLoc, PPCallbacks::EnterFile, FileType);
Ted Kremenekaf058b52008-12-02 19:46:31 +0000156 }
157}
Chris Lattner1eed7342008-03-09 04:10:46 +0000158
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 Lattner9dc9c202009-02-15 20:52:18 +0000161void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
Richard Smith5edd5832012-08-30 13:38:46 +0000162 MacroInfo *Macro, MacroArgs *Args) {
David Blaikie6d5038c2014-08-29 19:36:52 +0000163 std::unique_ptr<TokenLexer> TokLexer;
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000164 if (NumCachedTokenLexers == 0) {
David Blaikie6d5038c2014-08-29 19:36:52 +0000165 TokLexer = llvm::make_unique<TokenLexer>(Tok, ILEnd, Macro, Args, *this);
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000166 } else {
David Blaikie6d5038c2014-08-29 19:36:52 +0000167 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000168 TokLexer->Init(Tok, ILEnd, Macro, Args);
169 }
170
Ted Kremenek7c1e61d2008-11-13 16:51:03 +0000171 PushIncludeMacroStack();
Craig Topperd2d442c2014-05-17 23:10:59 +0000172 CurDirLookup = nullptr;
David Blaikie6d5038c2014-08-29 19:36:52 +0000173 CurTokenLexer = std::move(TokLexer);
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000174 if (CurLexerKind != CLK_LexAfterModuleImport)
175 CurLexerKind = CLK_TokenLexer;
Chris Lattner1eed7342008-03-09 04:10:46 +0000176}
177
178/// EnterTokenStream - Add a "macro" context to the top of the include stack,
Chris Lattner3e468322008-03-10 06:06:04 +0000179/// 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///
190void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
191 bool DisableMacroExpansion,
192 bool OwnsTokens) {
Richard Smithbdf54a212014-09-23 21:05:52 +0000193 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 Kyrtzidis9fd15712012-12-22 04:48:10 +0000212 // Create a macro expander to expand from the specified token stream.
David Blaikie6d5038c2014-08-29 19:36:52 +0000213 std::unique_ptr<TokenLexer> TokLexer;
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000214 if (NumCachedTokenLexers == 0) {
David Blaikie6d5038c2014-08-29 19:36:52 +0000215 TokLexer = llvm::make_unique<TokenLexer>(
216 Toks, NumToks, DisableMacroExpansion, OwnsTokens, *this);
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000217 } else {
David Blaikie6d5038c2014-08-29 19:36:52 +0000218 TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000219 TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens);
220 }
221
Chris Lattner1eed7342008-03-09 04:10:46 +0000222 // Save our current state.
Ted Kremenek7c1e61d2008-11-13 16:51:03 +0000223 PushIncludeMacroStack();
Craig Topperd2d442c2014-05-17 23:10:59 +0000224 CurDirLookup = nullptr;
David Blaikie6d5038c2014-08-29 19:36:52 +0000225 CurTokenLexer = std::move(TokLexer);
Douglas Gregoraf5c4842011-09-07 23:11:54 +0000226 if (CurLexerKind != CLK_LexAfterModuleImport)
227 CurLexerKind = CLK_TokenLexer;
Chris Lattner1eed7342008-03-09 04:10:46 +0000228}
229
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000230/// \brief Compute the relative path that names the given file relative to
231/// the given directory.
232static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir,
233 const FileEntry *File,
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000234 SmallString<128> &Result) {
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000235 Result.clear();
236
237 StringRef FilePath = File->getDir()->getName();
238 StringRef Path = FilePath;
239 while (!Path.empty()) {
240 if (const DirectoryEntry *CurDir = FM.getDirectory(Path)) {
241 if (CurDir == Dir) {
242 Result = FilePath.substr(Path.size());
243 llvm::sys::path::append(Result,
244 llvm::sys::path::filename(File->getName()));
245 return;
246 }
247 }
248
249 Path = llvm::sys::path::parent_path(Path);
250 }
251
252 Result = File->getName();
253}
254
Eli Friedman0834a4b2013-09-19 00:41:32 +0000255void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) {
256 if (CurTokenLexer) {
257 CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result);
258 return;
259 }
260 if (CurLexer) {
261 CurLexer->PropagateLineStartLeadingSpaceInfo(Result);
262 return;
263 }
264 // FIXME: Handle other kinds of lexers? It generally shouldn't matter,
265 // but it might if they're empty?
266}
267
Richard Smith34f30512013-11-23 04:06:09 +0000268/// \brief Determine the location to use as the end of the buffer for a lexer.
269///
270/// If the file ends with a newline, form the EOF token on the newline itself,
271/// rather than "on the line following it", which doesn't exist. This makes
272/// diagnostics relating to the end of file include the last file that the user
273/// actually typed, which is goodness.
274const char *Preprocessor::getCurLexerEndPos() {
275 const char *EndPos = CurLexer->BufferEnd;
276 if (EndPos != CurLexer->BufferStart &&
277 (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
278 --EndPos;
279
280 // Handle \n\r and \r\n:
281 if (EndPos != CurLexer->BufferStart &&
282 (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
283 EndPos[-1] != EndPos[0])
284 --EndPos;
285 }
286
287 return EndPos;
288}
289
290
Chris Lattner1eed7342008-03-09 04:10:46 +0000291/// HandleEndOfFile - This callback is invoked when the lexer hits the end of
292/// the current file. This either returns the EOF token or pops a level off
293/// the include stack and keeps going.
294bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
295 assert(!CurTokenLexer &&
296 "Ending a file when currently in a macro!");
Mike Stump11289f42009-09-09 15:08:12 +0000297
Chris Lattner1eed7342008-03-09 04:10:46 +0000298 // See if this file had a controlling macro.
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000299 if (CurPPLexer) { // Not ending a macro, ignore it.
Mike Stump11289f42009-09-09 15:08:12 +0000300 if (const IdentifierInfo *ControllingMacro =
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000301 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
Steve Naroff3fa455a2009-04-24 20:03:17 +0000302 // Okay, this has a controlling macro, remember in HeaderFileInfo.
Mike Stump11289f42009-09-09 15:08:12 +0000303 if (const FileEntry *FE =
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000304 SourceMgr.getFileEntryForID(CurPPLexer->getFileID())) {
Chris Lattner1eed7342008-03-09 04:10:46 +0000305 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
Argyrios Kyrtzidis9ef53ce2014-04-09 18:21:23 +0000306 if (MacroInfo *MI =
307 getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) {
308 MI->UsedForHeaderGuard = true;
309 }
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000310 if (const IdentifierInfo *DefinedMacro =
311 CurPPLexer->MIOpt.GetDefinedMacro()) {
Richard Smith20e883e2015-04-29 23:20:19 +0000312 if (!isMacroDefined(ControllingMacro) &&
Richard Trieu33a4b3d2013-06-12 21:20:57 +0000313 DefinedMacro != ControllingMacro &&
314 HeaderInfo.FirstTimeLexingFile(FE)) {
Ismail Pazarbasi8d0f2f32013-10-12 23:17:37 +0000315
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 Trieu33a4b3d2013-06-12 21:20:57 +0000341 }
342 }
343 }
Chris Lattner1eed7342008-03-09 04:10:46 +0000344 }
345 }
Mike Stump11289f42009-09-09 15:08:12 +0000346
John McCall95ff2702011-10-18 00:44:04 +0000347 // 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 McCall43d4dd42011-10-18 01:36:41 +0000351 !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
John McCall32f5fe12011-09-30 05:12:12 +0000352 Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited);
353
354 // Recover by leaving immediately.
355 PragmaARCCFCodeAuditedLoc = SourceLocation();
356 }
357
Chris Lattner1eed7342008-03-09 04:10:46 +0000358 // If this is a #include'd file, pop it off the include stack and continue
359 // lexing the #includer file.
360 if (!IncludeMacroStack.empty()) {
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000361
362 // If we lexed the code-completion file, act as if we reached EOF.
363 if (isCodeCompletionEnabled() && CurPPLexer &&
364 SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
365 CodeCompletionFileLoc) {
366 if (CurLexer) {
367 Result.startToken();
368 CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
369 CurLexer.reset();
370 } else {
371 assert(CurPTHLexer && "Got EOF but no current lexer set!");
372 CurPTHLexer->getEOF(Result);
373 CurPTHLexer.reset();
374 }
375
Craig Topperd2d442c2014-05-17 23:10:59 +0000376 CurPPLexer = nullptr;
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +0000377 return true;
378 }
379
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +0000380 if (!isEndOfMacro && CurPPLexer &&
381 SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) {
382 // Notify SourceManager to record the number of FileIDs that were created
383 // during lexing of the #include'd file.
384 unsigned NumFIDs =
385 SourceMgr.local_sloc_entry_size() -
386 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
387 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
388 }
389
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000390 FileID ExitedFID;
391 if (Callbacks && !isEndOfMacro && CurPPLexer)
392 ExitedFID = CurPPLexer->getFileID();
Richard Smith34f30512013-11-23 04:06:09 +0000393
Richard Smith67294e22014-01-31 20:47:44 +0000394 bool LeavingSubmodule = CurSubmodule && CurLexer;
Richard Smith34f30512013-11-23 04:06:09 +0000395 if (LeavingSubmodule) {
Richard Smith67294e22014-01-31 20:47:44 +0000396 // Notify the parser that we've left the module.
Richard Smith34f30512013-11-23 04:06:09 +0000397 const char *EndPos = getCurLexerEndPos();
398 Result.startToken();
399 CurLexer->BufferPtr = EndPos;
400 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
401 Result.setAnnotationEndLoc(Result.getLocation());
Richard Smith67294e22014-01-31 20:47:44 +0000402 Result.setAnnotationValue(CurSubmodule);
Richard Smithb8b2ed62015-04-23 18:18:26 +0000403
404 // We're done with this submodule.
405 LeaveSubmodule();
Richard Smith34f30512013-11-23 04:06:09 +0000406 }
407
Chris Lattner1eed7342008-03-09 04:10:46 +0000408 // We're done with the #included file.
409 RemoveTopOfLexerStack();
410
Eli Friedman0834a4b2013-09-19 00:41:32 +0000411 // Propagate info about start-of-line/leading white-space/etc.
412 PropagateLineStartLeadingSpaceInfo(Result);
413
Chris Lattner1eed7342008-03-09 04:10:46 +0000414 // Notify the client, if desired, that we are in a new source file.
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000415 if (Callbacks && !isEndOfMacro && CurPPLexer) {
Chris Lattner66a740e2008-10-27 01:19:25 +0000416 SrcMgr::CharacteristicKind FileType =
Chris Lattner4fd8b952009-01-19 08:01:53 +0000417 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
418 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
Argyrios Kyrtzidis7a70d2f2011-10-11 17:29:44 +0000419 PPCallbacks::ExitFile, FileType, ExitedFID);
Chris Lattner1eed7342008-03-09 04:10:46 +0000420 }
421
Richard Smith34f30512013-11-23 04:06:09 +0000422 // Client should lex another token unless we generated an EOM.
423 return LeavingSubmodule;
Chris Lattner1eed7342008-03-09 04:10:46 +0000424 }
425
Richard Smith34f30512013-11-23 04:06:09 +0000426 // If this is the end of the main file, form an EOF token.
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000427 if (CurLexer) {
Richard Smith34f30512013-11-23 04:06:09 +0000428 const char *EndPos = getCurLexerEndPos();
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000429 Result.startToken();
430 CurLexer->BufferPtr = EndPos;
431 CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
Mike Stump11289f42009-09-09 15:08:12 +0000432
Argyrios Kyrtzidis9fd15712012-12-22 04:48:10 +0000433 if (isCodeCompletionEnabled()) {
434 // Inserting the code-completion point increases the source buffer by 1,
435 // but the main FileID was created before inserting the point.
436 // Compensate by reducing the EOF location by 1, otherwise the location
437 // will point to the next FileID.
438 // FIXME: This is hacky, the code-completion point should probably be
439 // inserted before the main FileID is created.
440 if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
441 Result.setLocation(Result.getLocation().getLocWithOffset(-1));
442 }
443
Axel Naumann2eb1d902012-03-16 10:40:17 +0000444 if (!isIncrementalProcessingEnabled())
445 // We're done with lexing.
446 CurLexer.reset();
Chris Lattner190f64e2009-02-13 23:06:48 +0000447 } else {
448 assert(CurPTHLexer && "Got EOF but no current lexer set!");
Ted Kremenek78cc2472008-12-23 19:24:24 +0000449 CurPTHLexer->getEOF(Result);
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000450 CurPTHLexer.reset();
Chris Lattner1eed7342008-03-09 04:10:46 +0000451 }
Axel Naumann2eb1d902012-03-16 10:40:17 +0000452
453 if (!isIncrementalProcessingEnabled())
Craig Topperd2d442c2014-05-17 23:10:59 +0000454 CurPPLexer = nullptr;
Chris Lattner1eed7342008-03-09 04:10:46 +0000455
Argyrios Kyrtzidis8ed74142014-03-08 21:18:26 +0000456 if (TUKind == TU_Complete) {
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000457 // This is the end of the top-level file. 'WarnUnusedMacroLocs' has
458 // collected all macro locations that we need to warn because they are not
459 // used.
460 for (WarnUnusedMacroLocsTy::iterator
461 I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
462 I!=E; ++I)
463 Diag(*I, diag::pp_macro_not_used);
464 }
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000465
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000466 // If we are building a module that has an umbrella header, make sure that
467 // each of the headers within the directory covered by the umbrella header
468 // was actually included by the umbrella header.
469 if (Module *Mod = getCurrentModule()) {
470 if (Mod->getUmbrellaHeader()) {
471 SourceLocation StartLoc
472 = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
473
Alp Tokerd4a3f0e2014-06-15 23:30:39 +0000474 if (!getDiagnostics().isIgnored(diag::warn_uncovered_module_header,
475 StartLoc)) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000476 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
Richard Smith2b63d152015-05-16 02:28:53 +0000477 const DirectoryEntry *Dir = Mod->getUmbrellaDir().Entry;
Ben Langmuir54cbc702014-06-25 23:53:43 +0000478 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
Rafael Espindolac0809172014-06-12 14:02:15 +0000479 std::error_code EC;
Ben Langmuir54cbc702014-06-25 23:53:43 +0000480 for (vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), End;
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000481 Entry != End && !EC; Entry.increment(EC)) {
482 using llvm::StringSwitch;
483
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000484 // Check whether this entry has an extension typically associated with
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000485 // headers.
Ben Langmuir54cbc702014-06-25 23:53:43 +0000486 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->getName()))
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000487 .Cases(".h", ".H", ".hh", ".hpp", true)
488 .Default(false))
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000489 continue;
490
Ben Langmuir54cbc702014-06-25 23:53:43 +0000491 if (const FileEntry *Header =
492 getFileManager().getFile(Entry->getName()))
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000493 if (!getSourceManager().hasFileInfo(Header)) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000494 if (!ModMap.isHeaderInUnavailableModule(Header)) {
495 // Find the relative path that would access this header.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000496 SmallString<128> RelativePath;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000497 computeRelativePath(FileMgr, Dir, Header, RelativePath);
498 Diag(StartLoc, diag::warn_uncovered_module_header)
Douglas Gregor8f1f3332013-01-04 18:58:28 +0000499 << Mod->getFullModuleName() << RelativePath;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000500 }
Douglas Gregorfe76cfd2011-12-23 00:23:59 +0000501 }
502 }
503 }
504 }
505 }
Douglas Gregorf4e76b82013-05-20 13:49:41 +0000506
Chris Lattner1eed7342008-03-09 04:10:46 +0000507 return true;
508}
509
510/// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
511/// hits the end of its token stream.
512bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000513 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner1eed7342008-03-09 04:10:46 +0000514 "Ending a macro when currently in a #include file!");
515
Argyrios Kyrtzidis8cc04592011-06-29 22:20:11 +0000516 if (!MacroExpandingLexersStack.empty() &&
517 MacroExpandingLexersStack.back().first == CurTokenLexer.get())
518 removeCachedMacroExpandedTokensOfLastLexer();
519
Chris Lattner1eed7342008-03-09 04:10:46 +0000520 // Delete or cache the now-dead macro expander.
521 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremeneka0d2a162008-11-13 17:11:24 +0000522 CurTokenLexer.reset();
Chris Lattner1eed7342008-03-09 04:10:46 +0000523 else
David Blaikie6d5038c2014-08-29 19:36:52 +0000524 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
Chris Lattner1eed7342008-03-09 04:10:46 +0000525
526 // Handle this like a #include file being popped off the stack.
Chris Lattner1eed7342008-03-09 04:10:46 +0000527 return HandleEndOfFile(Result, true);
528}
529
530/// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
531/// lexer stack. This should only be used in situations where the current
532/// state of the top-of-stack lexer is unknown.
533void Preprocessor::RemoveTopOfLexerStack() {
534 assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
Mike Stump11289f42009-09-09 15:08:12 +0000535
Chris Lattner1eed7342008-03-09 04:10:46 +0000536 if (CurTokenLexer) {
537 // Delete or cache the now-dead macro expander.
538 if (NumCachedTokenLexers == TokenLexerCacheSize)
Ted Kremeneka0d2a162008-11-13 17:11:24 +0000539 CurTokenLexer.reset();
Chris Lattner1eed7342008-03-09 04:10:46 +0000540 else
David Blaikie6d5038c2014-08-29 19:36:52 +0000541 TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
Mike Stump11289f42009-09-09 15:08:12 +0000542 }
543
Ted Kremenek7c1e61d2008-11-13 16:51:03 +0000544 PopIncludeMacroStack();
Chris Lattner1eed7342008-03-09 04:10:46 +0000545}
546
547/// HandleMicrosoftCommentPaste - When the macro expander pastes together a
548/// comment (/##/) in microsoft mode, this method handles updating the current
549/// state, returning the token on the next source line.
550void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000551 assert(CurTokenLexer && !CurPPLexer &&
Chris Lattner1eed7342008-03-09 04:10:46 +0000552 "Pasted comment can only be formed from macro");
Mike Stump11289f42009-09-09 15:08:12 +0000553
Chris Lattner1eed7342008-03-09 04:10:46 +0000554 // We handle this by scanning for the closest real lexer, switching it to
555 // raw mode and preprocessor mode. This will cause it to return \n as an
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000556 // explicit EOD token.
Craig Topperd2d442c2014-05-17 23:10:59 +0000557 PreprocessorLexer *FoundLexer = nullptr;
Chris Lattner1eed7342008-03-09 04:10:46 +0000558 bool LexerWasInPPMode = false;
559 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
560 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
Craig Topperd2d442c2014-05-17 23:10:59 +0000561 if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer.
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattner1eed7342008-03-09 04:10:46 +0000563 // Once we find a real lexer, mark it as raw mode (disabling macro
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000564 // expansions) and preprocessor mode (return EOD). We know that the lexer
Chris Lattner1eed7342008-03-09 04:10:46 +0000565 // was *not* in raw mode before, because the macro that the comment came
566 // from was expanded. However, it could have already been in preprocessor
567 // mode (#if COMMENT) in which case we have to return it to that mode and
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000568 // return EOD.
Ted Kremeneka2c3c8d2008-11-19 22:43:49 +0000569 FoundLexer = ISI.ThePPLexer;
Chris Lattner1eed7342008-03-09 04:10:46 +0000570 FoundLexer->LexingRawMode = true;
571 LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
572 FoundLexer->ParsingPreprocessorDirective = true;
573 break;
574 }
Mike Stump11289f42009-09-09 15:08:12 +0000575
Chris Lattner1eed7342008-03-09 04:10:46 +0000576 // Okay, we either found and switched over the lexer, or we didn't find a
577 // lexer. In either case, finish off the macro the comment came from, getting
578 // the next token.
579 if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000580
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000581 // Discarding comments as long as we don't have EOF or EOD. This 'comments
Chris Lattner1eed7342008-03-09 04:10:46 +0000582 // out' the rest of the line, including any tokens that came from other macros
583 // that were active, as in:
584 // #define submacro a COMMENT b
585 // submacro c
586 // which should lex to 'a' only: 'b' and 'c' should be removed.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000587 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
Chris Lattner1eed7342008-03-09 04:10:46 +0000588 Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000589
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000590 // If we got an eod token, then we successfully found the end of the line.
591 if (Tok.is(tok::eod)) {
Chris Lattner1eed7342008-03-09 04:10:46 +0000592 assert(FoundLexer && "Can't get end of line without an active lexer");
593 // Restore the lexer back to normal mode instead of raw mode.
594 FoundLexer->LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +0000595
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000596 // If the lexer was already in preprocessor mode, just return the EOD token
Chris Lattner1eed7342008-03-09 04:10:46 +0000597 // to finish the preprocessor line.
598 if (LexerWasInPPMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner1eed7342008-03-09 04:10:46 +0000600 // Otherwise, switch out of PP mode and return the next lexed token.
601 FoundLexer->ParsingPreprocessorDirective = false;
602 return Lex(Tok);
603 }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner1eed7342008-03-09 04:10:46 +0000605 // If we got an EOF token, then we reached the end of the token stream but
606 // didn't find an explicit \n. This can only happen if there was no lexer
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000607 // active (an active lexer would return EOD at EOF if there was no \n in
Chris Lattner1eed7342008-03-09 04:10:46 +0000608 // preprocessor directive mode), so just return EOF as our token.
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000609 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
Chris Lattner1eed7342008-03-09 04:10:46 +0000610}
Richard Smithb8b2ed62015-04-23 18:18:26 +0000611
Richard Smith50474bf2015-04-23 23:29:05 +0000612void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc) {
Richard Smith04765ae2015-05-21 01:20:10 +0000613 if (!getLangOpts().ModulesLocalVisibility) {
614 // Just track that we entered this submodule.
615 BuildingSubmoduleStack.push_back(
616 BuildingSubmoduleInfo(M, ImportLoc, CurSubmoduleState));
617 return;
618 }
Richard Smithee977932015-05-01 21:22:17 +0000619
Richard Smith04765ae2015-05-21 01:20:10 +0000620 // Resolve as much of the module definition as we can now, before we enter
621 // one of its headers.
622 // FIXME: Can we enable Complain here?
623 // FIXME: Can we do this when local visibility is disabled?
624 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
625 ModMap.resolveExports(M, /*Complain=*/false);
626 ModMap.resolveUses(M, /*Complain=*/false);
627 ModMap.resolveConflicts(M, /*Complain=*/false);
Richard Smith42413142015-05-15 20:05:43 +0000628
Richard Smith04765ae2015-05-21 01:20:10 +0000629 // If this is the first time we've entered this module, set up its state.
Richard Smithe5202932015-05-21 01:26:53 +0000630 auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
Richard Smith04765ae2015-05-21 01:20:10 +0000631 auto &State = R.first->second;
632 bool FirstTime = R.second;
633 if (FirstTime) {
634 // Determine the set of starting macros for this submodule; take these
635 // from the "null" module (the predefines buffer).
636 auto &StartingMacros = NullSubmoduleState.Macros;
637
638 // Restore to the starting state.
639 // FIXME: Do this lazily, when each macro name is first referenced.
640 for (auto &Macro : StartingMacros) {
641 MacroState MS(Macro.second.getLatest());
642 MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros());
643 State.Macros.insert(std::make_pair(Macro.first, std::move(MS)));
644 }
645 }
646
647 // Track that we entered this module.
648 BuildingSubmoduleStack.push_back(
649 BuildingSubmoduleInfo(M, ImportLoc, CurSubmoduleState));
650
651 // Switch to this submodule as the current submodule.
652 CurSubmoduleState = &State;
653
654 // This module is visible to itself.
655 if (FirstTime)
Richard Smith42413142015-05-15 20:05:43 +0000656 makeModuleVisible(M, ImportLoc);
Richard Smithb8b2ed62015-04-23 18:18:26 +0000657}
658
659void Preprocessor::LeaveSubmodule() {
660 auto &Info = BuildingSubmoduleStack.back();
661
Richard Smithdbbc5232015-05-14 02:25:44 +0000662 Module *LeavingMod = Info.M;
663 SourceLocation ImportLoc = Info.ImportLoc;
664
Richard Smithb8b2ed62015-04-23 18:18:26 +0000665 // Create ModuleMacros for any macros defined in this submodule.
Richard Smith04765ae2015-05-21 01:20:10 +0000666 for (auto &Macro : CurSubmoduleState->Macros) {
Richard Smithb8b2ed62015-04-23 18:18:26 +0000667 auto *II = const_cast<IdentifierInfo*>(Macro.first);
Richard Smithee977932015-05-01 21:22:17 +0000668
669 // Find the starting point for the MacroDirective chain in this submodule.
Richard Smith04765ae2015-05-21 01:20:10 +0000670 MacroDirective *OldMD = nullptr;
671 if (getLangOpts().ModulesLocalVisibility) {
672 // FIXME: It'd be better to start at the state from when we most recently
673 // entered this submodule, but it doesn't really matter.
674 auto &PredefMacros = NullSubmoduleState.Macros;
Richard Smithee977932015-05-01 21:22:17 +0000675 auto PredefMacroIt = PredefMacros.find(Macro.first);
676 if (PredefMacroIt == PredefMacros.end())
677 OldMD = nullptr;
678 else
679 OldMD = PredefMacroIt->second.getLatest();
680 }
Richard Smithb8b2ed62015-04-23 18:18:26 +0000681
682 // This module may have exported a new macro. If so, create a ModuleMacro
683 // representing that fact.
684 bool ExplicitlyPublic = false;
Richard Smithee977932015-05-01 21:22:17 +0000685 for (auto *MD = Macro.second.getLatest(); MD != OldMD;
Richard Smithb8b2ed62015-04-23 18:18:26 +0000686 MD = MD->getPrevious()) {
Richard Smith1e172852015-04-28 21:05:07 +0000687 assert(MD && "broken macro directive chain");
688
Richard Smith38477db2015-05-02 00:45:56 +0000689 // Stop on macros defined in other submodules we #included along the way.
Richard Smithee977932015-05-01 21:22:17 +0000690 // There's no point doing this if we're tracking local submodule
Richard Smith38477db2015-05-02 00:45:56 +0000691 // visibility, since there can be no such directives in our list.
Richard Smithee977932015-05-01 21:22:17 +0000692 if (!getLangOpts().ModulesLocalVisibility) {
693 Module *Mod = getModuleContainingLocation(MD->getLocation());
Richard Smithdbbc5232015-05-14 02:25:44 +0000694 if (Mod != LeavingMod)
Richard Smith38477db2015-05-02 00:45:56 +0000695 break;
Richard Smithee977932015-05-01 21:22:17 +0000696 }
Richard Smithb8b2ed62015-04-23 18:18:26 +0000697
698 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
699 // The latest visibility directive for a name in a submodule affects
700 // all the directives that come before it.
701 if (VisMD->isPublic())
702 ExplicitlyPublic = true;
703 else if (!ExplicitlyPublic)
704 // Private with no following public directive: not exported.
705 break;
706 } else {
707 MacroInfo *Def = nullptr;
708 if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
709 Def = DefMD->getInfo();
710
711 // FIXME: Issue a warning if multiple headers for the same submodule
712 // define a macro, rather than silently ignoring all but the first.
713 bool IsNew;
Richard Smith32dbd692015-05-02 01:14:40 +0000714 // Don't bother creating a module macro if it would represent a #undef
715 // that doesn't override anything.
716 if (Def || !Macro.second.getOverriddenMacros().empty())
Richard Smithdbbc5232015-05-14 02:25:44 +0000717 addModuleMacro(LeavingMod, II, Def,
718 Macro.second.getOverriddenMacros(), IsNew);
Richard Smithb8b2ed62015-04-23 18:18:26 +0000719 break;
720 }
721 }
Richard Smith753e0072015-04-27 23:21:38 +0000722 }
Richard Smithb8b2ed62015-04-23 18:18:26 +0000723
Richard Smith04765ae2015-05-21 01:20:10 +0000724 // Put back the outer module's state, if we're tracking it.
Richard Smithee977932015-05-01 21:22:17 +0000725 if (getLangOpts().ModulesLocalVisibility)
Richard Smith04765ae2015-05-21 01:20:10 +0000726 CurSubmoduleState = Info.OuterSubmoduleState;
Richard Smithee977932015-05-01 21:22:17 +0000727
Richard Smithb8b2ed62015-04-23 18:18:26 +0000728 BuildingSubmoduleStack.pop_back();
Richard Smithdbbc5232015-05-14 02:25:44 +0000729
730 // A nested #include makes the included submodule visible.
Richard Smith04765ae2015-05-21 01:20:10 +0000731 if (!BuildingSubmoduleStack.empty() || !getLangOpts().ModulesLocalVisibility)
Richard Smithdbbc5232015-05-14 02:25:44 +0000732 makeModuleVisible(LeavingMod, ImportLoc);
Richard Smithb8b2ed62015-04-23 18:18:26 +0000733}