FrontendActions.cpp revision d58c03f42ebb4e548c2b53fa25b1cfe02ebb9ac0
1//===--- 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"
12#include "clang/Lex/Preprocessor.h"
13#include "clang/Parse/Parser.h"
14#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
25//===----------------------------------------------------------------------===//
26// AST Consumer Actions
27//===----------------------------------------------------------------------===//
28
29ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI,
30                                               llvm::StringRef InFile) {
31  return CreateAnalysisConsumer(CI.getPreprocessor(),
32                                CI.getFrontendOpts().OutputFile,
33                                CI.getAnalyzerOpts());
34}
35
36ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
37                                               llvm::StringRef InFile) {
38  return CreateASTPrinter(CI.createDefaultOutputFile(false, InFile));
39}
40
41ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
42                                                  llvm::StringRef InFile) {
43  return CreateASTPrinterXML(CI.createDefaultOutputFile(false, InFile,
44                                                        "xml"));
45}
46
47ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
48                                              llvm::StringRef InFile) {
49  return CreateASTDumper();
50}
51
52ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
53                                              llvm::StringRef InFile) {
54  return CreateASTViewer();
55}
56
57ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
58                                                       llvm::StringRef InFile) {
59  return CreateDeclContextPrinter();
60}
61
62ASTConsumer *DumpRecordAction::CreateASTConsumer(CompilerInstance &CI,
63                                                 llvm::StringRef InFile) {
64  return CreateRecordLayoutDumper();
65}
66
67ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
68                                                  llvm::StringRef InFile) {
69  const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
70  if (CI.getFrontendOpts().RelocatablePCH &&
71      Sysroot.empty()) {
72    CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
73    return 0;
74  }
75
76  llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
77  if (CI.getFrontendOpts().RelocatablePCH)
78    return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str());
79
80  return CreatePCHGenerator(CI.getPreprocessor(), OS);
81}
82
83ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI,
84                                                llvm::StringRef InFile) {
85  return CreateHTMLPrinter(CI.createDefaultOutputFile(false, InFile),
86                           CI.getPreprocessor());
87}
88
89ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
90                                                      llvm::StringRef InFile) {
91  return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
92}
93
94FixItAction::FixItAction() {}
95FixItAction::~FixItAction() {}
96
97ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI,
98                                            llvm::StringRef InFile) {
99  return new ASTConsumer();
100}
101
102/// AddFixItLocations - Add any individual user specified "fix-it" locations,
103/// and return true on success.
104static bool AddFixItLocations(CompilerInstance &CI,
105                              FixItRewriter &FixItRewrite) {
106  const std::vector<ParsedSourceLocation> &Locs =
107    CI.getFrontendOpts().FixItLocations;
108  for (unsigned i = 0, e = Locs.size(); i != e; ++i) {
109    const FileEntry *File = CI.getFileManager().getFile(Locs[i].FileName);
110    if (!File) {
111      CI.getDiagnostics().Report(diag::err_fe_unable_to_find_fixit_file)
112        << Locs[i].FileName;
113      return false;
114    }
115
116    RequestedSourceLocation Requested;
117    Requested.File = File;
118    Requested.Line = Locs[i].Line;
119    Requested.Column = Locs[i].Column;
120    FixItRewrite.addFixItLocation(Requested);
121  }
122
123  return true;
124}
125
126bool FixItAction::BeginSourceFileAction(CompilerInstance &CI,
127                                        llvm::StringRef Filename) {
128  Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
129                                   CI.getLangOpts()));
130  if (!AddFixItLocations(CI, *Rewriter))
131    return false;
132
133  return true;
134}
135
136void FixItAction::EndSourceFileAction() {
137  const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
138  Rewriter->WriteFixedFile(getCurrentFile(), FEOpts.OutputFile);
139}
140
141ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI,
142                                                  llvm::StringRef InFile) {
143  return CreateObjCRewriter(InFile,
144                            CI.createDefaultOutputFile(true, InFile, "cpp"),
145                            CI.getDiagnostics(), CI.getLangOpts(),
146                            CI.getDiagnosticOpts().NoRewriteMacros);
147}
148
149ASTConsumer *RewriteBlocksAction::CreateASTConsumer(CompilerInstance &CI,
150                                                    llvm::StringRef InFile) {
151  return CreateBlockRewriter(InFile, CI.getDiagnostics(), CI.getLangOpts());
152}
153
154ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
155                                                 llvm::StringRef InFile) {
156  return new ASTConsumer();
157}
158
159CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {}
160
161ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI,
162                                              llvm::StringRef InFile) {
163  BackendAction BA = static_cast<BackendAction>(Act);
164  llvm::OwningPtr<llvm::raw_ostream> OS;
165  if (BA == Backend_EmitAssembly)
166    OS.reset(CI.createDefaultOutputFile(false, InFile, "s"));
167  else if (BA == Backend_EmitLL)
168    OS.reset(CI.createDefaultOutputFile(false, InFile, "ll"));
169  else if (BA == Backend_EmitBC)
170    OS.reset(CI.createDefaultOutputFile(true, InFile, "bc"));
171
172  return CreateBackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(),
173                               CI.getCodeGenOpts(), CI.getTargetOpts(), InFile,
174                               OS.take(), CI.getLLVMContext());
175}
176
177EmitAssemblyAction::EmitAssemblyAction()
178  : CodeGenAction(Backend_EmitAssembly) {}
179
180EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {}
181
182EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {}
183
184EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {}
185
186//===----------------------------------------------------------------------===//
187// Preprocessor Actions
188//===----------------------------------------------------------------------===//
189
190void DumpRawTokensAction::ExecuteAction() {
191  Preprocessor &PP = getCompilerInstance().getPreprocessor();
192  SourceManager &SM = PP.getSourceManager();
193
194  // Start lexing the specified input file.
195  Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions());
196  RawLex.SetKeepWhitespaceMode(true);
197
198  Token RawTok;
199  RawLex.LexFromRawLexer(RawTok);
200  while (RawTok.isNot(tok::eof)) {
201    PP.DumpToken(RawTok, true);
202    fprintf(stderr, "\n");
203    RawLex.LexFromRawLexer(RawTok);
204  }
205}
206
207void DumpTokensAction::ExecuteAction() {
208  Preprocessor &PP = getCompilerInstance().getPreprocessor();
209  // Start preprocessing the specified input file.
210  Token Tok;
211  PP.EnterMainSourceFile();
212  do {
213    PP.Lex(Tok);
214    PP.DumpToken(Tok, true);
215    fprintf(stderr, "\n");
216  } while (Tok.isNot(tok::eof));
217}
218
219void GeneratePTHAction::ExecuteAction() {
220  CompilerInstance &CI = getCompilerInstance();
221  if (CI.getFrontendOpts().OutputFile.empty() ||
222      CI.getFrontendOpts().OutputFile == "-") {
223    // FIXME: Don't fail this way.
224    // FIXME: Verify that we can actually seek in the given file.
225    llvm::errs() << "ERROR: PTH requires an seekable file for output!\n";
226    ::exit(1);
227  }
228  llvm::raw_fd_ostream *OS =
229    CI.createDefaultOutputFile(true, getCurrentFile());
230  CacheTokens(CI.getPreprocessor(), OS);
231}
232
233void ParseOnlyAction::ExecuteAction() {
234  Preprocessor &PP = getCompilerInstance().getPreprocessor();
235  llvm::OwningPtr<Action> PA(new MinimalAction(PP));
236
237  Parser P(PP, *PA);
238  PP.EnterMainSourceFile();
239  P.ParseTranslationUnit();
240}
241
242void PreprocessOnlyAction::ExecuteAction() {
243  Preprocessor &PP = getCompilerInstance().getPreprocessor();
244
245  Token Tok;
246  // Start parsing the specified input file.
247  PP.EnterMainSourceFile();
248  do {
249    PP.Lex(Tok);
250  } while (Tok.isNot(tok::eof));
251}
252
253void PrintParseAction::ExecuteAction() {
254  CompilerInstance &CI = getCompilerInstance();
255  Preprocessor &PP = getCompilerInstance().getPreprocessor();
256  llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
257  llvm::OwningPtr<Action> PA(CreatePrintParserActionsAction(PP, OS));
258
259  Parser P(PP, *PA);
260  PP.EnterMainSourceFile();
261  P.ParseTranslationUnit();
262}
263
264void PrintPreprocessedAction::ExecuteAction() {
265  CompilerInstance &CI = getCompilerInstance();
266  llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
267  DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
268                           CI.getPreprocessorOutputOpts());
269}
270
271void RewriteMacrosAction::ExecuteAction() {
272  CompilerInstance &CI = getCompilerInstance();
273  llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
274  RewriteMacrosInInput(CI.getPreprocessor(), OS);
275}
276
277void RewriteTestAction::ExecuteAction() {
278  CompilerInstance &CI = getCompilerInstance();
279  llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile());
280  DoRewriteTest(CI.getPreprocessor(), OS);
281}
282