blob: ce3e841b904a658430ef9b5b8cdac63ec60cb248 [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
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000137 const std::string &OutputFile = CI.getFrontendOpts().OutputFile;
138 if (Locs.empty() && !OutputFile.empty()) {
139 // FIXME: we will issue "FIX-IT applied suggested code changes" for every
140 // input, but only the main file will actually be rewritten.
141 const std::vector<std::pair<FrontendOptions::InputKind, std::string> > &Inputs =
142 CI.getFrontendOpts().Inputs;
143 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
144 const FileEntry *File = CI.getFileManager().getFile(Inputs[i].second);
145 assert(File && "Input file not found in FileManager");
146 RequestedSourceLocation Requested;
147 Requested.File = File;
148 Requested.Line = 0;
149 Requested.Column = 0;
150 FixItRewrite.addFixItLocation(Requested);
151 }
152 }
153
Daniel Dunbar8305d012009-11-14 10:42:46 +0000154 return true;
155}
156
157bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
158 llvm::StringRef Filename) {
159 Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
160 CI.getLangOpts()));
161 if (!AddFixItLocations(CI, *Rewriter))
162 return false;
163
164 return true;
165}
166
167void FixItAction::EndSourceFileAction() {
168 const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
Nick Lewyckyd4a97a12010-04-15 06:46:58 +0000169 if (!FEOpts.OutputFile.empty()) {
170 // When called with 'clang -fixit -o filename' output only the main file.
171
172 const SourceManager &SM = getCompilerInstance().getSourceManager();
173 FileID MainFileID = SM.getMainFileID();
174 if (!Rewriter->IsModified(MainFileID)) {
175 getCompilerInstance().getDiagnostics().Report(
176 diag::note_fixit_main_file_unchanged);
177 return;
178 }
179
180 llvm::OwningPtr<llvm::raw_ostream> OwnedStream;
181 llvm::raw_ostream *OutFile;
182 if (FEOpts.OutputFile == "-") {
183 OutFile = &llvm::outs();
184 } else {
185 std::string Err;
186 OutFile = new llvm::raw_fd_ostream(FEOpts.OutputFile.c_str(), Err,
187 llvm::raw_fd_ostream::F_Binary);
188 OwnedStream.reset(OutFile);
189 }
190
191 Rewriter->WriteFixedFile(MainFileID, *OutFile);
192 return;
193 }
194
195 // Otherwise rewrite all files.
196 Rewriter->WriteFixedFiles();
Daniel Dunbar8305d012009-11-14 10:42:46 +0000197}
198
199ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
200 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +0000201 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp"))
202 return CreateObjCRewriter(InFile, OS,
203 CI.getDiagnostics(), CI.getLangOpts(),
204 CI.getDiagnosticOpts().NoRewriteMacros);
205 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000206}
207
Daniel Dunbar8305d012009-11-14 10:42:46 +0000208ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
209 llvm::StringRef InFile) {
210 return new ASTConsumer();
211}
212
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000213//===----------------------------------------------------------------------===//
214// Preprocessor Actions
215//===----------------------------------------------------------------------===//
216
217void DumpRawTokensAction::ExecuteAction() {
218 Preprocessor &PP = getCompilerInstance().getPreprocessor();
219 SourceManager &SM = PP.getSourceManager();
220
221 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000222 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
223 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000224 RawLex.SetKeepWhitespaceMode(true);
225
226 Token RawTok;
227 RawLex.LexFromRawLexer(RawTok);
228 while (RawTok.isNot(tok::eof)) {
229 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000230 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000231 RawLex.LexFromRawLexer(RawTok);
232 }
233}
234
235void DumpTokensAction::ExecuteAction() {
236 Preprocessor &PP = getCompilerInstance().getPreprocessor();
237 // Start preprocessing the specified input file.
238 Token Tok;
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000239 if (PP.EnterMainSourceFile())
240 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000241 do {
242 PP.Lex(Tok);
243 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000244 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000245 } while (Tok.isNot(tok::eof));
246}
247
248void GeneratePTHAction::ExecuteAction() {
249 CompilerInstance &CI = getCompilerInstance();
250 if (CI.getFrontendOpts().OutputFile.empty() ||
251 CI.getFrontendOpts().OutputFile == "-") {
252 // FIXME: Don't fail this way.
253 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000254 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000255 }
256 llvm::raw_fd_ostream *OS =
257 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000258 if (!OS) return;
259
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000260 CacheTokens(CI.getPreprocessor(), OS);
261}
262
263void ParseOnlyAction::ExecuteAction() {
264 Preprocessor &PP = getCompilerInstance().getPreprocessor();
265 llvm::OwningPtr<Action> PA(new MinimalAction(PP));
266
267 Parser P(PP, *PA);
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000268 if (PP.EnterMainSourceFile())
269 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000270 P.ParseTranslationUnit();
271}
272
273void PreprocessOnlyAction::ExecuteAction() {
274 Preprocessor &PP = getCompilerInstance().getPreprocessor();
275
276 Token Tok;
277 // Start parsing the specified input file.
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000278 if (PP.EnterMainSourceFile())
279 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000280 do {
281 PP.Lex(Tok);
282 } while (Tok.isNot(tok::eof));
283}
284
285void PrintParseAction::ExecuteAction() {
286 CompilerInstance &CI = getCompilerInstance();
287 Preprocessor &PP = getCompilerInstance().getPreprocessor();
288 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000289 if (!OS) return;
290
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000291 llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
292
293 Parser P(PP, *PA);
Douglas Gregordbf8ee62010-03-17 15:44:30 +0000294 if (PP.EnterMainSourceFile())
295 return;
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000296 P.ParseTranslationUnit();
297}
298
299void PrintPreprocessedAction::ExecuteAction() {
300 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000301 // Output file needs to be set to 'Binary', to avoid converting Unix style
302 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
303 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000304 if (!OS) return;
305
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000306 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
307 CI.getPreprocessorOutputOpts());
308}
309
310void RewriteMacrosAction::ExecuteAction() {
311 CompilerInstance &CI = getCompilerInstance();
312 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000313 if (!OS) return;
314
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000315 RewriteMacrosInInput(CI.getPreprocessor(), OS);
316}
317
318void RewriteTestAction::ExecuteAction() {
319 CompilerInstance &CI = getCompilerInstance();
320 llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000321 if (!OS) return;
322
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000323 DoRewriteTest(CI.getPreprocessor(), OS);
324}