blob: 3b8d792e3af24d4e9bc6e778b817a8adabe172ad [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,
80 const Module *Imported) override;
Mehdi Amini99d1b292016-10-01 16:38:28 +000081 void WriteLineInfo(StringRef Filename, int Line,
David Blaikied5321242012-06-06 18:52:13 +000082 SrcMgr::CharacteristicKind FileType,
Reid Klecknere2793c02014-09-05 16:49:50 +000083 StringRef Extra = StringRef());
84 void WriteImplicitModuleImport(const Module *Mod);
David Blaikied5321242012-06-06 18:52:13 +000085 void OutputContentUpTo(const MemoryBuffer &FromFile,
86 unsigned &WriteFrom, unsigned WriteTo,
87 StringRef EOL, int &lines,
Alp Toker08c25002013-12-13 17:04:55 +000088 bool EnsureNewline);
David Blaikied5321242012-06-06 18:52:13 +000089 void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
90 const MemoryBuffer &FromFile, StringRef EOL,
91 unsigned &NextToWrite, int &Lines);
Benjamin Kramerb10e6152013-04-16 19:08:41 +000092 bool HandleHasInclude(FileID FileId, Lexer &RawLex,
93 const DirectoryLookup *Lookup, Token &Tok,
94 bool &FileExists);
Justin Bogner0707fd02015-07-01 04:40:10 +000095 const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
96 const Module *FindModuleAtLocation(SourceLocation Loc) const;
Richard Smithd1386302017-05-04 00:29:54 +000097 const Module *FindEnteredModule(SourceLocation Loc) const;
David Blaikied5321242012-06-06 18:52:13 +000098 StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
99};
100
101} // end anonymous namespace
102
103/// Initializes an InclusionRewriter with a \p PP source and \p OS destination.
104InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000105 bool ShowLineMarkers,
106 bool UseLineDirectives)
Reid Klecknere2793c02014-09-05 16:49:50 +0000107 : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
108 PredefinesBuffer(nullptr), ShowLineMarkers(ShowLineMarkers),
Eric Christopher8213f7f2015-02-26 00:29:54 +0000109 UseLineDirectives(UseLineDirectives),
Justin Bogner0707fd02015-07-01 04:40:10 +0000110 LastInclusionLocation(SourceLocation()) {}
David Blaikied5321242012-06-06 18:52:13 +0000111
112/// Write appropriate line information as either #line directives or GNU line
113/// markers depending on what mode we're in, including the \p Filename and
114/// \p Line we are located at, using the specified \p EOL line separator, and
115/// any \p Extra context specifiers in GNU line directives.
Mehdi Amini99d1b292016-10-01 16:38:28 +0000116void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line,
David Blaikied5321242012-06-06 18:52:13 +0000117 SrcMgr::CharacteristicKind FileType,
Reid Klecknere2793c02014-09-05 16:49:50 +0000118 StringRef Extra) {
David Blaikied5321242012-06-06 18:52:13 +0000119 if (!ShowLineMarkers)
120 return;
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000121 if (UseLineDirectives) {
Eli Friedman9fc443a2013-09-17 00:51:31 +0000122 OS << "#line" << ' ' << Line << ' ' << '"';
123 OS.write_escaped(Filename);
124 OS << '"';
David Blaikied5321242012-06-06 18:52:13 +0000125 } else {
126 // Use GNU linemarkers as described here:
127 // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
Eli Friedman80e45b82013-08-29 01:42:42 +0000128 OS << '#' << ' ' << Line << ' ' << '"';
129 OS.write_escaped(Filename);
130 OS << '"';
David Blaikied5321242012-06-06 18:52:13 +0000131 if (!Extra.empty())
132 OS << Extra;
133 if (FileType == SrcMgr::C_System)
134 // "`3' This indicates that the following text comes from a system header
135 // file, so certain warnings should be suppressed."
136 OS << " 3";
137 else if (FileType == SrcMgr::C_ExternCSystem)
138 // as above for `3', plus "`4' This indicates that the following text
139 // should be treated as being wrapped in an implicit extern "C" block."
140 OS << " 3 4";
141 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000142 OS << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000143}
144
Reid Klecknere2793c02014-09-05 16:49:50 +0000145void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
Richard Smith9565c75b2017-06-19 23:09:36 +0000146 OS << "#pragma clang module import " << Mod->getFullModuleName(true)
Reid Klecknere2793c02014-09-05 16:49:50 +0000147 << " /* clang -frewrite-includes: implicit import */" << MainEOL;
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000148}
149
David Blaikied5321242012-06-06 18:52:13 +0000150/// FileChanged - Whenever the preprocessor enters or exits a #include file
151/// it invokes this handler.
152void InclusionRewriter::FileChanged(SourceLocation Loc,
153 FileChangeReason Reason,
154 SrcMgr::CharacteristicKind NewFileType,
155 FileID) {
156 if (Reason != EnterFile)
157 return;
Justin Bogner0707fd02015-07-01 04:40:10 +0000158 if (LastInclusionLocation.isInvalid())
David Blaikied5321242012-06-06 18:52:13 +0000159 // we didn't reach this file (eg: the main file) via an inclusion directive
160 return;
Justin Bogner0707fd02015-07-01 04:40:10 +0000161 FileID Id = FullSourceLoc(Loc, SM).getFileID();
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000162 auto P = FileIncludes.insert(
163 std::make_pair(LastInclusionLocation.getRawEncoding(),
164 IncludedFile(Id, NewFileType, PP.GetCurDirLookup())));
Justin Bogner2510ba32015-07-01 05:41:50 +0000165 (void)P;
Justin Bogner0707fd02015-07-01 04:40:10 +0000166 assert(P.second && "Unexpected revisitation of the same include directive");
167 LastInclusionLocation = SourceLocation();
David Blaikied5321242012-06-06 18:52:13 +0000168}
169
170/// Called whenever an inclusion is skipped due to canonical header protection
171/// macros.
Nikola Smiljanicfb891fc2015-05-12 11:48:05 +0000172void InclusionRewriter::FileSkipped(const FileEntry &/*SkippedFile*/,
David Blaikied5321242012-06-06 18:52:13 +0000173 const Token &/*FilenameTok*/,
174 SrcMgr::CharacteristicKind /*FileType*/) {
Yaron Kerened1fe5d2015-10-03 05:15:57 +0000175 assert(LastInclusionLocation.isValid() &&
Justin Bogner0707fd02015-07-01 04:40:10 +0000176 "A file, that wasn't found via an inclusion directive, was skipped");
177 LastInclusionLocation = SourceLocation();
David Blaikied5321242012-06-06 18:52:13 +0000178}
179
180/// This should be called whenever the preprocessor encounters include
181/// directives. It does not say whether the file has been included, but it
182/// provides more information about the directive (hash location instead
183/// of location inside the included file). It is assumed that the matching
Richard Smith4b46f722017-06-02 01:05:44 +0000184/// FileChanged() or FileSkipped() is called after this (or neither is
185/// called if this #include results in an error or does not textually include
186/// anything).
David Blaikied5321242012-06-06 18:52:13 +0000187void InclusionRewriter::InclusionDirective(SourceLocation HashLoc,
188 const Token &/*IncludeTok*/,
189 StringRef /*FileName*/,
190 bool /*IsAngled*/,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000191 CharSourceRange /*FilenameRange*/,
David Blaikied5321242012-06-06 18:52:13 +0000192 const FileEntry * /*File*/,
David Blaikied5321242012-06-06 18:52:13 +0000193 StringRef /*SearchPath*/,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000194 StringRef /*RelativePath*/,
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000195 const Module *Imported) {
Justin Bogner0707fd02015-07-01 04:40:10 +0000196 if (Imported) {
Justin Bogner879d4202015-07-01 04:53:19 +0000197 auto P = ModuleIncludes.insert(
198 std::make_pair(HashLoc.getRawEncoding(), Imported));
Justin Bogner2510ba32015-07-01 05:41:50 +0000199 (void)P;
Justin Bogner0707fd02015-07-01 04:40:10 +0000200 assert(P.second && "Unexpected revisitation of the same include directive");
201 } else
202 LastInclusionLocation = HashLoc;
David Blaikied5321242012-06-06 18:52:13 +0000203}
204
205/// Simple lookup for a SourceLocation (specifically one denoting the hash in
206/// an inclusion directive) in the map of inclusion information, FileChanges.
Justin Bogner0707fd02015-07-01 04:40:10 +0000207const InclusionRewriter::IncludedFile *
208InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
209 const auto I = FileIncludes.find(Loc.getRawEncoding());
210 if (I != FileIncludes.end())
David Blaikied5321242012-06-06 18:52:13 +0000211 return &I->second;
Craig Topper8ae12032014-05-07 06:21:57 +0000212 return nullptr;
David Blaikied5321242012-06-06 18:52:13 +0000213}
214
Justin Bogner0707fd02015-07-01 04:40:10 +0000215/// Simple lookup for a SourceLocation (specifically one denoting the hash in
216/// an inclusion directive) in the map of module inclusion information.
217const Module *
218InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
219 const auto I = ModuleIncludes.find(Loc.getRawEncoding());
220 if (I != ModuleIncludes.end())
221 return I->second;
222 return nullptr;
223}
224
Richard Smithd1386302017-05-04 00:29:54 +0000225/// Simple lookup for a SourceLocation (specifically one denoting the hash in
226/// an inclusion directive) in the map of module entry information.
227const Module *
228InclusionRewriter::FindEnteredModule(SourceLocation Loc) const {
229 const auto I = ModuleEntryIncludes.find(Loc.getRawEncoding());
230 if (I != ModuleEntryIncludes.end())
231 return I->second;
232 return nullptr;
233}
234
David Blaikied5321242012-06-06 18:52:13 +0000235/// Detect the likely line ending style of \p FromFile by examining the first
236/// newline found within it.
237static StringRef DetectEOL(const MemoryBuffer &FromFile) {
Reid Klecknere2793c02014-09-05 16:49:50 +0000238 // Detect what line endings the file uses, so that added content does not mix
239 // the style. We need to check for "\r\n" first because "\n\r" will match
240 // "\r\n\r\n".
David Blaikied5321242012-06-06 18:52:13 +0000241 const char *Pos = strchr(FromFile.getBufferStart(), '\n');
Craig Topper8ae12032014-05-07 06:21:57 +0000242 if (!Pos)
David Blaikied5321242012-06-06 18:52:13 +0000243 return "\n";
David Blaikied5321242012-06-06 18:52:13 +0000244 if (Pos - 1 >= FromFile.getBufferStart() && Pos[-1] == '\r')
245 return "\r\n";
Reid Klecknere2793c02014-09-05 16:49:50 +0000246 if (Pos + 1 < FromFile.getBufferEnd() && Pos[1] == '\r')
247 return "\n\r";
David Blaikied5321242012-06-06 18:52:13 +0000248 return "\n";
249}
250
Reid Klecknere2793c02014-09-05 16:49:50 +0000251void InclusionRewriter::detectMainFileEOL() {
252 bool Invalid;
253 const MemoryBuffer &FromFile = *SM.getBuffer(SM.getMainFileID(), &Invalid);
254 assert(!Invalid);
255 if (Invalid)
256 return; // Should never happen, but whatever.
257 MainEOL = DetectEOL(FromFile);
258}
259
David Blaikied5321242012-06-06 18:52:13 +0000260/// Writes out bytes from \p FromFile, starting at \p NextToWrite and ending at
261/// \p WriteTo - 1.
262void InclusionRewriter::OutputContentUpTo(const MemoryBuffer &FromFile,
263 unsigned &WriteFrom, unsigned WriteTo,
Reid Klecknere2793c02014-09-05 16:49:50 +0000264 StringRef LocalEOL, int &Line,
David Blaikied5321242012-06-06 18:52:13 +0000265 bool EnsureNewline) {
266 if (WriteTo <= WriteFrom)
267 return;
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +0000268 if (&FromFile == PredefinesBuffer) {
269 // Ignore the #defines of the predefines buffer.
270 WriteFrom = WriteTo;
271 return;
272 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000273
274 // If we would output half of a line ending, advance one character to output
275 // the whole line ending. All buffers are null terminated, so looking ahead
276 // one byte is safe.
277 if (LocalEOL.size() == 2 &&
278 LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
279 LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
280 WriteTo++;
281
282 StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
283 WriteTo - WriteFrom);
284
285 if (MainEOL == LocalEOL) {
286 OS << TextToWrite;
287 // count lines manually, it's faster than getPresumedLoc()
288 Line += TextToWrite.count(LocalEOL);
289 if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
290 OS << MainEOL;
291 } else {
292 // Output the file one line at a time, rewriting the line endings as we go.
293 StringRef Rest = TextToWrite;
294 while (!Rest.empty()) {
295 StringRef LineText;
296 std::tie(LineText, Rest) = Rest.split(LocalEOL);
297 OS << LineText;
298 Line++;
299 if (!Rest.empty())
300 OS << MainEOL;
301 }
302 if (TextToWrite.endswith(LocalEOL) || EnsureNewline)
303 OS << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000304 }
305 WriteFrom = WriteTo;
306}
307
308/// Print characters from \p FromFile starting at \p NextToWrite up until the
309/// inclusion directive at \p StartToken, then print out the inclusion
310/// inclusion directive disabled by a #if directive, updating \p NextToWrite
311/// and \p Line to track the number of source lines visited and the progress
312/// through the \p FromFile buffer.
313void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
314 const Token &StartToken,
315 const MemoryBuffer &FromFile,
Reid Klecknere2793c02014-09-05 16:49:50 +0000316 StringRef LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000317 unsigned &NextToWrite, int &Line) {
318 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000319 SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
320 false);
David Blaikied5321242012-06-06 18:52:13 +0000321 Token DirectiveToken;
322 do {
323 DirectiveLex.LexFromRawLexer(DirectiveToken);
324 } while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof));
Lubos Lunak72cad682014-05-01 21:10:08 +0000325 if (&FromFile == PredefinesBuffer) {
326 // OutputContentUpTo() would not output anything anyway.
327 return;
328 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000329 OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000330 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000331 SM.getFileOffset(DirectiveToken.getLocation()) +
332 DirectiveToken.getLength(),
333 LocalEOL, Line, true);
334 OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000335}
336
337/// Find the next identifier in the pragma directive specified by \p RawToken.
338StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
339 Token &RawToken) {
340 RawLex.LexFromRawLexer(RawToken);
341 if (RawToken.is(tok::raw_identifier))
342 PP.LookUpIdentifierInfo(RawToken);
343 if (RawToken.is(tok::identifier))
344 return RawToken.getIdentifierInfo()->getName();
345 return StringRef();
346}
347
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000348// Expand __has_include and __has_include_next if possible. If there's no
349// definitive answer return false.
350bool InclusionRewriter::HandleHasInclude(
351 FileID FileId, Lexer &RawLex, const DirectoryLookup *Lookup, Token &Tok,
352 bool &FileExists) {
353 // Lex the opening paren.
354 RawLex.LexFromRawLexer(Tok);
355 if (Tok.isNot(tok::l_paren))
356 return false;
357
358 RawLex.LexFromRawLexer(Tok);
359
360 SmallString<128> FilenameBuffer;
361 StringRef Filename;
362 // Since the raw lexer doesn't give us angle_literals we have to parse them
363 // ourselves.
364 // FIXME: What to do if the file name is a macro?
365 if (Tok.is(tok::less)) {
366 RawLex.LexFromRawLexer(Tok);
367
368 FilenameBuffer += '<';
369 do {
370 if (Tok.is(tok::eod)) // Sanity check.
371 return false;
372
373 if (Tok.is(tok::raw_identifier))
374 PP.LookUpIdentifierInfo(Tok);
375
376 // Get the string piece.
377 SmallVector<char, 128> TmpBuffer;
378 bool Invalid = false;
379 StringRef TmpName = PP.getSpelling(Tok, TmpBuffer, &Invalid);
380 if (Invalid)
381 return false;
382
383 FilenameBuffer += TmpName;
384
385 RawLex.LexFromRawLexer(Tok);
386 } while (Tok.isNot(tok::greater));
387
388 FilenameBuffer += '>';
389 Filename = FilenameBuffer;
390 } else {
391 if (Tok.isNot(tok::string_literal))
392 return false;
393
394 bool Invalid = false;
395 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
396 if (Invalid)
397 return false;
398 }
399
400 // Lex the closing paren.
401 RawLex.LexFromRawLexer(Tok);
402 if (Tok.isNot(tok::r_paren))
403 return false;
404
405 // Now ask HeaderInfo if it knows about the header.
406 // FIXME: Subframeworks aren't handled here. Do we care?
407 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
408 const DirectoryLookup *CurDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000409 const FileEntry *FileEnt = PP.getSourceManager().getFileEntryForID(FileId);
410 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1>
411 Includers;
412 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
Richard Smith3d5b48c2015-10-16 21:42:56 +0000413 // FIXME: Why don't we call PP.LookupFile here?
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000414 const FileEntry *File = PP.getHeaderSearchInfo().LookupFile(
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000415 Filename, SourceLocation(), isAngled, Lookup, CurDir, Includers, nullptr,
Duncan P. N. Exon Smithcfc1f6a2017-04-27 21:41:51 +0000416 nullptr, nullptr, nullptr, nullptr);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000417
Craig Topper8ae12032014-05-07 06:21:57 +0000418 FileExists = File != nullptr;
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000419 return true;
420}
421
Benjamin Kramere2881572013-10-13 12:02:16 +0000422/// Use a raw lexer to analyze \p FileId, incrementally copying parts of it
David Blaikied5321242012-06-06 18:52:13 +0000423/// and including content of included files recursively.
Richard Smithc7cacdc2017-04-29 00:54:03 +0000424void InclusionRewriter::Process(FileID FileId,
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000425 SrcMgr::CharacteristicKind FileType,
426 const DirectoryLookup *DirLookup) {
David Blaikied5321242012-06-06 18:52:13 +0000427 bool Invalid;
428 const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid);
Justin Bogner0707fd02015-07-01 04:40:10 +0000429 assert(!Invalid && "Attempting to process invalid inclusion");
Mehdi Amini99d1b292016-10-01 16:38:28 +0000430 StringRef FileName = FromFile.getBufferIdentifier();
David Blaikied5321242012-06-06 18:52:13 +0000431 Lexer RawLex(FileId, &FromFile, PP.getSourceManager(), PP.getLangOpts());
432 RawLex.SetCommentRetentionState(false);
433
Reid Klecknere2793c02014-09-05 16:49:50 +0000434 StringRef LocalEOL = DetectEOL(FromFile);
David Blaikied5321242012-06-06 18:52:13 +0000435
Lubos Lunak10961c02014-05-01 13:50:44 +0000436 // Per the GNU docs: "1" indicates entering a new file.
Lubos Lunak72cad682014-05-01 21:10:08 +0000437 if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID())
Reid Klecknere2793c02014-09-05 16:49:50 +0000438 WriteLineInfo(FileName, 1, FileType, "");
Lubos Lunak10961c02014-05-01 13:50:44 +0000439 else
Reid Klecknere2793c02014-09-05 16:49:50 +0000440 WriteLineInfo(FileName, 1, FileType, " 1");
David Blaikied5321242012-06-06 18:52:13 +0000441
442 if (SM.getFileIDSize(FileId) == 0)
Richard Smithc7cacdc2017-04-29 00:54:03 +0000443 return;
David Blaikied5321242012-06-06 18:52:13 +0000444
Alp Toker3dfeafd2013-11-28 07:21:44 +0000445 // The next byte to be copied from the source file, which may be non-zero if
446 // the lexer handled a BOM.
Alp Toker52937ab2013-12-05 17:28:42 +0000447 unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
448 assert(SM.getLineNumber(FileId, NextToWrite) == 1);
David Blaikied5321242012-06-06 18:52:13 +0000449 int Line = 1; // The current input file line number.
450
451 Token RawToken;
452 RawLex.LexFromRawLexer(RawToken);
453
454 // TODO: Consider adding a switch that strips possibly unimportant content,
455 // such as comments, to reduce the size of repro files.
456 while (RawToken.isNot(tok::eof)) {
457 if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) {
458 RawLex.setParsingPreprocessorDirective(true);
459 Token HashToken = RawToken;
460 RawLex.LexFromRawLexer(RawToken);
461 if (RawToken.is(tok::raw_identifier))
462 PP.LookUpIdentifierInfo(RawToken);
Craig Topper8ae12032014-05-07 06:21:57 +0000463 if (RawToken.getIdentifierInfo() != nullptr) {
David Blaikied5321242012-06-06 18:52:13 +0000464 switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
465 case tok::pp_include:
466 case tok::pp_include_next:
467 case tok::pp_import: {
Reid Klecknere2793c02014-09-05 16:49:50 +0000468 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
David Blaikied5321242012-06-06 18:52:13 +0000469 Line);
Lubos Lunak4526b462014-05-01 21:11:57 +0000470 if (FileId != PP.getPredefinesFileID())
Reid Klecknere2793c02014-09-05 16:49:50 +0000471 WriteLineInfo(FileName, Line - 1, FileType, "");
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000472 StringRef LineInfoExtra;
Justin Bogner0707fd02015-07-01 04:40:10 +0000473 SourceLocation Loc = HashToken.getLocation();
Richard Smithc51c38b2017-04-29 00:34:47 +0000474 if (const Module *Mod = FindModuleAtLocation(Loc))
Justin Bogner0707fd02015-07-01 04:40:10 +0000475 WriteImplicitModuleImport(Mod);
476 else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
Richard Smithd1386302017-05-04 00:29:54 +0000477 const Module *Mod = FindEnteredModule(Loc);
478 if (Mod)
Richard Smith9565c75b2017-06-19 23:09:36 +0000479 OS << "#pragma clang module begin "
480 << Mod->getFullModuleName(true) << "\n";
Richard Smithd1386302017-05-04 00:29:54 +0000481
Richard Smithc7cacdc2017-04-29 00:54:03 +0000482 // Include and recursively process the file.
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000483 Process(Inc->Id, Inc->FileType, Inc->DirLookup);
Richard Smithd1386302017-05-04 00:29:54 +0000484
485 if (Mod)
Richard Smith9565c75b2017-06-19 23:09:36 +0000486 OS << "#pragma clang module end /*"
487 << Mod->getFullModuleName(true) << "*/\n";
Richard Smithd1386302017-05-04 00:29:54 +0000488
Richard Smithc7cacdc2017-04-29 00:54:03 +0000489 // Add line marker to indicate we're returning from an included
490 // file.
491 LineInfoExtra = " 2";
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000492 }
493 // fix up lineinfo (since commented out directive changed line
494 // numbers) for inclusions that were skipped due to header guards
Reid Klecknere2793c02014-09-05 16:49:50 +0000495 WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
David Blaikied5321242012-06-06 18:52:13 +0000496 break;
497 }
498 case tok::pp_pragma: {
499 StringRef Identifier = NextIdentifierName(RawLex, RawToken);
500 if (Identifier == "clang" || Identifier == "GCC") {
501 if (NextIdentifierName(RawLex, RawToken) == "system_header") {
502 // keep the directive in, commented out
Reid Klecknere2793c02014-09-05 16:49:50 +0000503 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000504 NextToWrite, Line);
505 // update our own type
506 FileType = SM.getFileCharacteristic(RawToken.getLocation());
Reid Klecknere2793c02014-09-05 16:49:50 +0000507 WriteLineInfo(FileName, Line, FileType);
David Blaikied5321242012-06-06 18:52:13 +0000508 }
509 } else if (Identifier == "once") {
510 // keep the directive in, commented out
Reid Klecknere2793c02014-09-05 16:49:50 +0000511 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000512 NextToWrite, Line);
Reid Klecknere2793c02014-09-05 16:49:50 +0000513 WriteLineInfo(FileName, Line, FileType);
David Blaikied5321242012-06-06 18:52:13 +0000514 }
515 break;
516 }
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000517 case tok::pp_if:
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000518 case tok::pp_elif: {
519 bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
520 tok::pp_elif);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000521 // Rewrite special builtin macros to avoid pulling in host details.
522 do {
523 // Walk over the directive.
524 RawLex.LexFromRawLexer(RawToken);
525 if (RawToken.is(tok::raw_identifier))
526 PP.LookUpIdentifierInfo(RawToken);
527
528 if (RawToken.is(tok::identifier)) {
529 bool HasFile;
530 SourceLocation Loc = RawToken.getLocation();
531
532 // Rewrite __has_include(x)
533 if (RawToken.getIdentifierInfo()->isStr("__has_include")) {
Craig Topper8ae12032014-05-07 06:21:57 +0000534 if (!HandleHasInclude(FileId, RawLex, nullptr, RawToken,
535 HasFile))
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000536 continue;
537 // Rewrite __has_include_next(x)
538 } else if (RawToken.getIdentifierInfo()->isStr(
539 "__has_include_next")) {
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000540 if (DirLookup)
541 ++DirLookup;
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000542
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000543 if (!HandleHasInclude(FileId, RawLex, DirLookup, RawToken,
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000544 HasFile))
545 continue;
546 } else {
547 continue;
548 }
549 // Replace the macro with (0) or (1), followed by the commented
550 // out macro for reference.
551 OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc),
Reid Klecknere2793c02014-09-05 16:49:50 +0000552 LocalEOL, Line, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000553 OS << '(' << (int) HasFile << ")/*";
554 OutputContentUpTo(FromFile, NextToWrite,
555 SM.getFileOffset(RawToken.getLocation()) +
Reid Klecknere2793c02014-09-05 16:49:50 +0000556 RawToken.getLength(),
557 LocalEOL, Line, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000558 OS << "*/";
559 }
560 } while (RawToken.isNot(tok::eod));
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000561 if (elif) {
562 OutputContentUpTo(FromFile, NextToWrite,
563 SM.getFileOffset(RawToken.getLocation()) +
564 RawToken.getLength(),
Reid Klecknere2793c02014-09-05 16:49:50 +0000565 LocalEOL, Line, /*EnsureNewline=*/ true);
566 WriteLineInfo(FileName, Line, FileType);
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000567 }
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000568 break;
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000569 }
570 case tok::pp_endif:
571 case tok::pp_else: {
572 // We surround every #include by #if 0 to comment it out, but that
573 // changes line numbers. These are fixed up right after that, but
574 // the whole #include could be inside a preprocessor conditional
575 // that is not processed. So it is necessary to fix the line
576 // numbers one the next line after each #else/#endif as well.
577 RawLex.SetKeepWhitespaceMode(true);
578 do {
579 RawLex.LexFromRawLexer(RawToken);
580 } while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof));
Reid Klecknere2793c02014-09-05 16:49:50 +0000581 OutputContentUpTo(FromFile, NextToWrite,
582 SM.getFileOffset(RawToken.getLocation()) +
583 RawToken.getLength(),
584 LocalEOL, Line, /*EnsureNewline=*/ true);
585 WriteLineInfo(FileName, Line, FileType);
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000586 RawLex.SetKeepWhitespaceMode(false);
587 }
David Blaikied5321242012-06-06 18:52:13 +0000588 default:
589 break;
590 }
591 }
592 RawLex.setParsingPreprocessorDirective(false);
593 }
594 RawLex.LexFromRawLexer(RawToken);
595 }
596 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000597 SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
598 Line, /*EnsureNewline=*/true);
David Blaikied5321242012-06-06 18:52:13 +0000599}
600
David Blaikie619117a2012-06-14 17:36:01 +0000601/// InclusionRewriterInInput - Implement -frewrite-includes mode.
David Blaikied5321242012-06-06 18:52:13 +0000602void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
603 const PreprocessorOutputOptions &Opts) {
604 SourceManager &SM = PP.getSourceManager();
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000605 InclusionRewriter *Rewrite = new InclusionRewriter(
606 PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
Reid Klecknere2793c02014-09-05 16:49:50 +0000607 Rewrite->detectMainFileEOL();
608
Craig Topperb8a70532014-09-10 04:53:53 +0000609 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
Lubos Lunak576a0412014-05-01 12:54:03 +0000610 PP.IgnorePragmas();
David Blaikied5321242012-06-06 18:52:13 +0000611
612 // First let the preprocessor process the entire file and call callbacks.
613 // Callbacks will record which #include's were actually performed.
614 PP.EnterMainSourceFile();
615 Token Tok;
616 // Only preprocessor directives matter here, so disable macro expansion
617 // everywhere else as an optimization.
618 // TODO: It would be even faster if the preprocessor could be switched
619 // to a mode where it would parse only preprocessor directives and comments,
620 // nothing else matters for parsing or processing.
621 PP.SetMacroExpansionOnlyInDirectives();
622 do {
623 PP.Lex(Tok);
Richard Smithd1386302017-05-04 00:29:54 +0000624 if (Tok.is(tok::annot_module_begin))
625 Rewrite->handleModuleBegin(Tok);
David Blaikied5321242012-06-06 18:52:13 +0000626 } while (Tok.isNot(tok::eof));
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +0000627 Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID()));
Volodymyr Sapsai1f70bdd2018-04-13 17:43:15 +0000628 Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User, nullptr);
629 Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User, nullptr);
David Blaikied5321242012-06-06 18:52:13 +0000630 OS->flush();
631}