blob: c1568cd522340a03be4eaaaddb2379154dc393d7 [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
10#include "clang/Rewrite/FrontendActions.h"
11#include "clang/AST/ASTConsumer.h"
12#include "clang/Lex/Preprocessor.h"
13#include "clang/Parse/Parser.h"
14#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000015#include "clang/Frontend/FrontendActions.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
18#include "clang/Frontend/Utils.h"
19#include "clang/Rewrite/ASTConsumers.h"
20#include "clang/Rewrite/FixItRewriter.h"
21#include "clang/Rewrite/Rewriters.h"
22#include "llvm/ADT/OwningPtr.h"
23#include "llvm/Support/raw_ostream.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000024#include "llvm/Support/Path.h"
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000025#include "llvm/Support/FileSystem.h"
Daniel Dunbar9b414d32010-06-15 17:48:49 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// AST Consumer Actions
30//===----------------------------------------------------------------------===//
31
32ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000033 StringRef InFile) {
34 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar9b414d32010-06-15 17:48:49 +000035 return CreateHTMLPrinter(OS, CI.getPreprocessor());
36 return 0;
37}
38
39FixItAction::FixItAction() {}
40FixItAction::~FixItAction() {}
41
42ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000043 StringRef InFile) {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000044 return new ASTConsumer();
45}
46
Benjamin Kramer79ba2a62010-10-22 16:48:22 +000047namespace {
Nick Lewycky96872c42010-08-15 16:47:39 +000048class FixItRewriteInPlace : public FixItOptions {
49public:
50 std::string RewriteFilename(const std::string &Filename) { return Filename; }
51};
52
Nick Lewycky1450f262010-08-13 17:31:00 +000053class FixItActionSuffixInserter : public FixItOptions {
Daniel Dunbar9b414d32010-06-15 17:48:49 +000054 std::string NewSuffix;
55
56public:
Nick Lewycky1450f262010-08-13 17:31:00 +000057 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
58 : NewSuffix(NewSuffix) {
59 this->FixWhatYouCan = FixWhatYouCan;
60 }
Daniel Dunbar9b414d32010-06-15 17:48:49 +000061
62 std::string RewriteFilename(const std::string &Filename) {
Michael J. Spencerd5b08be2010-12-18 04:13:32 +000063 llvm::SmallString<128> Path(Filename);
64 llvm::sys::path::replace_extension(Path,
65 NewSuffix + llvm::sys::path::extension(Path));
66 return Path.str();
Daniel Dunbar9b414d32010-06-15 17:48:49 +000067 }
68};
Argyrios Kyrtzidis61d679a2012-01-26 02:40:48 +000069
70class FixItRewriteToTemp : public FixItOptions {
71public:
72 std::string RewriteFilename(const std::string &Filename) {
73 llvm::SmallString<128> Path;
74 Path = llvm::sys::path::filename(Filename);
75 Path += "-%%%%%%%%";
76 Path += llvm::sys::path::extension(Filename);
77 int fd;
78 llvm::SmallString<128> NewPath;
79 if (llvm::sys::fs::unique_file(Path.str(), fd, NewPath)
80 == llvm::errc::success)
81 ::close(fd);
82 return NewPath.str();
83 }
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();
113 llvm::OwningPtr<FrontendAction> FixAction(new SyntaxOnlyAction());
114 FixAction->BeginSourceFile(CI, FEOpts.Inputs[0]);
115
116 llvm::OwningPtr<FixItOptions> FixItOpts;
117 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();
127
128 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
129
130 FixAction->EndSourceFile();
131 CI.setSourceManager(0);
132 CI.setFileManager(0);
133 }
134 if (err)
135 return false;
136 CI.getDiagnosticClient().clear();
137
138 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
139 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
140 RewrittenFiles.begin(), RewrittenFiles.end());
141 PPOpts.RemappedFilesKeepOriginalName = false;
142
143 return true;
144}
145
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000146//===----------------------------------------------------------------------===//
147// Preprocessor Actions
148//===----------------------------------------------------------------------===//
149
150ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000151 StringRef InFile) {
152 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000153 return CreateObjCRewriter(InFile, OS,
154 CI.getDiagnostics(), CI.getLangOpts(),
155 CI.getDiagnosticOpts().NoRewriteMacros);
156 return 0;
157}
158
159void RewriteMacrosAction::ExecuteAction() {
160 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000161 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000162 if (!OS) return;
163
164 RewriteMacrosInInput(CI.getPreprocessor(), OS);
165}
166
167void RewriteTestAction::ExecuteAction() {
168 CompilerInstance &CI = getCompilerInstance();
Chris Lattner5f9e2722011-07-23 10:55:15 +0000169 raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar9b414d32010-06-15 17:48:49 +0000170 if (!OS) return;
171
172 DoRewriteTest(CI.getPreprocessor(), OS);
173}