blob: e454321c0cce48d74363616fdb6be9abaa1d7390 [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
John McCallf3514242010-11-24 11:21:45 +000062ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
63 llvm::StringRef InFile) {
64 llvm::raw_ostream *OS;
65 if (CI.getFrontendOpts().OutputFile.empty())
66 OS = &llvm::outs();
67 else
68 OS = CI.createDefaultOutputFile(false, InFile);
69 if (!OS) return 0;
70 return CreateASTDumperXML(*OS);
71}
72
Daniel Dunbar8305d012009-11-14 10:42:46 +000073ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
74 llvm::StringRef InFile) {
75 return CreateASTViewer();
76}
77
78ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
79 llvm::StringRef InFile) {
80 return CreateDeclContextPrinter();
81}
82
Daniel Dunbar8305d012009-11-14 10:42:46 +000083ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
84 llvm::StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000085 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000086 std::string OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000087 llvm::raw_ostream *OS = 0;
88 bool Chaining;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000089 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS, Chaining))
Daniel Dunbar8305d012009-11-14 10:42:46 +000090 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000091
92 const char *isysroot = CI.getFrontendOpts().RelocatablePCH ?
93 Sysroot.c_str() : 0;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000094 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Chaining, isysroot, OS);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000095}
96
97bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
98 llvm::StringRef InFile,
99 std::string &Sysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000100 std::string &OutputFile,
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000101 llvm::raw_ostream *&OS,
102 bool &Chaining) {
103 Sysroot = CI.getHeaderSearchOpts().Sysroot;
104 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redl11c3dc42010-08-17 17:55:38 +0000105 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000106 return true;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000107 }
108
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000109 // We use createOutputFile here because this is exposed via libclang, and we
110 // must disable the RemoveFileOnSignal behavior.
111 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
112 /*RemoveFileOnSignal=*/false, InFile);
Daniel Dunbar36043592009-12-03 09:13:30 +0000113 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000114 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +0000115
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000116 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000117 Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
118 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
119 return false;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000120}
121
Daniel Dunbar8305d012009-11-14 10:42:46 +0000122ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI,
123 llvm::StringRef InFile) {
124 return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance);
125}
126
Daniel Dunbar8305d012009-11-14 10:42:46 +0000127ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
128 llvm::StringRef InFile) {
129 return new ASTConsumer();
130}
131
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000132//===----------------------------------------------------------------------===//
133// Preprocessor Actions
134//===----------------------------------------------------------------------===//
135
136void DumpRawTokensAction::ExecuteAction() {
137 Preprocessor &PP = getCompilerInstance().getPreprocessor();
138 SourceManager &SM = PP.getSourceManager();
139
140 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000141 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
142 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000143 RawLex.SetKeepWhitespaceMode(true);
144
145 Token RawTok;
146 RawLex.LexFromRawLexer(RawTok);
147 while (RawTok.isNot(tok::eof)) {
148 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000149 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000150 RawLex.LexFromRawLexer(RawTok);
151 }
152}
153
154void DumpTokensAction::ExecuteAction() {
155 Preprocessor &PP = getCompilerInstance().getPreprocessor();
156 // Start preprocessing the specified input file.
157 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000158 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000159 do {
160 PP.Lex(Tok);
161 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000162 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000163 } while (Tok.isNot(tok::eof));
164}
165
166void GeneratePTHAction::ExecuteAction() {
167 CompilerInstance &CI = getCompilerInstance();
168 if (CI.getFrontendOpts().OutputFile.empty() ||
169 CI.getFrontendOpts().OutputFile == "-") {
170 // FIXME: Don't fail this way.
171 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000172 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000173 }
174 llvm::raw_fd_ostream *OS =
175 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000176 if (!OS) return;
177
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000178 CacheTokens(CI.getPreprocessor(), OS);
179}
180
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000181void PreprocessOnlyAction::ExecuteAction() {
182 Preprocessor &PP = getCompilerInstance().getPreprocessor();
183
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000184 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000185 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000186
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000187 Token Tok;
188 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000189 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000190 do {
191 PP.Lex(Tok);
192 } while (Tok.isNot(tok::eof));
193}
194
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000195void PrintPreprocessedAction::ExecuteAction() {
196 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000197 // Output file needs to be set to 'Binary', to avoid converting Unix style
198 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
199 llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000200 if (!OS) return;
201
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000202 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
203 CI.getPreprocessorOutputOpts());
204}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000205
206void PrintPreambleAction::ExecuteAction() {
207 switch (getCurrentFileKind()) {
208 case IK_C:
209 case IK_CXX:
210 case IK_ObjC:
211 case IK_ObjCXX:
212 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000213 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000214 break;
215
216 case IK_None:
217 case IK_Asm:
218 case IK_PreprocessedC:
219 case IK_PreprocessedCXX:
220 case IK_PreprocessedObjC:
221 case IK_PreprocessedObjCXX:
222 case IK_AST:
223 case IK_LLVM_IR:
224 // We can't do anything with these.
225 return;
226 }
227
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000228 CompilerInstance &CI = getCompilerInstance();
229 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000230 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000231 if (Buffer) {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000232 unsigned Preamble = Lexer::ComputePreamble(Buffer).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000233 llvm::outs().write(Buffer->getBufferStart(), Preamble);
234 delete Buffer;
235 }
236}