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