1 //===--- FrontendActions.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Rewrite/Frontend/FrontendActions.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/Basic/CharInfo.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Frontend/FrontendActions.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/Utils.h"
17 #include "clang/Lex/Preprocessor.h"
18 #include "clang/Lex/PreprocessorOptions.h"
19 #include "clang/Rewrite/Frontend/ASTConsumers.h"
20 #include "clang/Rewrite/Frontend/FixItRewriter.h"
21 #include "clang/Rewrite/Frontend/Rewriters.h"
22 #include "clang/Serialization/ASTReader.h"
23 #include "clang/Serialization/Module.h"
24 #include "clang/Serialization/ModuleManager.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/Support/CrashRecoveryContext.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/raw_ostream.h"
33 using namespace clang;
35 //===----------------------------------------------------------------------===//
36 // AST Consumer Actions
37 //===----------------------------------------------------------------------===//
39 std::unique_ptr<ASTConsumer>
40 HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
41 if (std::unique_ptr<raw_ostream> OS =
42 CI.createDefaultOutputFile(false, InFile))
43 return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
47 FixItAction::FixItAction() {}
48 FixItAction::~FixItAction() {}
50 std::unique_ptr<ASTConsumer>
51 FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
52 return llvm::make_unique<ASTConsumer>();
56 class FixItRewriteInPlace : public FixItOptions {
58 FixItRewriteInPlace() { InPlace = true; }
60 std::string RewriteFilename(const std::string &Filename, int &fd) override {
61 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
65 class FixItActionSuffixInserter : public FixItOptions {
66 std::string NewSuffix;
69 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
70 : NewSuffix(std::move(NewSuffix)) {
71 this->FixWhatYouCan = FixWhatYouCan;
74 std::string RewriteFilename(const std::string &Filename, int &fd) override {
76 SmallString<128> Path(Filename);
77 llvm::sys::path::replace_extension(Path,
78 NewSuffix + llvm::sys::path::extension(Path));
83 class FixItRewriteToTemp : public FixItOptions {
85 std::string RewriteFilename(const std::string &Filename, int &fd) override {
86 SmallString<128> Path;
87 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
88 llvm::sys::path::extension(Filename).drop_front(), fd,
93 } // end anonymous namespace
95 bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) {
96 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
97 if (!FEOpts.FixItSuffix.empty()) {
98 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
99 FEOpts.FixWhatYouCan));
101 FixItOpts.reset(new FixItRewriteInPlace);
102 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
104 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
105 CI.getLangOpts(), FixItOpts.get()));
109 void FixItAction::EndSourceFileAction() {
110 // Otherwise rewrite all files.
111 Rewriter->WriteFixedFiles();
114 bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
116 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
119 const FrontendOptions &FEOpts = CI.getFrontendOpts();
120 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
121 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
122 std::unique_ptr<FixItOptions> FixItOpts;
123 if (FEOpts.FixToTemporaries)
124 FixItOpts.reset(new FixItRewriteToTemp());
126 FixItOpts.reset(new FixItRewriteInPlace());
127 FixItOpts->Silent = true;
128 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
129 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
130 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
131 CI.getLangOpts(), FixItOpts.get());
132 FixAction->Execute();
134 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
136 FixAction->EndSourceFile();
137 CI.setSourceManager(nullptr);
138 CI.setFileManager(nullptr);
145 CI.getDiagnosticClient().clear();
146 CI.getDiagnostics().Reset();
148 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
149 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
150 RewrittenFiles.begin(), RewrittenFiles.end());
151 PPOpts.RemappedFilesKeepOriginalName = false;
156 #ifdef CLANG_ENABLE_OBJC_REWRITER
158 std::unique_ptr<ASTConsumer>
159 RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
160 if (std::unique_ptr<raw_ostream> OS =
161 CI.createDefaultOutputFile(false, InFile, "cpp")) {
162 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
163 return CreateModernObjCRewriter(
164 InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
165 CI.getDiagnosticOpts().NoRewriteMacros,
166 (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
167 return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
169 CI.getDiagnosticOpts().NoRewriteMacros);
176 //===----------------------------------------------------------------------===//
177 // Preprocessor Actions
178 //===----------------------------------------------------------------------===//
180 void RewriteMacrosAction::ExecuteAction() {
181 CompilerInstance &CI = getCompilerInstance();
182 std::unique_ptr<raw_ostream> OS =
183 CI.createDefaultOutputFile(true, getCurrentFile());
186 RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
189 void RewriteTestAction::ExecuteAction() {
190 CompilerInstance &CI = getCompilerInstance();
191 std::unique_ptr<raw_ostream> OS =
192 CI.createDefaultOutputFile(false, getCurrentFile());
195 DoRewriteTest(CI.getPreprocessor(), OS.get());
198 class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
199 CompilerInstance &CI;
200 std::weak_ptr<raw_ostream> Out;
202 llvm::DenseSet<const FileEntry*> Rewritten;
205 RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out)
206 : CI(CI), Out(Out) {}
208 void visitModuleFile(StringRef Filename,
209 serialization::ModuleKind Kind) override {
210 auto *File = CI.getFileManager().getFile(Filename);
211 assert(File && "missing file for loaded module?");
213 // Only rewrite each module file once.
214 if (!Rewritten.insert(File).second)
217 serialization::ModuleFile *MF =
218 CI.getModuleManager()->getModuleManager().lookup(File);
219 assert(File && "missing module file for loaded module?");
221 // Not interested in PCH / preambles.
225 auto OS = Out.lock();
226 assert(OS && "loaded module file after finishing rewrite action?");
228 (*OS) << "#pragma clang module build ";
229 if (isValidIdentifier(MF->ModuleName))
230 (*OS) << MF->ModuleName;
233 OS->write_escaped(MF->ModuleName);
238 // Rewrite the contents of the module in a separate compiler instance.
239 CompilerInstance Instance(CI.getPCHContainerOperations(),
240 &CI.getPreprocessor().getPCMCache());
241 Instance.setInvocation(
242 std::make_shared<CompilerInvocation>(CI.getInvocation()));
243 Instance.createDiagnostics(
244 new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
245 /*ShouldOwnClient=*/true);
246 Instance.getFrontendOpts().DisableFree = false;
247 Instance.getFrontendOpts().Inputs.clear();
248 Instance.getFrontendOpts().Inputs.emplace_back(
249 Filename, InputKind(InputKind::Unknown, InputKind::Precompiled));
250 Instance.getFrontendOpts().ModuleFiles.clear();
251 Instance.getFrontendOpts().ModuleMapFiles.clear();
252 // Don't recursively rewrite imports. We handle them all at the top level.
253 Instance.getPreprocessorOutputOpts().RewriteImports = false;
255 llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
256 RewriteIncludesAction Action;
257 Action.OutputStream = OS;
258 Instance.ExecuteAction(Action);
261 (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n";
265 bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
267 OutputStream = CI.createDefaultOutputFile(true, getCurrentFile());
272 auto &OS = *OutputStream;
274 // If we're preprocessing a module map, start by dumping the contents of the
275 // module itself before switching to the input buffer.
276 auto &Input = getCurrentInput();
277 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
278 if (Input.isFile()) {
280 OS.write_escaped(Input.getFile());
283 getCurrentModule()->print(OS);
284 OS << "#pragma clang module contents\n";
287 // If we're rewriting imports, set up a listener to track when we import
289 if (CI.getPreprocessorOutputOpts().RewriteImports) {
290 CI.createModuleManager();
291 CI.getModuleManager()->addListener(
292 llvm::make_unique<RewriteImportsListener>(CI, OutputStream));
298 void RewriteIncludesAction::ExecuteAction() {
299 CompilerInstance &CI = getCompilerInstance();
301 // If we're rewriting imports, emit the module build output first rather
302 // than switching back and forth (potentially in the middle of a line).
303 if (CI.getPreprocessorOutputOpts().RewriteImports) {
305 llvm::raw_string_ostream OS(Buffer);
307 RewriteIncludesInInput(CI.getPreprocessor(), &OS,
308 CI.getPreprocessorOutputOpts());
310 (*OutputStream) << OS.str();
312 RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(),
313 CI.getPreprocessorOutputOpts());
316 OutputStream.reset();