blob: 6f84da96300c7422ed4ed7d54791fc809d381afb [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"
Douglas Gregor10efbaf2011-09-23 23:43:36 +000025#include "llvm/Support/system_error.h"
26
Daniel Dunbar8305d012009-11-14 10:42:46 +000027using namespace clang;
28
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000029//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000030// Custom Actions
31//===----------------------------------------------------------------------===//
32
33ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 StringRef InFile) {
Daniel Dunbar27585952010-03-19 19:44:04 +000035 return new ASTConsumer();
36}
37
38void InitOnlyAction::ExecuteAction() {
39}
40
41//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000042// AST Consumer Actions
43//===----------------------------------------------------------------------===//
44
Daniel Dunbar8305d012009-11-14 10:42:46 +000045ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000046 StringRef InFile) {
47 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar36043592009-12-03 09:13:30 +000048 return CreateASTPrinter(OS);
49 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000050}
51
Daniel Dunbar8305d012009-11-14 10:42:46 +000052ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000053 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000054 return CreateASTDumper();
55}
56
John McCallf3514242010-11-24 11:21:45 +000057ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000058 StringRef InFile) {
59 raw_ostream *OS;
John McCallf3514242010-11-24 11:21:45 +000060 if (CI.getFrontendOpts().OutputFile.empty())
61 OS = &llvm::outs();
62 else
63 OS = CI.createDefaultOutputFile(false, InFile);
64 if (!OS) return 0;
65 return CreateASTDumperXML(*OS);
66}
67
Daniel Dunbar8305d012009-11-14 10:42:46 +000068ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000069 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000070 return CreateASTViewer();
71}
72
73ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000074 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000075 return CreateDeclContextPrinter();
76}
77
Daniel Dunbar8305d012009-11-14 10:42:46 +000078ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000079 StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000080 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000081 std::string OutputFile;
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 raw_ostream *OS = 0;
Douglas Gregor9293ba82011-08-25 22:35:51 +000083 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
Daniel Dunbar8305d012009-11-14 10:42:46 +000084 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000085
Douglas Gregor832d6202011-07-22 16:35:34 +000086 if (!CI.getFrontendOpts().RelocatablePCH)
87 Sysroot.clear();
Douglas Gregor7143aab2011-09-01 17:04:32 +000088 return new PCHGenerator(CI.getPreprocessor(), OutputFile, MakeModule,
89 Sysroot, OS);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000090}
91
92bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000093 StringRef InFile,
Douglas Gregor1d715ac2010-08-03 08:14:03 +000094 std::string &Sysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000095 std::string &OutputFile,
Douglas Gregor9293ba82011-08-25 22:35:51 +000096 raw_ostream *&OS) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000097 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.
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000105 // We use a temporary to avoid race conditions.
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000106 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000107 /*RemoveFileOnSignal=*/false, InFile,
108 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar36043592009-12-03 09:13:30 +0000109 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000110 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +0000111
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000112 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000113 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();
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000186 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroffd57d7c02010-01-05 17:33:23 +0000187 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000188 //
189 // Look to see what type of line endings the file uses. If there's a
190 // CRLF, then we won't open the file up in binary mode. If there is
191 // just an LF or CR, then we will open the file up in binary mode.
192 // In this fashion, the output format should match the input format, unless
193 // the input format has inconsistent line endings.
194 //
195 // This should be a relatively fast operation since most files won't have
196 // all of their source code on a single line. However, that is still a
197 // concern, so if we scan for too long, we'll just assume the file should
198 // be opened in binary mode.
199 bool BinaryMode = true;
200 bool InvalidFile = false;
201 const SourceManager& SM = CI.getSourceManager();
202 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
203 &InvalidFile);
204 if (!InvalidFile) {
205 const char *cur = Buffer->getBufferStart();
206 const char *end = Buffer->getBufferEnd();
207 const char *next = (cur != end) ? cur + 1 : end;
208
209 // Limit ourselves to only scanning 256 characters into the source
210 // file. This is mostly a sanity check in case the file has no
211 // newlines whatsoever.
212 if (end - cur > 256) end = cur + 256;
213
214 while (next < end) {
215 if (*cur == 0x0D) { // CR
216 if (*next == 0x0A) // CRLF
217 BinaryMode = false;
218
219 break;
220 } else if (*cur == 0x0A) // LF
221 break;
222
223 ++cur, ++next;
224 }
225 }
226
227 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000228 if (!OS) return;
229
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000230 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
231 CI.getPreprocessorOutputOpts());
232}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000233
234void PrintPreambleAction::ExecuteAction() {
235 switch (getCurrentFileKind()) {
236 case IK_C:
237 case IK_CXX:
238 case IK_ObjC:
239 case IK_ObjCXX:
240 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000241 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000242 break;
243
244 case IK_None:
245 case IK_Asm:
246 case IK_PreprocessedC:
247 case IK_PreprocessedCXX:
248 case IK_PreprocessedObjC:
249 case IK_PreprocessedObjCXX:
250 case IK_AST:
251 case IK_LLVM_IR:
252 // We can't do anything with these.
253 return;
254 }
255
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000256 CompilerInstance &CI = getCompilerInstance();
257 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000258 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000259 if (Buffer) {
Argyrios Kyrtzidis03c107a2011-08-25 20:39:19 +0000260 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000261 llvm::outs().write(Buffer->getBufferStart(), Preamble);
262 delete Buffer;
263 }
264}