xref: /minix3/external/bsd/llvm/dist/clang/lib/Frontend/Rewrite/FrontendActions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===--- FrontendActions.cpp ----------------------------------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*0a6a1f1dSLionel Sambuc // License. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Frontend/FrontendActions.h"
11*0a6a1f1dSLionel Sambuc #include "clang/AST/ASTConsumer.h"
12*0a6a1f1dSLionel Sambuc #include "clang/Basic/FileManager.h"
13*0a6a1f1dSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
14*0a6a1f1dSLionel Sambuc #include "clang/Frontend/FrontendActions.h"
15*0a6a1f1dSLionel Sambuc #include "clang/Frontend/FrontendDiagnostic.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Frontend/Utils.h"
17*0a6a1f1dSLionel Sambuc #include "clang/Lex/Preprocessor.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Parse/Parser.h"
19*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Frontend/ASTConsumers.h"
20*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Frontend/FixItRewriter.h"
21*0a6a1f1dSLionel Sambuc #include "clang/Rewrite/Frontend/Rewriters.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/FileSystem.h"
23*0a6a1f1dSLionel Sambuc #include "llvm/Support/Path.h"
24*0a6a1f1dSLionel Sambuc #include "llvm/Support/raw_ostream.h"
25*0a6a1f1dSLionel Sambuc #include <memory>
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc using namespace clang;
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
30*0a6a1f1dSLionel Sambuc // AST Consumer Actions
31*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
32*0a6a1f1dSLionel Sambuc 
33*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)34*0a6a1f1dSLionel Sambuc HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
35*0a6a1f1dSLionel Sambuc   if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
36*0a6a1f1dSLionel Sambuc     return CreateHTMLPrinter(OS, CI.getPreprocessor());
37*0a6a1f1dSLionel Sambuc   return nullptr;
38*0a6a1f1dSLionel Sambuc }
39*0a6a1f1dSLionel Sambuc 
FixItAction()40*0a6a1f1dSLionel Sambuc FixItAction::FixItAction() {}
~FixItAction()41*0a6a1f1dSLionel Sambuc FixItAction::~FixItAction() {}
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)44*0a6a1f1dSLionel Sambuc FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
45*0a6a1f1dSLionel Sambuc   return llvm::make_unique<ASTConsumer>();
46*0a6a1f1dSLionel Sambuc }
47*0a6a1f1dSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc namespace {
49*0a6a1f1dSLionel Sambuc class FixItRewriteInPlace : public FixItOptions {
50*0a6a1f1dSLionel Sambuc public:
RewriteFilename(const std::string & Filename,int & fd)51*0a6a1f1dSLionel Sambuc   std::string RewriteFilename(const std::string &Filename, int &fd) override {
52*0a6a1f1dSLionel Sambuc     fd = -1;
53*0a6a1f1dSLionel Sambuc     return Filename;
54*0a6a1f1dSLionel Sambuc   }
55*0a6a1f1dSLionel Sambuc };
56*0a6a1f1dSLionel Sambuc 
57*0a6a1f1dSLionel Sambuc class FixItActionSuffixInserter : public FixItOptions {
58*0a6a1f1dSLionel Sambuc   std::string NewSuffix;
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc public:
FixItActionSuffixInserter(std::string NewSuffix,bool FixWhatYouCan)61*0a6a1f1dSLionel Sambuc   FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
62*0a6a1f1dSLionel Sambuc     : NewSuffix(NewSuffix) {
63*0a6a1f1dSLionel Sambuc       this->FixWhatYouCan = FixWhatYouCan;
64*0a6a1f1dSLionel Sambuc   }
65*0a6a1f1dSLionel Sambuc 
RewriteFilename(const std::string & Filename,int & fd)66*0a6a1f1dSLionel Sambuc   std::string RewriteFilename(const std::string &Filename, int &fd) override {
67*0a6a1f1dSLionel Sambuc     fd = -1;
68*0a6a1f1dSLionel Sambuc     SmallString<128> Path(Filename);
69*0a6a1f1dSLionel Sambuc     llvm::sys::path::replace_extension(Path,
70*0a6a1f1dSLionel Sambuc       NewSuffix + llvm::sys::path::extension(Path));
71*0a6a1f1dSLionel Sambuc     return Path.str();
72*0a6a1f1dSLionel Sambuc   }
73*0a6a1f1dSLionel Sambuc };
74*0a6a1f1dSLionel Sambuc 
75*0a6a1f1dSLionel Sambuc class FixItRewriteToTemp : public FixItOptions {
76*0a6a1f1dSLionel Sambuc public:
RewriteFilename(const std::string & Filename,int & fd)77*0a6a1f1dSLionel Sambuc   std::string RewriteFilename(const std::string &Filename, int &fd) override {
78*0a6a1f1dSLionel Sambuc     SmallString<128> Path;
79*0a6a1f1dSLionel Sambuc     llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
80*0a6a1f1dSLionel Sambuc                                        llvm::sys::path::extension(Filename), fd,
81*0a6a1f1dSLionel Sambuc                                        Path);
82*0a6a1f1dSLionel Sambuc     return Path.str();
83*0a6a1f1dSLionel Sambuc   }
84*0a6a1f1dSLionel Sambuc };
85*0a6a1f1dSLionel Sambuc } // end anonymous namespace
86*0a6a1f1dSLionel Sambuc 
BeginSourceFileAction(CompilerInstance & CI,StringRef Filename)87*0a6a1f1dSLionel Sambuc bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
88*0a6a1f1dSLionel Sambuc                                         StringRef Filename) {
89*0a6a1f1dSLionel Sambuc   const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
90*0a6a1f1dSLionel Sambuc   if (!FEOpts.FixItSuffix.empty()) {
91*0a6a1f1dSLionel Sambuc     FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
92*0a6a1f1dSLionel Sambuc                                                   FEOpts.FixWhatYouCan));
93*0a6a1f1dSLionel Sambuc   } else {
94*0a6a1f1dSLionel Sambuc     FixItOpts.reset(new FixItRewriteInPlace);
95*0a6a1f1dSLionel Sambuc     FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
96*0a6a1f1dSLionel Sambuc   }
97*0a6a1f1dSLionel Sambuc   Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
98*0a6a1f1dSLionel Sambuc                                    CI.getLangOpts(), FixItOpts.get()));
99*0a6a1f1dSLionel Sambuc   return true;
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc 
EndSourceFileAction()102*0a6a1f1dSLionel Sambuc void FixItAction::EndSourceFileAction() {
103*0a6a1f1dSLionel Sambuc   // Otherwise rewrite all files.
104*0a6a1f1dSLionel Sambuc   Rewriter->WriteFixedFiles();
105*0a6a1f1dSLionel Sambuc }
106*0a6a1f1dSLionel Sambuc 
BeginInvocation(CompilerInstance & CI)107*0a6a1f1dSLionel Sambuc bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
108*0a6a1f1dSLionel Sambuc 
109*0a6a1f1dSLionel Sambuc   std::vector<std::pair<std::string, std::string> > RewrittenFiles;
110*0a6a1f1dSLionel Sambuc   bool err = false;
111*0a6a1f1dSLionel Sambuc   {
112*0a6a1f1dSLionel Sambuc     const FrontendOptions &FEOpts = CI.getFrontendOpts();
113*0a6a1f1dSLionel Sambuc     std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
114*0a6a1f1dSLionel Sambuc     if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
115*0a6a1f1dSLionel Sambuc       std::unique_ptr<FixItOptions> FixItOpts;
116*0a6a1f1dSLionel Sambuc       if (FEOpts.FixToTemporaries)
117*0a6a1f1dSLionel Sambuc         FixItOpts.reset(new FixItRewriteToTemp());
118*0a6a1f1dSLionel Sambuc       else
119*0a6a1f1dSLionel Sambuc         FixItOpts.reset(new FixItRewriteInPlace());
120*0a6a1f1dSLionel Sambuc       FixItOpts->Silent = true;
121*0a6a1f1dSLionel Sambuc       FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
122*0a6a1f1dSLionel Sambuc       FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
123*0a6a1f1dSLionel Sambuc       FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
124*0a6a1f1dSLionel Sambuc                              CI.getLangOpts(), FixItOpts.get());
125*0a6a1f1dSLionel Sambuc       FixAction->Execute();
126*0a6a1f1dSLionel Sambuc 
127*0a6a1f1dSLionel Sambuc       err = Rewriter.WriteFixedFiles(&RewrittenFiles);
128*0a6a1f1dSLionel Sambuc 
129*0a6a1f1dSLionel Sambuc       FixAction->EndSourceFile();
130*0a6a1f1dSLionel Sambuc       CI.setSourceManager(nullptr);
131*0a6a1f1dSLionel Sambuc       CI.setFileManager(nullptr);
132*0a6a1f1dSLionel Sambuc     } else {
133*0a6a1f1dSLionel Sambuc       err = true;
134*0a6a1f1dSLionel Sambuc     }
135*0a6a1f1dSLionel Sambuc   }
136*0a6a1f1dSLionel Sambuc   if (err)
137*0a6a1f1dSLionel Sambuc     return false;
138*0a6a1f1dSLionel Sambuc   CI.getDiagnosticClient().clear();
139*0a6a1f1dSLionel Sambuc   CI.getDiagnostics().Reset();
140*0a6a1f1dSLionel Sambuc 
141*0a6a1f1dSLionel Sambuc   PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
142*0a6a1f1dSLionel Sambuc   PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
143*0a6a1f1dSLionel Sambuc                               RewrittenFiles.begin(), RewrittenFiles.end());
144*0a6a1f1dSLionel Sambuc   PPOpts.RemappedFilesKeepOriginalName = false;
145*0a6a1f1dSLionel Sambuc 
146*0a6a1f1dSLionel Sambuc   return true;
147*0a6a1f1dSLionel Sambuc }
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc #ifdef CLANG_ENABLE_OBJC_REWRITER
150*0a6a1f1dSLionel Sambuc 
151*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)152*0a6a1f1dSLionel Sambuc RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
153*0a6a1f1dSLionel Sambuc   if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) {
154*0a6a1f1dSLionel Sambuc     if (CI.getLangOpts().ObjCRuntime.isNonFragile())
155*0a6a1f1dSLionel Sambuc       return CreateModernObjCRewriter(InFile, OS,
156*0a6a1f1dSLionel Sambuc                                 CI.getDiagnostics(), CI.getLangOpts(),
157*0a6a1f1dSLionel Sambuc                                 CI.getDiagnosticOpts().NoRewriteMacros,
158*0a6a1f1dSLionel Sambuc                                 (CI.getCodeGenOpts().getDebugInfo() !=
159*0a6a1f1dSLionel Sambuc                                  CodeGenOptions::NoDebugInfo));
160*0a6a1f1dSLionel Sambuc     return CreateObjCRewriter(InFile, OS,
161*0a6a1f1dSLionel Sambuc                               CI.getDiagnostics(), CI.getLangOpts(),
162*0a6a1f1dSLionel Sambuc                               CI.getDiagnosticOpts().NoRewriteMacros);
163*0a6a1f1dSLionel Sambuc   }
164*0a6a1f1dSLionel Sambuc   return nullptr;
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc 
167*0a6a1f1dSLionel Sambuc #endif
168*0a6a1f1dSLionel Sambuc 
169*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
170*0a6a1f1dSLionel Sambuc // Preprocessor Actions
171*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
172*0a6a1f1dSLionel Sambuc 
ExecuteAction()173*0a6a1f1dSLionel Sambuc void RewriteMacrosAction::ExecuteAction() {
174*0a6a1f1dSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
175*0a6a1f1dSLionel Sambuc   raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
176*0a6a1f1dSLionel Sambuc   if (!OS) return;
177*0a6a1f1dSLionel Sambuc 
178*0a6a1f1dSLionel Sambuc   RewriteMacrosInInput(CI.getPreprocessor(), OS);
179*0a6a1f1dSLionel Sambuc }
180*0a6a1f1dSLionel Sambuc 
ExecuteAction()181*0a6a1f1dSLionel Sambuc void RewriteTestAction::ExecuteAction() {
182*0a6a1f1dSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
183*0a6a1f1dSLionel Sambuc   raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
184*0a6a1f1dSLionel Sambuc   if (!OS) return;
185*0a6a1f1dSLionel Sambuc 
186*0a6a1f1dSLionel Sambuc   DoRewriteTest(CI.getPreprocessor(), OS);
187*0a6a1f1dSLionel Sambuc }
188*0a6a1f1dSLionel Sambuc 
ExecuteAction()189*0a6a1f1dSLionel Sambuc void RewriteIncludesAction::ExecuteAction() {
190*0a6a1f1dSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
191*0a6a1f1dSLionel Sambuc   raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
192*0a6a1f1dSLionel Sambuc   if (!OS) return;
193*0a6a1f1dSLionel Sambuc 
194*0a6a1f1dSLionel Sambuc   RewriteIncludesInInput(CI.getPreprocessor(), OS,
195*0a6a1f1dSLionel Sambuc                          CI.getPreprocessorOutputOpts());
196*0a6a1f1dSLionel Sambuc }
197