1 //===--- FrontendActions.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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/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"
34 using namespace clang;
36 //===----------------------------------------------------------------------===//
37 // AST Consumer Actions
38 //===----------------------------------------------------------------------===//
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());
48 FixItAction::FixItAction() {}
49 FixItAction::~FixItAction() {}
51 std::unique_ptr<ASTConsumer>
52 FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
53 return std::make_unique<ASTConsumer>();
57 class FixItRewriteInPlace : public FixItOptions {
59 FixItRewriteInPlace() { InPlace = true; }
61 std::string RewriteFilename(const std::string &Filename, int &fd) override {
62 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
66 class FixItActionSuffixInserter : public FixItOptions {
67 std::string NewSuffix;
70 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
71 : NewSuffix(std::move(NewSuffix)) {
72 this->FixWhatYouCan = FixWhatYouCan;
75 std::string RewriteFilename(const std::string &Filename, int &fd) override {
77 SmallString<128> Path(Filename);
78 llvm::sys::path::replace_extension(Path,
79 NewSuffix + llvm::sys::path::extension(Path));
84 class FixItRewriteToTemp : public FixItOptions {
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,
94 } // end anonymous namespace
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));
102 FixItOpts.reset(new FixItRewriteInPlace);
103 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
105 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
106 CI.getLangOpts(), FixItOpts.get()));
110 void FixItAction::EndSourceFileAction() {
111 // Otherwise rewrite all files.
112 Rewriter->WriteFixedFiles();
115 bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
117 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
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());
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 if (llvm::Error Err = FixAction->Execute()) {
134 // FIXME this drops the error on the floor.
135 consumeError(std::move(Err));
139 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
141 FixAction->EndSourceFile();
142 CI.setSourceManager(nullptr);
143 CI.setFileManager(nullptr);
150 CI.getDiagnosticClient().clear();
151 CI.getDiagnostics().Reset();
153 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
154 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
155 RewrittenFiles.begin(), RewrittenFiles.end());
156 PPOpts.RemappedFilesKeepOriginalName = false;
161 #if CLANG_ENABLE_OBJC_REWRITER
163 std::unique_ptr<ASTConsumer>
164 RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
165 if (std::unique_ptr<raw_ostream> OS =
166 CI.createDefaultOutputFile(false, InFile, "cpp")) {
167 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
168 return CreateModernObjCRewriter(
169 InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
170 CI.getDiagnosticOpts().NoRewriteMacros,
171 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
172 return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
174 CI.getDiagnosticOpts().NoRewriteMacros);
181 //===----------------------------------------------------------------------===//
182 // Preprocessor Actions
183 //===----------------------------------------------------------------------===//
185 void RewriteMacrosAction::ExecuteAction() {
186 CompilerInstance &CI = getCompilerInstance();
187 std::unique_ptr<raw_ostream> OS =
188 CI.createDefaultOutputFile(true, getCurrentFileOrBufferName());
191 RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
194 void RewriteTestAction::ExecuteAction() {
195 CompilerInstance &CI = getCompilerInstance();
196 std::unique_ptr<raw_ostream> OS =
197 CI.createDefaultOutputFile(false, getCurrentFileOrBufferName());
200 DoRewriteTest(CI.getPreprocessor(), OS.get());
203 class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
204 CompilerInstance &CI;
205 std::weak_ptr<raw_ostream> Out;
207 llvm::DenseSet<const FileEntry*> Rewritten;
210 RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out)
211 : CI(CI), Out(Out) {}
213 void visitModuleFile(StringRef Filename,
214 serialization::ModuleKind Kind) override {
215 auto File = CI.getFileManager().getFile(Filename);
216 assert(File && "missing file for loaded module?");
218 // Only rewrite each module file once.
219 if (!Rewritten.insert(*File).second)
222 serialization::ModuleFile *MF =
223 CI.getModuleManager()->getModuleManager().lookup(*File);
224 assert(MF && "missing module file for loaded module?");
226 // Not interested in PCH / preambles.
230 auto OS = Out.lock();
231 assert(OS && "loaded module file after finishing rewrite action?");
233 (*OS) << "#pragma clang module build ";
234 if (isValidIdentifier(MF->ModuleName))
235 (*OS) << MF->ModuleName;
238 OS->write_escaped(MF->ModuleName);
243 // Rewrite the contents of the module in a separate compiler instance.
244 CompilerInstance Instance(CI.getPCHContainerOperations(),
245 &CI.getModuleCache());
246 Instance.setInvocation(
247 std::make_shared<CompilerInvocation>(CI.getInvocation()));
248 Instance.createDiagnostics(
249 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
250 /*ShouldOwnClient=*/true);
251 Instance.getFrontendOpts().DisableFree = false;
252 Instance.getFrontendOpts().Inputs.clear();
253 Instance.getFrontendOpts().Inputs.emplace_back(
254 Filename, InputKind(Language::Unknown, InputKind::Precompiled));
255 Instance.getFrontendOpts().ModuleFiles.clear();
256 Instance.getFrontendOpts().ModuleMapFiles.clear();
257 // Don't recursively rewrite imports. We handle them all at the top level.
258 Instance.getPreprocessorOutputOpts().RewriteImports = false;
260 llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
261 RewriteIncludesAction Action;
262 Action.OutputStream = OS;
263 Instance.ExecuteAction(Action);
266 (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n";
270 bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
273 CI.createDefaultOutputFile(true, getCurrentFileOrBufferName());
278 auto &OS = *OutputStream;
280 // If we're preprocessing a module map, start by dumping the contents of the
281 // module itself before switching to the input buffer.
282 auto &Input = getCurrentInput();
283 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
284 if (Input.isFile()) {
286 OS.write_escaped(Input.getFile());
289 getCurrentModule()->print(OS);
290 OS << "#pragma clang module contents\n";
293 // If we're rewriting imports, set up a listener to track when we import
295 if (CI.getPreprocessorOutputOpts().RewriteImports) {
296 CI.createModuleManager();
297 CI.getModuleManager()->addListener(
298 std::make_unique<RewriteImportsListener>(CI, OutputStream));
304 void RewriteIncludesAction::ExecuteAction() {
305 CompilerInstance &CI = getCompilerInstance();
307 // If we're rewriting imports, emit the module build output first rather
308 // than switching back and forth (potentially in the middle of a line).
309 if (CI.getPreprocessorOutputOpts().RewriteImports) {
311 llvm::raw_string_ostream OS(Buffer);
313 RewriteIncludesInInput(CI.getPreprocessor(), &OS,
314 CI.getPreprocessorOutputOpts());
316 (*OutputStream) << OS.str();
318 RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(),
319 CI.getPreprocessorOutputOpts());
322 OutputStream.reset();