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