PPLexerChange.cpp 34 KB

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