blob: 03cc844c390b7744a8cedf3d587a75fa9597be86 [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;
Justin Bogner0707fd02015-07-01 04:40:10 +000035 IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType)
36 : Id(Id), FileType(FileType) {}
David Blaikied5321242012-06-06 18:52:13 +000037 };
Dmitri Gribenko4280e5c2012-06-08 23:13:42 +000038 Preprocessor &PP; ///< Used to find inclusion directives.
39 SourceManager &SM; ///< Used to read and manage source files.
40 raw_ostream &OS; ///< The destination stream for rewritten contents.
Reid Klecknere2793c02014-09-05 16:49:50 +000041 StringRef MainEOL; ///< The line ending marker to use.
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +000042 const llvm::MemoryBuffer *PredefinesBuffer; ///< The preprocessor predefines.
Dmitri Gribenko4280e5c2012-06-08 23:13:42 +000043 bool ShowLineMarkers; ///< Show #line markers.
Reid Kleckner1df0fea2015-02-26 00:17:25 +000044 bool UseLineDirectives; ///< Use of line directives or line markers.
Justin Bogner0707fd02015-07-01 04:40:10 +000045 /// 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
David Blaikied5321242012-06-06 18:52:13 +000050 /// various \c PPCallbacks callbacks.
Justin Bogner0707fd02015-07-01 04:40:10 +000051 SourceLocation LastInclusionLocation;
David Blaikied5321242012-06-06 18:52:13 +000052public:
Reid Kleckner1df0fea2015-02-26 00:17:25 +000053 InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers,
54 bool UseLineDirectives);
David Blaikied5321242012-06-06 18:52:13 +000055 bool Process(FileID FileId, SrcMgr::CharacteristicKind FileType);
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +000056 void setPredefinesBuffer(const llvm::MemoryBuffer *Buf) {
57 PredefinesBuffer = Buf;
58 }
Reid Klecknere2793c02014-09-05 16:49:50 +000059 void detectMainFileEOL();
David Blaikied5321242012-06-06 18:52:13 +000060private:
Craig Topperfb6b25b2014-03-15 04:29:04 +000061 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
62 SrcMgr::CharacteristicKind FileType,
63 FileID PrevFID) override;
Nikola Smiljanicfb891fc2015-05-12 11:48:05 +000064 void FileSkipped(const FileEntry &SkippedFile, const Token &FilenameTok,
Craig Topperfb6b25b2014-03-15 04:29:04 +000065 SrcMgr::CharacteristicKind FileType) override;
66 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
67 StringRef FileName, bool IsAngled,
68 CharSourceRange FilenameRange, const FileEntry *File,
69 StringRef SearchPath, StringRef RelativePath,
70 const Module *Imported) override;
David Blaikied5321242012-06-06 18:52:13 +000071 void WriteLineInfo(const char *Filename, int Line,
72 SrcMgr::CharacteristicKind FileType,
Reid Klecknere2793c02014-09-05 16:49:50 +000073 StringRef Extra = StringRef());
74 void WriteImplicitModuleImport(const Module *Mod);
David Blaikied5321242012-06-06 18:52:13 +000075 void OutputContentUpTo(const MemoryBuffer &FromFile,
76 unsigned &WriteFrom, unsigned WriteTo,
77 StringRef EOL, int &lines,
Alp Toker08c25002013-12-13 17:04:55 +000078 bool EnsureNewline);
David Blaikied5321242012-06-06 18:52:13 +000079 void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
80 const MemoryBuffer &FromFile, StringRef EOL,
81 unsigned &NextToWrite, int &Lines);
Benjamin Kramerb10e6152013-04-16 19:08:41 +000082 bool HandleHasInclude(FileID FileId, Lexer &RawLex,
83 const DirectoryLookup *Lookup, Token &Tok,
84 bool &FileExists);
Justin Bogner0707fd02015-07-01 04:40:10 +000085 const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
86 const Module *FindModuleAtLocation(SourceLocation Loc) const;
David Blaikied5321242012-06-06 18:52:13 +000087 StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
88};
89
90} // end anonymous namespace
91
92/// Initializes an InclusionRewriter with a \p PP source and \p OS destination.
93InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
Reid Kleckner1df0fea2015-02-26 00:17:25 +000094 bool ShowLineMarkers,
95 bool UseLineDirectives)
Reid Klecknere2793c02014-09-05 16:49:50 +000096 : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
97 PredefinesBuffer(nullptr), ShowLineMarkers(ShowLineMarkers),
Eric Christopher8213f7f2015-02-26 00:29:54 +000098 UseLineDirectives(UseLineDirectives),
Justin Bogner0707fd02015-07-01 04:40:10 +000099 LastInclusionLocation(SourceLocation()) {}
David Blaikied5321242012-06-06 18:52:13 +0000100
101/// Write appropriate line information as either #line directives or GNU line
102/// markers depending on what mode we're in, including the \p Filename and
103/// \p Line we are located at, using the specified \p EOL line separator, and
104/// any \p Extra context specifiers in GNU line directives.
105void InclusionRewriter::WriteLineInfo(const char *Filename, int Line,
106 SrcMgr::CharacteristicKind FileType,
Reid Klecknere2793c02014-09-05 16:49:50 +0000107 StringRef Extra) {
David Blaikied5321242012-06-06 18:52:13 +0000108 if (!ShowLineMarkers)
109 return;
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000110 if (UseLineDirectives) {
Eli Friedman9fc443a2013-09-17 00:51:31 +0000111 OS << "#line" << ' ' << Line << ' ' << '"';
112 OS.write_escaped(Filename);
113 OS << '"';
David Blaikied5321242012-06-06 18:52:13 +0000114 } else {
115 // Use GNU linemarkers as described here:
116 // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
Eli Friedman80e45b82013-08-29 01:42:42 +0000117 OS << '#' << ' ' << Line << ' ' << '"';
118 OS.write_escaped(Filename);
119 OS << '"';
David Blaikied5321242012-06-06 18:52:13 +0000120 if (!Extra.empty())
121 OS << Extra;
122 if (FileType == SrcMgr::C_System)
123 // "`3' This indicates that the following text comes from a system header
124 // file, so certain warnings should be suppressed."
125 OS << " 3";
126 else if (FileType == SrcMgr::C_ExternCSystem)
127 // as above for `3', plus "`4' This indicates that the following text
128 // should be treated as being wrapped in an implicit extern "C" block."
129 OS << " 3 4";
130 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000131 OS << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000132}
133
Reid Klecknere2793c02014-09-05 16:49:50 +0000134void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000135 OS << "@import " << Mod->getFullModuleName() << ";"
Reid Klecknere2793c02014-09-05 16:49:50 +0000136 << " /* clang -frewrite-includes: implicit import */" << MainEOL;
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000137}
138
David Blaikied5321242012-06-06 18:52:13 +0000139/// FileChanged - Whenever the preprocessor enters or exits a #include file
140/// it invokes this handler.
141void InclusionRewriter::FileChanged(SourceLocation Loc,
142 FileChangeReason Reason,
143 SrcMgr::CharacteristicKind NewFileType,
144 FileID) {
145 if (Reason != EnterFile)
146 return;
Justin Bogner0707fd02015-07-01 04:40:10 +0000147 if (LastInclusionLocation.isInvalid())
David Blaikied5321242012-06-06 18:52:13 +0000148 // we didn't reach this file (eg: the main file) via an inclusion directive
149 return;
Justin Bogner0707fd02015-07-01 04:40:10 +0000150 FileID Id = FullSourceLoc(Loc, SM).getFileID();
Justin Bogner879d4202015-07-01 04:53:19 +0000151 auto P = FileIncludes.insert(std::make_pair(
152 LastInclusionLocation.getRawEncoding(), IncludedFile(Id, NewFileType)));
Justin Bogner0707fd02015-07-01 04:40:10 +0000153 assert(P.second && "Unexpected revisitation of the same include directive");
154 LastInclusionLocation = SourceLocation();
David Blaikied5321242012-06-06 18:52:13 +0000155}
156
157/// Called whenever an inclusion is skipped due to canonical header protection
158/// macros.
Nikola Smiljanicfb891fc2015-05-12 11:48:05 +0000159void InclusionRewriter::FileSkipped(const FileEntry &/*SkippedFile*/,
David Blaikied5321242012-06-06 18:52:13 +0000160 const Token &/*FilenameTok*/,
161 SrcMgr::CharacteristicKind /*FileType*/) {
Justin Bogner0707fd02015-07-01 04:40:10 +0000162 assert(!LastInclusionLocation.isInvalid() &&
163 "A file, that wasn't found via an inclusion directive, was skipped");
164 LastInclusionLocation = SourceLocation();
David Blaikied5321242012-06-06 18:52:13 +0000165}
166
167/// This should be called whenever the preprocessor encounters include
168/// directives. It does not say whether the file has been included, but it
169/// provides more information about the directive (hash location instead
170/// of location inside the included file). It is assumed that the matching
171/// FileChanged() or FileSkipped() is called after this.
172void InclusionRewriter::InclusionDirective(SourceLocation HashLoc,
173 const Token &/*IncludeTok*/,
174 StringRef /*FileName*/,
175 bool /*IsAngled*/,
Argyrios Kyrtzidis4fcd2882012-09-27 01:42:07 +0000176 CharSourceRange /*FilenameRange*/,
David Blaikied5321242012-06-06 18:52:13 +0000177 const FileEntry * /*File*/,
David Blaikied5321242012-06-06 18:52:13 +0000178 StringRef /*SearchPath*/,
Argyrios Kyrtzidis19d78b72012-09-29 01:06:10 +0000179 StringRef /*RelativePath*/,
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000180 const Module *Imported) {
Justin Bogner0707fd02015-07-01 04:40:10 +0000181 assert(LastInclusionLocation.isInvalid() &&
182 "Another inclusion directive was found before the previous one "
183 "was processed");
184 if (Imported) {
Justin Bogner879d4202015-07-01 04:53:19 +0000185 auto P = ModuleIncludes.insert(
186 std::make_pair(HashLoc.getRawEncoding(), Imported));
Justin Bogner0707fd02015-07-01 04:40:10 +0000187 assert(P.second && "Unexpected revisitation of the same include directive");
188 } else
189 LastInclusionLocation = HashLoc;
David Blaikied5321242012-06-06 18:52:13 +0000190}
191
192/// Simple lookup for a SourceLocation (specifically one denoting the hash in
193/// an inclusion directive) in the map of inclusion information, FileChanges.
Justin Bogner0707fd02015-07-01 04:40:10 +0000194const InclusionRewriter::IncludedFile *
195InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
196 const auto I = FileIncludes.find(Loc.getRawEncoding());
197 if (I != FileIncludes.end())
David Blaikied5321242012-06-06 18:52:13 +0000198 return &I->second;
Craig Topper8ae12032014-05-07 06:21:57 +0000199 return nullptr;
David Blaikied5321242012-06-06 18:52:13 +0000200}
201
Justin Bogner0707fd02015-07-01 04:40:10 +0000202/// Simple lookup for a SourceLocation (specifically one denoting the hash in
203/// an inclusion directive) in the map of module inclusion information.
204const Module *
205InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
206 const auto I = ModuleIncludes.find(Loc.getRawEncoding());
207 if (I != ModuleIncludes.end())
208 return I->second;
209 return nullptr;
210}
211
David Blaikied5321242012-06-06 18:52:13 +0000212/// Detect the likely line ending style of \p FromFile by examining the first
213/// newline found within it.
214static StringRef DetectEOL(const MemoryBuffer &FromFile) {
Reid Klecknere2793c02014-09-05 16:49:50 +0000215 // Detect what line endings the file uses, so that added content does not mix
216 // the style. We need to check for "\r\n" first because "\n\r" will match
217 // "\r\n\r\n".
David Blaikied5321242012-06-06 18:52:13 +0000218 const char *Pos = strchr(FromFile.getBufferStart(), '\n');
Craig Topper8ae12032014-05-07 06:21:57 +0000219 if (!Pos)
David Blaikied5321242012-06-06 18:52:13 +0000220 return "\n";
David Blaikied5321242012-06-06 18:52:13 +0000221 if (Pos - 1 >= FromFile.getBufferStart() && Pos[-1] == '\r')
222 return "\r\n";
Reid Klecknere2793c02014-09-05 16:49:50 +0000223 if (Pos + 1 < FromFile.getBufferEnd() && Pos[1] == '\r')
224 return "\n\r";
David Blaikied5321242012-06-06 18:52:13 +0000225 return "\n";
226}
227
Reid Klecknere2793c02014-09-05 16:49:50 +0000228void InclusionRewriter::detectMainFileEOL() {
229 bool Invalid;
230 const MemoryBuffer &FromFile = *SM.getBuffer(SM.getMainFileID(), &Invalid);
231 assert(!Invalid);
232 if (Invalid)
233 return; // Should never happen, but whatever.
234 MainEOL = DetectEOL(FromFile);
235}
236
David Blaikied5321242012-06-06 18:52:13 +0000237/// Writes out bytes from \p FromFile, starting at \p NextToWrite and ending at
238/// \p WriteTo - 1.
239void InclusionRewriter::OutputContentUpTo(const MemoryBuffer &FromFile,
240 unsigned &WriteFrom, unsigned WriteTo,
Reid Klecknere2793c02014-09-05 16:49:50 +0000241 StringRef LocalEOL, int &Line,
David Blaikied5321242012-06-06 18:52:13 +0000242 bool EnsureNewline) {
243 if (WriteTo <= WriteFrom)
244 return;
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +0000245 if (&FromFile == PredefinesBuffer) {
246 // Ignore the #defines of the predefines buffer.
247 WriteFrom = WriteTo;
248 return;
249 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000250
251 // If we would output half of a line ending, advance one character to output
252 // the whole line ending. All buffers are null terminated, so looking ahead
253 // one byte is safe.
254 if (LocalEOL.size() == 2 &&
255 LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
256 LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
257 WriteTo++;
258
259 StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
260 WriteTo - WriteFrom);
261
262 if (MainEOL == LocalEOL) {
263 OS << TextToWrite;
264 // count lines manually, it's faster than getPresumedLoc()
265 Line += TextToWrite.count(LocalEOL);
266 if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
267 OS << MainEOL;
268 } else {
269 // Output the file one line at a time, rewriting the line endings as we go.
270 StringRef Rest = TextToWrite;
271 while (!Rest.empty()) {
272 StringRef LineText;
273 std::tie(LineText, Rest) = Rest.split(LocalEOL);
274 OS << LineText;
275 Line++;
276 if (!Rest.empty())
277 OS << MainEOL;
278 }
279 if (TextToWrite.endswith(LocalEOL) || EnsureNewline)
280 OS << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000281 }
282 WriteFrom = WriteTo;
283}
284
285/// Print characters from \p FromFile starting at \p NextToWrite up until the
286/// inclusion directive at \p StartToken, then print out the inclusion
287/// inclusion directive disabled by a #if directive, updating \p NextToWrite
288/// and \p Line to track the number of source lines visited and the progress
289/// through the \p FromFile buffer.
290void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
291 const Token &StartToken,
292 const MemoryBuffer &FromFile,
Reid Klecknere2793c02014-09-05 16:49:50 +0000293 StringRef LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000294 unsigned &NextToWrite, int &Line) {
295 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000296 SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
297 false);
David Blaikied5321242012-06-06 18:52:13 +0000298 Token DirectiveToken;
299 do {
300 DirectiveLex.LexFromRawLexer(DirectiveToken);
301 } while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof));
Lubos Lunak72cad682014-05-01 21:10:08 +0000302 if (&FromFile == PredefinesBuffer) {
303 // OutputContentUpTo() would not output anything anyway.
304 return;
305 }
Reid Klecknere2793c02014-09-05 16:49:50 +0000306 OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000307 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000308 SM.getFileOffset(DirectiveToken.getLocation()) +
309 DirectiveToken.getLength(),
310 LocalEOL, Line, true);
311 OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
David Blaikied5321242012-06-06 18:52:13 +0000312}
313
314/// Find the next identifier in the pragma directive specified by \p RawToken.
315StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
316 Token &RawToken) {
317 RawLex.LexFromRawLexer(RawToken);
318 if (RawToken.is(tok::raw_identifier))
319 PP.LookUpIdentifierInfo(RawToken);
320 if (RawToken.is(tok::identifier))
321 return RawToken.getIdentifierInfo()->getName();
322 return StringRef();
323}
324
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000325// Expand __has_include and __has_include_next if possible. If there's no
326// definitive answer return false.
327bool InclusionRewriter::HandleHasInclude(
328 FileID FileId, Lexer &RawLex, const DirectoryLookup *Lookup, Token &Tok,
329 bool &FileExists) {
330 // Lex the opening paren.
331 RawLex.LexFromRawLexer(Tok);
332 if (Tok.isNot(tok::l_paren))
333 return false;
334
335 RawLex.LexFromRawLexer(Tok);
336
337 SmallString<128> FilenameBuffer;
338 StringRef Filename;
339 // Since the raw lexer doesn't give us angle_literals we have to parse them
340 // ourselves.
341 // FIXME: What to do if the file name is a macro?
342 if (Tok.is(tok::less)) {
343 RawLex.LexFromRawLexer(Tok);
344
345 FilenameBuffer += '<';
346 do {
347 if (Tok.is(tok::eod)) // Sanity check.
348 return false;
349
350 if (Tok.is(tok::raw_identifier))
351 PP.LookUpIdentifierInfo(Tok);
352
353 // Get the string piece.
354 SmallVector<char, 128> TmpBuffer;
355 bool Invalid = false;
356 StringRef TmpName = PP.getSpelling(Tok, TmpBuffer, &Invalid);
357 if (Invalid)
358 return false;
359
360 FilenameBuffer += TmpName;
361
362 RawLex.LexFromRawLexer(Tok);
363 } while (Tok.isNot(tok::greater));
364
365 FilenameBuffer += '>';
366 Filename = FilenameBuffer;
367 } else {
368 if (Tok.isNot(tok::string_literal))
369 return false;
370
371 bool Invalid = false;
372 Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid);
373 if (Invalid)
374 return false;
375 }
376
377 // Lex the closing paren.
378 RawLex.LexFromRawLexer(Tok);
379 if (Tok.isNot(tok::r_paren))
380 return false;
381
382 // Now ask HeaderInfo if it knows about the header.
383 // FIXME: Subframeworks aren't handled here. Do we care?
384 bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename);
385 const DirectoryLookup *CurDir;
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000386 const FileEntry *FileEnt = PP.getSourceManager().getFileEntryForID(FileId);
387 SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1>
388 Includers;
389 Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000390 const FileEntry *File = PP.getHeaderSearchInfo().LookupFile(
Manuel Klimek9af34ae2014-08-12 08:25:57 +0000391 Filename, SourceLocation(), isAngled, nullptr, CurDir, Includers, nullptr,
392 nullptr, nullptr, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000393
Craig Topper8ae12032014-05-07 06:21:57 +0000394 FileExists = File != nullptr;
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000395 return true;
396}
397
Benjamin Kramere2881572013-10-13 12:02:16 +0000398/// Use a raw lexer to analyze \p FileId, incrementally copying parts of it
David Blaikied5321242012-06-06 18:52:13 +0000399/// and including content of included files recursively.
400bool InclusionRewriter::Process(FileID FileId,
401 SrcMgr::CharacteristicKind FileType)
402{
403 bool Invalid;
404 const MemoryBuffer &FromFile = *SM.getBuffer(FileId, &Invalid);
Justin Bogner0707fd02015-07-01 04:40:10 +0000405 assert(!Invalid && "Attempting to process invalid inclusion");
David Blaikied5321242012-06-06 18:52:13 +0000406 const char *FileName = FromFile.getBufferIdentifier();
407 Lexer RawLex(FileId, &FromFile, PP.getSourceManager(), PP.getLangOpts());
408 RawLex.SetCommentRetentionState(false);
409
Reid Klecknere2793c02014-09-05 16:49:50 +0000410 StringRef LocalEOL = DetectEOL(FromFile);
David Blaikied5321242012-06-06 18:52:13 +0000411
Lubos Lunak10961c02014-05-01 13:50:44 +0000412 // Per the GNU docs: "1" indicates entering a new file.
Lubos Lunak72cad682014-05-01 21:10:08 +0000413 if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID())
Reid Klecknere2793c02014-09-05 16:49:50 +0000414 WriteLineInfo(FileName, 1, FileType, "");
Lubos Lunak10961c02014-05-01 13:50:44 +0000415 else
Reid Klecknere2793c02014-09-05 16:49:50 +0000416 WriteLineInfo(FileName, 1, FileType, " 1");
David Blaikied5321242012-06-06 18:52:13 +0000417
418 if (SM.getFileIDSize(FileId) == 0)
Argyrios Kyrtzidis953ef332013-04-10 01:53:37 +0000419 return false;
David Blaikied5321242012-06-06 18:52:13 +0000420
Alp Toker3dfeafd2013-11-28 07:21:44 +0000421 // The next byte to be copied from the source file, which may be non-zero if
422 // the lexer handled a BOM.
Alp Toker52937ab2013-12-05 17:28:42 +0000423 unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
424 assert(SM.getLineNumber(FileId, NextToWrite) == 1);
David Blaikied5321242012-06-06 18:52:13 +0000425 int Line = 1; // The current input file line number.
426
427 Token RawToken;
428 RawLex.LexFromRawLexer(RawToken);
429
430 // TODO: Consider adding a switch that strips possibly unimportant content,
431 // such as comments, to reduce the size of repro files.
432 while (RawToken.isNot(tok::eof)) {
433 if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) {
434 RawLex.setParsingPreprocessorDirective(true);
435 Token HashToken = RawToken;
436 RawLex.LexFromRawLexer(RawToken);
437 if (RawToken.is(tok::raw_identifier))
438 PP.LookUpIdentifierInfo(RawToken);
Craig Topper8ae12032014-05-07 06:21:57 +0000439 if (RawToken.getIdentifierInfo() != nullptr) {
David Blaikied5321242012-06-06 18:52:13 +0000440 switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
441 case tok::pp_include:
442 case tok::pp_include_next:
443 case tok::pp_import: {
Reid Klecknere2793c02014-09-05 16:49:50 +0000444 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
David Blaikied5321242012-06-06 18:52:13 +0000445 Line);
Lubos Lunak4526b462014-05-01 21:11:57 +0000446 if (FileId != PP.getPredefinesFileID())
Reid Klecknere2793c02014-09-05 16:49:50 +0000447 WriteLineInfo(FileName, Line - 1, FileType, "");
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000448 StringRef LineInfoExtra;
Justin Bogner0707fd02015-07-01 04:40:10 +0000449 SourceLocation Loc = HashToken.getLocation();
450 if (const Module *Mod = FindModuleAtLocation(Loc))
451 WriteImplicitModuleImport(Mod);
452 else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
453 // include and recursively process the file
454 if (Process(Inc->Id, Inc->FileType)) {
David Blaikied5321242012-06-06 18:52:13 +0000455 // and set lineinfo back to this file, if the nested one was
456 // actually included
457 // `2' indicates returning to a file (after having included
458 // another file.
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000459 LineInfoExtra = " 2";
Argyrios Kyrtzidis953ef332013-04-10 01:53:37 +0000460 }
Argyrios Kyrtzidiscf22d1f2013-04-10 01:53:50 +0000461 }
462 // fix up lineinfo (since commented out directive changed line
463 // numbers) for inclusions that were skipped due to header guards
Reid Klecknere2793c02014-09-05 16:49:50 +0000464 WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
David Blaikied5321242012-06-06 18:52:13 +0000465 break;
466 }
467 case tok::pp_pragma: {
468 StringRef Identifier = NextIdentifierName(RawLex, RawToken);
469 if (Identifier == "clang" || Identifier == "GCC") {
470 if (NextIdentifierName(RawLex, RawToken) == "system_header") {
471 // keep the directive in, commented out
Reid Klecknere2793c02014-09-05 16:49:50 +0000472 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000473 NextToWrite, Line);
474 // update our own type
475 FileType = SM.getFileCharacteristic(RawToken.getLocation());
Reid Klecknere2793c02014-09-05 16:49:50 +0000476 WriteLineInfo(FileName, Line, FileType);
David Blaikied5321242012-06-06 18:52:13 +0000477 }
478 } else if (Identifier == "once") {
479 // keep the directive in, commented out
Reid Klecknere2793c02014-09-05 16:49:50 +0000480 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
David Blaikied5321242012-06-06 18:52:13 +0000481 NextToWrite, Line);
Reid Klecknere2793c02014-09-05 16:49:50 +0000482 WriteLineInfo(FileName, Line, FileType);
David Blaikied5321242012-06-06 18:52:13 +0000483 }
484 break;
485 }
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000486 case tok::pp_if:
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000487 case tok::pp_elif: {
488 bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
489 tok::pp_elif);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000490 // Rewrite special builtin macros to avoid pulling in host details.
491 do {
492 // Walk over the directive.
493 RawLex.LexFromRawLexer(RawToken);
494 if (RawToken.is(tok::raw_identifier))
495 PP.LookUpIdentifierInfo(RawToken);
496
497 if (RawToken.is(tok::identifier)) {
498 bool HasFile;
499 SourceLocation Loc = RawToken.getLocation();
500
501 // Rewrite __has_include(x)
502 if (RawToken.getIdentifierInfo()->isStr("__has_include")) {
Craig Topper8ae12032014-05-07 06:21:57 +0000503 if (!HandleHasInclude(FileId, RawLex, nullptr, RawToken,
504 HasFile))
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000505 continue;
506 // Rewrite __has_include_next(x)
507 } else if (RawToken.getIdentifierInfo()->isStr(
508 "__has_include_next")) {
509 const DirectoryLookup *Lookup = PP.GetCurDirLookup();
510 if (Lookup)
511 ++Lookup;
512
513 if (!HandleHasInclude(FileId, RawLex, Lookup, RawToken,
514 HasFile))
515 continue;
516 } else {
517 continue;
518 }
519 // Replace the macro with (0) or (1), followed by the commented
520 // out macro for reference.
521 OutputContentUpTo(FromFile, NextToWrite, SM.getFileOffset(Loc),
Reid Klecknere2793c02014-09-05 16:49:50 +0000522 LocalEOL, Line, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000523 OS << '(' << (int) HasFile << ")/*";
524 OutputContentUpTo(FromFile, NextToWrite,
525 SM.getFileOffset(RawToken.getLocation()) +
Reid Klecknere2793c02014-09-05 16:49:50 +0000526 RawToken.getLength(),
527 LocalEOL, Line, false);
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000528 OS << "*/";
529 }
530 } while (RawToken.isNot(tok::eod));
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000531 if (elif) {
532 OutputContentUpTo(FromFile, NextToWrite,
533 SM.getFileOffset(RawToken.getLocation()) +
534 RawToken.getLength(),
Reid Klecknere2793c02014-09-05 16:49:50 +0000535 LocalEOL, Line, /*EnsureNewline=*/ true);
536 WriteLineInfo(FileName, Line, FileType);
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000537 }
Benjamin Kramerb10e6152013-04-16 19:08:41 +0000538 break;
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000539 }
540 case tok::pp_endif:
541 case tok::pp_else: {
542 // We surround every #include by #if 0 to comment it out, but that
543 // changes line numbers. These are fixed up right after that, but
544 // the whole #include could be inside a preprocessor conditional
545 // that is not processed. So it is necessary to fix the line
546 // numbers one the next line after each #else/#endif as well.
547 RawLex.SetKeepWhitespaceMode(true);
548 do {
549 RawLex.LexFromRawLexer(RawToken);
550 } while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof));
Reid Klecknere2793c02014-09-05 16:49:50 +0000551 OutputContentUpTo(FromFile, NextToWrite,
552 SM.getFileOffset(RawToken.getLocation()) +
553 RawToken.getLength(),
554 LocalEOL, Line, /*EnsureNewline=*/ true);
555 WriteLineInfo(FileName, Line, FileType);
Lubos Lunak4c22f6a2013-07-20 14:23:27 +0000556 RawLex.SetKeepWhitespaceMode(false);
557 }
David Blaikied5321242012-06-06 18:52:13 +0000558 default:
559 break;
560 }
561 }
562 RawLex.setParsingPreprocessorDirective(false);
563 }
564 RawLex.LexFromRawLexer(RawToken);
565 }
566 OutputContentUpTo(FromFile, NextToWrite,
Reid Klecknere2793c02014-09-05 16:49:50 +0000567 SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
568 Line, /*EnsureNewline=*/true);
David Blaikied5321242012-06-06 18:52:13 +0000569 return true;
570}
571
David Blaikie619117a2012-06-14 17:36:01 +0000572/// InclusionRewriterInInput - Implement -frewrite-includes mode.
David Blaikied5321242012-06-06 18:52:13 +0000573void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
574 const PreprocessorOutputOptions &Opts) {
575 SourceManager &SM = PP.getSourceManager();
Reid Kleckner1df0fea2015-02-26 00:17:25 +0000576 InclusionRewriter *Rewrite = new InclusionRewriter(
577 PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
Reid Klecknere2793c02014-09-05 16:49:50 +0000578 Rewrite->detectMainFileEOL();
579
Craig Topperb8a70532014-09-10 04:53:53 +0000580 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
Lubos Lunak576a0412014-05-01 12:54:03 +0000581 PP.IgnorePragmas();
David Blaikied5321242012-06-06 18:52:13 +0000582
583 // First let the preprocessor process the entire file and call callbacks.
584 // Callbacks will record which #include's were actually performed.
585 PP.EnterMainSourceFile();
586 Token Tok;
587 // Only preprocessor directives matter here, so disable macro expansion
588 // everywhere else as an optimization.
589 // TODO: It would be even faster if the preprocessor could be switched
590 // to a mode where it would parse only preprocessor directives and comments,
591 // nothing else matters for parsing or processing.
592 PP.SetMacroExpansionOnlyInDirectives();
593 do {
594 PP.Lex(Tok);
595 } while (Tok.isNot(tok::eof));
Argyrios Kyrtzidis17ff2e52013-07-26 15:32:04 +0000596 Rewrite->setPredefinesBuffer(SM.getBuffer(PP.getPredefinesFileID()));
597 Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User);
David Blaikied5321242012-06-06 18:52:13 +0000598 Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User);
599 OS->flush();
600}