PPLexerChange.cpp 34 KB

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