1 //===--- PPLexerChange.cpp - Handle changing lexers in the preprocessor ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements pieces of the Preprocessor interface that manage the
10 // current lexer stack.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/HeaderSearch.h"
17 #include "clang/Lex/LexDiagnostic.h"
18 #include "clang/Lex/MacroInfo.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBufferRef.h"
24 #include "llvm/Support/Path.h"
25 
26 using namespace clang;
27 
28 //===----------------------------------------------------------------------===//
29 // Miscellaneous Methods.
30 //===----------------------------------------------------------------------===//
31 
32 /// isInPrimaryFile - Return true if we're in the top-level file, not in a
33 /// \#include.  This looks through macro expansions and active _Pragma lexers.
34 bool Preprocessor::isInPrimaryFile() const {
35   if (IsFileLexer())
36     return IncludeMacroStack.empty();
37 
38   // If there are any stacked lexers, we're in a #include.
39   assert(IsFileLexer(IncludeMacroStack[0]) &&
40          "Top level include stack isn't our primary lexer?");
41   return llvm::none_of(
42       llvm::drop_begin(IncludeMacroStack),
43       [&](const IncludeStackInfo &ISI) -> bool { return IsFileLexer(ISI); });
44 }
45 
46 /// getCurrentLexer - Return the current file lexer being lexed from.  Note
47 /// that this ignores any potentially active macro expansions and _Pragma
48 /// expansions going on at the time.
49 PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
50   if (IsFileLexer())
51     return CurPPLexer;
52 
53   // Look for a stacked lexer.
54   for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
55     if (IsFileLexer(ISI))
56       return ISI.ThePPLexer;
57   }
58   return nullptr;
59 }
60 
61 
62 //===----------------------------------------------------------------------===//
63 // Methods for Entering and Callbacks for leaving various contexts
64 //===----------------------------------------------------------------------===//
65 
66 /// EnterSourceFile - Add a source file to the top of the include stack and
67 /// start lexing tokens from it instead of the current buffer.
68 bool Preprocessor::EnterSourceFile(FileID FID, ConstSearchDirIterator CurDir,
69                                    SourceLocation Loc,
70                                    bool IsFirstIncludeOfFile) {
71   assert(!CurTokenLexer && "Cannot #include a file inside a macro!");
72   ++NumEnteredSourceFiles;
73 
74   if (MaxIncludeStackDepth < IncludeMacroStack.size())
75     MaxIncludeStackDepth = IncludeMacroStack.size();
76 
77   // Get the MemoryBuffer for this FID, if it fails, we fail.
78   llvm::Optional<llvm::MemoryBufferRef> InputFile =
79       getSourceManager().getBufferOrNone(FID, Loc);
80   if (!InputFile) {
81     SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID);
82     Diag(Loc, diag::err_pp_error_opening_file)
83         << std::string(SourceMgr.getBufferName(FileStart)) << "";
84     return true;
85   }
86 
87   if (isCodeCompletionEnabled() &&
88       SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) {
89     CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID);
90     CodeCompletionLoc =
91         CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset);
92   }
93 
94   Lexer *TheLexer = new Lexer(FID, *InputFile, *this, IsFirstIncludeOfFile);
95   if (getPreprocessorOpts().DependencyDirectivesForFile &&
96       FID != PredefinesFileID) {
97     if (Optional<FileEntryRef> File = SourceMgr.getFileEntryRefForID(FID)) {
98       if (Optional<ArrayRef<dependency_directives_scan::Directive>>
99               DepDirectives =
100                   getPreprocessorOpts().DependencyDirectivesForFile(*File)) {
101         TheLexer->DepDirectives = *DepDirectives;
102       }
103     }
104   }
105 
106   EnterSourceFileWithLexer(TheLexer, CurDir);
107   return false;
108 }
109 
110 /// EnterSourceFileWithLexer - Add a source file to the top of the include stack
111 ///  and start lexing tokens from it instead of the current buffer.
112 void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
113                                             ConstSearchDirIterator CurDir) {
114   PreprocessorLexer *PrevPPLexer = CurPPLexer;
115 
116   // Add the current lexer to the include stack.
117   if (CurPPLexer || CurTokenLexer)
118     PushIncludeMacroStack();
119 
120   CurLexer.reset(TheLexer);
121   CurPPLexer = TheLexer;
122   CurDirLookup = CurDir;
123   CurLexerSubmodule = nullptr;
124   if (CurLexerKind != CLK_LexAfterModuleImport)
125     CurLexerKind = TheLexer->isDependencyDirectivesLexer()
126                        ? CLK_DependencyDirectivesLexer
127                        : CLK_Lexer;
128 
129   // Notify the client, if desired, that we are in a new source file.
130   if (Callbacks && !CurLexer->Is_PragmaLexer) {
131     SrcMgr::CharacteristicKind FileType =
132        SourceMgr.getFileCharacteristic(CurLexer->getFileLoc());
133 
134     FileID PrevFID;
135     SourceLocation EnterLoc;
136     if (PrevPPLexer) {
137       PrevFID = PrevPPLexer->getFileID();
138       EnterLoc = PrevPPLexer->getSourceLocation();
139     }
140     Callbacks->FileChanged(CurLexer->getFileLoc(), PPCallbacks::EnterFile,
141                            FileType, PrevFID);
142     Callbacks->LexedFileChanged(CurLexer->getFileID(),
143                                 PPCallbacks::LexedFileChangeReason::EnterFile,
144                                 FileType, PrevFID, EnterLoc);
145   }
146 }
147 
148 /// EnterMacro - Add a Macro to the top of the include stack and start lexing
149 /// tokens from it instead of the current buffer.
150 void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd,
151                               MacroInfo *Macro, MacroArgs *Args) {
152   std::unique_ptr<TokenLexer> TokLexer;
153   if (NumCachedTokenLexers == 0) {
154     TokLexer = std::make_unique<TokenLexer>(Tok, ILEnd, Macro, Args, *this);
155   } else {
156     TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
157     TokLexer->Init(Tok, ILEnd, Macro, Args);
158   }
159 
160   PushIncludeMacroStack();
161   CurDirLookup = nullptr;
162   CurTokenLexer = std::move(TokLexer);
163   if (CurLexerKind != CLK_LexAfterModuleImport)
164     CurLexerKind = CLK_TokenLexer;
165 }
166 
167 /// EnterTokenStream - Add a "macro" context to the top of the include stack,
168 /// which will cause the lexer to start returning the specified tokens.
169 ///
170 /// If DisableMacroExpansion is true, tokens lexed from the token stream will
171 /// not be subject to further macro expansion.  Otherwise, these tokens will
172 /// be re-macro-expanded when/if expansion is enabled.
173 ///
174 /// If OwnsTokens is false, this method assumes that the specified stream of
175 /// tokens has a permanent owner somewhere, so they do not need to be copied.
176 /// If it is true, it assumes the array of tokens is allocated with new[] and
177 /// must be freed.
178 ///
179 void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks,
180                                     bool DisableMacroExpansion, bool OwnsTokens,
181                                     bool IsReinject) {
182   if (CurLexerKind == CLK_CachingLexer) {
183     if (CachedLexPos < CachedTokens.size()) {
184       assert(IsReinject && "new tokens in the middle of cached stream");
185       // We're entering tokens into the middle of our cached token stream. We
186       // can't represent that, so just insert the tokens into the buffer.
187       CachedTokens.insert(CachedTokens.begin() + CachedLexPos,
188                           Toks, Toks + NumToks);
189       if (OwnsTokens)
190         delete [] Toks;
191       return;
192     }
193 
194     // New tokens are at the end of the cached token sequnece; insert the
195     // token stream underneath the caching lexer.
196     ExitCachingLexMode();
197     EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
198                      IsReinject);
199     EnterCachingLexMode();
200     return;
201   }
202 
203   // Create a macro expander to expand from the specified token stream.
204   std::unique_ptr<TokenLexer> TokLexer;
205   if (NumCachedTokenLexers == 0) {
206     TokLexer = std::make_unique<TokenLexer>(
207         Toks, NumToks, DisableMacroExpansion, OwnsTokens, IsReinject, *this);
208   } else {
209     TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]);
210     TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens,
211                    IsReinject);
212   }
213 
214   // Save our current state.
215   PushIncludeMacroStack();
216   CurDirLookup = nullptr;
217   CurTokenLexer = std::move(TokLexer);
218   if (CurLexerKind != CLK_LexAfterModuleImport)
219     CurLexerKind = CLK_TokenLexer;
220 }
221 
222 /// Compute the relative path that names the given file relative to
223 /// the given directory.
224 static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir,
225                                 const FileEntry *File,
226                                 SmallString<128> &Result) {
227   Result.clear();
228 
229   StringRef FilePath = File->getDir()->getName();
230   StringRef Path = FilePath;
231   while (!Path.empty()) {
232     if (auto CurDir = FM.getDirectory(Path)) {
233       if (*CurDir == Dir) {
234         Result = FilePath.substr(Path.size());
235         llvm::sys::path::append(Result,
236                                 llvm::sys::path::filename(File->getName()));
237         return;
238       }
239     }
240 
241     Path = llvm::sys::path::parent_path(Path);
242   }
243 
244   Result = File->getName();
245 }
246 
247 void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) {
248   if (CurTokenLexer) {
249     CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result);
250     return;
251   }
252   if (CurLexer) {
253     CurLexer->PropagateLineStartLeadingSpaceInfo(Result);
254     return;
255   }
256   // FIXME: Handle other kinds of lexers?  It generally shouldn't matter,
257   // but it might if they're empty?
258 }
259 
260 /// Determine the location to use as the end of the buffer for a lexer.
261 ///
262 /// If the file ends with a newline, form the EOF token on the newline itself,
263 /// rather than "on the line following it", which doesn't exist.  This makes
264 /// diagnostics relating to the end of file include the last file that the user
265 /// actually typed, which is goodness.
266 const char *Preprocessor::getCurLexerEndPos() {
267   const char *EndPos = CurLexer->BufferEnd;
268   if (EndPos != CurLexer->BufferStart &&
269       (EndPos[-1] == '\n' || EndPos[-1] == '\r')) {
270     --EndPos;
271 
272     // Handle \n\r and \r\n:
273     if (EndPos != CurLexer->BufferStart &&
274         (EndPos[-1] == '\n' || EndPos[-1] == '\r') &&
275         EndPos[-1] != EndPos[0])
276       --EndPos;
277   }
278 
279   return EndPos;
280 }
281 
282 static void collectAllSubModulesWithUmbrellaHeader(
283     const Module &Mod, SmallVectorImpl<const Module *> &SubMods) {
284   if (Mod.getUmbrellaHeader())
285     SubMods.push_back(&Mod);
286   for (auto *M : Mod.submodules())
287     collectAllSubModulesWithUmbrellaHeader(*M, SubMods);
288 }
289 
290 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
291   const Module::Header &UmbrellaHeader = Mod.getUmbrellaHeader();
292   assert(UmbrellaHeader.Entry && "Module must use umbrella header");
293   const FileID &File = SourceMgr.translateFile(UmbrellaHeader.Entry);
294   SourceLocation ExpectedHeadersLoc = SourceMgr.getLocForEndOfFile(File);
295   if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header,
296                                  ExpectedHeadersLoc))
297     return;
298 
299   ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
300   const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry;
301   llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
302   std::error_code EC;
303   for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC),
304        End;
305        Entry != End && !EC; Entry.increment(EC)) {
306     using llvm::StringSwitch;
307 
308     // Check whether this entry has an extension typically associated with
309     // headers.
310     if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path()))
311              .Cases(".h", ".H", ".hh", ".hpp", true)
312              .Default(false))
313       continue;
314 
315     if (auto Header = getFileManager().getFile(Entry->path()))
316       if (!getSourceManager().hasFileInfo(*Header)) {
317         if (!ModMap.isHeaderInUnavailableModule(*Header)) {
318           // Find the relative path that would access this header.
319           SmallString<128> RelativePath;
320           computeRelativePath(FileMgr, Dir, *Header, RelativePath);
321           Diag(ExpectedHeadersLoc, diag::warn_uncovered_module_header)
322               << Mod.getFullModuleName() << RelativePath;
323         }
324       }
325   }
326 }
327 
328 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
329 /// the current file.  This either returns the EOF token or pops a level off
330 /// the include stack and keeps going.
331 bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
332   assert(!CurTokenLexer &&
333          "Ending a file when currently in a macro!");
334 
335   // If we have an unclosed module region from a pragma at the end of a
336   // module, complain and close it now.
337   const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
338   if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
339       !BuildingSubmoduleStack.empty() &&
340       BuildingSubmoduleStack.back().IsPragma) {
341     Diag(BuildingSubmoduleStack.back().ImportLoc,
342          diag::err_pp_module_begin_without_module_end);
343     Module *M = LeaveSubmodule(/*ForPragma*/true);
344 
345     Result.startToken();
346     const char *EndPos = getCurLexerEndPos();
347     CurLexer->BufferPtr = EndPos;
348     CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
349     Result.setAnnotationEndLoc(Result.getLocation());
350     Result.setAnnotationValue(M);
351     return true;
352   }
353 
354   // See if this file had a controlling macro.
355   if (CurPPLexer) {  // Not ending a macro, ignore it.
356     if (const IdentifierInfo *ControllingMacro =
357           CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
358       // Okay, this has a controlling macro, remember in HeaderFileInfo.
359       if (const FileEntry *FE = CurPPLexer->getFileEntry()) {
360         HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
361         if (MacroInfo *MI =
362               getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro)))
363           MI->setUsedForHeaderGuard(true);
364         if (const IdentifierInfo *DefinedMacro =
365               CurPPLexer->MIOpt.GetDefinedMacro()) {
366           if (!isMacroDefined(ControllingMacro) &&
367               DefinedMacro != ControllingMacro &&
368               CurLexer->isFirstTimeLexingFile()) {
369 
370             // If the edit distance between the two macros is more than 50%,
371             // DefinedMacro may not be header guard, or can be header guard of
372             // another header file. Therefore, it maybe defining something
373             // completely different. This can be observed in the wild when
374             // handling feature macros or header guards in different files.
375 
376             const StringRef ControllingMacroName = ControllingMacro->getName();
377             const StringRef DefinedMacroName = DefinedMacro->getName();
378             const size_t MaxHalfLength = std::max(ControllingMacroName.size(),
379                                                   DefinedMacroName.size()) / 2;
380             const unsigned ED = ControllingMacroName.edit_distance(
381                 DefinedMacroName, true, MaxHalfLength);
382             if (ED <= MaxHalfLength) {
383               // Emit a warning for a bad header guard.
384               Diag(CurPPLexer->MIOpt.GetMacroLocation(),
385                    diag::warn_header_guard)
386                   << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro;
387               Diag(CurPPLexer->MIOpt.GetDefinedLocation(),
388                    diag::note_header_guard)
389                   << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro
390                   << ControllingMacro
391                   << FixItHint::CreateReplacement(
392                          CurPPLexer->MIOpt.GetDefinedLocation(),
393                          ControllingMacro->getName());
394             }
395           }
396         }
397       }
398     }
399   }
400 
401   // Complain about reaching a true EOF within arc_cf_code_audited.
402   // We don't want to complain about reaching the end of a macro
403   // instantiation or a _Pragma.
404   if (PragmaARCCFCodeAuditedInfo.second.isValid() && !isEndOfMacro &&
405       !(CurLexer && CurLexer->Is_PragmaLexer)) {
406     Diag(PragmaARCCFCodeAuditedInfo.second,
407          diag::err_pp_eof_in_arc_cf_code_audited);
408 
409     // Recover by leaving immediately.
410     PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
411   }
412 
413   // Complain about reaching a true EOF within assume_nonnull.
414   // We don't want to complain about reaching the end of a macro
415   // instantiation or a _Pragma.
416   if (PragmaAssumeNonNullLoc.isValid() &&
417       !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) {
418     // If we're at the end of generating a preamble, we should record the
419     // unterminated \#pragma clang assume_nonnull so we can restore it later
420     // when the preamble is loaded into the main file.
421     if (isRecordingPreamble() && isInPrimaryFile())
422       PreambleRecordedPragmaAssumeNonNullLoc = PragmaAssumeNonNullLoc;
423     else
424       Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull);
425     // Recover by leaving immediately.
426     PragmaAssumeNonNullLoc = SourceLocation();
427   }
428 
429   bool LeavingPCHThroughHeader = false;
430 
431   // If this is a #include'd file, pop it off the include stack and continue
432   // lexing the #includer file.
433   if (!IncludeMacroStack.empty()) {
434 
435     // If we lexed the code-completion file, act as if we reached EOF.
436     if (isCodeCompletionEnabled() && CurPPLexer &&
437         SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) ==
438             CodeCompletionFileLoc) {
439       assert(CurLexer && "Got EOF but no current lexer set!");
440       Result.startToken();
441       CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
442       CurLexer.reset();
443 
444       CurPPLexer = nullptr;
445       recomputeCurLexerKind();
446       return true;
447     }
448 
449     if (!isEndOfMacro && CurPPLexer &&
450         (SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid() ||
451          // Predefines file doesn't have a valid include location.
452          (PredefinesFileID.isValid() &&
453           CurPPLexer->getFileID() == PredefinesFileID))) {
454       // Notify SourceManager to record the number of FileIDs that were created
455       // during lexing of the #include'd file.
456       unsigned NumFIDs =
457           SourceMgr.local_sloc_entry_size() -
458           CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
459       SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
460     }
461 
462     bool ExitedFromPredefinesFile = false;
463     FileID ExitedFID;
464     if (!isEndOfMacro && CurPPLexer) {
465       ExitedFID = CurPPLexer->getFileID();
466 
467       assert(PredefinesFileID.isValid() &&
468              "HandleEndOfFile is called before PredefinesFileId is set");
469       ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID);
470     }
471 
472     if (LeavingSubmodule) {
473       // We're done with this submodule.
474       Module *M = LeaveSubmodule(/*ForPragma*/false);
475 
476       // Notify the parser that we've left the module.
477       const char *EndPos = getCurLexerEndPos();
478       Result.startToken();
479       CurLexer->BufferPtr = EndPos;
480       CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
481       Result.setAnnotationEndLoc(Result.getLocation());
482       Result.setAnnotationValue(M);
483     }
484 
485     bool FoundPCHThroughHeader = false;
486     if (CurPPLexer && creatingPCHWithThroughHeader() &&
487         isPCHThroughHeader(
488             SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
489       FoundPCHThroughHeader = true;
490 
491     // We're done with the #included file.
492     RemoveTopOfLexerStack();
493 
494     // Propagate info about start-of-line/leading white-space/etc.
495     PropagateLineStartLeadingSpaceInfo(Result);
496 
497     // Notify the client, if desired, that we are in a new source file.
498     if (Callbacks && !isEndOfMacro && CurPPLexer) {
499       SourceLocation Loc = CurPPLexer->getSourceLocation();
500       SrcMgr::CharacteristicKind FileType =
501           SourceMgr.getFileCharacteristic(Loc);
502       Callbacks->FileChanged(Loc, PPCallbacks::ExitFile, FileType, ExitedFID);
503       Callbacks->LexedFileChanged(CurPPLexer->getFileID(),
504                                   PPCallbacks::LexedFileChangeReason::ExitFile,
505                                   FileType, ExitedFID, Loc);
506     }
507 
508     // Restore conditional stack as well as the recorded
509     // \#pragma clang assume_nonnull from the preamble right after exiting
510     // from the predefines file.
511     if (ExitedFromPredefinesFile) {
512       replayPreambleConditionalStack();
513       if (PreambleRecordedPragmaAssumeNonNullLoc.isValid())
514         PragmaAssumeNonNullLoc = PreambleRecordedPragmaAssumeNonNullLoc;
515     }
516 
517     if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader &&
518         (isInPrimaryFile() ||
519          CurPPLexer->getFileID() == getPredefinesFileID())) {
520       // Leaving the through header. Continue directly to end of main file
521       // processing.
522       LeavingPCHThroughHeader = true;
523     } else {
524       // Client should lex another token unless we generated an EOM.
525       return LeavingSubmodule;
526     }
527   }
528 
529   // If this is the end of the main file, form an EOF token.
530   assert(CurLexer && "Got EOF but no current lexer set!");
531   const char *EndPos = getCurLexerEndPos();
532   Result.startToken();
533   CurLexer->BufferPtr = EndPos;
534   CurLexer->FormTokenWithChars(Result, EndPos, tok::eof);
535 
536   if (isCodeCompletionEnabled()) {
537     // Inserting the code-completion point increases the source buffer by 1,
538     // but the main FileID was created before inserting the point.
539     // Compensate by reducing the EOF location by 1, otherwise the location
540     // will point to the next FileID.
541     // FIXME: This is hacky, the code-completion point should probably be
542     // inserted before the main FileID is created.
543     if (CurLexer->getFileLoc() == CodeCompletionFileLoc)
544       Result.setLocation(Result.getLocation().getLocWithOffset(-1));
545   }
546 
547   if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader) {
548     // Reached the end of the compilation without finding the through header.
549     Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen)
550         << PPOpts->PCHThroughHeader << 0;
551   }
552 
553   if (!isIncrementalProcessingEnabled())
554     // We're done with lexing.
555     CurLexer.reset();
556 
557   if (!isIncrementalProcessingEnabled())
558     CurPPLexer = nullptr;
559 
560   if (TUKind == TU_Complete) {
561     // This is the end of the top-level file. 'WarnUnusedMacroLocs' has
562     // collected all macro locations that we need to warn because they are not
563     // used.
564     for (WarnUnusedMacroLocsTy::iterator
565            I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end();
566            I!=E; ++I)
567       Diag(*I, diag::pp_macro_not_used);
568   }
569 
570   // If we are building a module that has an umbrella header, make sure that
571   // each of the headers within the directory, including all submodules, is
572   // covered by the umbrella header was actually included by the umbrella
573   // header.
574   if (Module *Mod = getCurrentModule()) {
575     llvm::SmallVector<const Module *, 4> AllMods;
576     collectAllSubModulesWithUmbrellaHeader(*Mod, AllMods);
577     for (auto *M : AllMods)
578       diagnoseMissingHeaderInUmbrellaDir(*M);
579   }
580 
581   return true;
582 }
583 
584 /// HandleEndOfTokenLexer - This callback is invoked when the current TokenLexer
585 /// hits the end of its token stream.
586 bool Preprocessor::HandleEndOfTokenLexer(Token &Result) {
587   assert(CurTokenLexer && !CurPPLexer &&
588          "Ending a macro when currently in a #include file!");
589 
590   if (!MacroExpandingLexersStack.empty() &&
591       MacroExpandingLexersStack.back().first == CurTokenLexer.get())
592     removeCachedMacroExpandedTokensOfLastLexer();
593 
594   // Delete or cache the now-dead macro expander.
595   if (NumCachedTokenLexers == TokenLexerCacheSize)
596     CurTokenLexer.reset();
597   else
598     TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
599 
600   // Handle this like a #include file being popped off the stack.
601   return HandleEndOfFile(Result, true);
602 }
603 
604 /// RemoveTopOfLexerStack - Pop the current lexer/macro exp off the top of the
605 /// lexer stack.  This should only be used in situations where the current
606 /// state of the top-of-stack lexer is unknown.
607 void Preprocessor::RemoveTopOfLexerStack() {
608   assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load");
609 
610   if (CurTokenLexer) {
611     // Delete or cache the now-dead macro expander.
612     if (NumCachedTokenLexers == TokenLexerCacheSize)
613       CurTokenLexer.reset();
614     else
615       TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer);
616   }
617 
618   PopIncludeMacroStack();
619 }
620 
621 /// HandleMicrosoftCommentPaste - When the macro expander pastes together a
622 /// comment (/##/) in microsoft mode, this method handles updating the current
623 /// state, returning the token on the next source line.
624 void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) {
625   assert(CurTokenLexer && !CurPPLexer &&
626          "Pasted comment can only be formed from macro");
627   // We handle this by scanning for the closest real lexer, switching it to
628   // raw mode and preprocessor mode.  This will cause it to return \n as an
629   // explicit EOD token.
630   PreprocessorLexer *FoundLexer = nullptr;
631   bool LexerWasInPPMode = false;
632   for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
633     if (ISI.ThePPLexer == nullptr) continue;  // Scan for a real lexer.
634 
635     // Once we find a real lexer, mark it as raw mode (disabling macro
636     // expansions) and preprocessor mode (return EOD).  We know that the lexer
637     // was *not* in raw mode before, because the macro that the comment came
638     // from was expanded.  However, it could have already been in preprocessor
639     // mode (#if COMMENT) in which case we have to return it to that mode and
640     // return EOD.
641     FoundLexer = ISI.ThePPLexer;
642     FoundLexer->LexingRawMode = true;
643     LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective;
644     FoundLexer->ParsingPreprocessorDirective = true;
645     break;
646   }
647 
648   // Okay, we either found and switched over the lexer, or we didn't find a
649   // lexer.  In either case, finish off the macro the comment came from, getting
650   // the next token.
651   if (!HandleEndOfTokenLexer(Tok)) Lex(Tok);
652 
653   // Discarding comments as long as we don't have EOF or EOD.  This 'comments
654   // out' the rest of the line, including any tokens that came from other macros
655   // that were active, as in:
656   //  #define submacro a COMMENT b
657   //    submacro c
658   // which should lex to 'a' only: 'b' and 'c' should be removed.
659   while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof))
660     Lex(Tok);
661 
662   // If we got an eod token, then we successfully found the end of the line.
663   if (Tok.is(tok::eod)) {
664     assert(FoundLexer && "Can't get end of line without an active lexer");
665     // Restore the lexer back to normal mode instead of raw mode.
666     FoundLexer->LexingRawMode = false;
667 
668     // If the lexer was already in preprocessor mode, just return the EOD token
669     // to finish the preprocessor line.
670     if (LexerWasInPPMode) return;
671 
672     // Otherwise, switch out of PP mode and return the next lexed token.
673     FoundLexer->ParsingPreprocessorDirective = false;
674     return Lex(Tok);
675   }
676 
677   // If we got an EOF token, then we reached the end of the token stream but
678   // didn't find an explicit \n.  This can only happen if there was no lexer
679   // active (an active lexer would return EOD at EOF if there was no \n in
680   // preprocessor directive mode), so just return EOF as our token.
681   assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
682 }
683 
684 void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
685                                   bool ForPragma) {
686   if (!getLangOpts().ModulesLocalVisibility) {
687     // Just track that we entered this submodule.
688     BuildingSubmoduleStack.push_back(
689         BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
690                               PendingModuleMacroNames.size()));
691     if (Callbacks)
692       Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
693     return;
694   }
695 
696   // Resolve as much of the module definition as we can now, before we enter
697   // one of its headers.
698   // FIXME: Can we enable Complain here?
699   // FIXME: Can we do this when local visibility is disabled?
700   ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
701   ModMap.resolveExports(M, /*Complain=*/false);
702   ModMap.resolveUses(M, /*Complain=*/false);
703   ModMap.resolveConflicts(M, /*Complain=*/false);
704 
705   // If this is the first time we've entered this module, set up its state.
706   auto R = Submodules.insert(std::make_pair(M, SubmoduleState()));
707   auto &State = R.first->second;
708   bool FirstTime = R.second;
709   if (FirstTime) {
710     // Determine the set of starting macros for this submodule; take these
711     // from the "null" module (the predefines buffer).
712     //
713     // FIXME: If we have local visibility but not modules enabled, the
714     // NullSubmoduleState is polluted by #defines in the top-level source
715     // file.
716     auto &StartingMacros = NullSubmoduleState.Macros;
717 
718     // Restore to the starting state.
719     // FIXME: Do this lazily, when each macro name is first referenced.
720     for (auto &Macro : StartingMacros) {
721       // Skip uninteresting macros.
722       if (!Macro.second.getLatest() &&
723           Macro.second.getOverriddenMacros().empty())
724         continue;
725 
726       MacroState MS(Macro.second.getLatest());
727       MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros());
728       State.Macros.insert(std::make_pair(Macro.first, std::move(MS)));
729     }
730   }
731 
732   // Track that we entered this module.
733   BuildingSubmoduleStack.push_back(
734       BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
735                             PendingModuleMacroNames.size()));
736 
737   if (Callbacks)
738     Callbacks->EnteredSubmodule(M, ImportLoc, ForPragma);
739 
740   // Switch to this submodule as the current submodule.
741   CurSubmoduleState = &State;
742 
743   // This module is visible to itself.
744   if (FirstTime)
745     makeModuleVisible(M, ImportLoc);
746 }
747 
748 bool Preprocessor::needModuleMacros() const {
749   // If we're not within a submodule, we never need to create ModuleMacros.
750   if (BuildingSubmoduleStack.empty())
751     return false;
752   // If we are tracking module macro visibility even for textually-included
753   // headers, we need ModuleMacros.
754   if (getLangOpts().ModulesLocalVisibility)
755     return true;
756   // Otherwise, we only need module macros if we're actually compiling a module
757   // interface.
758   return getLangOpts().isCompilingModule();
759 }
760 
761 Module *Preprocessor::LeaveSubmodule(bool ForPragma) {
762   if (BuildingSubmoduleStack.empty() ||
763       BuildingSubmoduleStack.back().IsPragma != ForPragma) {
764     assert(ForPragma && "non-pragma module enter/leave mismatch");
765     return nullptr;
766   }
767 
768   auto &Info = BuildingSubmoduleStack.back();
769 
770   Module *LeavingMod = Info.M;
771   SourceLocation ImportLoc = Info.ImportLoc;
772 
773   if (!needModuleMacros() ||
774       (!getLangOpts().ModulesLocalVisibility &&
775        LeavingMod->getTopLevelModuleName() != getLangOpts().CurrentModule)) {
776     // If we don't need module macros, or this is not a module for which we
777     // are tracking macro visibility, don't build any, and preserve the list
778     // of pending names for the surrounding submodule.
779     BuildingSubmoduleStack.pop_back();
780 
781     if (Callbacks)
782       Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
783 
784     makeModuleVisible(LeavingMod, ImportLoc);
785     return LeavingMod;
786   }
787 
788   // Create ModuleMacros for any macros defined in this submodule.
789   llvm::SmallPtrSet<const IdentifierInfo*, 8> VisitedMacros;
790   for (unsigned I = Info.OuterPendingModuleMacroNames;
791        I != PendingModuleMacroNames.size(); ++I) {
792     auto *II = const_cast<IdentifierInfo*>(PendingModuleMacroNames[I]);
793     if (!VisitedMacros.insert(II).second)
794       continue;
795 
796     auto MacroIt = CurSubmoduleState->Macros.find(II);
797     if (MacroIt == CurSubmoduleState->Macros.end())
798       continue;
799     auto &Macro = MacroIt->second;
800 
801     // Find the starting point for the MacroDirective chain in this submodule.
802     MacroDirective *OldMD = nullptr;
803     auto *OldState = Info.OuterSubmoduleState;
804     if (getLangOpts().ModulesLocalVisibility)
805       OldState = &NullSubmoduleState;
806     if (OldState && OldState != CurSubmoduleState) {
807       // FIXME: It'd be better to start at the state from when we most recently
808       // entered this submodule, but it doesn't really matter.
809       auto &OldMacros = OldState->Macros;
810       auto OldMacroIt = OldMacros.find(II);
811       if (OldMacroIt == OldMacros.end())
812         OldMD = nullptr;
813       else
814         OldMD = OldMacroIt->second.getLatest();
815     }
816 
817     // This module may have exported a new macro. If so, create a ModuleMacro
818     // representing that fact.
819     bool ExplicitlyPublic = false;
820     for (auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->getPrevious()) {
821       assert(MD && "broken macro directive chain");
822 
823       if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
824         // The latest visibility directive for a name in a submodule affects
825         // all the directives that come before it.
826         if (VisMD->isPublic())
827           ExplicitlyPublic = true;
828         else if (!ExplicitlyPublic)
829           // Private with no following public directive: not exported.
830           break;
831       } else {
832         MacroInfo *Def = nullptr;
833         if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
834           Def = DefMD->getInfo();
835 
836         // FIXME: Issue a warning if multiple headers for the same submodule
837         // define a macro, rather than silently ignoring all but the first.
838         bool IsNew;
839         // Don't bother creating a module macro if it would represent a #undef
840         // that doesn't override anything.
841         if (Def || !Macro.getOverriddenMacros().empty())
842           addModuleMacro(LeavingMod, II, Def,
843                          Macro.getOverriddenMacros(), IsNew);
844 
845         if (!getLangOpts().ModulesLocalVisibility) {
846           // This macro is exposed to the rest of this compilation as a
847           // ModuleMacro; we don't need to track its MacroDirective any more.
848           Macro.setLatest(nullptr);
849           Macro.setOverriddenMacros(*this, {});
850         }
851         break;
852       }
853     }
854   }
855   PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
856 
857   // FIXME: Before we leave this submodule, we should parse all the other
858   // headers within it. Otherwise, we're left with an inconsistent state
859   // where we've made the module visible but don't yet have its complete
860   // contents.
861 
862   // Put back the outer module's state, if we're tracking it.
863   if (getLangOpts().ModulesLocalVisibility)
864     CurSubmoduleState = Info.OuterSubmoduleState;
865 
866   BuildingSubmoduleStack.pop_back();
867 
868   if (Callbacks)
869     Callbacks->LeftSubmodule(LeavingMod, ImportLoc, ForPragma);
870 
871   // A nested #include makes the included submodule visible.
872   makeModuleVisible(LeavingMod, ImportLoc);
873   return LeavingMod;
874 }
875