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