blob: 6d7c6a8460bff108cf0da793c6f593e8ea553a92 [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"
22#include "llvm/Support/raw_ostream.h"
23using namespace clang;
24
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000025//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000026// Custom Actions
27//===----------------------------------------------------------------------===//
28
29ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
30 llvm::StringRef InFile) {
31 return new ASTConsumer();
32}
33
34void InitOnlyAction::ExecuteAction() {
35}
36
37//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000038// AST Consumer Actions
39//===----------------------------------------------------------------------===//
40
Daniel Dunbar8305d012009-11-14 10:42:46 +000041ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
42 llvm::StringRef InFile) {
43 return CreateAnalysisConsumer(CI.getPreprocessor(),
44 CI.getFrontendOpts().OutputFile,
45 CI.getAnalyzerOpts());
46}
47
48ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
49 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000050 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
51 return CreateASTPrinter(OS);
52 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000053}
54
55ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
56 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000057 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
58 return CreateASTPrinterXML(OS);
59 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000060}
61
62ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
63 llvm::StringRef InFile) {
64 return CreateASTDumper();
65}
66
67ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
68 llvm::StringRef InFile) {
69 return CreateASTViewer();
70}
71
72ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
73 llvm::StringRef InFile) {
74 return CreateDeclContextPrinter();
75}
76
Daniel Dunbar8305d012009-11-14 10:42:46 +000077ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
78 llvm::StringRef InFile) {
79 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
80 if (CI.getFrontendOpts().RelocatablePCH &&
81 Sysroot.empty()) {
82 CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
83 return 0;
84 }
85
86 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
Daniel Dunbar36043592009-12-03 09:13:30 +000087 if (!OS)
88 return 0;
89
Daniel Dunbar8305d012009-11-14 10:42:46 +000090 if (CI.getFrontendOpts().RelocatablePCH)
91 return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
92
93 return CreatePCHGenerator(CI.getPreprocessor(), OS);
94}
95
96ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
97 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000098 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
99 return CreateHTMLPrinter(OS, CI.getPreprocessor());
100 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000101}
102
103ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
104 llvm::StringRef InFile) {
105 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
106}
107
108FixItAction::FixItAction() {}
109FixItAction::~FixItAction() {}
110
111ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000112 llvm::StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000113 return new ASTConsumer();
114}
115
116/// AddFixItLocations - Add any individual user specified "fix-it" locations,
117/// and return true on success.
118static bool AddFixItLocations(CompilerInstance &CI,
119 FixItRewriter &FixItRewrite) {
120 const std::vector<ParsedSourceLocation> &Locs =
121 CI.getFrontendOpts().FixItLocations;
122 for (unsigned i = 0, e = Locs.size(); i != e; ++i) {
123 const FileEntry *File = CI.getFileManager().getFile(Locs[i].FileName);
124 if (!File) {
125 CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file)
126 << Locs[i].FileName;
127 return false;
128 }
129
130 RequestedSourceLocation Requested;
131 Requested.File = File;
132 Requested.Line = Locs[i].Line;
133 Requested.Column = Locs[i].Column;
134 FixItRewrite.addFixItLocation(Requested);
135 }
136
137 return true;
138}
139
140bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
141 llvm::StringRef Filename) {
142 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
143 CI.getLangOpts()));
144 if (!AddFixItLocations(CI, *Rewriter))
145 return false;
146
147 return true;
148}
149
150void FixItAction::EndSourceFileAction() {
151 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
152 Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile);
153}
154
155ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
156 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000157 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
158 return CreateObjCRewriter(InFile, OS,
159 CI.getDiagnostics(), CI.getLangOpts(),
160 CI.getDiagnosticOpts().NoRewriteMacros);
161 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000162}
163
Daniel Dunbar8305d012009-11-14 10:42:46 +0000164ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
165 llvm::StringRef InFile) {
166 return new ASTConsumer();
167}
168
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000169//===----------------------------------------------------------------------===//
170// Preprocessor Actions
171//===----------------------------------------------------------------------===//
172
173void DumpRawTokensAction::ExecuteAction() {
174 Preprocessor &PP = getCompilerInstance().getPreprocessor();
175 SourceManager &SM = PP.getSourceManager();
176
177 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000178 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
179 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000180 RawLex.SetKeepWhitespaceMode(true);
181
182 Token RawTok;
183 RawLex.LexFromRawLexer(RawTok);
184 while (RawTok.isNot(tok::eof)) {
185 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000186 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000187 RawLex.LexFromRawLexer(RawTok);
188 }
189}
190
191void DumpTokensAction::ExecuteAction() {
192 Preprocessor &PP = getCompilerInstance().getPreprocessor();
193 // Start preprocessing the specified input file.
194 Token Tok;
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000195 if (PP.EnterMainSourceFile())
196 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000197 do {
198 PP.Lex(Tok);
199 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000200 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000201 } while (Tok.isNot(tok::eof));
202}
203
204void GeneratePTHAction::ExecuteAction() {
205 CompilerInstance &CI = getCompilerInstance();
206 if (CI.getFrontendOpts().OutputFile.empty() ||
207 CI.getFrontendOpts().OutputFile == "-") {
208 // FIXME: Don't fail this way.
209 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000210 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000211 }
212 llvm::raw_fd_ostream *OS =
213 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000214 if (!OS) return;
215
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000216 CacheTokens(CI.getPreprocessor(), OS);
217}
218
219void ParseOnlyAction::ExecuteAction() {
220 Preprocessor &PP = getCompilerInstance().getPreprocessor();
221 llvm::OwningPtr<Action> PA(new MinimalAction(PP));
222
223 Parser P(PP, *PA);
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000224 if (PP.EnterMainSourceFile())
225 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000226 P.ParseTranslationUnit();
227}
228
229void PreprocessOnlyAction::ExecuteAction() {
230 Preprocessor &PP = getCompilerInstance().getPreprocessor();
231
232 Token Tok;
233 // Start parsing the specified input file.
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000234 if (PP.EnterMainSourceFile())
235 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000236 do {
237 PP.Lex(Tok);
238 } while (Tok.isNot(tok::eof));
239}
240
241void PrintParseAction::ExecuteAction() {
242 CompilerInstance &CI = getCompilerInstance();
243 Preprocessor &PP = getCompilerInstance().getPreprocessor();
244 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000245 if (!OS) return;
246
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000247 llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
248
249 Parser P(PP, *PA);
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000250 if (PP.EnterMainSourceFile())
251 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000252 P.ParseTranslationUnit();
253}
254
255void PrintPreprocessedAction::ExecuteAction() {
256 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000257 // Output file needs to be set to 'Binary', to avoid converting Unix style
258 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
259 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000260 if (!OS) return;
261
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000262 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
263 CI.getPreprocessorOutputOpts());
264}
265
266void RewriteMacrosAction::ExecuteAction() {
267 CompilerInstance &CI = getCompilerInstance();
268 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000269 if (!OS) return;
270
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000271 RewriteMacrosInInput(CI.getPreprocessor(), OS);
272}
273
274void RewriteTestAction::ExecuteAction() {
275 CompilerInstance &CI = getCompilerInstance();
276 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000277 if (!OS) return;
278
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000279 DoRewriteTest(CI.getPreprocessor(), OS);
280}