PPLexerChange.cpp 29 KB

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