blob: b0f85f1ad6d07c5fbb6b88e4c6f9d087277287f7 [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"
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000021#include "llvm/ADT/OwningPtr.h"
Douglas Gregorf033f1d2010-07-20 20:18:03 +000022#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000023#include "llvm/Support/raw_ostream.h"
24using namespace clang;
25
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000026//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000027// Custom Actions
28//===----------------------------------------------------------------------===//
29
30ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
31 llvm::StringRef InFile) {
32 return new ASTConsumer();
33}
34
35void InitOnlyAction::ExecuteAction() {
36}
37
38//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000039// AST Consumer Actions
40//===----------------------------------------------------------------------===//
41
Daniel Dunbar8305d012009-11-14 10:42:46 +000042ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
43 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000044 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
45 return CreateASTPrinter(OS);
46 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000047}
48
49ASTConsumer *ASTPrintXMLAction::CreateASTConsumer(CompilerInstance &CI,
50 llvm::StringRef InFile) {
Daniel Dunbar36043592009-12-03 09:13:30 +000051 if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "xml"))
52 return CreateASTPrinterXML(OS);
53 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000054}
55
56ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
57 llvm::StringRef InFile) {
58 return CreateASTDumper();
59}
60
61ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
62 llvm::StringRef InFile) {
63 return CreateASTViewer();
64}
65
66ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
67 llvm::StringRef InFile) {
68 return CreateDeclContextPrinter();
69}
70
Daniel Dunbar8305d012009-11-14 10:42:46 +000071ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
72 llvm::StringRef InFile) {
73 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
74 if (CI.getFrontendOpts().RelocatablePCH &&
75 Sysroot.empty()) {
76 CI.getDiagnostics().Report(diag::err_relocatable_without_without_isysroot);
77 return 0;
78 }
79
80 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, InFile);
Daniel Dunbar36043592009-12-03 09:13:30 +000081 if (!OS)
82 return 0;
83
Sebastian Redlffaab3e2010-07-30 00:29:29 +000084 bool Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
85 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
Sebastian Redl6a695cf2010-07-09 17:40:12 +000086 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
87 Sysroot.c_str() : 0;
Sebastian Redlffaab3e2010-07-30 00:29:29 +000088 return CreatePCHGenerator(CI.getPreprocessor(), OS, Chaining, isysroot);
Daniel Dunbar8305d012009-11-14 10:42:46 +000089}
90
Daniel Dunbar8305d012009-11-14 10:42:46 +000091ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
92 llvm::StringRef InFile) {
93 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
94}
95
Daniel Dunbar8305d012009-11-14 10:42:46 +000096ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
97 llvm::StringRef InFile) {
98 return new ASTConsumer();
99}
100
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000101//===----------------------------------------------------------------------===//
102// Preprocessor Actions
103//===----------------------------------------------------------------------===//
104
105void DumpRawTokensAction::ExecuteAction() {
106 Preprocessor &PP = getCompilerInstance().getPreprocessor();
107 SourceManager &SM = PP.getSourceManager();
108
109 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000110 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
111 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000112 RawLex.SetKeepWhitespaceMode(true);
113
114 Token RawTok;
115 RawLex.LexFromRawLexer(RawTok);
116 while (RawTok.isNot(tok::eof)) {
117 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000118 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000119 RawLex.LexFromRawLexer(RawTok);
120 }
121}
122
123void DumpTokensAction::ExecuteAction() {
124 Preprocessor &PP = getCompilerInstance().getPreprocessor();
125 // Start preprocessing the specified input file.
126 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000127 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000128 do {
129 PP.Lex(Tok);
130 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000131 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000132 } while (Tok.isNot(tok::eof));
133}
134
135void GeneratePTHAction::ExecuteAction() {
136 CompilerInstance &CI = getCompilerInstance();
137 if (CI.getFrontendOpts().OutputFile.empty() ||
138 CI.getFrontendOpts().OutputFile == "-") {
139 // FIXME: Don't fail this way.
140 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000141 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000142 }
143 llvm::raw_fd_ostream *OS =
144 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000145 if (!OS) return;
146
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000147 CacheTokens(CI.getPreprocessor(), OS);
148}
149
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000150void PreprocessOnlyAction::ExecuteAction() {
151 Preprocessor &PP = getCompilerInstance().getPreprocessor();
152
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000153 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000154 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000155
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000156 Token Tok;
157 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000158 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000159 do {
160 PP.Lex(Tok);
161 } while (Tok.isNot(tok::eof));
162}
163
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000164void PrintPreprocessedAction::ExecuteAction() {
165 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000166 // Output file needs to be set to 'Binary', to avoid converting Unix style
167 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
168 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000169 if (!OS) return;
170
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000171 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
172 CI.getPreprocessorOutputOpts());
173}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000174
175void PrintPreambleAction::ExecuteAction() {
176 switch (getCurrentFileKind()) {
177 case IK_C:
178 case IK_CXX:
179 case IK_ObjC:
180 case IK_ObjCXX:
181 case IK_OpenCL:
182 break;
183
184 case IK_None:
185 case IK_Asm:
186 case IK_PreprocessedC:
187 case IK_PreprocessedCXX:
188 case IK_PreprocessedObjC:
189 case IK_PreprocessedObjCXX:
190 case IK_AST:
191 case IK_LLVM_IR:
192 // We can't do anything with these.
193 return;
194 }
195
196 llvm::MemoryBuffer *Buffer = llvm::MemoryBuffer::getFile(getCurrentFile());
197 if (Buffer) {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000198 unsigned Preamble = Lexer::ComputePreamble(Buffer).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000199 llvm::outs().write(Buffer->getBufferStart(), Preamble);
200 delete Buffer;
201 }
202}