blob: 24960cf6a0c96dba142d98ca33a390a0d8b1f56d [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 Gregor77d029f2011-12-08 19:11:24 +000024#include "llvm/Support/FileSystem.h"
Douglas Gregorf033f1d2010-07-20 20:18:03 +000025#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor10efbaf2011-09-23 23:43:36 +000027#include "llvm/Support/system_error.h"
28
Daniel Dunbar8305d012009-11-14 10:42:46 +000029using namespace clang;
30
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000031//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000032// Custom Actions
33//===----------------------------------------------------------------------===//
34
35ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000036 StringRef InFile) {
Daniel Dunbar27585952010-03-19 19:44:04 +000037 return new ASTConsumer();
38}
39
40void InitOnlyAction::ExecuteAction() {
41}
42
43//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000044// AST Consumer Actions
45//===----------------------------------------------------------------------===//
46
Daniel Dunbar8305d012009-11-14 10:42:46 +000047ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000048 StringRef InFile) {
49 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000050 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
Daniel Dunbar36043592009-12-03 09:13:30 +000051 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000052}
53
Daniel Dunbar8305d012009-11-14 10:42:46 +000054ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000055 StringRef InFile) {
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000056 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter);
Daniel Dunbar8305d012009-11-14 10:42:46 +000057}
58
Alexander Kornienko171af642012-07-31 09:37:40 +000059ASTConsumer *ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI,
60 StringRef InFile) {
61 return CreateASTDeclNodeLister();
62}
63
John McCallf3514242010-11-24 11:21:45 +000064ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000065 StringRef InFile) {
66 raw_ostream *OS;
John McCallf3514242010-11-24 11:21:45 +000067 if (CI.getFrontendOpts().OutputFile.empty())
68 OS = &llvm::outs();
69 else
70 OS = CI.createDefaultOutputFile(false, InFile);
71 if (!OS) return 0;
72 return CreateASTDumperXML(*OS);
73}
74
Daniel Dunbar8305d012009-11-14 10:42:46 +000075ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000076 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000077 return CreateASTViewer();
78}
79
80ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000081 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000082 return CreateDeclContextPrinter();
83}
84
Daniel Dunbar8305d012009-11-14 10:42:46 +000085ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000086 StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000087 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000088 std::string OutputFile;
Chris Lattner5f9e2722011-07-23 10:55:15 +000089 raw_ostream *OS = 0;
Douglas Gregor9293ba82011-08-25 22:35:51 +000090 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
Daniel Dunbar8305d012009-11-14 10:42:46 +000091 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000092
Douglas Gregor832d6202011-07-22 16:35:34 +000093 if (!CI.getFrontendOpts().RelocatablePCH)
94 Sysroot.clear();
Douglas Gregora8cc6ce2011-11-30 04:39:39 +000095 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000096}
97
98bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000099 StringRef InFile,
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000100 std::string &Sysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000101 std::string &OutputFile,
Douglas Gregor9293ba82011-08-25 22:35:51 +0000102 raw_ostream *&OS) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000103 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.
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000111 // We use a temporary to avoid race conditions.
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000112 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000113 /*RemoveFileOnSignal=*/false, InFile,
114 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar36043592009-12-03 09:13:30 +0000115 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000116 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +0000117
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000118 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000119 return false;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000120}
121
Douglas Gregordb1cde72011-11-16 00:09:06 +0000122ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
123 StringRef InFile) {
124 std::string Sysroot;
125 std::string OutputFile;
126 raw_ostream *OS = 0;
127 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
128 return 0;
129
Douglas Gregora8cc6ce2011-11-30 04:39:39 +0000130 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
Douglas Gregordb1cde72011-11-16 00:09:06 +0000131 Sysroot, OS);
132}
133
Douglas Gregor261e75b2011-11-16 17:04:00 +0000134/// \brief Collect the set of header includes needed to construct the given
135/// module.
136///
137/// \param Module The module we're collecting includes from.
Douglas Gregor8075ce62011-12-06 17:15:11 +0000138///
James Dennett5e9dda62012-06-15 21:48:19 +0000139/// \param Includes Will be augmented with the set of \#includes or \#imports
Douglas Gregor8075ce62011-12-06 17:15:11 +0000140/// needed to load all of the named headers.
Douglas Gregor261e75b2011-11-16 17:04:00 +0000141static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
Douglas Gregor752769f2012-01-05 00:04:05 +0000142 FileManager &FileMgr,
143 ModuleMap &ModMap,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000144 clang::Module *Module,
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000145 SmallString<256> &Includes) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000146 // Don't collect any headers for unavailable modules.
147 if (!Module->isAvailable())
148 return;
149
Douglas Gregor8075ce62011-12-06 17:15:11 +0000150 // Add includes for each of these headers.
Douglas Gregor2f04f182012-02-02 18:42:48 +0000151 for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
152 if (LangOpts.ObjC1)
153 Includes += "#import \"";
154 else
155 Includes += "#include \"";
156 Includes += Module->Headers[I]->getName();
157 Includes += "\"\n";
158 }
Douglas Gregor8075ce62011-12-06 17:15:11 +0000159
Douglas Gregor10694ce2011-12-08 17:39:04 +0000160 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
Douglas Gregor2f04f182012-02-02 18:42:48 +0000161 if (Module->Parent) {
162 // Include the umbrella header for submodules.
163 if (LangOpts.ObjC1)
164 Includes += "#import \"";
165 else
166 Includes += "#include \"";
167 Includes += UmbrellaHeader->getName();
168 Includes += "\"\n";
169 }
Douglas Gregor77d029f2011-12-08 19:11:24 +0000170 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
Douglas Gregor752769f2012-01-05 00:04:05 +0000171 // Add all of the headers we find in this subdirectory.
Douglas Gregor77d029f2011-12-08 19:11:24 +0000172 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000173 SmallString<128> DirNative;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000174 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
Douglas Gregor3b29bb92011-12-12 19:13:53 +0000175 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
176 DirEnd;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000177 Dir != DirEnd && !EC; Dir.increment(EC)) {
178 // Check whether this entry has an extension typically associated with
179 // headers.
180 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
181 .Cases(".h", ".H", ".hh", ".hpp", true)
182 .Default(false))
183 continue;
184
Douglas Gregor752769f2012-01-05 00:04:05 +0000185 // If this header is marked 'unavailable' in this module, don't include
186 // it.
187 if (const FileEntry *Header = FileMgr.getFile(Dir->path()))
188 if (ModMap.isHeaderInUnavailableModule(Header))
189 continue;
190
Douglas Gregor2f04f182012-02-02 18:42:48 +0000191 // Include this header umbrella header for submodules.
192 if (LangOpts.ObjC1)
193 Includes += "#import \"";
194 else
195 Includes += "#include \"";
196 Includes += Dir->path();
197 Includes += "\"\n";
Douglas Gregor77d029f2011-12-08 19:11:24 +0000198 }
Douglas Gregor261e75b2011-11-16 17:04:00 +0000199 }
200
201 // Recurse into submodules.
Douglas Gregorb7a78192012-01-04 23:32:19 +0000202 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
203 SubEnd = Module->submodule_end();
Douglas Gregor8075ce62011-12-06 17:15:11 +0000204 Sub != SubEnd; ++Sub)
Douglas Gregor2f04f182012-02-02 18:42:48 +0000205 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000206}
207
Douglas Gregordb1cde72011-11-16 00:09:06 +0000208bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
209 StringRef Filename) {
210 // Find the module map file.
211 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
212 if (!ModuleMap) {
213 CI.getDiagnostics().Report(diag::err_module_map_not_found)
214 << Filename;
215 return false;
216 }
217
218 // Parse the module map file.
219 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
220 if (HS.loadModuleMapFile(ModuleMap))
221 return false;
222
223 if (CI.getLangOpts().CurrentModule.empty()) {
224 CI.getDiagnostics().Report(diag::err_missing_module_name);
225
226 // FIXME: Eventually, we could consider asking whether there was just
227 // a single module described in the module map, and use that as a
228 // default. Then it would be fairly trivial to just "compile" a module
229 // map with a single module (the common case).
230 return false;
231 }
232
233 // Dig out the module definition.
Douglas Gregore434ec72012-01-29 17:08:11 +0000234 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
235 /*AllowSearch=*/false);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000236 if (!Module) {
237 CI.getDiagnostics().Report(diag::err_missing_module)
238 << CI.getLangOpts().CurrentModule << Filename;
239
240 return false;
241 }
Douglas Gregor51f564f2011-12-31 04:05:44 +0000242
243 // Check whether we can build this module at all.
244 StringRef Feature;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000245 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Feature)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000246 CI.getDiagnostics().Report(diag::err_module_unavailable)
247 << Module->getFullModuleName()
248 << Feature;
249
250 return false;
251 }
252
Douglas Gregor10694ce2011-12-08 17:39:04 +0000253 // Do we have an umbrella header for this module?
254 const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader();
255
Douglas Gregor261e75b2011-11-16 17:04:00 +0000256 // Collect the set of #includes we need to build the module.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000257 SmallString<256> HeaderContents;
Douglas Gregor752769f2012-01-05 00:04:05 +0000258 collectModuleHeaderIncludes(CI.getLangOpts(), CI.getFileManager(),
259 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
Douglas Gregor2f04f182012-02-02 18:42:48 +0000260 Module, HeaderContents);
Douglas Gregor10694ce2011-12-08 17:39:04 +0000261 if (UmbrellaHeader && HeaderContents.empty()) {
Douglas Gregor261e75b2011-11-16 17:04:00 +0000262 // Simple case: we have an umbrella header and there are no additional
263 // includes, we can just parse the umbrella header directly.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000264 setCurrentInput(FrontendInputFile(UmbrellaHeader->getName(),
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000265 getCurrentFileKind(),
266 Module->IsSystem));
Douglas Gregor261e75b2011-11-16 17:04:00 +0000267 return true;
Douglas Gregordb1cde72011-11-16 00:09:06 +0000268 }
269
Douglas Gregor261e75b2011-11-16 17:04:00 +0000270 FileManager &FileMgr = CI.getFileManager();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000271 SmallString<128> HeaderName;
Douglas Gregor261e75b2011-11-16 17:04:00 +0000272 time_t ModTime;
Douglas Gregor10694ce2011-12-08 17:39:04 +0000273 if (UmbrellaHeader) {
Douglas Gregor261e75b2011-11-16 17:04:00 +0000274 // Read in the umbrella header.
275 // FIXME: Go through the source manager; the umbrella header may have
276 // been overridden.
277 std::string ErrorStr;
278 llvm::MemoryBuffer *UmbrellaContents
Douglas Gregor10694ce2011-12-08 17:39:04 +0000279 = FileMgr.getBufferForFile(UmbrellaHeader, &ErrorStr);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000280 if (!UmbrellaContents) {
281 CI.getDiagnostics().Report(diag::err_missing_umbrella_header)
Douglas Gregor10694ce2011-12-08 17:39:04 +0000282 << UmbrellaHeader->getName() << ErrorStr;
Douglas Gregor261e75b2011-11-16 17:04:00 +0000283 return false;
284 }
285
286 // Combine the contents of the umbrella header with the automatically-
287 // generated includes.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000288 SmallString<256> OldContents = HeaderContents;
Douglas Gregor261e75b2011-11-16 17:04:00 +0000289 HeaderContents = UmbrellaContents->getBuffer();
290 HeaderContents += "\n\n";
291 HeaderContents += "/* Module includes */\n";
292 HeaderContents += OldContents;
293
294 // Pretend that we're parsing the umbrella header.
Douglas Gregor10694ce2011-12-08 17:39:04 +0000295 HeaderName = UmbrellaHeader->getName();
296 ModTime = UmbrellaHeader->getModificationTime();
Douglas Gregor261e75b2011-11-16 17:04:00 +0000297
298 delete UmbrellaContents;
299 } else {
300 // Pick an innocuous-sounding name for the umbrella header.
301 HeaderName = Module->Name + ".h";
302 if (FileMgr.getFile(HeaderName, /*OpenFile=*/false,
303 /*CacheFailure=*/false)) {
304 // Try again!
305 HeaderName = Module->Name + "-module.h";
306 if (FileMgr.getFile(HeaderName, /*OpenFile=*/false,
307 /*CacheFailure=*/false)) {
308 // Pick something ridiculous and go with it.
309 HeaderName = Module->Name + "-module.hmod";
310 }
311 }
312 ModTime = time(0);
313 }
314
315 // Remap the contents of the header name we're using to our synthesized
316 // buffer.
317 const FileEntry *HeaderFile = FileMgr.getVirtualFile(HeaderName,
318 HeaderContents.size(),
319 ModTime);
320 llvm::MemoryBuffer *HeaderContentsBuf
321 = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000322 CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf);
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000323 setCurrentInput(FrontendInputFile(HeaderName, getCurrentFileKind(),
324 Module->IsSystem));
Douglas Gregordb1cde72011-11-16 00:09:06 +0000325 return true;
326}
327
328bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
329 StringRef InFile,
330 std::string &Sysroot,
331 std::string &OutputFile,
332 raw_ostream *&OS) {
333 // If no output file was provided, figure out where this module would go
334 // in the module cache.
335 if (CI.getFrontendOpts().OutputFile.empty()) {
336 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000337 SmallString<256> ModuleFileName(HS.getModuleCachePath());
Douglas Gregordb1cde72011-11-16 00:09:06 +0000338 llvm::sys::path::append(ModuleFileName,
339 CI.getLangOpts().CurrentModule + ".pcm");
340 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
341 }
342
343 // We use createOutputFile here because this is exposed via libclang, and we
344 // must disable the RemoveFileOnSignal behavior.
345 // We use a temporary to avoid race conditions.
346 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
347 /*RemoveFileOnSignal=*/false, InFile,
Daniel Dunbar12f28ab2012-03-03 00:36:02 +0000348 /*Extension=*/"", /*useTemporary=*/true,
349 /*CreateMissingDirectories=*/true);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000350 if (!OS)
351 return true;
352
353 OutputFile = CI.getFrontendOpts().OutputFile;
354 return false;
355}
356
Daniel Dunbar8305d012009-11-14 10:42:46 +0000357ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000358 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000359 return new ASTConsumer();
360}
361
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000362//===----------------------------------------------------------------------===//
363// Preprocessor Actions
364//===----------------------------------------------------------------------===//
365
366void DumpRawTokensAction::ExecuteAction() {
367 Preprocessor &PP = getCompilerInstance().getPreprocessor();
368 SourceManager &SM = PP.getSourceManager();
369
370 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000371 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikie4e4d0842012-03-11 07:00:24 +0000372 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000373 RawLex.SetKeepWhitespaceMode(true);
374
375 Token RawTok;
376 RawLex.LexFromRawLexer(RawTok);
377 while (RawTok.isNot(tok::eof)) {
378 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000379 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000380 RawLex.LexFromRawLexer(RawTok);
381 }
382}
383
384void DumpTokensAction::ExecuteAction() {
385 Preprocessor &PP = getCompilerInstance().getPreprocessor();
386 // Start preprocessing the specified input file.
387 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000388 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000389 do {
390 PP.Lex(Tok);
391 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000392 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000393 } while (Tok.isNot(tok::eof));
394}
395
396void GeneratePTHAction::ExecuteAction() {
397 CompilerInstance &CI = getCompilerInstance();
398 if (CI.getFrontendOpts().OutputFile.empty() ||
399 CI.getFrontendOpts().OutputFile == "-") {
400 // FIXME: Don't fail this way.
401 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000402 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000403 }
404 llvm::raw_fd_ostream *OS =
405 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000406 if (!OS) return;
407
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000408 CacheTokens(CI.getPreprocessor(), OS);
409}
410
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000411void PreprocessOnlyAction::ExecuteAction() {
412 Preprocessor &PP = getCompilerInstance().getPreprocessor();
413
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000414 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000415 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000416
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000417 Token Tok;
418 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000419 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000420 do {
421 PP.Lex(Tok);
422 } while (Tok.isNot(tok::eof));
423}
424
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000425void PrintPreprocessedAction::ExecuteAction() {
426 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000427 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroffd57d7c02010-01-05 17:33:23 +0000428 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000429 //
430 // Look to see what type of line endings the file uses. If there's a
431 // CRLF, then we won't open the file up in binary mode. If there is
432 // just an LF or CR, then we will open the file up in binary mode.
433 // In this fashion, the output format should match the input format, unless
434 // the input format has inconsistent line endings.
435 //
436 // This should be a relatively fast operation since most files won't have
437 // all of their source code on a single line. However, that is still a
438 // concern, so if we scan for too long, we'll just assume the file should
439 // be opened in binary mode.
440 bool BinaryMode = true;
441 bool InvalidFile = false;
442 const SourceManager& SM = CI.getSourceManager();
443 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
444 &InvalidFile);
445 if (!InvalidFile) {
446 const char *cur = Buffer->getBufferStart();
447 const char *end = Buffer->getBufferEnd();
448 const char *next = (cur != end) ? cur + 1 : end;
449
450 // Limit ourselves to only scanning 256 characters into the source
451 // file. This is mostly a sanity check in case the file has no
452 // newlines whatsoever.
453 if (end - cur > 256) end = cur + 256;
454
455 while (next < end) {
456 if (*cur == 0x0D) { // CR
457 if (*next == 0x0A) // CRLF
458 BinaryMode = false;
459
460 break;
461 } else if (*cur == 0x0A) // LF
462 break;
463
464 ++cur, ++next;
465 }
466 }
467
468 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000469 if (!OS) return;
470
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000471 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
472 CI.getPreprocessorOutputOpts());
473}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000474
475void PrintPreambleAction::ExecuteAction() {
476 switch (getCurrentFileKind()) {
477 case IK_C:
478 case IK_CXX:
479 case IK_ObjC:
480 case IK_ObjCXX:
481 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000482 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000483 break;
484
485 case IK_None:
486 case IK_Asm:
487 case IK_PreprocessedC:
488 case IK_PreprocessedCXX:
489 case IK_PreprocessedObjC:
490 case IK_PreprocessedObjCXX:
491 case IK_AST:
492 case IK_LLVM_IR:
493 // We can't do anything with these.
494 return;
495 }
496
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000497 CompilerInstance &CI = getCompilerInstance();
498 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000499 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000500 if (Buffer) {
Argyrios Kyrtzidis03c107a2011-08-25 20:39:19 +0000501 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000502 llvm::outs().write(Buffer->getBufferStart(), Preamble);
503 delete Buffer;
504 }
505}