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