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/FileManager.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/Parse/Parser.h"
19 #include "clang/Rewrite/Frontend/ASTConsumers.h"
20 #include "clang/Rewrite/Frontend/FixItRewriter.h"
21 #include "clang/Rewrite/Frontend/Rewriters.h"
22 #include "llvm/ADT/OwningPtr.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/raw_ostream.h"
27 using namespace clang;
29 //===----------------------------------------------------------------------===//
30 // AST Consumer Actions
31 //===----------------------------------------------------------------------===//
33 ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
35 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
36 return CreateHTMLPrinter(OS, CI.getPreprocessor());
40 FixItAction::FixItAction() {}
41 FixItAction::~FixItAction() {}
43 ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
45 return new ASTConsumer();
49 class FixItRewriteInPlace : public FixItOptions {
51 std::string RewriteFilename(const std::string &Filename, int &fd) {
57 class FixItActionSuffixInserter : public FixItOptions {
58 std::string NewSuffix;
61 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
62 : NewSuffix(NewSuffix) {
63 this->FixWhatYouCan = FixWhatYouCan;
66 std::string RewriteFilename(const std::string &Filename, int &fd) {
68 SmallString<128> Path(Filename);
69 llvm::sys::path::replace_extension(Path,
70 NewSuffix + llvm::sys::path::extension(Path));
75 class FixItRewriteToTemp : public FixItOptions {
77 std::string RewriteFilename(const std::string &Filename, int &fd) {
78 SmallString<128> Path;
79 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
80 llvm::sys::path::extension(Filename), fd,
85 } // end anonymous namespace
87 bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
89 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
90 if (!FEOpts.FixItSuffix.empty()) {
91 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
92 FEOpts.FixWhatYouCan));
94 FixItOpts.reset(new FixItRewriteInPlace);
95 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
97 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
98 CI.getLangOpts(), FixItOpts.get()));
102 void FixItAction::EndSourceFileAction() {
103 // Otherwise rewrite all files.
104 Rewriter->WriteFixedFiles();
107 bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
109 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
112 const FrontendOptions &FEOpts = CI.getFrontendOpts();
113 OwningPtr<FrontendAction> FixAction(new SyntaxOnlyAction());
114 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
115 OwningPtr<FixItOptions> FixItOpts;
116 if (FEOpts.FixToTemporaries)
117 FixItOpts.reset(new FixItRewriteToTemp());
119 FixItOpts.reset(new FixItRewriteInPlace());
120 FixItOpts->Silent = true;
121 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
122 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
123 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
124 CI.getLangOpts(), FixItOpts.get());
125 FixAction->Execute();
127 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
129 FixAction->EndSourceFile();
130 CI.setSourceManager(0);
131 CI.setFileManager(0);
138 CI.getDiagnosticClient().clear();
139 CI.getDiagnostics().Reset();
141 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
142 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
143 RewrittenFiles.begin(), RewrittenFiles.end());
144 PPOpts.RemappedFilesKeepOriginalName = false;
149 //===----------------------------------------------------------------------===//
150 // Preprocessor Actions
151 //===----------------------------------------------------------------------===//
153 ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
155 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
156 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
157 return CreateModernObjCRewriter(InFile, OS,
158 CI.getDiagnostics(), CI.getLangOpts(),
159 CI.getDiagnosticOpts().NoRewriteMacros,
160 (CI.getCodeGenOpts().getDebugInfo() !=
161 CodeGenOptions::NoDebugInfo));
162 return CreateObjCRewriter(InFile, OS,
163 CI.getDiagnostics(), CI.getLangOpts(),
164 CI.getDiagnosticOpts().NoRewriteMacros);
169 void RewriteMacrosAction::ExecuteAction() {
170 CompilerInstance &CI = getCompilerInstance();
171 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
174 RewriteMacrosInInput(CI.getPreprocessor(), OS);
177 void RewriteTestAction::ExecuteAction() {
178 CompilerInstance &CI = getCompilerInstance();
179 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
182 DoRewriteTest(CI.getPreprocessor(), OS);
185 void RewriteIncludesAction::ExecuteAction() {
186 CompilerInstance &CI = getCompilerInstance();
187 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
190 RewriteIncludesInInput(CI.getPreprocessor(), OS,
191 CI.getPreprocessorOutputOpts());