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