blob: 2e7baa3d9581b08e2ead22edb72dbfcdd64d3f19 [file] [log] [blame]
David Blaikied5321242012-06-06 18:52:13 +00001//===--- 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
Ted Kremenekcdf81492012-09-01 05:09:24 +000015#include "clang/Rewrite/Frontend/Rewriters.h"
David Blaikied5321242012-06-06 18:52:13 +000016#include "clang/Basic/SourceManager.h"
17#include "clang/Frontend/PreprocessorOutputOptions.h"
Benjamin Kramerb10e6152013-04-16 19:08:41 +000018#include "clang/Lex/HeaderSearch.h"
Lubos Lunakba5ee4d2013-07-20 14:30:01 +000019#include "clang/Lex/Pragma.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "clang/Lex/Preprocessor.h"
Benjamin Kramerb10e6152013-04-16 19:08:41 +000021#include "llvm/ADT/SmallString.h"
David Blaikied5321242012-06-06 18:52:13 +000022#include "llvm/Support/raw_ostream.h"
23
24using namespace clang;
25using namespace llvm;
26
27namespace {
28
29class InclusionRewriter : public PPCallbacks {
30 /// Information about which #includes were actually performed,
31 /// created by preprocessor callbacks.
Justin Bogner0707fd02015-07-01 04:40:10 +000032 struct IncludedFile {
David Blaikied5321242012-06-06 18:52:13 +000033 FileID Id;
34 SrcMgr::CharacteristicKind FileType;
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +000035 const DirectoryLookup *DirLookup;
36 IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType,
37 const DirectoryLookup *DirLookup)
38 : Id(Id), FileType(FileType), DirLookup(DirLookup) {}
David Blaikied5321242012-06-06 18:52:13 +000039 };
Dmitri Gribenko4280e5c2012-06-08 23:13:42 +000040 Preprocessor &PP; ///< Used to find inclusion directives.
41 SourceManager &SM; ///< Used to read and manage source files.
42 raw_ostream &OS; ///< The destination stream for rewritten contents.
Reid Klecknere2793c02014-09-05 16:49:50 +000043 StringRef MainEOL; ///< The line ending marker to use.
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +000044 const llvm::MemoryBuffer *PredefinesBuffer; ///< The preprocessor predefines.
Dmitri Gribenko4280e5c2012-06-08 23:13:42 +000045 bool ShowLineMarkers; ///< Show #line markers.
Reid Kleckner1df0fea2015-02-26 00:17:25 +000046 bool UseLineDirectives; ///< Use of line directives or line markers.
Justin Bogner0707fd02015-07-01 04:40:10 +000047 /// Tracks where inclusions that change the file are found.
48 std::map<unsigned, IncludedFile> FileIncludes;
49 /// Tracks where inclusions that import modules are found.
50 std::map<unsigned, const Module *> ModuleIncludes;
Richard Smithd1386302017-05-04 00:29:54 +000051 /// Tracks where inclusions that enter modules (in a module build) are found.
52 std::map<unsigned, const Module *> ModuleEntryIncludes;
Justin Bogner0707fd02015-07-01 04:40:10 +000053 /// Used transitively for building up the FileIncludes mapping over the
David Blaikied5321242012-06-06 18:52:13 +000054 /// various \c PPCallbacks callbacks.
Justin Bogner0707fd02015-07-01 04:40:10 +000055 SourceLocation LastInclusionLocation;
David Blaikied5321242012-06-06 18:52:13 +000056public:
Reid Kleckner1df0fea2015-02-26 00:17:25 +000057 InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers,
58 bool UseLineDirectives);
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +000059 void Process(FileID FileId, SrcMgr::CharacteristicKind FileType,
60 const DirectoryLookup *DirLookup);
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +000061 void setPredefinesBuffer(const llvm::MemoryBuffer *Buf) {
62 PredefinesBuffer = Buf;
63 }
Reid Klecknere2793c02014-09-05 16:49:50 +000064 void detectMainFileEOL();
Richard Smithd1386302017-05-04 00:29:54 +000065 void handleModuleBegin(Token &Tok) {
66 assert(Tok.getKind() == tok::annot_module_begin);
67 ModuleEntryIncludes.insert({Tok.getLocation().getRawEncoding(),
68 (Module *)Tok.getAnnotationValue()});
69 }
David Blaikied5321242012-06-06 18:52:13 +000070private:
Craig Topperfb6b25b2014-03-15 04:29:04 +000071 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
72 SrcMgr::CharacteristicKind FileType,
73 FileID PrevFID) override;
Nikola Smiljanicfb891fc2015-05-12 11:48:05 +000074 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
Craig Topperfb6b25b2014-03-15 04:29:04 +000075 SrcMgr::CharacteristicKind FileType) override;
76 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
77 StringRef FileName, bool IsAngled,
78 CharSourceRange FilenameRange, const FileEntry *File,
79 StringRef SearchPath, StringRef RelativePath,
Julie Hockett96fbe582018-05-10 19:05:36 +000080 const Module *Imported,
81 SrcMgr::CharacteristicKind FileType) override;
Mehdi Amini99d1b292016-10-01 16:38:28 +000082 void WriteLineInfo(StringRef Filename, int Line,
David Blaikied5321242012-06-06 18:52:13 +000083 SrcMgr::CharacteristicKind FileType,
Reid Klecknere2793c02014-09-05 16:49:50 +000084 StringRef Extra = StringRef());
85 void WriteImplicitModuleImport(const Module *Mod);
David Blaikied5321242012-06-06 18:52:13 +000086 void OutputContentUpTo(const MemoryBuffer &FromFile,
87 unsigned &WriteFrom, unsigned WriteTo,
88 StringRef EOL, int &lines,
Alp Toker08c25002013-12-13 17:04:55 +000089 bool EnsureNewline);
David Blaikied5321242012-06-06 18:52:13 +000090 void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
91 const MemoryBuffer &FromFile, StringRef EOL,
92 unsigned &NextToWrite, int &Lines);
Benjamin Kramerb10e6152013-04-16 19:08:41 +000093 bool HandleHasInclude(FileID FileId, Lexer &RawLex,
94 const DirectoryLookup *Lookup, Token &Tok,
95 bool &FileExists);
Justin Bogner0707fd02015-07-01 04:40:10 +000096 const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
97 const Module *FindModuleAtLocation(SourceLocation Loc) const;
Richard Smithd1386302017-05-04 00:29:54 +000098 const Module *FindEnteredModule(SourceLocation Loc) const;
David Blaikied5321242012-06-06 18:52:13 +000099 StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
100};
101
102} // end anonymous namespace
103
104/// Initializes an InclusionRewriter with a \p PP source and \p OS destination.
105InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000106 bool ShowLineMarkers,
107 bool UseLineDirectives)
Reid Klecknere2793c02014-09-05 16:49:50 +0000108 : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
109 PredefinesBuffer(nullptr), ShowLineMarkers(ShowLineMarkers),
Eric Christopher8213f7f2015-02-26 00:29:54 +0000110 UseLineDirectives(UseLineDirectives),
Justin Bogner0707fd02015-07-01 04:40:10 +0000111 LastInclusionLocation(SourceLocation()) {}
David Blaikied5321242012-06-06 18:52:13 +0000112
113/// Write appropriate line information as either #line directives or GNU line
114/// markers depending on what mode we're in, including the \p Filename and
115/// \p Line we are located at, using the specified \p EOL line separator, and
116/// any \p Extra context specifiers in GNU line directives.
Mehdi Amini99d1b292016-10-01 16:38:28 +0000117void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line,
David Blaikied5321242012-06-06 18:52:13 +0000118 SrcMgr::CharacteristicKind FileType,
Reid Klecknere2793c02014-09-05 16:49:50 +0000119 StringRef Extra) {
David Blaikied5321242012-06-06 18:52:13 +0000120 if (!ShowLineMarkers)
121 return;
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000122 if (UseLineDirectives) {
Eli Friedman9fc443a2013-09-17 00:51:31 +0000123 OS << "#line" << ' ' << Line << ' ' << '"';
124 OS.write_escaped(Filename);
125 OS << '"';
David Blaikied5321242012-06-06 18:52:13 +0000126 } else {
127 // Use GNU linemarkers as described here:
128 // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
Eli Friedman80e45b82013-08-29 01:42:42 +0000129 OS << '#' << ' ' << Line << ' ' << '"';
130 OS.write_escaped(Filename);
131 OS << '"';
David Blaikied5321242012-06-06 18:52:13 +0000132 if (!Extra.empty())
133 OS << Extra;
134 if (FileType == SrcMgr::C_System)
135 // "`3' This indicates that the following text comes from a system header
136 // file, so certain warnings should be suppressed."
137 OS << " 3";
138 else if (FileType == SrcMgr::C_ExternCSystem)
139 // as above for `3', plus "`4' This indicates that the following text
140 // should be treated as being wrapped in an implicit extern "C" block."
141 OS << " 3 4";
142 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000143 OS << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000144}
145
Reid Klecknere2793c02014-09-05 16:49:50 +0000146void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
Richard Smith9565c75b2017-06-19 23:09:36 +0000147 OS << "#pragma clang module import " << Mod->getFullModuleName(true)
Reid Klecknere2793c02014-09-05 16:49:50 +0000148 << " /* clang -frewrite-includes: implicit import */" << MainEOL;
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000149}
150
David Blaikied5321242012-06-06 18:52:13 +0000151/// FileChanged - Whenever the preprocessor enters or exits a #include file
152/// it invokes this handler.
153void InclusionRewriter::FileChanged(SourceLocation Loc,
154 FileChangeReason Reason,
155 SrcMgr::CharacteristicKind NewFileType,
156 FileID) {
157 if (Reason != EnterFile)
158 return;
Justin Bogner0707fd02015-07-01 04:40:10 +0000159 if (LastInclusionLocation.isInvalid())
David Blaikied5321242012-06-06 18:52:13 +0000160 // we didn't reach this file (eg: the main file) via an inclusion directive
161 return;
Justin Bogner0707fd02015-07-01 04:40:10 +0000162 FileID Id = FullSourceLoc(Loc, SM).getFileID();
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000163 auto P = FileIncludes.insert(
164 std::make_pair(LastInclusionLocation.getRawEncoding(),
165 IncludedFile(Id, NewFileType, PP.GetCurDirLookup())));
Justin Bogner2510ba32015-07-01 05:41:50 +0000166 (void)P;
Justin Bogner0707fd02015-07-01 04:40:10 +0000167 assert(P.second && "Unexpected revisitation of the same include directive");
168 LastInclusionLocation = SourceLocation();
David Blaikied5321242012-06-06 18:52:13 +0000169}
170
171/// Called whenever an inclusion is skipped due to canonical header protection
172/// macros.
Nikola Smiljanicfb891fc2015-05-12 11:48:05 +0000173void InclusionRewriter::FileSkipped(const FileEntry &/*SkippedFile*/,
David Blaikied5321242012-06-06 18:52:13 +0000174 const Token &/*FilenameTok*/,
175 SrcMgr::CharacteristicKind /*FileType*/) {
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000176 assert(LastInclusionLocation.isValid() &&
Justin Bogner0707fd02015-07-01 04:40:10 +0000177 "A file, that wasn't found via an inclusion directive, was skipped");
178 LastInclusionLocation = SourceLocation();
David Blaikied5321242012-06-06 18:52:13 +0000179}
180
181/// This should be called whenever the preprocessor encounters include
182/// directives. It does not say whether the file has been included, but it
183/// provides more information about the directive (hash location instead
184/// of location inside the included file). It is assumed that the matching
Richard Smith4b46f722017-06-02 01:05:44 +0000185/// FileChanged() or FileSkipped() is called after this (or neither is
186/// called if this #include results in an error or does not textually include
187/// anything).
David Blaikied5321242012-06-06 18:52:13 +0000188void InclusionRewriter::InclusionDirective(SourceLocation HashLoc,
189 const Token &/*IncludeTok*/,
190 StringRef /*FileName*/,
191 bool /*IsAngled*/,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000192 CharSourceRange /*FilenameRange*/,
David Blaikied5321242012-06-06 18:52:13 +0000193 const FileEntry * /*File*/,
David Blaikied5321242012-06-06 18:52:13 +0000194 StringRef /*SearchPath*/,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000195 StringRef /*RelativePath*/,
Julie Hockett96fbe582018-05-10 19:05:36 +0000196 const Module *Imported,
197 SrcMgr::CharacteristicKind FileType){
Justin Bogner0707fd02015-07-01 04:40:10 +0000198 if (Imported) {
Justin Bogner879d4202015-07-01 04:53:19 +0000199 auto P = ModuleIncludes.insert(
200 std::make_pair(HashLoc.getRawEncoding(), Imported));
Justin Bogner2510ba32015-07-01 05:41:50 +0000201 (void)P;
Justin Bogner0707fd02015-07-01 04:40:10 +0000202 assert(P.second && "Unexpected revisitation of the same include directive");
203 } else
204 LastInclusionLocation = HashLoc;
David Blaikied5321242012-06-06 18:52:13 +0000205}
206
207/// Simple lookup for a SourceLocation (specifically one denoting the hash in
208/// an inclusion directive) in the map of inclusion information, FileChanges.
Justin Bogner0707fd02015-07-01 04:40:10 +0000209const InclusionRewriter::IncludedFile *
210InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
211 const auto I = FileIncludes.find(Loc.getRawEncoding());
212 if (I != FileIncludes.end())
David Blaikied5321242012-06-06 18:52:13 +0000213 return &I->second;
Craig Topper8ae12032014-05-07 06:21:57 +0000214 return nullptr;
David Blaikied5321242012-06-06 18:52:13 +0000215}
216
Justin Bogner0707fd02015-07-01 04:40:10 +0000217/// Simple lookup for a SourceLocation (specifically one denoting the hash in
218/// an inclusion directive) in the map of module inclusion information.
219const Module *
220InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
221 const auto I = ModuleIncludes.find(Loc.getRawEncoding());
222 if (I != ModuleIncludes.end())
223 return I->second;
224 return nullptr;
225}
226
Richard Smithd1386302017-05-04 00:29:54 +0000227/// Simple lookup for a SourceLocation (specifically one denoting the hash in
228/// an inclusion directive) in the map of module entry information.
229const Module *
230InclusionRewriter::FindEnteredModule(SourceLocation Loc) const {
231 const auto I = ModuleEntryIncludes.find(Loc.getRawEncoding());
232 if (I != ModuleEntryIncludes.end())
233 return I->second;
234 return nullptr;
235}
236
David Blaikied5321242012-06-06 18:52:13 +0000237/// Detect the likely line ending style of \p FromFile by examining the first
238/// newline found within it.
239static StringRef DetectEOL(const MemoryBuffer &FromFile) {
Reid Klecknere2793c02014-09-05 16:49:50 +0000240 // Detect what line endings the file uses, so that added content does not mix
241 // the style. We need to check for "\r\n" first because "\n\r" will match
242 // "\r\n\r\n".
David Blaikied5321242012-06-06 18:52:13 +0000243 const char *Pos = strchr(FromFile.getBufferStart(), '\n');
Craig Topper8ae12032014-05-07 06:21:57 +0000244 if (!Pos)
David Blaikied5321242012-06-06 18:52:13 +0000245 return "\n";
David Blaikied5321242012-06-06 18:52:13 +0000246 if (Pos - 1 >= FromFile.getBufferStart() && Pos[-1] == '\r')
247 return "\r\n";
Reid Klecknere2793c02014-09-05 16:49:50 +0000248 if (Pos + 1 < FromFile.getBufferEnd() && Pos[1] == '\r')
249 return "\n\r";
David Blaikied5321242012-06-06 18:52:13 +0000250 return "\n";
251}
252
Reid Klecknere2793c02014-09-05 16:49:50 +0000253void InclusionRewriter::detectMainFileEOL() {
254 bool Invalid;
255 const MemoryBuffer &FromFile = *SM.getBuffer(SM.getMainFileID(), &Invalid);
256 assert(!Invalid);
257 if (Invalid)
258 return; // Should never happen, but whatever.
259 MainEOL = DetectEOL(FromFile);
260}
261
David Blaikied5321242012-06-06 18:52:13 +0000262/// Writes out bytes from \p FromFile, starting at \p NextToWrite and ending at
263/// \p WriteTo - 1.
264void InclusionRewriter::OutputContentUpTo(const MemoryBuffer &FromFile,
265 unsigned &WriteFrom, unsigned WriteTo,
Reid Klecknere2793c02014-09-05 16:49:50 +0000266 StringRef LocalEOL, int &Line,
David Blaikied5321242012-06-06 18:52:13 +0000267 bool EnsureNewline) {
268 if (WriteTo <= WriteFrom)
269 return;
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +0000270 if (&FromFile == PredefinesBuffer) {
271 // Ignore the #defines of the predefines buffer.
272 WriteFrom = WriteTo;
273 return;
274 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000275
276 // If we would output half of a line ending, advance one character to output
277 // the whole line ending. All buffers are null terminated, so looking ahead
278 // one byte is safe.
279 if (LocalEOL.size() == 2 &&
280 LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
281 LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
282 WriteTo++;
283
284 StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
285 WriteTo - WriteFrom);
286
287 if (MainEOL == LocalEOL) {
288 OS << TextToWrite;
289 // count lines manually, it's faster than getPresumedLoc()
290 Line += TextToWrite.count(LocalEOL);
291 if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
292 OS << MainEOL;
293 } else {
294 // Output the file one line at a time, rewriting the line endings as we go.
295 StringRef Rest = TextToWrite;
296 while (!Rest.empty()) {
297 StringRef LineText;
298 std::tie(LineText, Rest) = Rest.split(LocalEOL);
299 OS << LineText;
300 Line++;
301 if (!Rest.empty())
302 OS << MainEOL;
303 }
304 if (TextToWrite.endswith(LocalEOL) || EnsureNewline)
305 OS << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000306 }
307 WriteFrom = WriteTo;
308}
309
310/// Print characters from \p FromFile starting at \p NextToWrite up until the
311/// inclusion directive at \p StartToken, then print out the inclusion
312/// inclusion directive disabled by a #if directive, updating \p NextToWrite
313/// and \p Line to track the number of source lines visited and the progress
314/// through the \p FromFile buffer.
315void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
316 const Token &StartToken,
317 const MemoryBuffer &FromFile,
Reid Klecknere2793c02014-09-05 16:49:50 +0000318 StringRef LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000319 unsigned &NextToWrite, int &Line) {
320 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000321 SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
322 false);
David Blaikied5321242012-06-06 18:52:13 +0000323 Token DirectiveToken;
324 do {
325 DirectiveLex.LexFromRawLexer(DirectiveToken);
326 } while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof));
Lubos Lunak72cad682014-05-01 21:10:08 +0000327 if (&FromFile == PredefinesBuffer) {
328 // OutputContentUpTo() would not output anything anyway.
329 return;
330 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000331 OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000332 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000333 SM.getFileOffset(DirectiveToken.getLocation()) +
334 DirectiveToken.getLength(),
335 LocalEOL, Line, true);
336 OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000337}
338
339/// Find the next identifier in the pragma directive specified by \p RawToken.
340StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
341 Token &RawToken) {
342 RawLex.LexFromRawLexer(RawToken);
343 if (RawToken.is(tok::raw_identifier))
344 PP.LookUpIdentifierInfo(RawToken);
345 if (RawToken.is(tok::identifier))
346 return RawToken.getIdentifierInfo()->getName();
347 return StringRef();
348}
349
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000350// Expand __has_include and __has_include_next if possible. If there's no
351// definitive answer return false.
352bool InclusionRewriter::HandleHasInclude(
353 FileID FileId, Lexer &RawLex, const DirectoryLookup *Lookup, Token &Tok,
354 bool &FileExists) {
355 // Lex the opening paren.
356 RawLex.LexFromRawLexer(Tok);
357 if (Tok.isNot(tok::l_paren))
358 return false;
359
360 RawLex.LexFromRawLexer(Tok);
361
362 SmallString<128> FilenameBuffer;
363 StringRef Filename;
364 // Since the raw lexer doesn't give us angle_literals we have to parse them
365 // ourselves.
366 // FIXME: What to do if the file name is a macro?
367 if (Tok.is(tok::less)) {
368 RawLex.LexFromRawLexer(Tok);
369
370 FilenameBuffer += '<';
371 do {
372 if (Tok.is(tok::eod)) // Sanity check.
373 return false;
374
375 if (Tok.is(tok::raw_identifier))
376 PP.LookUpIdentifierInfo(Tok);
377
378 // Get the string piece.
379 SmallVector<char, 128> TmpBuffer;
380 bool Invalid = false;
381 StringRef TmpName = PP.getSpelling(Tok, TmpBuffer, &Invalid);
382 if (Invalid)
383 return false;
384
385 FilenameBuffer += TmpName;
386
387 RawLex.LexFromRawLexer(Tok);
388 } while (Tok.isNot(tok::greater));
389
390 FilenameBuffer += '>';
391 Filename = FilenameBuffer;
392 } else {
393 if (Tok.isNot(tok::string_literal))
394 return false;
395
396 bool Invalid = false;
397 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
398 if (Invalid)
399 return false;
400 }
401
402 // Lex the closing paren.
403 RawLex.LexFromRawLexer(Tok);
404 if (Tok.isNot(tok::r_paren))
405 return false;
406
407 // Now ask HeaderInfo if it knows about the header.
408 // FIXME: Subframeworks aren't handled here. Do we care?
409 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
410 const DirectoryLookup *CurDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000411 const FileEntry *FileEnt = PP.getSourceManager().getFileEntryForID(FileId);
412 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1>
413 Includers;
414 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
Richard Smith3d5b48c2015-10-16 21:42:56 +0000415 // FIXME: Why don't we call PP.LookupFile here?
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000416 const FileEntry *File = PP.getHeaderSearchInfo().LookupFile(
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000417 Filename, SourceLocation(), isAngled, Lookup, CurDir, Includers, nullptr,
Duncan P. N. Exon Smithcfc1f6a2017-04-27 21:41:51 +0000418 nullptr, nullptr, nullptr, nullptr);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000419
Craig Topper8ae12032014-05-07 06:21:57 +0000420 FileExists = File != nullptr;
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000421 return true;
422}
423
Benjamin Kramere2881572013-10-13 12:02:16 +0000424/// Use a raw lexer to analyze \p FileId, incrementally copying parts of it
David Blaikied5321242012-06-06 18:52:13 +0000425/// and including content of included files recursively.
Richard Smithc7cacdc2017-04-29 00:54:03 +0000426void InclusionRewriter::Process(FileID FileId,
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000427 SrcMgr::CharacteristicKind FileType,
428 const DirectoryLookup *DirLookup) {
David Blaikied5321242012-06-06 18:52:13 +0000429 bool Invalid;
430 const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid);
Justin Bogner0707fd02015-07-01 04:40:10 +0000431 assert(!Invalid && "Attempting to process invalid inclusion");
Mehdi Amini99d1b292016-10-01 16:38:28 +0000432 StringRef FileName = FromFile.getBufferIdentifier();
David Blaikied5321242012-06-06 18:52:13 +0000433 Lexer RawLex(FileId, &FromFile, PP.getSourceManager(), PP.getLangOpts());
434 RawLex.SetCommentRetentionState(false);
435
Reid Klecknere2793c02014-09-05 16:49:50 +0000436 StringRef LocalEOL = DetectEOL(FromFile);
David Blaikied5321242012-06-06 18:52:13 +0000437
Lubos Lunak10961c02014-05-01 13:50:44 +0000438 // Per the GNU docs: "1" indicates entering a new file.
Lubos Lunak72cad682014-05-01 21:10:08 +0000439 if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID())
Reid Klecknere2793c02014-09-05 16:49:50 +0000440 WriteLineInfo(FileName, 1, FileType, "");
Lubos Lunak10961c02014-05-01 13:50:44 +0000441 else
Reid Klecknere2793c02014-09-05 16:49:50 +0000442 WriteLineInfo(FileName, 1, FileType, " 1");
David Blaikied5321242012-06-06 18:52:13 +0000443
444 if (SM.getFileIDSize(FileId) == 0)
Richard Smithc7cacdc2017-04-29 00:54:03 +0000445 return;
David Blaikied5321242012-06-06 18:52:13 +0000446
Alp Toker3dfeafd2013-11-28 07:21:44 +0000447 // The next byte to be copied from the source file, which may be non-zero if
448 // the lexer handled a BOM.
Alp Toker52937ab2013-12-05 17:28:42 +0000449 unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
450 assert(SM.getLineNumber(FileId, NextToWrite) == 1);
David Blaikied5321242012-06-06 18:52:13 +0000451 int Line = 1; // The current input file line number.
452
453 Token RawToken;
454 RawLex.LexFromRawLexer(RawToken);
455
456 // TODO: Consider adding a switch that strips possibly unimportant content,
457 // such as comments, to reduce the size of repro files.
458 while (RawToken.isNot(tok::eof)) {
459 if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) {
460 RawLex.setParsingPreprocessorDirective(true);
461 Token HashToken = RawToken;
462 RawLex.LexFromRawLexer(RawToken);
463 if (RawToken.is(tok::raw_identifier))
464 PP.LookUpIdentifierInfo(RawToken);
Craig Topper8ae12032014-05-07 06:21:57 +0000465 if (RawToken.getIdentifierInfo() != nullptr) {
David Blaikied5321242012-06-06 18:52:13 +0000466 switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
467 case tok::pp_include:
468 case tok::pp_include_next:
469 case tok::pp_import: {
Reid Klecknere2793c02014-09-05 16:49:50 +0000470 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
David Blaikied5321242012-06-06 18:52:13 +0000471 Line);
Lubos Lunak4526b462014-05-01 21:11:57 +0000472 if (FileId != PP.getPredefinesFileID())
Reid Klecknere2793c02014-09-05 16:49:50 +0000473 WriteLineInfo(FileName, Line - 1, FileType, "");
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000474 StringRef LineInfoExtra;
Justin Bogner0707fd02015-07-01 04:40:10 +0000475 SourceLocation Loc = HashToken.getLocation();
Richard Smithc51c38b2017-04-29 00:34:47 +0000476 if (const Module *Mod = FindModuleAtLocation(Loc))
Justin Bogner0707fd02015-07-01 04:40:10 +0000477 WriteImplicitModuleImport(Mod);
478 else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
Richard Smithd1386302017-05-04 00:29:54 +0000479 const Module *Mod = FindEnteredModule(Loc);
480 if (Mod)
Richard Smith9565c75b2017-06-19 23:09:36 +0000481 OS << "#pragma clang module begin "
482 << Mod->getFullModuleName(true) << "\n";
Richard Smithd1386302017-05-04 00:29:54 +0000483
Richard Smithc7cacdc2017-04-29 00:54:03 +0000484 // Include and recursively process the file.
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000485 Process(Inc->Id, Inc->FileType, Inc->DirLookup);
Richard Smithd1386302017-05-04 00:29:54 +0000486
487 if (Mod)
Richard Smith9565c75b2017-06-19 23:09:36 +0000488 OS << "#pragma clang module end /*"
489 << Mod->getFullModuleName(true) << "*/\n";
Richard Smithd1386302017-05-04 00:29:54 +0000490
Richard Smithc7cacdc2017-04-29 00:54:03 +0000491 // Add line marker to indicate we're returning from an included
492 // file.
493 LineInfoExtra = " 2";
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000494 }
495 // fix up lineinfo (since commented out directive changed line
496 // numbers) for inclusions that were skipped due to header guards
Reid Klecknere2793c02014-09-05 16:49:50 +0000497 WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
David Blaikied5321242012-06-06 18:52:13 +0000498 break;
499 }
500 case tok::pp_pragma: {
501 StringRef Identifier = NextIdentifierName(RawLex, RawToken);
502 if (Identifier == "clang" || Identifier == "GCC") {
503 if (NextIdentifierName(RawLex, RawToken) == "system_header") {
504 // keep the directive in, commented out
Reid Klecknere2793c02014-09-05 16:49:50 +0000505 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000506 NextToWrite, Line);
507 // update our own type
508 FileType = SM.getFileCharacteristic(RawToken.getLocation());
Reid Klecknere2793c02014-09-05 16:49:50 +0000509 WriteLineInfo(FileName, Line, FileType);
David Blaikied5321242012-06-06 18:52:13 +0000510 }
511 } else if (Identifier == "once") {
512 // keep the directive in, commented out
Reid Klecknere2793c02014-09-05 16:49:50 +0000513 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000514 NextToWrite, Line);
Reid Klecknere2793c02014-09-05 16:49:50 +0000515 WriteLineInfo(FileName, Line, FileType);
David Blaikied5321242012-06-06 18:52:13 +0000516 }
517 break;
518 }
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000519 case tok::pp_if:
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000520 case tok::pp_elif: {
521 bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
522 tok::pp_elif);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000523 // Rewrite special builtin macros to avoid pulling in host details.
524 do {
525 // Walk over the directive.
526 RawLex.LexFromRawLexer(RawToken);
527 if (RawToken.is(tok::raw_identifier))
528 PP.LookUpIdentifierInfo(RawToken);
529
530 if (RawToken.is(tok::identifier)) {
531 bool HasFile;
532 SourceLocation Loc = RawToken.getLocation();
533
534 // Rewrite __has_include(x)
535 if (RawToken.getIdentifierInfo()->isStr("__has_include")) {
Craig Topper8ae12032014-05-07 06:21:57 +0000536 if (!HandleHasInclude(FileId, RawLex, nullptr, RawToken,
537 HasFile))
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000538 continue;
539 // Rewrite __has_include_next(x)
540 } else if (RawToken.getIdentifierInfo()->isStr(
541 "__has_include_next")) {
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000542 if (DirLookup)
543 ++DirLookup;
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000544
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000545 if (!HandleHasInclude(FileId, RawLex, DirLookup, RawToken,
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000546 HasFile))
547 continue;
548 } else {
549 continue;
550 }
551 // Replace the macro with (0) or (1), followed by the commented
552 // out macro for reference.
553 OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc),
Reid Klecknere2793c02014-09-05 16:49:50 +0000554 LocalEOL, Line, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000555 OS << '(' << (int) HasFile << ")/*";
556 OutputContentUpTo(FromFile, NextToWrite,
557 SM.getFileOffset(RawToken.getLocation()) +
Reid Klecknere2793c02014-09-05 16:49:50 +0000558 RawToken.getLength(),
559 LocalEOL, Line, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000560 OS << "*/";
561 }
562 } while (RawToken.isNot(tok::eod));
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000563 if (elif) {
564 OutputContentUpTo(FromFile, NextToWrite,
565 SM.getFileOffset(RawToken.getLocation()) +
566 RawToken.getLength(),
Reid Klecknere2793c02014-09-05 16:49:50 +0000567 LocalEOL, Line, /*EnsureNewline=*/ true);
568 WriteLineInfo(FileName, Line, FileType);
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000569 }
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000570 break;
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000571 }
572 case tok::pp_endif:
573 case tok::pp_else: {
574 // We surround every #include by #if 0 to comment it out, but that
575 // changes line numbers. These are fixed up right after that, but
576 // the whole #include could be inside a preprocessor conditional
577 // that is not processed. So it is necessary to fix the line
578 // numbers one the next line after each #else/#endif as well.
579 RawLex.SetKeepWhitespaceMode(true);
580 do {
581 RawLex.LexFromRawLexer(RawToken);
582 } while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof));
Reid Klecknere2793c02014-09-05 16:49:50 +0000583 OutputContentUpTo(FromFile, NextToWrite,
584 SM.getFileOffset(RawToken.getLocation()) +
585 RawToken.getLength(),
586 LocalEOL, Line, /*EnsureNewline=*/ true);
587 WriteLineInfo(FileName, Line, FileType);
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000588 RawLex.SetKeepWhitespaceMode(false);
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000589 break;
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000590 }
David Blaikied5321242012-06-06 18:52:13 +0000591 default:
592 break;
593 }
594 }
595 RawLex.setParsingPreprocessorDirective(false);
596 }
597 RawLex.LexFromRawLexer(RawToken);
598 }
599 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000600 SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
601 Line, /*EnsureNewline=*/true);
David Blaikied5321242012-06-06 18:52:13 +0000602}
603
David Blaikie619117a2012-06-14 17:36:01 +0000604/// InclusionRewriterInInput - Implement -frewrite-includes mode.
David Blaikied5321242012-06-06 18:52:13 +0000605void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
606 const PreprocessorOutputOptions &Opts) {
607 SourceManager &SM = PP.getSourceManager();
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000608 InclusionRewriter *Rewrite = new InclusionRewriter(
609 PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
Reid Klecknere2793c02014-09-05 16:49:50 +0000610 Rewrite->detectMainFileEOL();
611
Craig Topperb8a70532014-09-10 04:53:53 +0000612 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
Lubos Lunak576a0412014-05-01 12:54:03 +0000613 PP.IgnorePragmas();
David Blaikied5321242012-06-06 18:52:13 +0000614
615 // First let the preprocessor process the entire file and call callbacks.
616 // Callbacks will record which #include's were actually performed.
617 PP.EnterMainSourceFile();
618 Token Tok;
619 // Only preprocessor directives matter here, so disable macro expansion
620 // everywhere else as an optimization.
621 // TODO: It would be even faster if the preprocessor could be switched
622 // to a mode where it would parse only preprocessor directives and comments,
623 // nothing else matters for parsing or processing.
624 PP.SetMacroExpansionOnlyInDirectives();
625 do {
626 PP.Lex(Tok);
Richard Smithd1386302017-05-04 00:29:54 +0000627 if (Tok.is(tok::annot_module_begin))
628 Rewrite->handleModuleBegin(Tok);
David Blaikied5321242012-06-06 18:52:13 +0000629 } while (Tok.isNot(tok::eof));
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +0000630 Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID()));
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000631 Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User, nullptr);
632 Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User, nullptr);
David Blaikied5321242012-06-06 18:52:13 +0000633 OS->flush();
634}