blob: e9ec38818265073017c99b2803ce526d4b847436 [file] [log] [blame]
Daniel Dunbar9b414d32010-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 Kremenek305c6132012-09-01 05:09:24 +000010#include "clang/Rewrite/Frontend/FrontendActions.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Frontend/CompilerInstance.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000015#include "clang/Frontend/FrontendDiagnostic.h"
16#include "clang/Frontend/Utils.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/Parser.h"
Ted Kremenek305c6132012-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"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000022#include "llvm/ADT/OwningPtr.h"
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000023#include "llvm/Support/FileSystem.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000024#include "llvm/Support/Path.h"
25#include "llvm/Support/raw_ostream.h"
NAKAMURA Takumi02770392012-01-26 03:47:18 +000026
Daniel Dunbar9b414d32010-06-15 17:48:49 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// AST Consumer Actions
31//===----------------------------------------------------------------------===//
32
33ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 StringRef InFile) {
35 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar9b414d32010-06-15 17:48:49 +000036 return CreateHTMLPrinter(OS, CI.getPreprocessor());
37 return 0;
38}
39
40FixItAction::FixItAction() {}
41FixItAction::~FixItAction() {}
42
43ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000044 StringRef InFile) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000045 return new ASTConsumer();
46}
47
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000048namespace {
Nick Lewycky96872c42010-08-15 16:47:39 +000049class FixItRewriteInPlace : public FixItOptions {
50public:
Argyrios Kyrtzidisc8af9102012-01-26 04:19:04 +000051 std::string RewriteFilename(const std::string &Filename, int &fd) {
52 fd = -1;
53 return Filename;
54 }
Nick Lewycky96872c42010-08-15 16:47:39 +000055};
56
Nick Lewycky1450f262010-08-13 17:31:00 +000057class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000058 std::string NewSuffix;
59
60public:
Nick Lewycky1450f262010-08-13 17:31:00 +000061 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
62 : NewSuffix(NewSuffix) {
63 this->FixWhatYouCan = FixWhatYouCan;
64 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000065
Argyrios Kyrtzidisc8af9102012-01-26 04:19:04 +000066 std::string RewriteFilename(const std::string &Filename, int &fd) {
67 fd = -1;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000068 SmallString<128> Path(Filename);
Michael J. Spencerd5b08be2010-12-18 04:13:32 +000069 llvm::sys::path::replace_extension(Path,
70 NewSuffix + llvm::sys::path::extension(Path));
71 return Path.str();
Daniel Dunbar9b414d32010-06-15 17:48:49 +000072 }
73};
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000074
75class FixItRewriteToTemp : public FixItOptions {
76public:
Argyrios Kyrtzidisc8af9102012-01-26 04:19:04 +000077 std::string RewriteFilename(const std::string &Filename, int &fd) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +000078 SmallString<128> Path;
Rafael Espindola1ec4a862013-07-05 20:00:06 +000079 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
80 llvm::sys::path::extension(Filename), fd,
81 Path);
82 return Path.str();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000083 }
84};
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000085} // end anonymous namespace
Daniel Dunbar9b414d32010-06-15 17:48:49 +000086
87bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000088 StringRef Filename) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000089 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
90 if (!FEOpts.FixItSuffix.empty()) {
Nick Lewycky1450f262010-08-13 17:31:00 +000091 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
92 FEOpts.FixWhatYouCan));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000093 } else {
Nick Lewycky96872c42010-08-15 16:47:39 +000094 FixItOpts.reset(new FixItRewriteInPlace);
95 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
Daniel Dunbar9b414d32010-06-15 17:48:49 +000096 }
97 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewycky1450f262010-08-13 17:31:00 +000098 CI.getLangOpts(), FixItOpts.get()));
Daniel Dunbar9b414d32010-06-15 17:48:49 +000099 return true;
100}
101
102void FixItAction::EndSourceFileAction() {
103 // Otherwise rewrite all files.
104 Rewriter->WriteFixedFiles();
105}
106
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000107bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
108
109 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
110 bool err = false;
111 {
112 const FrontendOptions &FEOpts = CI.getFrontendOpts();
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000113 OwningPtr<FrontendAction> FixAction(new SyntaxOnlyAction());
Argyrios Kyrtzidis30ae84c2012-01-27 01:00:47 +0000114 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000115 OwningPtr<FixItOptions> FixItOpts;
Argyrios Kyrtzidis30ae84c2012-01-27 01:00:47 +0000116 if (FEOpts.FixToTemporaries)
117 FixItOpts.reset(new FixItRewriteToTemp());
118 else
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();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000126
Argyrios Kyrtzidis30ae84c2012-01-27 01:00:47 +0000127 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
128
129 FixAction->EndSourceFile();
130 CI.setSourceManager(0);
131 CI.setFileManager(0);
132 } else {
133 err = true;
134 }
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000135 }
136 if (err)
137 return false;
138 CI.getDiagnosticClient().clear();
Argyrios Kyrtzidis97a9cf32012-01-27 06:15:37 +0000139 CI.getDiagnostics().Reset();
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +0000140
141 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
142 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
143 RewrittenFiles.begin(), RewrittenFiles.end());
144 PPOpts.RemappedFilesKeepOriginalName = false;
145
146 return true;
147}
148
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000149//===----------------------------------------------------------------------===//
150// Preprocessor Actions
151//===----------------------------------------------------------------------===//
152
153ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154 StringRef InFile) {
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000155 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
John McCall260611a2012-06-20 06:18:46 +0000156 if (CI.getLangOpts().ObjCRuntime.isNonFragile())
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000157 return CreateModernObjCRewriter(InFile, OS,
158 CI.getDiagnostics(), CI.getLangOpts(),
Fariborz Jahanianada71912013-02-08 00:27:34 +0000159 CI.getDiagnosticOpts().NoRewriteMacros,
160 (CI.getCodeGenOpts().getDebugInfo() !=
161 CodeGenOptions::NoDebugInfo));
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000162 return CreateObjCRewriter(InFile, OS,
163 CI.getDiagnostics(), CI.getLangOpts(),
164 CI.getDiagnosticOpts().NoRewriteMacros);
Fariborz Jahanian64cb63a2012-02-07 17:11:38 +0000165 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000166 return 0;
167}
168
169void RewriteMacrosAction::ExecuteAction() {
170 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000172 if (!OS) return;
173
174 RewriteMacrosInInput(CI.getPreprocessor(), OS);
175}
176
177void RewriteTestAction::ExecuteAction() {
178 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000179 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000180 if (!OS) return;
181
182 DoRewriteTest(CI.getPreprocessor(), OS);
183}
David Blaikie8c0b3782012-06-06 18:52:13 +0000184
185void RewriteIncludesAction::ExecuteAction() {
186 CompilerInstance &CI = getCompilerInstance();
187 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
188 if (!OS) return;
189
190 RewriteIncludesInInput(CI.getPreprocessor(), OS,
191 CI.getPreprocessorOutputOpts());
192}