annotate clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @ 252:1f2b6ac9f198 llvm-original

LLVM16-1
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 18 Aug 2023 09:04:13 +0900
parents c4bab56944e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- InclusionRewriter.cpp - Rewrite includes into their expansions ---===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This code rewrites include invocations into their expansions. This gives you
anatofuz
parents:
diff changeset
10 // a file with all included files merged into it.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 #include "clang/Rewrite/Frontend/Rewriters.h"
anatofuz
parents:
diff changeset
15 #include "clang/Basic/SourceManager.h"
anatofuz
parents:
diff changeset
16 #include "clang/Frontend/PreprocessorOutputOptions.h"
anatofuz
parents:
diff changeset
17 #include "clang/Lex/Pragma.h"
anatofuz
parents:
diff changeset
18 #include "clang/Lex/Preprocessor.h"
anatofuz
parents:
diff changeset
19 #include "llvm/ADT/SmallString.h"
anatofuz
parents:
diff changeset
20 #include "llvm/Support/raw_ostream.h"
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
21 #include <optional>
150
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 using namespace clang;
anatofuz
parents:
diff changeset
24 using namespace llvm;
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 namespace {
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 class InclusionRewriter : public PPCallbacks {
anatofuz
parents:
diff changeset
29 /// Information about which #includes were actually performed,
anatofuz
parents:
diff changeset
30 /// created by preprocessor callbacks.
anatofuz
parents:
diff changeset
31 struct IncludedFile {
anatofuz
parents:
diff changeset
32 FileID Id;
anatofuz
parents:
diff changeset
33 SrcMgr::CharacteristicKind FileType;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
34 IncludedFile(FileID Id, SrcMgr::CharacteristicKind FileType)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
35 : Id(Id), FileType(FileType) {}
150
anatofuz
parents:
diff changeset
36 };
anatofuz
parents:
diff changeset
37 Preprocessor &PP; ///< Used to find inclusion directives.
anatofuz
parents:
diff changeset
38 SourceManager &SM; ///< Used to read and manage source files.
anatofuz
parents:
diff changeset
39 raw_ostream &OS; ///< The destination stream for rewritten contents.
anatofuz
parents:
diff changeset
40 StringRef MainEOL; ///< The line ending marker to use.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
41 llvm::MemoryBufferRef PredefinesBuffer; ///< The preprocessor predefines.
150
anatofuz
parents:
diff changeset
42 bool ShowLineMarkers; ///< Show #line markers.
anatofuz
parents:
diff changeset
43 bool UseLineDirectives; ///< Use of line directives or line markers.
anatofuz
parents:
diff changeset
44 /// Tracks where inclusions that change the file are found.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
45 std::map<SourceLocation, IncludedFile> FileIncludes;
150
anatofuz
parents:
diff changeset
46 /// Tracks where inclusions that import modules are found.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
47 std::map<SourceLocation, const Module *> ModuleIncludes;
150
anatofuz
parents:
diff changeset
48 /// Tracks where inclusions that enter modules (in a module build) are found.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
49 std::map<SourceLocation, const Module *> ModuleEntryIncludes;
150
anatofuz
parents:
diff changeset
50 /// Tracks where #if and #elif directives get evaluated and whether to true.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
51 std::map<SourceLocation, bool> IfConditions;
150
anatofuz
parents:
diff changeset
52 /// Used transitively for building up the FileIncludes mapping over the
anatofuz
parents:
diff changeset
53 /// various \c PPCallbacks callbacks.
anatofuz
parents:
diff changeset
54 SourceLocation LastInclusionLocation;
anatofuz
parents:
diff changeset
55 public:
anatofuz
parents:
diff changeset
56 InclusionRewriter(Preprocessor &PP, raw_ostream &OS, bool ShowLineMarkers,
anatofuz
parents:
diff changeset
57 bool UseLineDirectives);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
58 void Process(FileID FileId, SrcMgr::CharacteristicKind FileType);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
59 void setPredefinesBuffer(const llvm::MemoryBufferRef &Buf) {
150
anatofuz
parents:
diff changeset
60 PredefinesBuffer = Buf;
anatofuz
parents:
diff changeset
61 }
anatofuz
parents:
diff changeset
62 void detectMainFileEOL();
anatofuz
parents:
diff changeset
63 void handleModuleBegin(Token &Tok) {
anatofuz
parents:
diff changeset
64 assert(Tok.getKind() == tok::annot_module_begin);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
65 ModuleEntryIncludes.insert(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
66 {Tok.getLocation(), (Module *)Tok.getAnnotationValue()});
150
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68 private:
anatofuz
parents:
diff changeset
69 void FileChanged(SourceLocation Loc, FileChangeReason Reason,
anatofuz
parents:
diff changeset
70 SrcMgr::CharacteristicKind FileType,
anatofuz
parents:
diff changeset
71 FileID PrevFID) override;
anatofuz
parents:
diff changeset
72 void FileSkipped(const FileEntryRef &SkippedFile, const Token &FilenameTok,
anatofuz
parents:
diff changeset
73 SrcMgr::CharacteristicKind FileType) override;
anatofuz
parents:
diff changeset
74 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
anatofuz
parents:
diff changeset
75 StringRef FileName, bool IsAngled,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
76 CharSourceRange FilenameRange,
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
77 OptionalFileEntryRef File, StringRef SearchPath,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
78 StringRef RelativePath, const Module *Imported,
150
anatofuz
parents:
diff changeset
79 SrcMgr::CharacteristicKind FileType) override;
anatofuz
parents:
diff changeset
80 void If(SourceLocation Loc, SourceRange ConditionRange,
anatofuz
parents:
diff changeset
81 ConditionValueKind ConditionValue) override;
anatofuz
parents:
diff changeset
82 void Elif(SourceLocation Loc, SourceRange ConditionRange,
anatofuz
parents:
diff changeset
83 ConditionValueKind ConditionValue, SourceLocation IfLoc) override;
anatofuz
parents:
diff changeset
84 void WriteLineInfo(StringRef Filename, int Line,
anatofuz
parents:
diff changeset
85 SrcMgr::CharacteristicKind FileType,
anatofuz
parents:
diff changeset
86 StringRef Extra = StringRef());
anatofuz
parents:
diff changeset
87 void WriteImplicitModuleImport(const Module *Mod);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
88 void OutputContentUpTo(const MemoryBufferRef &FromFile, unsigned &WriteFrom,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
89 unsigned WriteTo, StringRef EOL, int &lines,
150
anatofuz
parents:
diff changeset
90 bool EnsureNewline);
anatofuz
parents:
diff changeset
91 void CommentOutDirective(Lexer &DirectivesLex, const Token &StartToken,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
92 const MemoryBufferRef &FromFile, StringRef EOL,
150
anatofuz
parents:
diff changeset
93 unsigned &NextToWrite, int &Lines);
anatofuz
parents:
diff changeset
94 const IncludedFile *FindIncludeAtLocation(SourceLocation Loc) const;
anatofuz
parents:
diff changeset
95 const Module *FindModuleAtLocation(SourceLocation Loc) const;
anatofuz
parents:
diff changeset
96 const Module *FindEnteredModule(SourceLocation Loc) const;
anatofuz
parents:
diff changeset
97 bool IsIfAtLocationTrue(SourceLocation Loc) const;
anatofuz
parents:
diff changeset
98 StringRef NextIdentifierName(Lexer &RawLex, Token &RawToken);
anatofuz
parents:
diff changeset
99 };
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 } // end anonymous namespace
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 /// Initializes an InclusionRewriter with a \p PP source and \p OS destination.
anatofuz
parents:
diff changeset
104 InclusionRewriter::InclusionRewriter(Preprocessor &PP, raw_ostream &OS,
anatofuz
parents:
diff changeset
105 bool ShowLineMarkers,
anatofuz
parents:
diff changeset
106 bool UseLineDirectives)
anatofuz
parents:
diff changeset
107 : PP(PP), SM(PP.getSourceManager()), OS(OS), MainEOL("\n"),
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
108 ShowLineMarkers(ShowLineMarkers), UseLineDirectives(UseLineDirectives),
150
anatofuz
parents:
diff changeset
109 LastInclusionLocation(SourceLocation()) {}
anatofuz
parents:
diff changeset
110
anatofuz
parents:
diff changeset
111 /// Write appropriate line information as either #line directives or GNU line
anatofuz
parents:
diff changeset
112 /// markers depending on what mode we're in, including the \p Filename and
anatofuz
parents:
diff changeset
113 /// \p Line we are located at, using the specified \p EOL line separator, and
anatofuz
parents:
diff changeset
114 /// any \p Extra context specifiers in GNU line directives.
anatofuz
parents:
diff changeset
115 void InclusionRewriter::WriteLineInfo(StringRef Filename, int Line,
anatofuz
parents:
diff changeset
116 SrcMgr::CharacteristicKind FileType,
anatofuz
parents:
diff changeset
117 StringRef Extra) {
anatofuz
parents:
diff changeset
118 if (!ShowLineMarkers)
anatofuz
parents:
diff changeset
119 return;
anatofuz
parents:
diff changeset
120 if (UseLineDirectives) {
anatofuz
parents:
diff changeset
121 OS << "#line" << ' ' << Line << ' ' << '"';
anatofuz
parents:
diff changeset
122 OS.write_escaped(Filename);
anatofuz
parents:
diff changeset
123 OS << '"';
anatofuz
parents:
diff changeset
124 } else {
anatofuz
parents:
diff changeset
125 // Use GNU linemarkers as described here:
anatofuz
parents:
diff changeset
126 // http://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html
anatofuz
parents:
diff changeset
127 OS << '#' << ' ' << Line << ' ' << '"';
anatofuz
parents:
diff changeset
128 OS.write_escaped(Filename);
anatofuz
parents:
diff changeset
129 OS << '"';
anatofuz
parents:
diff changeset
130 if (!Extra.empty())
anatofuz
parents:
diff changeset
131 OS << Extra;
anatofuz
parents:
diff changeset
132 if (FileType == SrcMgr::C_System)
anatofuz
parents:
diff changeset
133 // "`3' This indicates that the following text comes from a system header
anatofuz
parents:
diff changeset
134 // file, so certain warnings should be suppressed."
anatofuz
parents:
diff changeset
135 OS << " 3";
anatofuz
parents:
diff changeset
136 else if (FileType == SrcMgr::C_ExternCSystem)
anatofuz
parents:
diff changeset
137 // as above for `3', plus "`4' This indicates that the following text
anatofuz
parents:
diff changeset
138 // should be treated as being wrapped in an implicit extern "C" block."
anatofuz
parents:
diff changeset
139 OS << " 3 4";
anatofuz
parents:
diff changeset
140 }
anatofuz
parents:
diff changeset
141 OS << MainEOL;
anatofuz
parents:
diff changeset
142 }
anatofuz
parents:
diff changeset
143
anatofuz
parents:
diff changeset
144 void InclusionRewriter::WriteImplicitModuleImport(const Module *Mod) {
anatofuz
parents:
diff changeset
145 OS << "#pragma clang module import " << Mod->getFullModuleName(true)
anatofuz
parents:
diff changeset
146 << " /* clang -frewrite-includes: implicit import */" << MainEOL;
anatofuz
parents:
diff changeset
147 }
anatofuz
parents:
diff changeset
148
anatofuz
parents:
diff changeset
149 /// FileChanged - Whenever the preprocessor enters or exits a #include file
anatofuz
parents:
diff changeset
150 /// it invokes this handler.
anatofuz
parents:
diff changeset
151 void InclusionRewriter::FileChanged(SourceLocation Loc,
anatofuz
parents:
diff changeset
152 FileChangeReason Reason,
anatofuz
parents:
diff changeset
153 SrcMgr::CharacteristicKind NewFileType,
anatofuz
parents:
diff changeset
154 FileID) {
anatofuz
parents:
diff changeset
155 if (Reason != EnterFile)
anatofuz
parents:
diff changeset
156 return;
anatofuz
parents:
diff changeset
157 if (LastInclusionLocation.isInvalid())
anatofuz
parents:
diff changeset
158 // we didn't reach this file (eg: the main file) via an inclusion directive
anatofuz
parents:
diff changeset
159 return;
anatofuz
parents:
diff changeset
160 FileID Id = FullSourceLoc(Loc, SM).getFileID();
anatofuz
parents:
diff changeset
161 auto P = FileIncludes.insert(
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
162 std::make_pair(LastInclusionLocation, IncludedFile(Id, NewFileType)));
150
anatofuz
parents:
diff changeset
163 (void)P;
anatofuz
parents:
diff changeset
164 assert(P.second && "Unexpected revisitation of the same include directive");
anatofuz
parents:
diff changeset
165 LastInclusionLocation = SourceLocation();
anatofuz
parents:
diff changeset
166 }
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 /// Called whenever an inclusion is skipped due to canonical header protection
anatofuz
parents:
diff changeset
169 /// macros.
anatofuz
parents:
diff changeset
170 void InclusionRewriter::FileSkipped(const FileEntryRef & /*SkippedFile*/,
anatofuz
parents:
diff changeset
171 const Token & /*FilenameTok*/,
anatofuz
parents:
diff changeset
172 SrcMgr::CharacteristicKind /*FileType*/) {
anatofuz
parents:
diff changeset
173 assert(LastInclusionLocation.isValid() &&
anatofuz
parents:
diff changeset
174 "A file, that wasn't found via an inclusion directive, was skipped");
anatofuz
parents:
diff changeset
175 LastInclusionLocation = SourceLocation();
anatofuz
parents:
diff changeset
176 }
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 /// This should be called whenever the preprocessor encounters include
anatofuz
parents:
diff changeset
179 /// directives. It does not say whether the file has been included, but it
anatofuz
parents:
diff changeset
180 /// provides more information about the directive (hash location instead
anatofuz
parents:
diff changeset
181 /// of location inside the included file). It is assumed that the matching
anatofuz
parents:
diff changeset
182 /// FileChanged() or FileSkipped() is called after this (or neither is
anatofuz
parents:
diff changeset
183 /// called if this #include results in an error or does not textually include
anatofuz
parents:
diff changeset
184 /// anything).
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
185 void InclusionRewriter::InclusionDirective(
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
186 SourceLocation HashLoc, const Token & /*IncludeTok*/,
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
187 StringRef /*FileName*/, bool /*IsAngled*/,
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
188 CharSourceRange /*FilenameRange*/, OptionalFileEntryRef /*File*/,
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
189 StringRef /*SearchPath*/, StringRef /*RelativePath*/,
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
190 const Module *Imported, SrcMgr::CharacteristicKind FileType) {
150
anatofuz
parents:
diff changeset
191 if (Imported) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
192 auto P = ModuleIncludes.insert(std::make_pair(HashLoc, Imported));
150
anatofuz
parents:
diff changeset
193 (void)P;
anatofuz
parents:
diff changeset
194 assert(P.second && "Unexpected revisitation of the same include directive");
anatofuz
parents:
diff changeset
195 } else
anatofuz
parents:
diff changeset
196 LastInclusionLocation = HashLoc;
anatofuz
parents:
diff changeset
197 }
anatofuz
parents:
diff changeset
198
anatofuz
parents:
diff changeset
199 void InclusionRewriter::If(SourceLocation Loc, SourceRange ConditionRange,
anatofuz
parents:
diff changeset
200 ConditionValueKind ConditionValue) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
201 auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True));
150
anatofuz
parents:
diff changeset
202 (void)P;
anatofuz
parents:
diff changeset
203 assert(P.second && "Unexpected revisitation of the same if directive");
anatofuz
parents:
diff changeset
204 }
anatofuz
parents:
diff changeset
205
anatofuz
parents:
diff changeset
206 void InclusionRewriter::Elif(SourceLocation Loc, SourceRange ConditionRange,
anatofuz
parents:
diff changeset
207 ConditionValueKind ConditionValue,
anatofuz
parents:
diff changeset
208 SourceLocation IfLoc) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
209 auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True));
150
anatofuz
parents:
diff changeset
210 (void)P;
anatofuz
parents:
diff changeset
211 assert(P.second && "Unexpected revisitation of the same elif directive");
anatofuz
parents:
diff changeset
212 }
anatofuz
parents:
diff changeset
213
anatofuz
parents:
diff changeset
214 /// Simple lookup for a SourceLocation (specifically one denoting the hash in
anatofuz
parents:
diff changeset
215 /// an inclusion directive) in the map of inclusion information, FileChanges.
anatofuz
parents:
diff changeset
216 const InclusionRewriter::IncludedFile *
anatofuz
parents:
diff changeset
217 InclusionRewriter::FindIncludeAtLocation(SourceLocation Loc) const {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
218 const auto I = FileIncludes.find(Loc);
150
anatofuz
parents:
diff changeset
219 if (I != FileIncludes.end())
anatofuz
parents:
diff changeset
220 return &I->second;
anatofuz
parents:
diff changeset
221 return nullptr;
anatofuz
parents:
diff changeset
222 }
anatofuz
parents:
diff changeset
223
anatofuz
parents:
diff changeset
224 /// Simple lookup for a SourceLocation (specifically one denoting the hash in
anatofuz
parents:
diff changeset
225 /// an inclusion directive) in the map of module inclusion information.
anatofuz
parents:
diff changeset
226 const Module *
anatofuz
parents:
diff changeset
227 InclusionRewriter::FindModuleAtLocation(SourceLocation Loc) const {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
228 const auto I = ModuleIncludes.find(Loc);
150
anatofuz
parents:
diff changeset
229 if (I != ModuleIncludes.end())
anatofuz
parents:
diff changeset
230 return I->second;
anatofuz
parents:
diff changeset
231 return nullptr;
anatofuz
parents:
diff changeset
232 }
anatofuz
parents:
diff changeset
233
anatofuz
parents:
diff changeset
234 /// Simple lookup for a SourceLocation (specifically one denoting the hash in
anatofuz
parents:
diff changeset
235 /// an inclusion directive) in the map of module entry information.
anatofuz
parents:
diff changeset
236 const Module *
anatofuz
parents:
diff changeset
237 InclusionRewriter::FindEnteredModule(SourceLocation Loc) const {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
238 const auto I = ModuleEntryIncludes.find(Loc);
150
anatofuz
parents:
diff changeset
239 if (I != ModuleEntryIncludes.end())
anatofuz
parents:
diff changeset
240 return I->second;
anatofuz
parents:
diff changeset
241 return nullptr;
anatofuz
parents:
diff changeset
242 }
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 bool InclusionRewriter::IsIfAtLocationTrue(SourceLocation Loc) const {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
245 const auto I = IfConditions.find(Loc);
150
anatofuz
parents:
diff changeset
246 if (I != IfConditions.end())
anatofuz
parents:
diff changeset
247 return I->second;
anatofuz
parents:
diff changeset
248 return false;
anatofuz
parents:
diff changeset
249 }
anatofuz
parents:
diff changeset
250
anatofuz
parents:
diff changeset
251 void InclusionRewriter::detectMainFileEOL() {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
252 std::optional<MemoryBufferRef> FromFile =
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
253 *SM.getBufferOrNone(SM.getMainFileID());
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
254 assert(FromFile);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
255 if (!FromFile)
150
anatofuz
parents:
diff changeset
256 return; // Should never happen, but whatever.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
257 MainEOL = FromFile->getBuffer().detectEOL();
150
anatofuz
parents:
diff changeset
258 }
anatofuz
parents:
diff changeset
259
anatofuz
parents:
diff changeset
260 /// Writes out bytes from \p FromFile, starting at \p NextToWrite and ending at
anatofuz
parents:
diff changeset
261 /// \p WriteTo - 1.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
262 void InclusionRewriter::OutputContentUpTo(const MemoryBufferRef &FromFile,
150
anatofuz
parents:
diff changeset
263 unsigned &WriteFrom, unsigned WriteTo,
anatofuz
parents:
diff changeset
264 StringRef LocalEOL, int &Line,
anatofuz
parents:
diff changeset
265 bool EnsureNewline) {
anatofuz
parents:
diff changeset
266 if (WriteTo <= WriteFrom)
anatofuz
parents:
diff changeset
267 return;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
268 if (FromFile == PredefinesBuffer) {
150
anatofuz
parents:
diff changeset
269 // Ignore the #defines of the predefines buffer.
anatofuz
parents:
diff changeset
270 WriteFrom = WriteTo;
anatofuz
parents:
diff changeset
271 return;
anatofuz
parents:
diff changeset
272 }
anatofuz
parents:
diff changeset
273
anatofuz
parents:
diff changeset
274 // If we would output half of a line ending, advance one character to output
anatofuz
parents:
diff changeset
275 // the whole line ending. All buffers are null terminated, so looking ahead
anatofuz
parents:
diff changeset
276 // one byte is safe.
anatofuz
parents:
diff changeset
277 if (LocalEOL.size() == 2 &&
anatofuz
parents:
diff changeset
278 LocalEOL[0] == (FromFile.getBufferStart() + WriteTo)[-1] &&
anatofuz
parents:
diff changeset
279 LocalEOL[1] == (FromFile.getBufferStart() + WriteTo)[0])
anatofuz
parents:
diff changeset
280 WriteTo++;
anatofuz
parents:
diff changeset
281
anatofuz
parents:
diff changeset
282 StringRef TextToWrite(FromFile.getBufferStart() + WriteFrom,
anatofuz
parents:
diff changeset
283 WriteTo - WriteFrom);
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
284 // count lines manually, it's faster than getPresumedLoc()
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
285 Line += TextToWrite.count(LocalEOL);
150
anatofuz
parents:
diff changeset
286
anatofuz
parents:
diff changeset
287 if (MainEOL == LocalEOL) {
anatofuz
parents:
diff changeset
288 OS << TextToWrite;
anatofuz
parents:
diff changeset
289 } else {
anatofuz
parents:
diff changeset
290 // Output the file one line at a time, rewriting the line endings as we go.
anatofuz
parents:
diff changeset
291 StringRef Rest = TextToWrite;
anatofuz
parents:
diff changeset
292 while (!Rest.empty()) {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
293 // Identify and output the next line excluding an EOL sequence if present.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
294 size_t Idx = Rest.find(LocalEOL);
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
295 StringRef LineText = Rest.substr(0, Idx);
150
anatofuz
parents:
diff changeset
296 OS << LineText;
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
297 if (Idx != StringRef::npos) {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
298 // An EOL sequence was present, output the EOL sequence for the
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
299 // main source file and skip past the local EOL sequence.
150
anatofuz
parents:
diff changeset
300 OS << MainEOL;
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
301 Idx += LocalEOL.size();
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
302 }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
303 // Strip the line just handled. If Idx is npos or matches the end of the
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
304 // text, Rest will be set to an empty string and the loop will terminate.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
305 Rest = Rest.substr(Idx);
150
anatofuz
parents:
diff changeset
306 }
anatofuz
parents:
diff changeset
307 }
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
308 if (EnsureNewline && !TextToWrite.endswith(LocalEOL))
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
309 OS << MainEOL;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
310
150
anatofuz
parents:
diff changeset
311 WriteFrom = WriteTo;
anatofuz
parents:
diff changeset
312 }
anatofuz
parents:
diff changeset
313
anatofuz
parents:
diff changeset
314 /// Print characters from \p FromFile starting at \p NextToWrite up until the
anatofuz
parents:
diff changeset
315 /// inclusion directive at \p StartToken, then print out the inclusion
anatofuz
parents:
diff changeset
316 /// inclusion directive disabled by a #if directive, updating \p NextToWrite
anatofuz
parents:
diff changeset
317 /// and \p Line to track the number of source lines visited and the progress
anatofuz
parents:
diff changeset
318 /// through the \p FromFile buffer.
anatofuz
parents:
diff changeset
319 void InclusionRewriter::CommentOutDirective(Lexer &DirectiveLex,
anatofuz
parents:
diff changeset
320 const Token &StartToken,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
321 const MemoryBufferRef &FromFile,
150
anatofuz
parents:
diff changeset
322 StringRef LocalEOL,
anatofuz
parents:
diff changeset
323 unsigned &NextToWrite, int &Line) {
anatofuz
parents:
diff changeset
324 OutputContentUpTo(FromFile, NextToWrite,
anatofuz
parents:
diff changeset
325 SM.getFileOffset(StartToken.getLocation()), LocalEOL, Line,
anatofuz
parents:
diff changeset
326 false);
anatofuz
parents:
diff changeset
327 Token DirectiveToken;
anatofuz
parents:
diff changeset
328 do {
anatofuz
parents:
diff changeset
329 DirectiveLex.LexFromRawLexer(DirectiveToken);
anatofuz
parents:
diff changeset
330 } while (!DirectiveToken.is(tok::eod) && DirectiveToken.isNot(tok::eof));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
331 if (FromFile == PredefinesBuffer) {
150
anatofuz
parents:
diff changeset
332 // OutputContentUpTo() would not output anything anyway.
anatofuz
parents:
diff changeset
333 return;
anatofuz
parents:
diff changeset
334 }
anatofuz
parents:
diff changeset
335 OS << "#if 0 /* expanded by -frewrite-includes */" << MainEOL;
anatofuz
parents:
diff changeset
336 OutputContentUpTo(FromFile, NextToWrite,
anatofuz
parents:
diff changeset
337 SM.getFileOffset(DirectiveToken.getLocation()) +
anatofuz
parents:
diff changeset
338 DirectiveToken.getLength(),
anatofuz
parents:
diff changeset
339 LocalEOL, Line, true);
anatofuz
parents:
diff changeset
340 OS << "#endif /* expanded by -frewrite-includes */" << MainEOL;
anatofuz
parents:
diff changeset
341 }
anatofuz
parents:
diff changeset
342
anatofuz
parents:
diff changeset
343 /// Find the next identifier in the pragma directive specified by \p RawToken.
anatofuz
parents:
diff changeset
344 StringRef InclusionRewriter::NextIdentifierName(Lexer &RawLex,
anatofuz
parents:
diff changeset
345 Token &RawToken) {
anatofuz
parents:
diff changeset
346 RawLex.LexFromRawLexer(RawToken);
anatofuz
parents:
diff changeset
347 if (RawToken.is(tok::raw_identifier))
anatofuz
parents:
diff changeset
348 PP.LookUpIdentifierInfo(RawToken);
anatofuz
parents:
diff changeset
349 if (RawToken.is(tok::identifier))
anatofuz
parents:
diff changeset
350 return RawToken.getIdentifierInfo()->getName();
anatofuz
parents:
diff changeset
351 return StringRef();
anatofuz
parents:
diff changeset
352 }
anatofuz
parents:
diff changeset
353
anatofuz
parents:
diff changeset
354 /// Use a raw lexer to analyze \p FileId, incrementally copying parts of it
anatofuz
parents:
diff changeset
355 /// and including content of included files recursively.
anatofuz
parents:
diff changeset
356 void InclusionRewriter::Process(FileID FileId,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
357 SrcMgr::CharacteristicKind FileType) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
358 MemoryBufferRef FromFile;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
359 {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
360 auto B = SM.getBufferOrNone(FileId);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
361 assert(B && "Attempting to process invalid inclusion");
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
362 if (B)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
363 FromFile = *B;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
364 }
150
anatofuz
parents:
diff changeset
365 StringRef FileName = FromFile.getBufferIdentifier();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
366 Lexer RawLex(FileId, FromFile, PP.getSourceManager(), PP.getLangOpts());
150
anatofuz
parents:
diff changeset
367 RawLex.SetCommentRetentionState(false);
anatofuz
parents:
diff changeset
368
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
369 StringRef LocalEOL = FromFile.getBuffer().detectEOL();
150
anatofuz
parents:
diff changeset
370
anatofuz
parents:
diff changeset
371 // Per the GNU docs: "1" indicates entering a new file.
anatofuz
parents:
diff changeset
372 if (FileId == SM.getMainFileID() || FileId == PP.getPredefinesFileID())
anatofuz
parents:
diff changeset
373 WriteLineInfo(FileName, 1, FileType, "");
anatofuz
parents:
diff changeset
374 else
anatofuz
parents:
diff changeset
375 WriteLineInfo(FileName, 1, FileType, " 1");
anatofuz
parents:
diff changeset
376
anatofuz
parents:
diff changeset
377 if (SM.getFileIDSize(FileId) == 0)
anatofuz
parents:
diff changeset
378 return;
anatofuz
parents:
diff changeset
379
anatofuz
parents:
diff changeset
380 // The next byte to be copied from the source file, which may be non-zero if
anatofuz
parents:
diff changeset
381 // the lexer handled a BOM.
anatofuz
parents:
diff changeset
382 unsigned NextToWrite = SM.getFileOffset(RawLex.getSourceLocation());
anatofuz
parents:
diff changeset
383 assert(SM.getLineNumber(FileId, NextToWrite) == 1);
anatofuz
parents:
diff changeset
384 int Line = 1; // The current input file line number.
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 Token RawToken;
anatofuz
parents:
diff changeset
387 RawLex.LexFromRawLexer(RawToken);
anatofuz
parents:
diff changeset
388
anatofuz
parents:
diff changeset
389 // TODO: Consider adding a switch that strips possibly unimportant content,
anatofuz
parents:
diff changeset
390 // such as comments, to reduce the size of repro files.
anatofuz
parents:
diff changeset
391 while (RawToken.isNot(tok::eof)) {
anatofuz
parents:
diff changeset
392 if (RawToken.is(tok::hash) && RawToken.isAtStartOfLine()) {
anatofuz
parents:
diff changeset
393 RawLex.setParsingPreprocessorDirective(true);
anatofuz
parents:
diff changeset
394 Token HashToken = RawToken;
anatofuz
parents:
diff changeset
395 RawLex.LexFromRawLexer(RawToken);
anatofuz
parents:
diff changeset
396 if (RawToken.is(tok::raw_identifier))
anatofuz
parents:
diff changeset
397 PP.LookUpIdentifierInfo(RawToken);
anatofuz
parents:
diff changeset
398 if (RawToken.getIdentifierInfo() != nullptr) {
anatofuz
parents:
diff changeset
399 switch (RawToken.getIdentifierInfo()->getPPKeywordID()) {
anatofuz
parents:
diff changeset
400 case tok::pp_include:
anatofuz
parents:
diff changeset
401 case tok::pp_include_next:
anatofuz
parents:
diff changeset
402 case tok::pp_import: {
anatofuz
parents:
diff changeset
403 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL, NextToWrite,
anatofuz
parents:
diff changeset
404 Line);
anatofuz
parents:
diff changeset
405 if (FileId != PP.getPredefinesFileID())
anatofuz
parents:
diff changeset
406 WriteLineInfo(FileName, Line - 1, FileType, "");
anatofuz
parents:
diff changeset
407 StringRef LineInfoExtra;
anatofuz
parents:
diff changeset
408 SourceLocation Loc = HashToken.getLocation();
anatofuz
parents:
diff changeset
409 if (const Module *Mod = FindModuleAtLocation(Loc))
anatofuz
parents:
diff changeset
410 WriteImplicitModuleImport(Mod);
anatofuz
parents:
diff changeset
411 else if (const IncludedFile *Inc = FindIncludeAtLocation(Loc)) {
anatofuz
parents:
diff changeset
412 const Module *Mod = FindEnteredModule(Loc);
anatofuz
parents:
diff changeset
413 if (Mod)
anatofuz
parents:
diff changeset
414 OS << "#pragma clang module begin "
anatofuz
parents:
diff changeset
415 << Mod->getFullModuleName(true) << "\n";
anatofuz
parents:
diff changeset
416
anatofuz
parents:
diff changeset
417 // Include and recursively process the file.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
418 Process(Inc->Id, Inc->FileType);
150
anatofuz
parents:
diff changeset
419
anatofuz
parents:
diff changeset
420 if (Mod)
anatofuz
parents:
diff changeset
421 OS << "#pragma clang module end /*"
anatofuz
parents:
diff changeset
422 << Mod->getFullModuleName(true) << "*/\n";
anatofuz
parents:
diff changeset
423
anatofuz
parents:
diff changeset
424 // Add line marker to indicate we're returning from an included
anatofuz
parents:
diff changeset
425 // file.
anatofuz
parents:
diff changeset
426 LineInfoExtra = " 2";
anatofuz
parents:
diff changeset
427 }
anatofuz
parents:
diff changeset
428 // fix up lineinfo (since commented out directive changed line
anatofuz
parents:
diff changeset
429 // numbers) for inclusions that were skipped due to header guards
anatofuz
parents:
diff changeset
430 WriteLineInfo(FileName, Line, FileType, LineInfoExtra);
anatofuz
parents:
diff changeset
431 break;
anatofuz
parents:
diff changeset
432 }
anatofuz
parents:
diff changeset
433 case tok::pp_pragma: {
anatofuz
parents:
diff changeset
434 StringRef Identifier = NextIdentifierName(RawLex, RawToken);
anatofuz
parents:
diff changeset
435 if (Identifier == "clang" || Identifier == "GCC") {
anatofuz
parents:
diff changeset
436 if (NextIdentifierName(RawLex, RawToken) == "system_header") {
anatofuz
parents:
diff changeset
437 // keep the directive in, commented out
anatofuz
parents:
diff changeset
438 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
anatofuz
parents:
diff changeset
439 NextToWrite, Line);
anatofuz
parents:
diff changeset
440 // update our own type
anatofuz
parents:
diff changeset
441 FileType = SM.getFileCharacteristic(RawToken.getLocation());
anatofuz
parents:
diff changeset
442 WriteLineInfo(FileName, Line, FileType);
anatofuz
parents:
diff changeset
443 }
anatofuz
parents:
diff changeset
444 } else if (Identifier == "once") {
anatofuz
parents:
diff changeset
445 // keep the directive in, commented out
anatofuz
parents:
diff changeset
446 CommentOutDirective(RawLex, HashToken, FromFile, LocalEOL,
anatofuz
parents:
diff changeset
447 NextToWrite, Line);
anatofuz
parents:
diff changeset
448 WriteLineInfo(FileName, Line, FileType);
anatofuz
parents:
diff changeset
449 }
anatofuz
parents:
diff changeset
450 break;
anatofuz
parents:
diff changeset
451 }
anatofuz
parents:
diff changeset
452 case tok::pp_if:
anatofuz
parents:
diff changeset
453 case tok::pp_elif: {
anatofuz
parents:
diff changeset
454 bool elif = (RawToken.getIdentifierInfo()->getPPKeywordID() ==
anatofuz
parents:
diff changeset
455 tok::pp_elif);
anatofuz
parents:
diff changeset
456 bool isTrue = IsIfAtLocationTrue(RawToken.getLocation());
anatofuz
parents:
diff changeset
457 OutputContentUpTo(FromFile, NextToWrite,
anatofuz
parents:
diff changeset
458 SM.getFileOffset(HashToken.getLocation()),
anatofuz
parents:
diff changeset
459 LocalEOL, Line, /*EnsureNewline=*/true);
anatofuz
parents:
diff changeset
460 do {
anatofuz
parents:
diff changeset
461 RawLex.LexFromRawLexer(RawToken);
anatofuz
parents:
diff changeset
462 } while (!RawToken.is(tok::eod) && RawToken.isNot(tok::eof));
anatofuz
parents:
diff changeset
463 // We need to disable the old condition, but that is tricky.
anatofuz
parents:
diff changeset
464 // Trying to comment it out can easily lead to comment nesting.
anatofuz
parents:
diff changeset
465 // So instead make the condition harmless by making it enclose
anatofuz
parents:
diff changeset
466 // and empty block. Moreover, put it itself inside an #if 0 block
anatofuz
parents:
diff changeset
467 // to disable it from getting evaluated (e.g. __has_include_next
anatofuz
parents:
diff changeset
468 // warns if used from the primary source file).
anatofuz
parents:
diff changeset
469 OS << "#if 0 /* disabled by -frewrite-includes */" << MainEOL;
anatofuz
parents:
diff changeset
470 if (elif) {
anatofuz
parents:
diff changeset
471 OS << "#if 0" << MainEOL;
anatofuz
parents:
diff changeset
472 }
anatofuz
parents:
diff changeset
473 OutputContentUpTo(FromFile, NextToWrite,
anatofuz
parents:
diff changeset
474 SM.getFileOffset(RawToken.getLocation()) +
anatofuz
parents:
diff changeset
475 RawToken.getLength(),
anatofuz
parents:
diff changeset
476 LocalEOL, Line, /*EnsureNewline=*/true);
anatofuz
parents:
diff changeset
477 // Close the empty block and the disabling block.
anatofuz
parents:
diff changeset
478 OS << "#endif" << MainEOL;
anatofuz
parents:
diff changeset
479 OS << "#endif /* disabled by -frewrite-includes */" << MainEOL;
anatofuz
parents:
diff changeset
480 OS << (elif ? "#elif " : "#if ") << (isTrue ? "1" : "0")
anatofuz
parents:
diff changeset
481 << " /* evaluated by -frewrite-includes */" << MainEOL;
anatofuz
parents:
diff changeset
482 WriteLineInfo(FileName, Line, FileType);
anatofuz
parents:
diff changeset
483 break;
anatofuz
parents:
diff changeset
484 }
anatofuz
parents:
diff changeset
485 case tok::pp_endif:
anatofuz
parents:
diff changeset
486 case tok::pp_else: {
anatofuz
parents:
diff changeset
487 // We surround every #include by #if 0 to comment it out, but that
anatofuz
parents:
diff changeset
488 // changes line numbers. These are fixed up right after that, but
anatofuz
parents:
diff changeset
489 // the whole #include could be inside a preprocessor conditional
anatofuz
parents:
diff changeset
490 // that is not processed. So it is necessary to fix the line
anatofuz
parents:
diff changeset
491 // numbers one the next line after each #else/#endif as well.
anatofuz
parents:
diff changeset
492 RawLex.SetKeepWhitespaceMode(true);
anatofuz
parents:
diff changeset
493 do {
anatofuz
parents:
diff changeset
494 RawLex.LexFromRawLexer(RawToken);
anatofuz
parents:
diff changeset
495 } while (RawToken.isNot(tok::eod) && RawToken.isNot(tok::eof));
anatofuz
parents:
diff changeset
496 OutputContentUpTo(FromFile, NextToWrite,
anatofuz
parents:
diff changeset
497 SM.getFileOffset(RawToken.getLocation()) +
anatofuz
parents:
diff changeset
498 RawToken.getLength(),
anatofuz
parents:
diff changeset
499 LocalEOL, Line, /*EnsureNewline=*/ true);
anatofuz
parents:
diff changeset
500 WriteLineInfo(FileName, Line, FileType);
anatofuz
parents:
diff changeset
501 RawLex.SetKeepWhitespaceMode(false);
anatofuz
parents:
diff changeset
502 break;
anatofuz
parents:
diff changeset
503 }
anatofuz
parents:
diff changeset
504 default:
anatofuz
parents:
diff changeset
505 break;
anatofuz
parents:
diff changeset
506 }
anatofuz
parents:
diff changeset
507 }
anatofuz
parents:
diff changeset
508 RawLex.setParsingPreprocessorDirective(false);
anatofuz
parents:
diff changeset
509 }
anatofuz
parents:
diff changeset
510 RawLex.LexFromRawLexer(RawToken);
anatofuz
parents:
diff changeset
511 }
anatofuz
parents:
diff changeset
512 OutputContentUpTo(FromFile, NextToWrite,
anatofuz
parents:
diff changeset
513 SM.getFileOffset(SM.getLocForEndOfFile(FileId)), LocalEOL,
anatofuz
parents:
diff changeset
514 Line, /*EnsureNewline=*/true);
anatofuz
parents:
diff changeset
515 }
anatofuz
parents:
diff changeset
516
anatofuz
parents:
diff changeset
517 /// InclusionRewriterInInput - Implement -frewrite-includes mode.
anatofuz
parents:
diff changeset
518 void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS,
anatofuz
parents:
diff changeset
519 const PreprocessorOutputOptions &Opts) {
anatofuz
parents:
diff changeset
520 SourceManager &SM = PP.getSourceManager();
anatofuz
parents:
diff changeset
521 InclusionRewriter *Rewrite = new InclusionRewriter(
anatofuz
parents:
diff changeset
522 PP, *OS, Opts.ShowLineMarkers, Opts.UseLineDirectives);
anatofuz
parents:
diff changeset
523 Rewrite->detectMainFileEOL();
anatofuz
parents:
diff changeset
524
anatofuz
parents:
diff changeset
525 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Rewrite));
anatofuz
parents:
diff changeset
526 PP.IgnorePragmas();
anatofuz
parents:
diff changeset
527
anatofuz
parents:
diff changeset
528 // First let the preprocessor process the entire file and call callbacks.
anatofuz
parents:
diff changeset
529 // Callbacks will record which #include's were actually performed.
anatofuz
parents:
diff changeset
530 PP.EnterMainSourceFile();
anatofuz
parents:
diff changeset
531 Token Tok;
anatofuz
parents:
diff changeset
532 // Only preprocessor directives matter here, so disable macro expansion
anatofuz
parents:
diff changeset
533 // everywhere else as an optimization.
anatofuz
parents:
diff changeset
534 // TODO: It would be even faster if the preprocessor could be switched
anatofuz
parents:
diff changeset
535 // to a mode where it would parse only preprocessor directives and comments,
anatofuz
parents:
diff changeset
536 // nothing else matters for parsing or processing.
anatofuz
parents:
diff changeset
537 PP.SetMacroExpansionOnlyInDirectives();
anatofuz
parents:
diff changeset
538 do {
anatofuz
parents:
diff changeset
539 PP.Lex(Tok);
anatofuz
parents:
diff changeset
540 if (Tok.is(tok::annot_module_begin))
anatofuz
parents:
diff changeset
541 Rewrite->handleModuleBegin(Tok);
anatofuz
parents:
diff changeset
542 } while (Tok.isNot(tok::eof));
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
543 Rewrite->setPredefinesBuffer(SM.getBufferOrFake(PP.getPredefinesFileID()));
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
544 Rewrite->Process(PP.getPredefinesFileID(), SrcMgr::C_User);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
545 Rewrite->Process(SM.getMainFileID(), SrcMgr::C_User);
150
anatofuz
parents:
diff changeset
546 OS->flush();
anatofuz
parents:
diff changeset
547 }