blob: 6cd960be20d50f2f53ee1fda7ee9645c16337987 [file] [log] [blame]
Daniel Dunbar8305d012009-11-14 10:42:46 +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/Frontend/FrontendActions.h"
11#include "clang/AST/ASTConsumer.h"
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000012#include "clang/Lex/Preprocessor.h"
13#include "clang/Parse/Parser.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000014#include "clang/Basic/FileManager.h"
15#include "clang/Frontend/AnalysisConsumer.h"
16#include "clang/Frontend/ASTConsumers.h"
17#include "clang/Frontend/ASTUnit.h"
18#include "clang/Frontend/CompilerInstance.h"
19#include "clang/Frontend/FixItRewriter.h"
20#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Frontend/Utils.h"
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000022#include "llvm/ADT/OwningPtr.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000023#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000026//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000027// Custom Actions
28//===----------------------------------------------------------------------===//
29
30ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
31 llvm::StringRef InFile) {
32 return new ASTConsumer();
33}
34
35void InitOnlyAction::ExecuteAction() {
36}
37
38//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000039// AST Consumer Actions
40//===----------------------------------------------------------------------===//
41
Daniel Dunbar8305d012009-11-14 10:42:46 +000042ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
43 llvm::StringRef InFile) {
44 return CreateAnalysisConsumer(CI.getPreprocessor(),
45 CI.getFrontendOpts().OutputFile,
46 CI.getAnalyzerOpts());
47}
48
49ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
50 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000051 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
52 return CreateASTPrinter(OS);
53 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000054}
55
56ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
57 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000058 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
59 return CreateASTPrinterXML(OS);
60 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000061}
62
63ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
64 llvm::StringRef InFile) {
65 return CreateASTDumper();
66}
67
68ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
69 llvm::StringRef InFile) {
70 return CreateASTViewer();
71}
72
73ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
74 llvm::StringRef InFile) {
75 return CreateDeclContextPrinter();
76}
77
Daniel Dunbar8305d012009-11-14 10:42:46 +000078ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
79 llvm::StringRef InFile) {
80 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
81 if (CI.getFrontendOpts().RelocatablePCH &&
82 Sysroot.empty()) {
83 CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
84 return 0;
85 }
86
87 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
Daniel Dunbar36043592009-12-03 09:13:30 +000088 if (!OS)
89 return 0;
90
Daniel Dunbar8305d012009-11-14 10:42:46 +000091 if (CI.getFrontendOpts().RelocatablePCH)
92 return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
93
94 return CreatePCHGenerator(CI.getPreprocessor(), OS);
95}
96
97ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
98 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000099 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
100 return CreateHTMLPrinter(OS, CI.getPreprocessor());
101 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000102}
103
104ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
105 llvm::StringRef InFile) {
106 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
107}
108
109FixItAction::FixItAction() {}
110FixItAction::~FixItAction() {}
111
112ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000113 llvm::StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000114 return new ASTConsumer();
115}
116
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000117class FixItActionSuffixInserter : public FixItPathRewriter {
118 std::string NewSuffix;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000119
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000120public:
121 explicit FixItActionSuffixInserter(std::string NewSuffix)
122 : NewSuffix(NewSuffix) {}
123
124 std::string RewriteFilename(const std::string &Filename) {
125 llvm::sys::Path Path(Filename);
126 std::string Suffix = Path.getSuffix();
127 Path.eraseSuffix();
128 Path.appendSuffix(NewSuffix + "." + Suffix);
129 return Path.c_str();
Daniel Dunbar8305d012009-11-14 10:42:46 +0000130 }
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000131};
Daniel Dunbar8305d012009-11-14 10:42:46 +0000132
133bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
134 llvm::StringRef Filename) {
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000135 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
136 if (!FEOpts.FixItSuffix.empty()) {
137 PathRewriter.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix));
138 } else {
139 PathRewriter.reset();
140 }
Daniel Dunbar8305d012009-11-14 10:42:46 +0000141 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +0000142 CI.getLangOpts(), PathRewriter.get()));
Daniel Dunbar8305d012009-11-14 10:42:46 +0000143 return true;
144}
145
146void FixItAction::EndSourceFileAction() {
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000147 // Otherwise rewrite all files.
148 Rewriter->WriteFixedFiles();
Daniel Dunbar8305d012009-11-14 10:42:46 +0000149}
150
151ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
152 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000153 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
154 return CreateObjCRewriter(InFile, OS,
155 CI.getDiagnostics(), CI.getLangOpts(),
156 CI.getDiagnosticOpts().NoRewriteMacros);
157 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000158}
159
Daniel Dunbar8305d012009-11-14 10:42:46 +0000160ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
161 llvm::StringRef InFile) {
162 return new ASTConsumer();
163}
164
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000165//===----------------------------------------------------------------------===//
166// Preprocessor Actions
167//===----------------------------------------------------------------------===//
168
169void DumpRawTokensAction::ExecuteAction() {
170 Preprocessor &PP = getCompilerInstance().getPreprocessor();
171 SourceManager &SM = PP.getSourceManager();
172
173 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000174 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
175 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000176 RawLex.SetKeepWhitespaceMode(true);
177
178 Token RawTok;
179 RawLex.LexFromRawLexer(RawTok);
180 while (RawTok.isNot(tok::eof)) {
181 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000182 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000183 RawLex.LexFromRawLexer(RawTok);
184 }
185}
186
187void DumpTokensAction::ExecuteAction() {
188 Preprocessor &PP = getCompilerInstance().getPreprocessor();
189 // Start preprocessing the specified input file.
190 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000191 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000192 do {
193 PP.Lex(Tok);
194 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000195 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000196 } while (Tok.isNot(tok::eof));
197}
198
199void GeneratePTHAction::ExecuteAction() {
200 CompilerInstance &CI = getCompilerInstance();
201 if (CI.getFrontendOpts().OutputFile.empty() ||
202 CI.getFrontendOpts().OutputFile == "-") {
203 // FIXME: Don't fail this way.
204 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000205 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000206 }
207 llvm::raw_fd_ostream *OS =
208 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000209 if (!OS) return;
210
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000211 CacheTokens(CI.getPreprocessor(), OS);
212}
213
214void ParseOnlyAction::ExecuteAction() {
215 Preprocessor &PP = getCompilerInstance().getPreprocessor();
216 llvm::OwningPtr<Action> PA(new MinimalAction(PP));
217
218 Parser P(PP, *PA);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000219 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000220 P.ParseTranslationUnit();
221}
222
223void PreprocessOnlyAction::ExecuteAction() {
224 Preprocessor &PP = getCompilerInstance().getPreprocessor();
225
226 Token Tok;
227 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000228 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000229 do {
230 PP.Lex(Tok);
231 } while (Tok.isNot(tok::eof));
232}
233
234void PrintParseAction::ExecuteAction() {
235 CompilerInstance &CI = getCompilerInstance();
236 Preprocessor &PP = getCompilerInstance().getPreprocessor();
237 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000238 if (!OS) return;
239
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000240 llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
241
242 Parser P(PP, *PA);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000243 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000244 P.ParseTranslationUnit();
245}
246
247void PrintPreprocessedAction::ExecuteAction() {
248 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000249 // Output file needs to be set to 'Binary', to avoid converting Unix style
250 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
251 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000252 if (!OS) return;
253
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000254 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
255 CI.getPreprocessorOutputOpts());
256}
257
258void RewriteMacrosAction::ExecuteAction() {
259 CompilerInstance &CI = getCompilerInstance();
260 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000261 if (!OS) return;
262
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000263 RewriteMacrosInInput(CI.getPreprocessor(), OS);
264}
265
266void RewriteTestAction::ExecuteAction() {
267 CompilerInstance &CI = getCompilerInstance();
268 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000269 if (!OS) return;
270
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000271 DoRewriteTest(CI.getPreprocessor(), OS);
272}