blob: 8cf8adf37ed669dcb7ea502465411894d0d20575 [file] [log] [blame]
Daniel Dunbarc1b17292010-06-15 17:48:49 +00001//===--- 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
Ted Kremenekcdf81492012-09-01 05:09:24 +000010#include "clang/Rewrite/Frontend/FrontendActions.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Frontend/CompilerInstance.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Frontend/FrontendActions.h"
Daniel Dunbarc1b17292010-06-15 17:48:49 +000015#include "clang/Frontend/FrontendDiagnostic.h"
16#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/Parser.h"
Ted Kremenekcdf81492012-09-01 05:09:24 +000019#include "clang/Rewrite/Frontend/ASTConsumers.h"
20#include "clang/Rewrite/Frontend/FixItRewriter.h"
21#include "clang/Rewrite/Frontend/Rewriters.h"
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000022#include "llvm/Support/FileSystem.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/Path.h"
24#include "llvm/Support/raw_ostream.h"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000025#include <memory>
NAKAMURA Takumi69ee7d52012-01-26 03:47:18 +000026
Daniel Dunbarc1b17292010-06-15 17:48:49 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// AST Consumer Actions
31//===----------------------------------------------------------------------===//
32
David Blaikie6beb6aa2014-08-10 19:56:51 +000033std::unique_ptr<ASTConsumer>
34HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbarc1b17292010-06-15 17:48:49 +000036 return CreateHTMLPrinter(OS, CI.getPreprocessor());
Craig Topper8ae12032014-05-07 06:21:57 +000037 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000038}
39
40FixItAction::FixItAction() {}
41FixItAction::~FixItAction() {}
42
David Blaikie6beb6aa2014-08-10 19:56:51 +000043std::unique_ptr<ASTConsumer>
44FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
45 return llvm::make_unique<ASTConsumer>();
Daniel Dunbarc1b17292010-06-15 17:48:49 +000046}
47
Benjamin Kramerfb5e5842010-10-22 16:48:22 +000048namespace {
Nick Lewycky53f10422010-08-15 16:47:39 +000049class FixItRewriteInPlace : public FixItOptions {
50public:
Reid Kleckner3df5dd42015-06-17 17:47:30 +000051 FixItRewriteInPlace() { InPlace = true; }
52
Craig Topperfb6b25b2014-03-15 04:29:04 +000053 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Reid Kleckner3df5dd42015-06-17 17:47:30 +000054 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000055 }
Nick Lewycky53f10422010-08-15 16:47:39 +000056};
57
Nick Lewycky078a5e22010-08-13 17:31:00 +000058class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000059 std::string NewSuffix;
60
61public:
Nick Lewycky078a5e22010-08-13 17:31:00 +000062 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
63 : NewSuffix(NewSuffix) {
64 this->FixWhatYouCan = FixWhatYouCan;
65 }
Daniel Dunbarc1b17292010-06-15 17:48:49 +000066
Craig Topperfb6b25b2014-03-15 04:29:04 +000067 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Argyrios Kyrtzidis623e8772012-01-26 04:19:04 +000068 fd = -1;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000069 SmallString<128> Path(Filename);
Michael J. Spencere47230f2010-12-18 04:13:32 +000070 llvm::sys::path::replace_extension(Path,
71 NewSuffix + llvm::sys::path::extension(Path));
72 return Path.str();
Daniel Dunbarc1b17292010-06-15 17:48:49 +000073 }
74};
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000075
76class FixItRewriteToTemp : public FixItOptions {
77public:
Craig Topperfb6b25b2014-03-15 04:29:04 +000078 std::string RewriteFilename(const std::string &Filename, int &fd) override {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +000079 SmallString<128> Path;
Rafael Espindolaa36e78e2013-07-05 20:00:06 +000080 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
Argyrios Kyrtzidis203e9232015-09-09 16:48:47 +000081 llvm::sys::path::extension(Filename).drop_front(), fd,
Rafael Espindolaa36e78e2013-07-05 20:00:06 +000082 Path);
83 return Path.str();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +000084 }
85};
Benjamin Kramerfb5e5842010-10-22 16:48:22 +000086} // end anonymous namespace
Daniel Dunbarc1b17292010-06-15 17:48:49 +000087
88bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000089 StringRef Filename) {
Daniel Dunbarc1b17292010-06-15 17:48:49 +000090 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
91 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky078a5e22010-08-13 17:31:00 +000092 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
93 FEOpts.FixWhatYouCan));
Daniel Dunbarc1b17292010-06-15 17:48:49 +000094 } else {
Nick Lewycky53f10422010-08-15 16:47:39 +000095 FixItOpts.reset(new FixItRewriteInPlace);
96 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbarc1b17292010-06-15 17:48:49 +000097 }
98 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky078a5e22010-08-13 17:31:00 +000099 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000100 return true;
101}
102
103void FixItAction::EndSourceFileAction() {
104 // Otherwise rewrite all files.
105 Rewriter->WriteFixedFiles();
106}
107
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000108bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
109
110 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
111 bool err = false;
112 {
113 const FrontendOptions &FEOpts = CI.getFrontendOpts();
Ahmed Charlesb8984322014-03-07 20:03:18 +0000114 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000115 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000116 std::unique_ptr<FixItOptions> FixItOpts;
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000117 if (FEOpts.FixToTemporaries)
118 FixItOpts.reset(new FixItRewriteToTemp());
119 else
120 FixItOpts.reset(new FixItRewriteInPlace());
121 FixItOpts->Silent = true;
122 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
123 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
124 FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
125 CI.getLangOpts(), FixItOpts.get());
126 FixAction->Execute();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000127
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000128 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
129
130 FixAction->EndSourceFile();
Craig Topper8ae12032014-05-07 06:21:57 +0000131 CI.setSourceManager(nullptr);
132 CI.setFileManager(nullptr);
Argyrios Kyrtzidisaf0bdfc2012-01-27 01:00:47 +0000133 } else {
134 err = true;
135 }
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000136 }
137 if (err)
138 return false;
139 CI.getDiagnosticClient().clear();
Argyrios Kyrtzidis0b5ec2d2012-01-27 06:15:37 +0000140 CI.getDiagnostics().Reset();
Argyrios Kyrtzidis24e9aff2012-01-26 02:40:48 +0000141
142 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
143 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
144 RewrittenFiles.begin(), RewrittenFiles.end());
145 PPOpts.RemappedFilesKeepOriginalName = false;
146
147 return true;
148}
149
Alp Toker0621cb22014-07-16 16:48:33 +0000150#ifdef CLANG_ENABLE_OBJC_REWRITER
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000151
David Blaikie6beb6aa2014-08-10 19:56:51 +0000152std::unique_ptr<ASTConsumer>
153RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Fariborz Jahanian11671902012-02-07 17:11:38 +0000154 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
John McCall5fb5df92012-06-20 06:18:46 +0000155 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
Fariborz Jahanian11671902012-02-07 17:11:38 +0000156 return CreateModernObjCRewriter(InFile, OS,
157 CI.getDiagnostics(), CI.getLangOpts(),
Fariborz Jahaniane4c7e852013-02-08 00:27:34 +0000158 CI.getDiagnosticOpts().NoRewriteMacros,
159 (CI.getCodeGenOpts().getDebugInfo() !=
160 CodeGenOptions::NoDebugInfo));
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000161 return CreateObjCRewriter(InFile, OS,
162 CI.getDiagnostics(), CI.getLangOpts(),
163 CI.getDiagnosticOpts().NoRewriteMacros);
Fariborz Jahanian11671902012-02-07 17:11:38 +0000164 }
Craig Topper8ae12032014-05-07 06:21:57 +0000165 return nullptr;
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000166}
167
Alp Toker0621cb22014-07-16 16:48:33 +0000168#endif
169
170//===----------------------------------------------------------------------===//
171// Preprocessor Actions
172//===----------------------------------------------------------------------===//
173
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000174void RewriteMacrosAction::ExecuteAction() {
175 CompilerInstance &CI = getCompilerInstance();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000176 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000177 if (!OS) return;
178
179 RewriteMacrosInInput(CI.getPreprocessor(), OS);
180}
181
182void RewriteTestAction::ExecuteAction() {
183 CompilerInstance &CI = getCompilerInstance();
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000184 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbarc1b17292010-06-15 17:48:49 +0000185 if (!OS) return;
186
187 DoRewriteTest(CI.getPreprocessor(), OS);
188}
David Blaikied5321242012-06-06 18:52:13 +0000189
190void RewriteIncludesAction::ExecuteAction() {
191 CompilerInstance &CI = getCompilerInstance();
192 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
193 if (!OS) return;
194
195 RewriteIncludesInInput(CI.getPreprocessor(), OS,
196 CI.getPreprocessorOutputOpts());
197}