blob: 243f5277ab1fa4e2b234240492334ba3ac71033c [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"
Douglas Gregordb1cde72011-11-16 00:09:06 +000012#include "clang/Lex/HeaderSearch.h"
Daniel Dunbarc72cc502010-06-11 20:10:12 +000013#include "clang/Lex/Pragma.h"
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000014#include "clang/Lex/Preprocessor.h"
15#include "clang/Parse/Parser.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000016#include "clang/Basic/FileManager.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000017#include "clang/Frontend/ASTConsumers.h"
18#include "clang/Frontend/ASTUnit.h"
19#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000020#include "clang/Frontend/FrontendDiagnostic.h"
21#include "clang/Frontend/Utils.h"
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000022#include "clang/Serialization/ASTWriter.h"
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000023#include "llvm/ADT/OwningPtr.h"
Douglas Gregorf033f1d2010-07-20 20:18:03 +000024#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000025#include "llvm/Support/raw_ostream.h"
Douglas Gregor10efbaf2011-09-23 23:43:36 +000026#include "llvm/Support/system_error.h"
27
Daniel Dunbar8305d012009-11-14 10:42:46 +000028using namespace clang;
29
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000030//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000031// Custom Actions
32//===----------------------------------------------------------------------===//
33
34ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 StringRef InFile) {
Daniel Dunbar27585952010-03-19 19:44:04 +000036 return new ASTConsumer();
37}
38
39void InitOnlyAction::ExecuteAction() {
40}
41
42//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000043// AST Consumer Actions
44//===----------------------------------------------------------------------===//
45
Daniel Dunbar8305d012009-11-14 10:42:46 +000046ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000047 StringRef InFile) {
48 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Daniel Dunbar36043592009-12-03 09:13:30 +000049 return CreateASTPrinter(OS);
50 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000051}
52
Daniel Dunbar8305d012009-11-14 10:42:46 +000053ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000054 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000055 return CreateASTDumper();
56}
57
John McCallf3514242010-11-24 11:21:45 +000058ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000059 StringRef InFile) {
60 raw_ostream *OS;
John McCallf3514242010-11-24 11:21:45 +000061 if (CI.getFrontendOpts().OutputFile.empty())
62 OS = &llvm::outs();
63 else
64 OS = CI.createDefaultOutputFile(false, InFile);
65 if (!OS) return 0;
66 return CreateASTDumperXML(*OS);
67}
68
Daniel Dunbar8305d012009-11-14 10:42:46 +000069ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000070 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000071 return CreateASTViewer();
72}
73
74ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000075 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000076 return CreateDeclContextPrinter();
77}
78
Daniel Dunbar8305d012009-11-14 10:42:46 +000079ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000080 StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000081 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000082 std::string OutputFile;
Chris Lattner5f9e2722011-07-23 10:55:15 +000083 raw_ostream *OS = 0;
Douglas Gregor9293ba82011-08-25 22:35:51 +000084 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
Daniel Dunbar8305d012009-11-14 10:42:46 +000085 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000086
Douglas Gregor832d6202011-07-22 16:35:34 +000087 if (!CI.getFrontendOpts().RelocatablePCH)
88 Sysroot.clear();
Douglas Gregord2536a62011-11-15 22:58:25 +000089 return new PCHGenerator(CI.getPreprocessor(), OutputFile, MakeModule,
Douglas Gregor7143aab2011-09-01 17:04:32 +000090 Sysroot, OS);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000091}
92
93bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000094 StringRef InFile,
Douglas Gregor1d715ac2010-08-03 08:14:03 +000095 std::string &Sysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000096 std::string &OutputFile,
Douglas Gregor9293ba82011-08-25 22:35:51 +000097 raw_ostream *&OS) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000098 Sysroot = CI.getHeaderSearchOpts().Sysroot;
99 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redl11c3dc42010-08-17 17:55:38 +0000100 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000101 return true;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000102 }
103
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000104 // We use createOutputFile here because this is exposed via libclang, and we
105 // must disable the RemoveFileOnSignal behavior.
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000106 // We use a temporary to avoid race conditions.
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000107 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000108 /*RemoveFileOnSignal=*/false, InFile,
109 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar36043592009-12-03 09:13:30 +0000110 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000111 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +0000112
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000113 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000114 return false;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000115}
116
Douglas Gregordb1cde72011-11-16 00:09:06 +0000117ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
118 StringRef InFile) {
119 std::string Sysroot;
120 std::string OutputFile;
121 raw_ostream *OS = 0;
122 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
123 return 0;
124
125 return new PCHGenerator(CI.getPreprocessor(), OutputFile, /*MakeModule=*/true,
126 Sysroot, OS);
127}
128
129bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
130 StringRef Filename) {
131 // Find the module map file.
132 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
133 if (!ModuleMap) {
134 CI.getDiagnostics().Report(diag::err_module_map_not_found)
135 << Filename;
136 return false;
137 }
138
139 // Parse the module map file.
140 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
141 if (HS.loadModuleMapFile(ModuleMap))
142 return false;
143
144 if (CI.getLangOpts().CurrentModule.empty()) {
145 CI.getDiagnostics().Report(diag::err_missing_module_name);
146
147 // FIXME: Eventually, we could consider asking whether there was just
148 // a single module described in the module map, and use that as a
149 // default. Then it would be fairly trivial to just "compile" a module
150 // map with a single module (the common case).
151 return false;
152 }
153
154 // Dig out the module definition.
155 ModuleMap::Module *Module = HS.getModule(CI.getLangOpts().CurrentModule,
156 /*AllowSearch=*/false);
157 if (!Module) {
158 CI.getDiagnostics().Report(diag::err_missing_module)
159 << CI.getLangOpts().CurrentModule << Filename;
160
161 return false;
162 }
163
164 // If there is an umbrella header, use it as our actual input file.
165 if (Module->UmbrellaHeader) {
166 // FIXME: Deal with explicit submodule headers, which won't be contained
167 // within the umbrella header.
Douglas Gregordb1cde72011-11-16 00:09:06 +0000168 setCurrentFile(Module->UmbrellaHeader->getName(), getCurrentFileKind());
169 } else {
170 // FIXME: Deal with the non-umbrella case, where we have to synthesize
171 // a header to parse.
172 // FIXME: Diagnose, at least for now.
173 return false;
174 }
175
176 return true;
177}
178
179bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
180 StringRef InFile,
181 std::string &Sysroot,
182 std::string &OutputFile,
183 raw_ostream *&OS) {
184 // If no output file was provided, figure out where this module would go
185 // in the module cache.
186 if (CI.getFrontendOpts().OutputFile.empty()) {
187 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
188 llvm::SmallString<256> ModuleFileName(HS.getModuleCachePath());
189 llvm::sys::path::append(ModuleFileName,
190 CI.getLangOpts().CurrentModule + ".pcm");
191 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
192 }
193
194 // We use createOutputFile here because this is exposed via libclang, and we
195 // must disable the RemoveFileOnSignal behavior.
196 // We use a temporary to avoid race conditions.
197 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
198 /*RemoveFileOnSignal=*/false, InFile,
199 /*Extension=*/"", /*useTemporary=*/true);
200 if (!OS)
201 return true;
202
203 OutputFile = CI.getFrontendOpts().OutputFile;
204 return false;
205}
206
Daniel Dunbar8305d012009-11-14 10:42:46 +0000207ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000208 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000209 return new ASTConsumer();
210}
211
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000212//===----------------------------------------------------------------------===//
213// Preprocessor Actions
214//===----------------------------------------------------------------------===//
215
216void DumpRawTokensAction::ExecuteAction() {
217 Preprocessor &PP = getCompilerInstance().getPreprocessor();
218 SourceManager &SM = PP.getSourceManager();
219
220 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000221 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
222 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000223 RawLex.SetKeepWhitespaceMode(true);
224
225 Token RawTok;
226 RawLex.LexFromRawLexer(RawTok);
227 while (RawTok.isNot(tok::eof)) {
228 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000229 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000230 RawLex.LexFromRawLexer(RawTok);
231 }
232}
233
234void DumpTokensAction::ExecuteAction() {
235 Preprocessor &PP = getCompilerInstance().getPreprocessor();
236 // Start preprocessing the specified input file.
237 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000238 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000239 do {
240 PP.Lex(Tok);
241 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000242 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000243 } while (Tok.isNot(tok::eof));
244}
245
246void GeneratePTHAction::ExecuteAction() {
247 CompilerInstance &CI = getCompilerInstance();
248 if (CI.getFrontendOpts().OutputFile.empty() ||
249 CI.getFrontendOpts().OutputFile == "-") {
250 // FIXME: Don't fail this way.
251 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000252 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000253 }
254 llvm::raw_fd_ostream *OS =
255 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000256 if (!OS) return;
257
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000258 CacheTokens(CI.getPreprocessor(), OS);
259}
260
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000261void PreprocessOnlyAction::ExecuteAction() {
262 Preprocessor &PP = getCompilerInstance().getPreprocessor();
263
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000264 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000265 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000266
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000267 Token Tok;
268 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000269 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000270 do {
271 PP.Lex(Tok);
272 } while (Tok.isNot(tok::eof));
273}
274
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000275void PrintPreprocessedAction::ExecuteAction() {
276 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000277 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroffd57d7c02010-01-05 17:33:23 +0000278 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000279 //
280 // Look to see what type of line endings the file uses. If there's a
281 // CRLF, then we won't open the file up in binary mode. If there is
282 // just an LF or CR, then we will open the file up in binary mode.
283 // In this fashion, the output format should match the input format, unless
284 // the input format has inconsistent line endings.
285 //
286 // This should be a relatively fast operation since most files won't have
287 // all of their source code on a single line. However, that is still a
288 // concern, so if we scan for too long, we'll just assume the file should
289 // be opened in binary mode.
290 bool BinaryMode = true;
291 bool InvalidFile = false;
292 const SourceManager& SM = CI.getSourceManager();
293 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
294 &InvalidFile);
295 if (!InvalidFile) {
296 const char *cur = Buffer->getBufferStart();
297 const char *end = Buffer->getBufferEnd();
298 const char *next = (cur != end) ? cur + 1 : end;
299
300 // Limit ourselves to only scanning 256 characters into the source
301 // file. This is mostly a sanity check in case the file has no
302 // newlines whatsoever.
303 if (end - cur > 256) end = cur + 256;
304
305 while (next < end) {
306 if (*cur == 0x0D) { // CR
307 if (*next == 0x0A) // CRLF
308 BinaryMode = false;
309
310 break;
311 } else if (*cur == 0x0A) // LF
312 break;
313
314 ++cur, ++next;
315 }
316 }
317
318 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000319 if (!OS) return;
320
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000321 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
322 CI.getPreprocessorOutputOpts());
323}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000324
325void PrintPreambleAction::ExecuteAction() {
326 switch (getCurrentFileKind()) {
327 case IK_C:
328 case IK_CXX:
329 case IK_ObjC:
330 case IK_ObjCXX:
331 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000332 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000333 break;
334
335 case IK_None:
336 case IK_Asm:
337 case IK_PreprocessedC:
338 case IK_PreprocessedCXX:
339 case IK_PreprocessedObjC:
340 case IK_PreprocessedObjCXX:
341 case IK_AST:
342 case IK_LLVM_IR:
343 // We can't do anything with these.
344 return;
345 }
346
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000347 CompilerInstance &CI = getCompilerInstance();
348 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000349 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000350 if (Buffer) {
Argyrios Kyrtzidis03c107a2011-08-25 20:39:19 +0000351 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000352 llvm::outs().write(Buffer->getBufferStart(), Preamble);
353 delete Buffer;
354 }
355}