blob: 44cd2dfe9bf02cb7f50ca026ccc9ef5fa8b246a3 [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,
Chris Lattner5f9e2722011-07-23 10:55:15 +000032 StringRef InFile) {
Daniel Dunbar27585952010-03-19 19:44:04 +000033 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,
Chris Lattner5f9e2722011-07-23 10:55:15 +000044 StringRef InFile) {
45 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar36043592009-12-03 09:13:30 +000046 return CreateASTPrinter(OS);
47 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000048}
49
Daniel Dunbar8305d012009-11-14 10:42:46 +000050ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000051 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000052 return CreateASTDumper();
53}
54
John McCallf3514242010-11-24 11:21:45 +000055ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000056 StringRef InFile) {
57 raw_ostream *OS;
John McCallf3514242010-11-24 11:21:45 +000058 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
Daniel Dunbar8305d012009-11-14 10:42:46 +000066ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000067 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000068 return CreateASTViewer();
69}
70
71ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000072 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000073 return CreateDeclContextPrinter();
74}
75
Daniel Dunbar8305d012009-11-14 10:42:46 +000076ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000077 StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000078 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000079 std::string OutputFile;
Chris Lattner5f9e2722011-07-23 10:55:15 +000080 raw_ostream *OS = 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000081 bool Chaining;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000082 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS, Chaining))
Daniel Dunbar8305d012009-11-14 10:42:46 +000083 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000084
Douglas Gregor832d6202011-07-22 16:35:34 +000085 if (!CI.getFrontendOpts().RelocatablePCH)
86 Sysroot.clear();
87 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Chaining, Sysroot,
88 OS);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000089}
90
91bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000092 StringRef InFile,
Douglas Gregor1d715ac2010-08-03 08:14:03 +000093 std::string &Sysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000094 std::string &OutputFile,
Chris Lattner5f9e2722011-07-23 10:55:15 +000095 raw_ostream *&OS,
Douglas Gregor1d715ac2010-08-03 08:14:03 +000096 bool &Chaining) {
97 Sysroot = CI.getHeaderSearchOpts().Sysroot;
98 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redl11c3dc42010-08-17 17:55:38 +000099 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000100 return true;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000101 }
102
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000103 // 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);
Daniel Dunbar36043592009-12-03 09:13:30 +0000107 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000108 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +0000109
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000110 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000111 Chaining = CI.getInvocation().getFrontendOpts().ChainedPCH &&
112 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty();
113 return false;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000114}
115
Daniel Dunbar8305d012009-11-14 10:42:46 +0000116ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000117 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000118 return new ASTConsumer();
119}
120
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000121//===----------------------------------------------------------------------===//
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.
Chris Lattner6e290142009-11-30 04:18:44 +0000130 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
131 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000132 RawLex.SetKeepWhitespaceMode(true);
133
134 Token RawTok;
135 RawLex.LexFromRawLexer(RawTok);
136 while (RawTok.isNot(tok::eof)) {
137 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000138 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000139 RawLex.LexFromRawLexer(RawTok);
140 }
141}
142
143void DumpTokensAction::ExecuteAction() {
144 Preprocessor &PP = getCompilerInstance().getPreprocessor();
145 // Start preprocessing the specified input file.
146 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000147 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000148 do {
149 PP.Lex(Tok);
150 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000151 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000152 } 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.
Chris Lattner83e7a782010-04-07 22:58:06 +0000161 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000162 }
163 llvm::raw_fd_ostream *OS =
164 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000165 if (!OS) return;
166
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000167 CacheTokens(CI.getPreprocessor(), OS);
168}
169
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000170void PreprocessOnlyAction::ExecuteAction() {
171 Preprocessor &PP = getCompilerInstance().getPreprocessor();
172
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000173 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000174 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000175
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000176 Token Tok;
177 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000178 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000179 do {
180 PP.Lex(Tok);
181 } while (Tok.isNot(tok::eof));
182}
183
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000184void PrintPreprocessedAction::ExecuteAction() {
185 CompilerInstance &CI = getCompilerInstance();
Steve Naroffd57d7c02010-01-05 17:33:23 +0000186 // Output file needs to be set to 'Binary', to avoid converting Unix style
187 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Chris Lattner5f9e2722011-07-23 10:55:15 +0000188 raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000189 if (!OS) return;
190
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000191 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
192 CI.getPreprocessorOutputOpts());
193}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000194
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:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000202 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000203 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
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000217 CompilerInstance &CI = getCompilerInstance();
218 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000219 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000220 if (Buffer) {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000221 unsigned Preamble = Lexer::ComputePreamble(Buffer).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000222 llvm::outs().write(Buffer->getBufferStart(), Preamble);
223 delete Buffer;
224 }
225}