FrontendActions.cpp revision 832d620b4ae0fc5fe28561b885b4cfc65cf5c9ab
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/Pragma.h"
13#include "clang/Lex/Preprocessor.h"
14#include "clang/Parse/Parser.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Frontend/ASTConsumers.h"
17#include "clang/Frontend/ASTUnit.h"
18#include "clang/Frontend/CompilerInstance.h"
19#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Frontend/Utils.h"
21#include "clang/Serialization/ASTWriter.h"
22#include "llvm/ADT/OwningPtr.h"
23#include "llvm/Support/MemoryBuffer.h"
24#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Custom Actions
29//===----------------------------------------------------------------------===//
30
31ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
32                                               llvm::StringRef InFile) {
33  return new ASTConsumer();
34}
35
36void InitOnlyAction::ExecuteAction() {
37}
38
39//===----------------------------------------------------------------------===//
40// AST Consumer Actions
41//===----------------------------------------------------------------------===//
42
43ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
44                                               llvm::StringRef InFile) {
45  if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
46    return CreateASTPrinter(OS);
47  return 0;
48}
49
50ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
51                                              llvm::StringRef InFile) {
52  return CreateASTDumper();
53}
54
55ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
56                                                 llvm::StringRef InFile) {
57  llvm::raw_ostream *OS;
58  if (CI.getFrontendOpts().OutputFile.empty())
59    OS = &llvm::outs();
60  else
61    OS = CI.createDefaultOutputFile(false, InFile);
62  if (!OS) return 0;
63  return CreateASTDumperXML(*OS);
64}
65
66ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
67                                              llvm::StringRef InFile) {
68  return CreateASTViewer();
69}
70
71ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
72                                                       llvm::StringRef InFile) {
73  return CreateDeclContextPrinter();
74}
75
76ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
77                                                  llvm::StringRef InFile) {
78  std::string Sysroot;
79  std::string OutputFile;
80  llvm::raw_ostream *OS = 0;
81  bool Chaining;
82  if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS, Chaining))
83    return 0;
84
85  if (!CI.getFrontendOpts().RelocatablePCH)
86    Sysroot.clear();
87  return new PCHGenerator(CI.getPreprocessor(), OutputFile, Chaining, Sysroot,
88                          OS);
89}
90
91bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
92                                                    llvm::StringRef InFile,
93                                                    std::string &Sysroot,
94                                                    std::string &OutputFile,
95                                                    llvm::raw_ostream *&OS,
96                                                    bool &Chaining) {
97  Sysroot = CI.getHeaderSearchOpts().Sysroot;
98  if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
99    CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
100    return true;
101  }
102
103  // We use createOutputFile here because this is exposed via libclang, and we
104  // must disable the RemoveFileOnSignal behavior.
105  OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
106                           /*RemoveFileOnSignal=*/false, InFile);
107  if (!OS)
108    return true;
109
110  OutputFile = CI.getFrontendOpts().OutputFile;
111  Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
112             !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
113  return false;
114}
115
116ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
117                                                 llvm::StringRef InFile) {
118  return new ASTConsumer();
119}
120
121//===----------------------------------------------------------------------===//
122// Preprocessor Actions
123//===----------------------------------------------------------------------===//
124
125void DumpRawTokensAction::ExecuteAction() {
126  Preprocessor &PP = getCompilerInstance().getPreprocessor();
127  SourceManager &SM = PP.getSourceManager();
128
129  // Start lexing the specified input file.
130  const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
131  Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
132  RawLex.SetKeepWhitespaceMode(true);
133
134  Token RawTok;
135  RawLex.LexFromRawLexer(RawTok);
136  while (RawTok.isNot(tok::eof)) {
137    PP.DumpToken(RawTok, true);
138    llvm::errs() << "\n";
139    RawLex.LexFromRawLexer(RawTok);
140  }
141}
142
143void DumpTokensAction::ExecuteAction() {
144  Preprocessor &PP = getCompilerInstance().getPreprocessor();
145  // Start preprocessing the specified input file.
146  Token Tok;
147  PP.EnterMainSourceFile();
148  do {
149    PP.Lex(Tok);
150    PP.DumpToken(Tok, true);
151    llvm::errs() << "\n";
152  } while (Tok.isNot(tok::eof));
153}
154
155void GeneratePTHAction::ExecuteAction() {
156  CompilerInstance &CI = getCompilerInstance();
157  if (CI.getFrontendOpts().OutputFile.empty() ||
158      CI.getFrontendOpts().OutputFile == "-") {
159    // FIXME: Don't fail this way.
160    // FIXME: Verify that we can actually seek in the given file.
161    llvm::report_fatal_error("PTH requires a seekable file for output!");
162  }
163  llvm::raw_fd_ostream *OS =
164    CI.createDefaultOutputFile(true, getCurrentFile());
165  if (!OS) return;
166
167  CacheTokens(CI.getPreprocessor(), OS);
168}
169
170void PreprocessOnlyAction::ExecuteAction() {
171  Preprocessor &PP = getCompilerInstance().getPreprocessor();
172
173  // Ignore unknown pragmas.
174  PP.AddPragmaHandler(new EmptyPragmaHandler());
175
176  Token Tok;
177  // Start parsing the specified input file.
178  PP.EnterMainSourceFile();
179  do {
180    PP.Lex(Tok);
181  } while (Tok.isNot(tok::eof));
182}
183
184void PrintPreprocessedAction::ExecuteAction() {
185  CompilerInstance &CI = getCompilerInstance();
186  // Output file needs to be set to 'Binary', to avoid converting Unix style
187  // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
188  llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
189  if (!OS) return;
190
191  DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
192                           CI.getPreprocessorOutputOpts());
193}
194
195void PrintPreambleAction::ExecuteAction() {
196  switch (getCurrentFileKind()) {
197  case IK_C:
198  case IK_CXX:
199  case IK_ObjC:
200  case IK_ObjCXX:
201  case IK_OpenCL:
202  case IK_CUDA:
203    break;
204
205  case IK_None:
206  case IK_Asm:
207  case IK_PreprocessedC:
208  case IK_PreprocessedCXX:
209  case IK_PreprocessedObjC:
210  case IK_PreprocessedObjCXX:
211  case IK_AST:
212  case IK_LLVM_IR:
213    // We can't do anything with these.
214    return;
215  }
216
217  CompilerInstance &CI = getCompilerInstance();
218  llvm::MemoryBuffer *Buffer
219      = CI.getFileManager().getBufferForFile(getCurrentFile());
220  if (Buffer) {
221    unsigned Preamble = Lexer::ComputePreamble(Buffer).first;
222    llvm::outs().write(Buffer->getBufferStart(), Preamble);
223    delete Buffer;
224  }
225}
226