InclusionRewriter.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. //===--- InclusionRewriter.cpp - Rewrite includes into their expansions ---===//
  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 code rewrites include invocations into their expansions. This gives you
  11. // a file with all included files merged into it.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/Rewrite/Frontend/Rewriters.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Frontend/PreprocessorOutputOptions.h"
  17. #include "clang/Lex/HeaderSearch.h"
  18. #include "clang/Lex/Pragma.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace clang;
  23. using namespace llvm;
  24. namespace {
  25. class InclusionRewriter : public PPCallbacks {
  26. /// Information about which #includes were actually performed,
  27. /// created by preprocessor callbacks.
  28. struct IncludedFile {
  29. FileID Id;
  30. SrcMgr::CharacteristicKind FileType;
  31. IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType)
  32. : Id(Id), FileType(FileType) {}
  33. };
  34. Preprocessor &PP; ///< Used to find inclusion directives.
  35. SourceManager &SM; ///< Used to read and manage source files.
  36. raw_ostream &OS; ///< The destination stream for rewritten contents.
  37. StringRef MainEOL; ///< The line ending marker to use.
  38. const llvm::MemoryBuffer *PredefinesBuffer; ///< The preprocessor predefines.
  39. bool ShowLineMarkers; ///< Show #line markers.
  40. bool UseLineDirectives; ///< Use of line directives or line markers.
  41. /// Tracks where inclusions that change the file are found.
  42. std::map<unsigned, IncludedFile> FileIncludes;
  43. /// Tracks where inclusions that import modules are found.
  44. std::map<unsigned, const Module *> ModuleIncludes;
  45. /// Tracks where inclusions that enter modules (in a module build) are found.
  46. std::map<unsigned, const Module *> ModuleEntryIncludes;
  47. /// Used transitively for building up the FileIncludes mapping over the
  48. /// various \c PPCallbacks callbacks.
  49. SourceLocation LastInclusionLocation;
  50. public:
  51. InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers,
  52. bool UseLineDirectives);
  53. void Process(FileID FileId, SrcMgr::CharacteristicKind FileType);
  54. void setPredefinesBuffer(const llvm::MemoryBuffer *Buf) {
  55. PredefinesBuffer = Buf;
  56. }
  57. void detectMainFileEOL();
  58. void handleModuleBegin(Token &Tok) {
  59. assert(Tok.getKind() == tok::annot_module_begin);
  60. ModuleEntryIncludes.insert({Tok.getLocation().getRawEncoding(),
  61. (Module *)Tok.getAnnotationValue()});
  62. }
  63. private:
  64. void FileChanged(SourceLocation Loc, FileChangeReason Reason,
  65. SrcMgr::CharacteristicKind FileType,
  66. FileID PrevFID) override;
  67. void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
  68. SrcMgr::CharacteristicKind FileType) override;
  69. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
  70. StringRef FileName, bool IsAngled,
  71. CharSourceRange FilenameRange, const FileEntry *File,
  72. StringRef SearchPath, StringRef RelativePath,
  73. const Module *Imported) override;
  74. void WriteLineInfo(StringRef Filename, int Line,
  75. SrcMgr::CharacteristicKind FileType,
  76. StringRef Extra = StringRef());
  77. void WriteImplicitModuleImport(const Module *Mod);
  78. void OutputContentUpTo(const MemoryBuffer &FromFile,
  79. unsigned &WriteFrom, unsigned WriteTo,
  80. StringRef EOL, int &lines,
  81. bool EnsureNewline);
  82. void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
  83. const MemoryBuffer &FromFile, StringRef EOL,
  84. unsigned &NextToWrite, int &Lines);
  85. bool HandleHasInclude(FileID FileId, Lexer &RawLex,
  86. const DirectoryLookup *Lookup, Token &Tok,
  87. bool &FileExists);
  88. const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
  89. const Module *FindModuleAtLocation(SourceLocation Loc) const;
  90. const Module *FindEnteredModule(SourceLocation Loc) const;
  91. StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
  92. };
  93. } // end anonymous namespace
  94. /// Initializes an InclusionRewriter with a \p PP source and \p OS destination.
  95. InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
  96. bool ShowLineMarkers,
  97. bool UseLineDirectives)
  98. : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
  99. PredefinesBuffer(nullptr), ShowLineMarkers(ShowLineMarkers),
  100. UseLineDirectives(UseLineDirectives),
  101. LastInclusionLocation(SourceLocation()) {}
  102. /// Write appropriate line information as either #line directives or GNU line
  103. /// markers depending on what mode we're in, including the \p Filename and
  104. /// \p Line we are located at, using the specified \p EOL line separator, and
  105. /// any \p Extra context specifiers in GNU line directives.
  106. void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line,
  107. SrcMgr::CharacteristicKind FileType,
  108. StringRef Extra) {
  109. if (!ShowLineMarkers)
  110. return;
  111. if (UseLineDirectives) {
  112. OS << "#line" << ' ' << Line << ' ' << '"';
  113. OS.write_escaped(Filename);
  114. OS << '"';
  115. } else {
  116. // Use GNU linemarkers as described here:
  117. // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
  118. OS << '#' << ' ' << Line << ' ' << '"';
  119. OS.write_escaped(Filename);
  120. OS << '"';
  121. if (!Extra.empty())
  122. OS << Extra;
  123. if (FileType == SrcMgr::C_System)
  124. // "`3' This indicates that the following text comes from a system header
  125. // file, so certain warnings should be suppressed."
  126. OS << " 3";
  127. else if (FileType == SrcMgr::C_ExternCSystem)
  128. // as above for `3', plus "`4' This indicates that the following text
  129. // should be treated as being wrapped in an implicit extern "C" block."
  130. OS << " 3 4";
  131. }
  132. OS << MainEOL;
  133. }
  134. void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
  135. OS << "#pragma clang module import " << Mod->getFullModuleName()
  136. << " /* clang -frewrite-includes: implicit import */" << MainEOL;
  137. }
  138. /// FileChanged - Whenever the preprocessor enters or exits a #include file
  139. /// it invokes this handler.
  140. void InclusionRewriter::FileChanged(SourceLocation Loc,
  141. FileChangeReason Reason,
  142. SrcMgr::CharacteristicKind NewFileType,
  143. FileID) {
  144. if (Reason != EnterFile)
  145. return;
  146. if (LastInclusionLocation.isInvalid())
  147. // we didn't reach this file (eg: the main file) via an inclusion directive
  148. return;
  149. FileID Id = FullSourceLoc(Loc, SM).getFileID();
  150. auto P = FileIncludes.insert(std::make_pair(
  151. LastInclusionLocation.getRawEncoding(), IncludedFile(Id, NewFileType)));
  152. (void)P;
  153. assert(P.second && "Unexpected revisitation of the same include directive");
  154. LastInclusionLocation = SourceLocation();
  155. }
  156. /// Called whenever an inclusion is skipped due to canonical header protection
  157. /// macros.
  158. void InclusionRewriter::FileSkipped(const FileEntry &/*SkippedFile*/,
  159. const Token &/*FilenameTok*/,
  160. SrcMgr::CharacteristicKind /*FileType*/) {
  161. assert(LastInclusionLocation.isValid() &&
  162. "A file, that wasn't found via an inclusion directive, was skipped");
  163. LastInclusionLocation = SourceLocation();
  164. }
  165. /// This should be called whenever the preprocessor encounters include
  166. /// directives. It does not say whether the file has been included, but it
  167. /// provides more information about the directive (hash location instead
  168. /// of location inside the included file). It is assumed that the matching
  169. /// FileChanged() or FileSkipped() is called after this (or neither is
  170. /// called if this #include results in an error or does not textually include
  171. /// anything).
  172. void InclusionRewriter::InclusionDirective(SourceLocation HashLoc,
  173. const Token &/*IncludeTok*/,
  174. StringRef /*FileName*/,
  175. bool /*IsAngled*/,
  176. CharSourceRange /*FilenameRange*/,
  177. const FileEntry * /*File*/,
  178. StringRef /*SearchPath*/,
  179. StringRef /*RelativePath*/,
  180. const Module *Imported) {
  181. if (Imported) {
  182. auto P = ModuleIncludes.insert(
  183. std::make_pair(HashLoc.getRawEncoding(), Imported));
  184. (void)P;
  185. assert(P.second && "Unexpected revisitation of the same include directive");
  186. } else
  187. LastInclusionLocation = HashLoc;
  188. }
  189. /// Simple lookup for a SourceLocation (specifically one denoting the hash in
  190. /// an inclusion directive) in the map of inclusion information, FileChanges.
  191. const InclusionRewriter::IncludedFile *
  192. InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
  193. const auto I = FileIncludes.find(Loc.getRawEncoding());
  194. if (I != FileIncludes.end())
  195. return &I->second;
  196. return nullptr;
  197. }
  198. /// Simple lookup for a SourceLocation (specifically one denoting the hash in
  199. /// an inclusion directive) in the map of module inclusion information.
  200. const Module *
  201. InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
  202. const auto I = ModuleIncludes.find(Loc.getRawEncoding());
  203. if (I != ModuleIncludes.end())
  204. return I->second;
  205. return nullptr;
  206. }
  207. /// Simple lookup for a SourceLocation (specifically one denoting the hash in
  208. /// an inclusion directive) in the map of module entry information.
  209. const Module *
  210. InclusionRewriter::FindEnteredModule(SourceLocation Loc) const {
  211. const auto I = ModuleEntryIncludes.find(Loc.getRawEncoding());
  212. if (I != ModuleEntryIncludes.end())
  213. return I->second;
  214. return nullptr;
  215. }
  216. /// Detect the likely line ending style of \p FromFile by examining the first
  217. /// newline found within it.
  218. static StringRef DetectEOL(const MemoryBuffer &FromFile) {
  219. // Detect what line endings the file uses, so that added content does not mix
  220. // the style. We need to check for "\r\n" first because "\n\r" will match
  221. // "\r\n\r\n".
  222. const char *Pos = strchr(FromFile.getBufferStart(), '\n');
  223. if (!Pos)
  224. return "\n";
  225. if (Pos - 1 >= FromFile.getBufferStart() && Pos[-1] == '\r')
  226. return "\r\n";
  227. if (Pos + 1 < FromFile.getBufferEnd() && Pos[1] == '\r')
  228. return "\n\r";
  229. return "\n";
  230. }
  231. void InclusionRewriter::detectMainFileEOL() {
  232. bool Invalid;
  233. const MemoryBuffer &FromFile = *SM.getBuffer(SM.getMainFileID(), &Invalid);
  234. assert(!Invalid);
  235. if (Invalid)
  236. return; // Should never happen, but whatever.
  237. MainEOL = DetectEOL(FromFile);
  238. }
  239. /// Writes out bytes from \p FromFile, starting at \p NextToWrite and ending at
  240. /// \p WriteTo - 1.
  241. void InclusionRewriter::OutputContentUpTo(const MemoryBuffer &FromFile,
  242. unsigned &WriteFrom, unsigned WriteTo,
  243. StringRef LocalEOL, int &Line,
  244. bool EnsureNewline) {
  245. if (WriteTo <= WriteFrom)
  246. return;
  247. if (&FromFile == PredefinesBuffer) {
  248. // Ignore the #defines of the predefines buffer.
  249. WriteFrom = WriteTo;
  250. return;
  251. }
  252. // If we would output half of a line ending, advance one character to output
  253. // the whole line ending. All buffers are null terminated, so looking ahead
  254. // one byte is safe.
  255. if (LocalEOL.size() == 2 &&
  256. LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
  257. LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
  258. WriteTo++;
  259. StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
  260. WriteTo - WriteFrom);
  261. if (MainEOL == LocalEOL) {
  262. OS << TextToWrite;
  263. // count lines manually, it's faster than getPresumedLoc()
  264. Line += TextToWrite.count(LocalEOL);
  265. if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
  266. OS << MainEOL;
  267. } else {
  268. // Output the file one line at a time, rewriting the line endings as we go.
  269. StringRef Rest = TextToWrite;
  270. while (!Rest.empty()) {
  271. StringRef LineText;
  272. std::tie(LineText, Rest) = Rest.split(LocalEOL);
  273. OS << LineText;
  274. Line++;
  275. if (!Rest.empty())
  276. OS << MainEOL;
  277. }
  278. if (TextToWrite.endswith(LocalEOL) || EnsureNewline)
  279. OS << MainEOL;
  280. }
  281. WriteFrom = WriteTo;
  282. }
  283. /// Print characters from \p FromFile starting at \p NextToWrite up until the
  284. /// inclusion directive at \p StartToken, then print out the inclusion
  285. /// inclusion directive disabled by a #if directive, updating \p NextToWrite
  286. /// and \p Line to track the number of source lines visited and the progress
  287. /// through the \p FromFile buffer.
  288. void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
  289. const Token &StartToken,
  290. const MemoryBuffer &FromFile,
  291. StringRef LocalEOL,
  292. unsigned &NextToWrite, int &Line) {
  293. OutputContentUpTo(FromFile, NextToWrite,
  294. SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
  295. false);
  296. Token DirectiveToken;
  297. do {
  298. DirectiveLex.LexFromRawLexer(DirectiveToken);
  299. } while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof));
  300. if (&FromFile == PredefinesBuffer) {
  301. // OutputContentUpTo() would not output anything anyway.
  302. return;
  303. }
  304. OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
  305. OutputContentUpTo(FromFile, NextToWrite,
  306. SM.getFileOffset(DirectiveToken.getLocation()) +
  307. DirectiveToken.getLength(),
  308. LocalEOL, Line, true);
  309. OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
  310. }
  311. /// Find the next identifier in the pragma directive specified by \p RawToken.
  312. StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
  313. Token &RawToken) {
  314. RawLex.LexFromRawLexer(RawToken);
  315. if (RawToken.is(tok::raw_identifier))
  316. PP.LookUpIdentifierInfo(RawToken);
  317. if (RawToken.is(tok::identifier))
  318. return RawToken.getIdentifierInfo()->getName();
  319. return StringRef();
  320. }
  321. // Expand __has_include and __has_include_next if possible. If there's no
  322. // definitive answer return false.
  323. bool InclusionRewriter::HandleHasInclude(
  324. FileID FileId, Lexer &RawLex, const DirectoryLookup *Lookup, Token &Tok,
  325. bool &FileExists) {
  326. // Lex the opening paren.
  327. RawLex.LexFromRawLexer(Tok);
  328. if (Tok.isNot(tok::l_paren))
  329. return false;
  330. RawLex.LexFromRawLexer(Tok);
  331. SmallString<128> FilenameBuffer;
  332. StringRef Filename;
  333. // Since the raw lexer doesn't give us angle_literals we have to parse them
  334. // ourselves.
  335. // FIXME: What to do if the file name is a macro?
  336. if (Tok.is(tok::less)) {
  337. RawLex.LexFromRawLexer(Tok);
  338. FilenameBuffer += '<';
  339. do {
  340. if (Tok.is(tok::eod)) // Sanity check.
  341. return false;
  342. if (Tok.is(tok::raw_identifier))
  343. PP.LookUpIdentifierInfo(Tok);
  344. // Get the string piece.
  345. SmallVector<char, 128> TmpBuffer;
  346. bool Invalid = false;
  347. StringRef TmpName = PP.getSpelling(Tok, TmpBuffer, &Invalid);
  348. if (Invalid)
  349. return false;
  350. FilenameBuffer += TmpName;
  351. RawLex.LexFromRawLexer(Tok);
  352. } while (Tok.isNot(tok::greater));
  353. FilenameBuffer += '>';
  354. Filename = FilenameBuffer;
  355. } else {
  356. if (Tok.isNot(tok::string_literal))
  357. return false;
  358. bool Invalid = false;
  359. Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
  360. if (Invalid)
  361. return false;
  362. }
  363. // Lex the closing paren.
  364. RawLex.LexFromRawLexer(Tok);
  365. if (Tok.isNot(tok::r_paren))
  366. return false;
  367. // Now ask HeaderInfo if it knows about the header.
  368. // FIXME: Subframeworks aren't handled here. Do we care?
  369. bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
  370. const DirectoryLookup *CurDir;
  371. const FileEntry *FileEnt = PP.getSourceManager().getFileEntryForID(FileId);
  372. SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1>
  373. Includers;
  374. Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
  375. // FIXME: Why don't we call PP.LookupFile here?
  376. const FileEntry *File = PP.getHeaderSearchInfo().LookupFile(
  377. Filename, SourceLocation(), isAngled, nullptr, CurDir, Includers, nullptr,
  378. nullptr, nullptr, nullptr, nullptr);
  379. FileExists = File != nullptr;
  380. return true;
  381. }
  382. /// Use a raw lexer to analyze \p FileId, incrementally copying parts of it
  383. /// and including content of included files recursively.
  384. void InclusionRewriter::Process(FileID FileId,
  385. SrcMgr::CharacteristicKind FileType) {
  386. bool Invalid;
  387. const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid);
  388. assert(!Invalid && "Attempting to process invalid inclusion");
  389. StringRef FileName = FromFile.getBufferIdentifier();
  390. Lexer RawLex(FileId, &FromFile, PP.getSourceManager(), PP.getLangOpts());
  391. RawLex.SetCommentRetentionState(false);
  392. StringRef LocalEOL = DetectEOL(FromFile);
  393. // Per the GNU docs: "1" indicates entering a new file.
  394. if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID())
  395. WriteLineInfo(FileName, 1, FileType, "");
  396. else
  397. WriteLineInfo(FileName, 1, FileType, " 1");
  398. if (SM.getFileIDSize(FileId) == 0)
  399. return;
  400. // The next byte to be copied from the source file, which may be non-zero if
  401. // the lexer handled a BOM.
  402. unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
  403. assert(SM.getLineNumber(FileId, NextToWrite) == 1);
  404. int Line = 1; // The current input file line number.
  405. Token RawToken;
  406. RawLex.LexFromRawLexer(RawToken);
  407. // TODO: Consider adding a switch that strips possibly unimportant content,
  408. // such as comments, to reduce the size of repro files.
  409. while (RawToken.isNot(tok::eof)) {
  410. if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) {
  411. RawLex.setParsingPreprocessorDirective(true);
  412. Token HashToken = RawToken;
  413. RawLex.LexFromRawLexer(RawToken);
  414. if (RawToken.is(tok::raw_identifier))
  415. PP.LookUpIdentifierInfo(RawToken);
  416. if (RawToken.getIdentifierInfo() != nullptr) {
  417. switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
  418. case tok::pp_include:
  419. case tok::pp_include_next:
  420. case tok::pp_import: {
  421. CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
  422. Line);
  423. if (FileId != PP.getPredefinesFileID())
  424. WriteLineInfo(FileName, Line - 1, FileType, "");
  425. StringRef LineInfoExtra;
  426. SourceLocation Loc = HashToken.getLocation();
  427. if (const Module *Mod = FindModuleAtLocation(Loc))
  428. WriteImplicitModuleImport(Mod);
  429. else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
  430. const Module *Mod = FindEnteredModule(Loc);
  431. if (Mod)
  432. OS << "#pragma clang module begin " << Mod->getFullModuleName()
  433. << "\n";
  434. // Include and recursively process the file.
  435. Process(Inc->Id, Inc->FileType);
  436. if (Mod)
  437. OS << "#pragma clang module end /*" << Mod->getFullModuleName()
  438. << "*/\n";
  439. // Add line marker to indicate we're returning from an included
  440. // file.
  441. LineInfoExtra = " 2";
  442. }
  443. // fix up lineinfo (since commented out directive changed line
  444. // numbers) for inclusions that were skipped due to header guards
  445. WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
  446. break;
  447. }
  448. case tok::pp_pragma: {
  449. StringRef Identifier = NextIdentifierName(RawLex, RawToken);
  450. if (Identifier == "clang" || Identifier == "GCC") {
  451. if (NextIdentifierName(RawLex, RawToken) == "system_header") {
  452. // keep the directive in, commented out
  453. CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
  454. NextToWrite, Line);
  455. // update our own type
  456. FileType = SM.getFileCharacteristic(RawToken.getLocation());
  457. WriteLineInfo(FileName, Line, FileType);
  458. }
  459. } else if (Identifier == "once") {
  460. // keep the directive in, commented out
  461. CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
  462. NextToWrite, Line);
  463. WriteLineInfo(FileName, Line, FileType);
  464. }
  465. break;
  466. }
  467. case tok::pp_if:
  468. case tok::pp_elif: {
  469. bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
  470. tok::pp_elif);
  471. // Rewrite special builtin macros to avoid pulling in host details.
  472. do {
  473. // Walk over the directive.
  474. RawLex.LexFromRawLexer(RawToken);
  475. if (RawToken.is(tok::raw_identifier))
  476. PP.LookUpIdentifierInfo(RawToken);
  477. if (RawToken.is(tok::identifier)) {
  478. bool HasFile;
  479. SourceLocation Loc = RawToken.getLocation();
  480. // Rewrite __has_include(x)
  481. if (RawToken.getIdentifierInfo()->isStr("__has_include")) {
  482. if (!HandleHasInclude(FileId, RawLex, nullptr, RawToken,
  483. HasFile))
  484. continue;
  485. // Rewrite __has_include_next(x)
  486. } else if (RawToken.getIdentifierInfo()->isStr(
  487. "__has_include_next")) {
  488. const DirectoryLookup *Lookup = PP.GetCurDirLookup();
  489. if (Lookup)
  490. ++Lookup;
  491. if (!HandleHasInclude(FileId, RawLex, Lookup, RawToken,
  492. HasFile))
  493. continue;
  494. } else {
  495. continue;
  496. }
  497. // Replace the macro with (0) or (1), followed by the commented
  498. // out macro for reference.
  499. OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc),
  500. LocalEOL, Line, false);
  501. OS << '(' << (int) HasFile << ")/*";
  502. OutputContentUpTo(FromFile, NextToWrite,
  503. SM.getFileOffset(RawToken.getLocation()) +
  504. RawToken.getLength(),
  505. LocalEOL, Line, false);
  506. OS << "*/";
  507. }
  508. } while (RawToken.isNot(tok::eod));
  509. if (elif) {
  510. OutputContentUpTo(FromFile, NextToWrite,
  511. SM.getFileOffset(RawToken.getLocation()) +
  512. RawToken.getLength(),
  513. LocalEOL, Line, /*EnsureNewline=*/ true);
  514. WriteLineInfo(FileName, Line, FileType);
  515. }
  516. break;
  517. }
  518. case tok::pp_endif:
  519. case tok::pp_else: {
  520. // We surround every #include by #if 0 to comment it out, but that
  521. // changes line numbers. These are fixed up right after that, but
  522. // the whole #include could be inside a preprocessor conditional
  523. // that is not processed. So it is necessary to fix the line
  524. // numbers one the next line after each #else/#endif as well.
  525. RawLex.SetKeepWhitespaceMode(true);
  526. do {
  527. RawLex.LexFromRawLexer(RawToken);
  528. } while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof));
  529. OutputContentUpTo(FromFile, NextToWrite,
  530. SM.getFileOffset(RawToken.getLocation()) +
  531. RawToken.getLength(),
  532. LocalEOL, Line, /*EnsureNewline=*/ true);
  533. WriteLineInfo(FileName, Line, FileType);
  534. RawLex.SetKeepWhitespaceMode(false);
  535. }
  536. default:
  537. break;
  538. }
  539. }
  540. RawLex.setParsingPreprocessorDirective(false);
  541. }
  542. RawLex.LexFromRawLexer(RawToken);
  543. }
  544. OutputContentUpTo(FromFile, NextToWrite,
  545. SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
  546. Line, /*EnsureNewline=*/true);
  547. }
  548. /// InclusionRewriterInInput - Implement -frewrite-includes mode.
  549. void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
  550. const PreprocessorOutputOptions &Opts) {
  551. SourceManager &SM = PP.getSourceManager();
  552. InclusionRewriter *Rewrite = new InclusionRewriter(
  553. PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
  554. Rewrite->detectMainFileEOL();
  555. PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
  556. PP.IgnorePragmas();
  557. // First let the preprocessor process the entire file and call callbacks.
  558. // Callbacks will record which #include's were actually performed.
  559. PP.EnterMainSourceFile();
  560. Token Tok;
  561. // Only preprocessor directives matter here, so disable macro expansion
  562. // everywhere else as an optimization.
  563. // TODO: It would be even faster if the preprocessor could be switched
  564. // to a mode where it would parse only preprocessor directives and comments,
  565. // nothing else matters for parsing or processing.
  566. PP.SetMacroExpansionOnlyInDirectives();
  567. do {
  568. PP.Lex(Tok);
  569. if (Tok.is(tok::annot_module_begin))
  570. Rewrite->handleModuleBegin(Tok);
  571. } while (Tok.isNot(tok::eof));
  572. Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID()));
  573. Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User);
  574. Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User);
  575. OS->flush();
  576. }