blob: 47063f78b5d93808003a9b122776578d404a74c9 [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
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000134static SmallVectorImpl<char> &
135operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
136 Includes.append(RHS.begin(), RHS.end());
137 return Includes;
138}
139
140static void addHeaderInclude(StringRef HeaderName,
141 SmallVectorImpl<char> &Includes,
142 const LangOptions &LangOpts) {
143 if (LangOpts.ObjC1)
144 Includes += "#import \"";
145 else
146 Includes += "#include \"";
147 Includes += HeaderName;
148 Includes += "\"\n";
149}
150
151static void addHeaderInclude(const FileEntry *Header,
152 SmallVectorImpl<char> &Includes,
153 const LangOptions &LangOpts) {
154 addHeaderInclude(Header->getName(), Includes, LangOpts);
155}
156
Douglas Gregor261e75b2011-11-16 17:04:00 +0000157/// \brief Collect the set of header includes needed to construct the given
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000158/// module and update the TopHeaders file set of the module.
Douglas Gregor261e75b2011-11-16 17:04:00 +0000159///
160/// \param Module The module we're collecting includes from.
Douglas Gregor8075ce62011-12-06 17:15:11 +0000161///
James Dennett5e9dda62012-06-15 21:48:19 +0000162/// \param Includes Will be augmented with the set of \#includes or \#imports
Douglas Gregor8075ce62011-12-06 17:15:11 +0000163/// needed to load all of the named headers.
Douglas Gregor261e75b2011-11-16 17:04:00 +0000164static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
Douglas Gregor752769f2012-01-05 00:04:05 +0000165 FileManager &FileMgr,
166 ModuleMap &ModMap,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000167 clang::Module *Module,
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000168 SmallVectorImpl<char> &Includes) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000169 // Don't collect any headers for unavailable modules.
170 if (!Module->isAvailable())
171 return;
172
Douglas Gregor8075ce62011-12-06 17:15:11 +0000173 // Add includes for each of these headers.
Douglas Gregor2f04f182012-02-02 18:42:48 +0000174 for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000175 const FileEntry *Header = Module->Headers[I];
176 Module->TopHeaders.insert(Header);
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000177 addHeaderInclude(Header, Includes, LangOpts);
Douglas Gregor2f04f182012-02-02 18:42:48 +0000178 }
Douglas Gregor8075ce62011-12-06 17:15:11 +0000179
Douglas Gregor10694ce2011-12-08 17:39:04 +0000180 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000181 Module->TopHeaders.insert(UmbrellaHeader);
Douglas Gregor2f04f182012-02-02 18:42:48 +0000182 if (Module->Parent) {
183 // Include the umbrella header for submodules.
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000184 addHeaderInclude(UmbrellaHeader, Includes, LangOpts);
Douglas Gregor2f04f182012-02-02 18:42:48 +0000185 }
Douglas Gregor77d029f2011-12-08 19:11:24 +0000186 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
Douglas Gregor752769f2012-01-05 00:04:05 +0000187 // Add all of the headers we find in this subdirectory.
Douglas Gregor77d029f2011-12-08 19:11:24 +0000188 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000189 SmallString<128> DirNative;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000190 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
Douglas Gregor3b29bb92011-12-12 19:13:53 +0000191 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
192 DirEnd;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000193 Dir != DirEnd && !EC; Dir.increment(EC)) {
194 // Check whether this entry has an extension typically associated with
195 // headers.
196 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
197 .Cases(".h", ".H", ".hh", ".hpp", true)
198 .Default(false))
199 continue;
200
Douglas Gregor752769f2012-01-05 00:04:05 +0000201 // If this header is marked 'unavailable' in this module, don't include
202 // it.
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000203 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
Douglas Gregor752769f2012-01-05 00:04:05 +0000204 if (ModMap.isHeaderInUnavailableModule(Header))
205 continue;
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000206 Module->TopHeaders.insert(Header);
207 }
Douglas Gregor752769f2012-01-05 00:04:05 +0000208
Douglas Gregor2f04f182012-02-02 18:42:48 +0000209 // Include this header umbrella header for submodules.
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000210 addHeaderInclude(Dir->path(), Includes, LangOpts);
Douglas Gregor77d029f2011-12-08 19:11:24 +0000211 }
Douglas Gregor261e75b2011-11-16 17:04:00 +0000212 }
213
214 // Recurse into submodules.
Douglas Gregorb7a78192012-01-04 23:32:19 +0000215 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
216 SubEnd = Module->submodule_end();
Douglas Gregor8075ce62011-12-06 17:15:11 +0000217 Sub != SubEnd; ++Sub)
Douglas Gregor2f04f182012-02-02 18:42:48 +0000218 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000219}
220
Douglas Gregordb1cde72011-11-16 00:09:06 +0000221bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
222 StringRef Filename) {
223 // Find the module map file.
224 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
225 if (!ModuleMap) {
226 CI.getDiagnostics().Report(diag::err_module_map_not_found)
227 << Filename;
228 return false;
229 }
230
231 // Parse the module map file.
232 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
233 if (HS.loadModuleMapFile(ModuleMap))
234 return false;
235
236 if (CI.getLangOpts().CurrentModule.empty()) {
237 CI.getDiagnostics().Report(diag::err_missing_module_name);
238
239 // FIXME: Eventually, we could consider asking whether there was just
240 // a single module described in the module map, and use that as a
241 // default. Then it would be fairly trivial to just "compile" a module
242 // map with a single module (the common case).
243 return false;
244 }
245
246 // Dig out the module definition.
Douglas Gregore434ec72012-01-29 17:08:11 +0000247 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
248 /*AllowSearch=*/false);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000249 if (!Module) {
250 CI.getDiagnostics().Report(diag::err_missing_module)
251 << CI.getLangOpts().CurrentModule << Filename;
252
253 return false;
254 }
Douglas Gregor51f564f2011-12-31 04:05:44 +0000255
256 // Check whether we can build this module at all.
257 StringRef Feature;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000258 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Feature)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000259 CI.getDiagnostics().Report(diag::err_module_unavailable)
260 << Module->getFullModuleName()
261 << Feature;
262
263 return false;
264 }
265
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000266 FileManager &FileMgr = CI.getFileManager();
267
Douglas Gregor261e75b2011-11-16 17:04:00 +0000268 // Collect the set of #includes we need to build the module.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000269 SmallString<256> HeaderContents;
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000270 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
271 addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts());
272 collectModuleHeaderIncludes(CI.getLangOpts(), FileMgr,
Douglas Gregor752769f2012-01-05 00:04:05 +0000273 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
Douglas Gregor2f04f182012-02-02 18:42:48 +0000274 Module, HeaderContents);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000275
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000276 StringRef InputName = Module::getModuleInputBufferName();
277
278 // We consistently construct a buffer as input to build the module.
279 // This means the main file for modules will always be a virtual one.
280 // FIXME: Maybe allow using a memory buffer as input directly instead of
281 // messing with virtual files.
282 const FileEntry *HeaderFile = FileMgr.getVirtualFile(InputName,
Douglas Gregor261e75b2011-11-16 17:04:00 +0000283 HeaderContents.size(),
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000284 time(0));
Douglas Gregor261e75b2011-11-16 17:04:00 +0000285 llvm::MemoryBuffer *HeaderContentsBuf
286 = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000287 CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf);
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000288 setCurrentInput(FrontendInputFile(InputName, getCurrentFileKind(),
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000289 Module->IsSystem));
Douglas Gregordb1cde72011-11-16 00:09:06 +0000290 return true;
291}
292
293bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
294 StringRef InFile,
295 std::string &Sysroot,
296 std::string &OutputFile,
297 raw_ostream *&OS) {
298 // If no output file was provided, figure out where this module would go
299 // in the module cache.
300 if (CI.getFrontendOpts().OutputFile.empty()) {
301 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000302 SmallString<256> ModuleFileName(HS.getModuleCachePath());
Douglas Gregordb1cde72011-11-16 00:09:06 +0000303 llvm::sys::path::append(ModuleFileName,
304 CI.getLangOpts().CurrentModule + ".pcm");
305 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
306 }
307
308 // We use createOutputFile here because this is exposed via libclang, and we
309 // must disable the RemoveFileOnSignal behavior.
310 // We use a temporary to avoid race conditions.
311 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
312 /*RemoveFileOnSignal=*/false, InFile,
Daniel Dunbar12f28ab2012-03-03 00:36:02 +0000313 /*Extension=*/"", /*useTemporary=*/true,
314 /*CreateMissingDirectories=*/true);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000315 if (!OS)
316 return true;
317
318 OutputFile = CI.getFrontendOpts().OutputFile;
319 return false;
320}
321
Daniel Dunbar8305d012009-11-14 10:42:46 +0000322ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000323 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000324 return new ASTConsumer();
325}
326
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000327//===----------------------------------------------------------------------===//
328// Preprocessor Actions
329//===----------------------------------------------------------------------===//
330
331void DumpRawTokensAction::ExecuteAction() {
332 Preprocessor &PP = getCompilerInstance().getPreprocessor();
333 SourceManager &SM = PP.getSourceManager();
334
335 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000336 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikie4e4d0842012-03-11 07:00:24 +0000337 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000338 RawLex.SetKeepWhitespaceMode(true);
339
340 Token RawTok;
341 RawLex.LexFromRawLexer(RawTok);
342 while (RawTok.isNot(tok::eof)) {
343 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000344 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000345 RawLex.LexFromRawLexer(RawTok);
346 }
347}
348
349void DumpTokensAction::ExecuteAction() {
350 Preprocessor &PP = getCompilerInstance().getPreprocessor();
351 // Start preprocessing the specified input file.
352 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000353 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000354 do {
355 PP.Lex(Tok);
356 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000357 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000358 } while (Tok.isNot(tok::eof));
359}
360
361void GeneratePTHAction::ExecuteAction() {
362 CompilerInstance &CI = getCompilerInstance();
363 if (CI.getFrontendOpts().OutputFile.empty() ||
364 CI.getFrontendOpts().OutputFile == "-") {
365 // FIXME: Don't fail this way.
366 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000367 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000368 }
369 llvm::raw_fd_ostream *OS =
370 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000371 if (!OS) return;
372
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000373 CacheTokens(CI.getPreprocessor(), OS);
374}
375
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000376void PreprocessOnlyAction::ExecuteAction() {
377 Preprocessor &PP = getCompilerInstance().getPreprocessor();
378
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000379 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000380 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000381
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000382 Token Tok;
383 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000384 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000385 do {
386 PP.Lex(Tok);
387 } while (Tok.isNot(tok::eof));
388}
389
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000390void PrintPreprocessedAction::ExecuteAction() {
391 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000392 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroffd57d7c02010-01-05 17:33:23 +0000393 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000394 //
395 // Look to see what type of line endings the file uses. If there's a
396 // CRLF, then we won't open the file up in binary mode. If there is
397 // just an LF or CR, then we will open the file up in binary mode.
398 // In this fashion, the output format should match the input format, unless
399 // the input format has inconsistent line endings.
400 //
401 // This should be a relatively fast operation since most files won't have
402 // all of their source code on a single line. However, that is still a
403 // concern, so if we scan for too long, we'll just assume the file should
404 // be opened in binary mode.
405 bool BinaryMode = true;
406 bool InvalidFile = false;
407 const SourceManager& SM = CI.getSourceManager();
408 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
409 &InvalidFile);
410 if (!InvalidFile) {
411 const char *cur = Buffer->getBufferStart();
412 const char *end = Buffer->getBufferEnd();
413 const char *next = (cur != end) ? cur + 1 : end;
414
415 // Limit ourselves to only scanning 256 characters into the source
416 // file. This is mostly a sanity check in case the file has no
417 // newlines whatsoever.
418 if (end - cur > 256) end = cur + 256;
419
420 while (next < end) {
421 if (*cur == 0x0D) { // CR
422 if (*next == 0x0A) // CRLF
423 BinaryMode = false;
424
425 break;
426 } else if (*cur == 0x0A) // LF
427 break;
428
429 ++cur, ++next;
430 }
431 }
432
433 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000434 if (!OS) return;
435
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000436 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
437 CI.getPreprocessorOutputOpts());
438}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000439
440void PrintPreambleAction::ExecuteAction() {
441 switch (getCurrentFileKind()) {
442 case IK_C:
443 case IK_CXX:
444 case IK_ObjC:
445 case IK_ObjCXX:
446 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000447 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000448 break;
449
450 case IK_None:
451 case IK_Asm:
452 case IK_PreprocessedC:
453 case IK_PreprocessedCXX:
454 case IK_PreprocessedObjC:
455 case IK_PreprocessedObjCXX:
456 case IK_AST:
457 case IK_LLVM_IR:
458 // We can't do anything with these.
459 return;
460 }
461
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000462 CompilerInstance &CI = getCompilerInstance();
463 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000464 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000465 if (Buffer) {
Argyrios Kyrtzidis03c107a2011-08-25 20:39:19 +0000466 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000467 llvm::outs().write(Buffer->getBufferStart(), Preamble);
468 delete Buffer;
469 }
470}