blob: 5bc6506e1fd9499e9e5f8219b365f40d586425a3 [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 Dunbarc72cc502010-06-11 20:10:12 +000012#include "clang/Lex/Pragma.h"
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000013#include "clang/Lex/Preprocessor.h"
14#include "clang/Parse/Parser.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000015#include "clang/Basic/FileManager.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000016#include "clang/Frontend/ASTConsumers.h"
17#include "clang/Frontend/ASTUnit.h"
18#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000019#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Frontend/Utils.h"
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000021#include "clang/Serialization/ASTWriter.h"
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000022#include "llvm/ADT/OwningPtr.h"
Douglas Gregorf033f1d2010-07-20 20:18:03 +000023#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000024#include "llvm/Support/raw_ostream.h"
25using namespace clang;
26
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000027//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000028// 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//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000040// AST Consumer Actions
41//===----------------------------------------------------------------------===//
42
Daniel Dunbar8305d012009-11-14 10:42:46 +000043ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
44 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000045 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
46 return CreateASTPrinter(OS);
47 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000048}
49
50ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
51 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000052 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
53 return CreateASTPrinterXML(OS);
54 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000055}
56
57ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
58 llvm::StringRef InFile) {
59 return CreateASTDumper();
60}
61
62ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
63 llvm::StringRef InFile) {
64 return CreateASTViewer();
65}
66
67ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
68 llvm::StringRef InFile) {
69 return CreateDeclContextPrinter();
70}
71
Daniel Dunbar8305d012009-11-14 10:42:46 +000072ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
73 llvm::StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000074 std::string Sysroot;
75 llvm::raw_ostream *OS = 0;
76 bool Chaining;
77 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OS, Chaining))
Daniel Dunbar8305d012009-11-14 10:42:46 +000078 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000079
80 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
81 Sysroot.c_str() : 0;
82 return new PCHGenerator(CI.getPreprocessor(), Chaining, isysroot, OS);
83}
84
85bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
86 llvm::StringRef InFile,
87 std::string &Sysroot,
88 llvm::raw_ostream *&OS,
89 bool &Chaining) {
90 Sysroot = CI.getHeaderSearchOpts().Sysroot;
91 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redl11c3dc42010-08-17 17:55:38 +000092 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000093 return true;
Daniel Dunbar8305d012009-11-14 10:42:46 +000094 }
95
Douglas Gregor1d715ac2010-08-03 08:14:03 +000096 OS = CI.createDefaultOutputFile(true, InFile);
Daniel Dunbar36043592009-12-03 09:13:30 +000097 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +000098 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +000099
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000100 Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
101 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
102 return false;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000103}
104
Daniel Dunbar8305d012009-11-14 10:42:46 +0000105ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
106 llvm::StringRef InFile) {
107 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
108}
109
Daniel Dunbar8305d012009-11-14 10:42:46 +0000110ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
111 llvm::StringRef InFile) {
112 return new ASTConsumer();
113}
114
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000115//===----------------------------------------------------------------------===//
116// Preprocessor Actions
117//===----------------------------------------------------------------------===//
118
119void DumpRawTokensAction::ExecuteAction() {
120 Preprocessor &PP = getCompilerInstance().getPreprocessor();
121 SourceManager &SM = PP.getSourceManager();
122
123 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000124 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
125 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000126 RawLex.SetKeepWhitespaceMode(true);
127
128 Token RawTok;
129 RawLex.LexFromRawLexer(RawTok);
130 while (RawTok.isNot(tok::eof)) {
131 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000132 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000133 RawLex.LexFromRawLexer(RawTok);
134 }
135}
136
137void DumpTokensAction::ExecuteAction() {
138 Preprocessor &PP = getCompilerInstance().getPreprocessor();
139 // Start preprocessing the specified input file.
140 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000141 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000142 do {
143 PP.Lex(Tok);
144 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000145 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000146 } while (Tok.isNot(tok::eof));
147}
148
149void GeneratePTHAction::ExecuteAction() {
150 CompilerInstance &CI = getCompilerInstance();
151 if (CI.getFrontendOpts().OutputFile.empty() ||
152 CI.getFrontendOpts().OutputFile == "-") {
153 // FIXME: Don't fail this way.
154 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000155 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000156 }
157 llvm::raw_fd_ostream *OS =
158 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000159 if (!OS) return;
160
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000161 CacheTokens(CI.getPreprocessor(), OS);
162}
163
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000164void PreprocessOnlyAction::ExecuteAction() {
165 Preprocessor &PP = getCompilerInstance().getPreprocessor();
166
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000167 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000168 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000169
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000170 Token Tok;
171 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000172 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000173 do {
174 PP.Lex(Tok);
175 } while (Tok.isNot(tok::eof));
176}
177
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000178void PrintPreprocessedAction::ExecuteAction() {
179 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000180 // Output file needs to be set to 'Binary', to avoid converting Unix style
181 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
182 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000183 if (!OS) return;
184
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000185 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
186 CI.getPreprocessorOutputOpts());
187}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000188
189void PrintPreambleAction::ExecuteAction() {
190 switch (getCurrentFileKind()) {
191 case IK_C:
192 case IK_CXX:
193 case IK_ObjC:
194 case IK_ObjCXX:
195 case IK_OpenCL:
196 break;
197
198 case IK_None:
199 case IK_Asm:
200 case IK_PreprocessedC:
201 case IK_PreprocessedCXX:
202 case IK_PreprocessedObjC:
203 case IK_PreprocessedObjCXX:
204 case IK_AST:
205 case IK_LLVM_IR:
206 // We can't do anything with these.
207 return;
208 }
209
210 llvm::MemoryBuffer *Buffer = llvm::MemoryBuffer::getFile(getCurrentFile());
211 if (Buffer) {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000212 unsigned Preamble = Lexer::ComputePreamble(Buffer).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000213 llvm::outs().write(Buffer->getBufferStart(), Preamble);
214 delete Buffer;
215 }
216}