InclusionToSnippetRewriter.cpp 27 KB

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