PPLexerChange.cpp 32 KB

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