/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/Rewrite/FrontendActions.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- FrontendActions.cpp ----------------------------------------------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "clang/Rewrite/Frontend/FrontendActions.h" |
10 | | #include "clang/AST/ASTConsumer.h" |
11 | | #include "clang/Basic/CharInfo.h" |
12 | | #include "clang/Basic/LangStandard.h" |
13 | | #include "clang/Config/config.h" |
14 | | #include "clang/Frontend/CompilerInstance.h" |
15 | | #include "clang/Frontend/FrontendActions.h" |
16 | | #include "clang/Frontend/FrontendDiagnostic.h" |
17 | | #include "clang/Frontend/Utils.h" |
18 | | #include "clang/Lex/Preprocessor.h" |
19 | | #include "clang/Lex/PreprocessorOptions.h" |
20 | | #include "clang/Rewrite/Frontend/ASTConsumers.h" |
21 | | #include "clang/Rewrite/Frontend/FixItRewriter.h" |
22 | | #include "clang/Rewrite/Frontend/Rewriters.h" |
23 | | #include "clang/Serialization/ASTReader.h" |
24 | | #include "clang/Serialization/ModuleFile.h" |
25 | | #include "clang/Serialization/ModuleManager.h" |
26 | | #include "llvm/ADT/DenseSet.h" |
27 | | #include "llvm/Support/CrashRecoveryContext.h" |
28 | | #include "llvm/Support/FileSystem.h" |
29 | | #include "llvm/Support/Path.h" |
30 | | #include "llvm/Support/raw_ostream.h" |
31 | | #include <memory> |
32 | | #include <utility> |
33 | | |
34 | | using namespace clang; |
35 | | |
36 | | //===----------------------------------------------------------------------===// |
37 | | // AST Consumer Actions |
38 | | //===----------------------------------------------------------------------===// |
39 | | |
40 | | std::unique_ptr<ASTConsumer> |
41 | 3 | HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
42 | 3 | if (std::unique_ptr<raw_ostream> OS = |
43 | 3 | CI.createDefaultOutputFile(false, InFile)) |
44 | 3 | return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor()); |
45 | 0 | return nullptr; |
46 | 3 | } |
47 | | |
48 | 71 | FixItAction::FixItAction() {} |
49 | 71 | FixItAction::~FixItAction() {} |
50 | | |
51 | | std::unique_ptr<ASTConsumer> |
52 | 71 | FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
53 | 71 | return std::make_unique<ASTConsumer>(); |
54 | 71 | } |
55 | | |
56 | | namespace { |
57 | | class FixItRewriteInPlace : public FixItOptions { |
58 | | public: |
59 | 66 | FixItRewriteInPlace() { InPlace = true; } |
60 | | |
61 | 0 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
62 | 0 | llvm_unreachable("don't call RewriteFilename for inplace rewrites"); |
63 | 0 | } |
64 | | }; |
65 | | |
66 | | class FixItActionSuffixInserter : public FixItOptions { |
67 | | std::string NewSuffix; |
68 | | |
69 | | public: |
70 | | FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan) |
71 | 1 | : NewSuffix(std::move(NewSuffix)) { |
72 | 1 | this->FixWhatYouCan = FixWhatYouCan; |
73 | 1 | } |
74 | | |
75 | 1 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
76 | 1 | fd = -1; |
77 | 1 | SmallString<128> Path(Filename); |
78 | 1 | llvm::sys::path::replace_extension(Path, |
79 | 1 | NewSuffix + llvm::sys::path::extension(Path)); |
80 | 1 | return std::string(Path.str()); |
81 | 1 | } |
82 | | }; |
83 | | |
84 | | class FixItRewriteToTemp : public FixItOptions { |
85 | | public: |
86 | 4 | std::string RewriteFilename(const std::string &Filename, int &fd) override { |
87 | 4 | SmallString<128> Path; |
88 | 4 | llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename), |
89 | 4 | llvm::sys::path::extension(Filename).drop_front(), fd, |
90 | 4 | Path); |
91 | 4 | return std::string(Path.str()); |
92 | 4 | } |
93 | | }; |
94 | | } // end anonymous namespace |
95 | | |
96 | 67 | bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) { |
97 | 67 | const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); |
98 | 67 | if (!FEOpts.FixItSuffix.empty()) { |
99 | 1 | FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix, |
100 | 1 | FEOpts.FixWhatYouCan)); |
101 | 66 | } else { |
102 | 66 | FixItOpts.reset(new FixItRewriteInPlace); |
103 | 66 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
104 | 66 | } |
105 | 67 | Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), |
106 | 67 | CI.getLangOpts(), FixItOpts.get())); |
107 | 67 | return true; |
108 | 67 | } |
109 | | |
110 | 71 | void FixItAction::EndSourceFileAction() { |
111 | | // Otherwise rewrite all files. |
112 | 71 | Rewriter->WriteFixedFiles(); |
113 | 71 | } |
114 | | |
115 | 5 | bool FixItRecompile::BeginInvocation(CompilerInstance &CI) { |
116 | | |
117 | 5 | std::vector<std::pair<std::string, std::string> > RewrittenFiles; |
118 | 5 | bool err = false; |
119 | 5 | { |
120 | 5 | const FrontendOptions &FEOpts = CI.getFrontendOpts(); |
121 | 5 | std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction()); |
122 | 5 | if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) { |
123 | 5 | std::unique_ptr<FixItOptions> FixItOpts; |
124 | 5 | if (FEOpts.FixToTemporaries) |
125 | 5 | FixItOpts.reset(new FixItRewriteToTemp()); |
126 | 0 | else |
127 | 0 | FixItOpts.reset(new FixItRewriteInPlace()); |
128 | 5 | FixItOpts->Silent = true; |
129 | 5 | FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan; |
130 | 5 | FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings; |
131 | 5 | FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(), |
132 | 5 | CI.getLangOpts(), FixItOpts.get()); |
133 | 5 | if (llvm::Error Err = FixAction->Execute()) { |
134 | | // FIXME this drops the error on the floor. |
135 | 0 | consumeError(std::move(Err)); |
136 | 0 | return false; |
137 | 0 | } |
138 | | |
139 | 5 | err = Rewriter.WriteFixedFiles(&RewrittenFiles); |
140 | | |
141 | 5 | FixAction->EndSourceFile(); |
142 | 5 | CI.setSourceManager(nullptr); |
143 | 5 | CI.setFileManager(nullptr); |
144 | 5 | } else { |
145 | 0 | err = true; |
146 | 0 | } |
147 | 5 | } |
148 | 5 | if (err) |
149 | 1 | return false; |
150 | 4 | CI.getDiagnosticClient().clear(); |
151 | 4 | CI.getDiagnostics().Reset(); |
152 | | |
153 | 4 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
154 | 4 | PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(), |
155 | 4 | RewrittenFiles.begin(), RewrittenFiles.end()); |
156 | 4 | PPOpts.RemappedFilesKeepOriginalName = false; |
157 | | |
158 | 4 | return true; |
159 | 5 | } |
160 | | |
161 | | #if CLANG_ENABLE_OBJC_REWRITER |
162 | | |
163 | | std::unique_ptr<ASTConsumer> |
164 | 164 | RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
165 | 164 | if (std::unique_ptr<raw_ostream> OS = |
166 | 164 | CI.createDefaultOutputFile(false, InFile, "cpp")) { |
167 | 164 | if (CI.getLangOpts().ObjCRuntime.isNonFragile()) |
168 | 78 | return CreateModernObjCRewriter(std::string(InFile), std::move(OS), |
169 | 78 | CI.getDiagnostics(), CI.getLangOpts(), |
170 | 78 | CI.getDiagnosticOpts().NoRewriteMacros, |
171 | 78 | (CI.getCodeGenOpts().getDebugInfo() != |
172 | 78 | llvm::codegenoptions::NoDebugInfo)); |
173 | 86 | return CreateObjCRewriter(std::string(InFile), std::move(OS), |
174 | 86 | CI.getDiagnostics(), CI.getLangOpts(), |
175 | 86 | CI.getDiagnosticOpts().NoRewriteMacros); |
176 | 164 | } |
177 | 0 | return nullptr; |
178 | 164 | } |
179 | | |
180 | | #endif |
181 | | |
182 | | //===----------------------------------------------------------------------===// |
183 | | // Preprocessor Actions |
184 | | //===----------------------------------------------------------------------===// |
185 | | |
186 | 1 | void RewriteMacrosAction::ExecuteAction() { |
187 | 1 | CompilerInstance &CI = getCompilerInstance(); |
188 | 1 | std::unique_ptr<raw_ostream> OS = |
189 | 1 | CI.createDefaultOutputFile(/*Binary=*/true, getCurrentFileOrBufferName()); |
190 | 1 | if (!OS) return0 ; |
191 | | |
192 | 1 | RewriteMacrosInInput(CI.getPreprocessor(), OS.get()); |
193 | 1 | } |
194 | | |
195 | 0 | void RewriteTestAction::ExecuteAction() { |
196 | 0 | CompilerInstance &CI = getCompilerInstance(); |
197 | 0 | std::unique_ptr<raw_ostream> OS = |
198 | 0 | CI.createDefaultOutputFile(/*Binary=*/false, getCurrentFileOrBufferName()); |
199 | 0 | if (!OS) return; |
200 | | |
201 | 0 | DoRewriteTest(CI.getPreprocessor(), OS.get()); |
202 | 0 | } |
203 | | |
204 | | class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener { |
205 | | CompilerInstance &CI; |
206 | | std::weak_ptr<raw_ostream> Out; |
207 | | |
208 | | llvm::DenseSet<const FileEntry*> Rewritten; |
209 | | |
210 | | public: |
211 | | RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out) |
212 | 4 | : CI(CI), Out(Out) {} |
213 | | |
214 | | void visitModuleFile(StringRef Filename, |
215 | 9 | serialization::ModuleKind Kind) override { |
216 | 9 | auto File = CI.getFileManager().getFile(Filename); |
217 | 9 | assert(File && "missing file for loaded module?"); |
218 | | |
219 | | // Only rewrite each module file once. |
220 | 9 | if (!Rewritten.insert(*File).second) |
221 | 0 | return; |
222 | | |
223 | 9 | serialization::ModuleFile *MF = |
224 | 9 | CI.getASTReader()->getModuleManager().lookup(*File); |
225 | 9 | assert(MF && "missing module file for loaded module?"); |
226 | | |
227 | | // Not interested in PCH / preambles. |
228 | 9 | if (!MF->isModule()) |
229 | 0 | return; |
230 | | |
231 | 9 | auto OS = Out.lock(); |
232 | 9 | assert(OS && "loaded module file after finishing rewrite action?"); |
233 | | |
234 | 9 | (*OS) << "#pragma clang module build "; |
235 | 9 | if (isValidAsciiIdentifier(MF->ModuleName)) |
236 | 7 | (*OS) << MF->ModuleName; |
237 | 2 | else { |
238 | 2 | (*OS) << '"'; |
239 | 2 | OS->write_escaped(MF->ModuleName); |
240 | 2 | (*OS) << '"'; |
241 | 2 | } |
242 | 9 | (*OS) << '\n'; |
243 | | |
244 | | // Rewrite the contents of the module in a separate compiler instance. |
245 | 9 | CompilerInstance Instance(CI.getPCHContainerOperations(), |
246 | 9 | &CI.getModuleCache()); |
247 | 9 | Instance.setInvocation( |
248 | 9 | std::make_shared<CompilerInvocation>(CI.getInvocation())); |
249 | 9 | Instance.createDiagnostics( |
250 | 9 | new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()), |
251 | 9 | /*ShouldOwnClient=*/true); |
252 | 9 | Instance.getFrontendOpts().DisableFree = false; |
253 | 9 | Instance.getFrontendOpts().Inputs.clear(); |
254 | 9 | Instance.getFrontendOpts().Inputs.emplace_back( |
255 | 9 | Filename, InputKind(Language::Unknown, InputKind::Precompiled)); |
256 | 9 | Instance.getFrontendOpts().ModuleFiles.clear(); |
257 | 9 | Instance.getFrontendOpts().ModuleMapFiles.clear(); |
258 | | // Don't recursively rewrite imports. We handle them all at the top level. |
259 | 9 | Instance.getPreprocessorOutputOpts().RewriteImports = false; |
260 | | |
261 | 9 | llvm::CrashRecoveryContext().RunSafelyOnThread([&]() { |
262 | 9 | RewriteIncludesAction Action; |
263 | 9 | Action.OutputStream = OS; |
264 | 9 | Instance.ExecuteAction(Action); |
265 | 9 | }); |
266 | | |
267 | 9 | (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n"; |
268 | 9 | } |
269 | | }; |
270 | | |
271 | 76 | bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) { |
272 | 76 | if (!OutputStream) { |
273 | 67 | OutputStream = |
274 | 67 | CI.createDefaultOutputFile(/*Binary=*/true, getCurrentFileOrBufferName()); |
275 | 67 | if (!OutputStream) |
276 | 0 | return false; |
277 | 67 | } |
278 | | |
279 | 76 | auto &OS = *OutputStream; |
280 | | |
281 | | // If we're preprocessing a module map, start by dumping the contents of the |
282 | | // module itself before switching to the input buffer. |
283 | 76 | auto &Input = getCurrentInput(); |
284 | 76 | if (Input.getKind().getFormat() == InputKind::ModuleMap) { |
285 | 14 | if (Input.isFile()) { |
286 | 14 | OS << "# 1 \""; |
287 | 14 | OS.write_escaped(Input.getFile()); |
288 | 14 | OS << "\"\n"; |
289 | 14 | } |
290 | 14 | getCurrentModule()->print(OS); |
291 | 14 | OS << "#pragma clang module contents\n"; |
292 | 14 | } |
293 | | |
294 | | // If we're rewriting imports, set up a listener to track when we import |
295 | | // module files. |
296 | 76 | if (CI.getPreprocessorOutputOpts().RewriteImports) { |
297 | 4 | CI.createASTReader(); |
298 | 4 | CI.getASTReader()->addListener( |
299 | 4 | std::make_unique<RewriteImportsListener>(CI, OutputStream)); |
300 | 4 | } |
301 | | |
302 | 76 | return true; |
303 | 76 | } |
304 | | |
305 | 76 | void RewriteIncludesAction::ExecuteAction() { |
306 | 76 | CompilerInstance &CI = getCompilerInstance(); |
307 | | |
308 | | // If we're rewriting imports, emit the module build output first rather |
309 | | // than switching back and forth (potentially in the middle of a line). |
310 | 76 | if (CI.getPreprocessorOutputOpts().RewriteImports) { |
311 | 4 | std::string Buffer; |
312 | 4 | llvm::raw_string_ostream OS(Buffer); |
313 | | |
314 | 4 | RewriteIncludesInInput(CI.getPreprocessor(), &OS, |
315 | 4 | CI.getPreprocessorOutputOpts()); |
316 | | |
317 | 4 | (*OutputStream) << OS.str(); |
318 | 72 | } else { |
319 | 72 | RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(), |
320 | 72 | CI.getPreprocessorOutputOpts()); |
321 | 72 | } |
322 | | |
323 | 76 | OutputStream.reset(); |
324 | 76 | } |