blob: ffd3d907295faedb85c2cfc657b1980758481a84 [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
John McCallf3514242010-11-24 11:21:45 +000059ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000060 StringRef InFile) {
61 raw_ostream *OS;
John McCallf3514242010-11-24 11:21:45 +000062 if (CI.getFrontendOpts().OutputFile.empty())
63 OS = &llvm::outs();
64 else
65 OS = CI.createDefaultOutputFile(false, InFile);
66 if (!OS) return 0;
67 return CreateASTDumperXML(*OS);
68}
69
Daniel Dunbar8305d012009-11-14 10:42:46 +000070ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000071 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000072 return CreateASTViewer();
73}
74
75ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000076 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000077 return CreateDeclContextPrinter();
78}
79
Daniel Dunbar8305d012009-11-14 10:42:46 +000080ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000081 StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000082 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000083 std::string OutputFile;
Chris Lattner5f9e2722011-07-23 10:55:15 +000084 raw_ostream *OS = 0;
Douglas Gregor9293ba82011-08-25 22:35:51 +000085 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
Daniel Dunbar8305d012009-11-14 10:42:46 +000086 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000087
Douglas Gregor832d6202011-07-22 16:35:34 +000088 if (!CI.getFrontendOpts().RelocatablePCH)
89 Sysroot.clear();
Douglas Gregora8cc6ce2011-11-30 04:39:39 +000090 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, 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
Douglas Gregora8cc6ce2011-11-30 04:39:39 +0000125 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
Douglas Gregordb1cde72011-11-16 00:09:06 +0000126 Sysroot, OS);
127}
128
Douglas Gregor261e75b2011-11-16 17:04:00 +0000129/// \brief Collect the set of header includes needed to construct the given
130/// module.
131///
132/// \param Module The module we're collecting includes from.
Douglas Gregor8075ce62011-12-06 17:15:11 +0000133///
James Dennett5e9dda62012-06-15 21:48:19 +0000134/// \param Includes Will be augmented with the set of \#includes or \#imports
Douglas Gregor8075ce62011-12-06 17:15:11 +0000135/// needed to load all of the named headers.
Douglas Gregor261e75b2011-11-16 17:04:00 +0000136static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
Douglas Gregor752769f2012-01-05 00:04:05 +0000137 FileManager &FileMgr,
138 ModuleMap &ModMap,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000139 clang::Module *Module,
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000140 SmallString<256> &Includes) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000141 // Don't collect any headers for unavailable modules.
142 if (!Module->isAvailable())
143 return;
144
Douglas Gregor8075ce62011-12-06 17:15:11 +0000145 // Add includes for each of these headers.
Douglas Gregor2f04f182012-02-02 18:42:48 +0000146 for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
147 if (LangOpts.ObjC1)
148 Includes += "#import \"";
149 else
150 Includes += "#include \"";
151 Includes += Module->Headers[I]->getName();
152 Includes += "\"\n";
153 }
Douglas Gregor8075ce62011-12-06 17:15:11 +0000154
Douglas Gregor10694ce2011-12-08 17:39:04 +0000155 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
Douglas Gregor2f04f182012-02-02 18:42:48 +0000156 if (Module->Parent) {
157 // Include the umbrella header for submodules.
158 if (LangOpts.ObjC1)
159 Includes += "#import \"";
160 else
161 Includes += "#include \"";
162 Includes += UmbrellaHeader->getName();
163 Includes += "\"\n";
164 }
Douglas Gregor77d029f2011-12-08 19:11:24 +0000165 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
Douglas Gregor752769f2012-01-05 00:04:05 +0000166 // Add all of the headers we find in this subdirectory.
Douglas Gregor77d029f2011-12-08 19:11:24 +0000167 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000168 SmallString<128> DirNative;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000169 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
Douglas Gregor3b29bb92011-12-12 19:13:53 +0000170 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
171 DirEnd;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000172 Dir != DirEnd && !EC; Dir.increment(EC)) {
173 // Check whether this entry has an extension typically associated with
174 // headers.
175 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
176 .Cases(".h", ".H", ".hh", ".hpp", true)
177 .Default(false))
178 continue;
179
Douglas Gregor752769f2012-01-05 00:04:05 +0000180 // If this header is marked 'unavailable' in this module, don't include
181 // it.
182 if (const FileEntry *Header = FileMgr.getFile(Dir->path()))
183 if (ModMap.isHeaderInUnavailableModule(Header))
184 continue;
185
Douglas Gregor2f04f182012-02-02 18:42:48 +0000186 // Include this header umbrella header for submodules.
187 if (LangOpts.ObjC1)
188 Includes += "#import \"";
189 else
190 Includes += "#include \"";
191 Includes += Dir->path();
192 Includes += "\"\n";
Douglas Gregor77d029f2011-12-08 19:11:24 +0000193 }
Douglas Gregor261e75b2011-11-16 17:04:00 +0000194 }
195
196 // Recurse into submodules.
Douglas Gregorb7a78192012-01-04 23:32:19 +0000197 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
198 SubEnd = Module->submodule_end();
Douglas Gregor8075ce62011-12-06 17:15:11 +0000199 Sub != SubEnd; ++Sub)
Douglas Gregor2f04f182012-02-02 18:42:48 +0000200 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000201}
202
Douglas Gregordb1cde72011-11-16 00:09:06 +0000203bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
204 StringRef Filename) {
205 // Find the module map file.
206 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
207 if (!ModuleMap) {
208 CI.getDiagnostics().Report(diag::err_module_map_not_found)
209 << Filename;
210 return false;
211 }
212
213 // Parse the module map file.
214 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
215 if (HS.loadModuleMapFile(ModuleMap))
216 return false;
217
218 if (CI.getLangOpts().CurrentModule.empty()) {
219 CI.getDiagnostics().Report(diag::err_missing_module_name);
220
221 // FIXME: Eventually, we could consider asking whether there was just
222 // a single module described in the module map, and use that as a
223 // default. Then it would be fairly trivial to just "compile" a module
224 // map with a single module (the common case).
225 return false;
226 }
227
228 // Dig out the module definition.
Douglas Gregore434ec72012-01-29 17:08:11 +0000229 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
230 /*AllowSearch=*/false);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000231 if (!Module) {
232 CI.getDiagnostics().Report(diag::err_missing_module)
233 << CI.getLangOpts().CurrentModule << Filename;
234
235 return false;
236 }
Douglas Gregor51f564f2011-12-31 04:05:44 +0000237
238 // Check whether we can build this module at all.
239 StringRef Feature;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000240 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Feature)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000241 CI.getDiagnostics().Report(diag::err_module_unavailable)
242 << Module->getFullModuleName()
243 << Feature;
244
245 return false;
246 }
247
Douglas Gregor10694ce2011-12-08 17:39:04 +0000248 // Do we have an umbrella header for this module?
249 const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader();
250
Douglas Gregor261e75b2011-11-16 17:04:00 +0000251 // Collect the set of #includes we need to build the module.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000252 SmallString<256> HeaderContents;
Douglas Gregor752769f2012-01-05 00:04:05 +0000253 collectModuleHeaderIncludes(CI.getLangOpts(), CI.getFileManager(),
254 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
Douglas Gregor2f04f182012-02-02 18:42:48 +0000255 Module, HeaderContents);
Douglas Gregor10694ce2011-12-08 17:39:04 +0000256 if (UmbrellaHeader && HeaderContents.empty()) {
Douglas Gregor261e75b2011-11-16 17:04:00 +0000257 // Simple case: we have an umbrella header and there are no additional
258 // includes, we can just parse the umbrella header directly.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000259 setCurrentInput(FrontendInputFile(UmbrellaHeader->getName(),
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000260 getCurrentFileKind(),
261 Module->IsSystem));
Douglas Gregor261e75b2011-11-16 17:04:00 +0000262 return true;
Douglas Gregordb1cde72011-11-16 00:09:06 +0000263 }
264
Douglas Gregor261e75b2011-11-16 17:04:00 +0000265 FileManager &FileMgr = CI.getFileManager();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000266 SmallString<128> HeaderName;
Douglas Gregor261e75b2011-11-16 17:04:00 +0000267 time_t ModTime;
Douglas Gregor10694ce2011-12-08 17:39:04 +0000268 if (UmbrellaHeader) {
Douglas Gregor261e75b2011-11-16 17:04:00 +0000269 // Read in the umbrella header.
270 // FIXME: Go through the source manager; the umbrella header may have
271 // been overridden.
272 std::string ErrorStr;
273 llvm::MemoryBuffer *UmbrellaContents
Douglas Gregor10694ce2011-12-08 17:39:04 +0000274 = FileMgr.getBufferForFile(UmbrellaHeader, &ErrorStr);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000275 if (!UmbrellaContents) {
276 CI.getDiagnostics().Report(diag::err_missing_umbrella_header)
Douglas Gregor10694ce2011-12-08 17:39:04 +0000277 << UmbrellaHeader->getName() << ErrorStr;
Douglas Gregor261e75b2011-11-16 17:04:00 +0000278 return false;
279 }
280
281 // Combine the contents of the umbrella header with the automatically-
282 // generated includes.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000283 SmallString<256> OldContents = HeaderContents;
Douglas Gregor261e75b2011-11-16 17:04:00 +0000284 HeaderContents = UmbrellaContents->getBuffer();
285 HeaderContents += "\n\n";
286 HeaderContents += "/* Module includes */\n";
287 HeaderContents += OldContents;
288
289 // Pretend that we're parsing the umbrella header.
Douglas Gregor10694ce2011-12-08 17:39:04 +0000290 HeaderName = UmbrellaHeader->getName();
291 ModTime = UmbrellaHeader->getModificationTime();
Douglas Gregor261e75b2011-11-16 17:04:00 +0000292
293 delete UmbrellaContents;
294 } else {
295 // Pick an innocuous-sounding name for the umbrella header.
296 HeaderName = Module->Name + ".h";
297 if (FileMgr.getFile(HeaderName, /*OpenFile=*/false,
298 /*CacheFailure=*/false)) {
299 // Try again!
300 HeaderName = Module->Name + "-module.h";
301 if (FileMgr.getFile(HeaderName, /*OpenFile=*/false,
302 /*CacheFailure=*/false)) {
303 // Pick something ridiculous and go with it.
304 HeaderName = Module->Name + "-module.hmod";
305 }
306 }
307 ModTime = time(0);
308 }
309
310 // Remap the contents of the header name we're using to our synthesized
311 // buffer.
312 const FileEntry *HeaderFile = FileMgr.getVirtualFile(HeaderName,
313 HeaderContents.size(),
314 ModTime);
315 llvm::MemoryBuffer *HeaderContentsBuf
316 = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000317 CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf);
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000318 setCurrentInput(FrontendInputFile(HeaderName, getCurrentFileKind(),
319 Module->IsSystem));
Douglas Gregordb1cde72011-11-16 00:09:06 +0000320 return true;
321}
322
323bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
324 StringRef InFile,
325 std::string &Sysroot,
326 std::string &OutputFile,
327 raw_ostream *&OS) {
328 // If no output file was provided, figure out where this module would go
329 // in the module cache.
330 if (CI.getFrontendOpts().OutputFile.empty()) {
331 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000332 SmallString<256> ModuleFileName(HS.getModuleCachePath());
Douglas Gregordb1cde72011-11-16 00:09:06 +0000333 llvm::sys::path::append(ModuleFileName,
334 CI.getLangOpts().CurrentModule + ".pcm");
335 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
336 }
337
338 // We use createOutputFile here because this is exposed via libclang, and we
339 // must disable the RemoveFileOnSignal behavior.
340 // We use a temporary to avoid race conditions.
341 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
342 /*RemoveFileOnSignal=*/false, InFile,
Daniel Dunbar12f28ab2012-03-03 00:36:02 +0000343 /*Extension=*/"", /*useTemporary=*/true,
344 /*CreateMissingDirectories=*/true);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000345 if (!OS)
346 return true;
347
348 OutputFile = CI.getFrontendOpts().OutputFile;
349 return false;
350}
351
Daniel Dunbar8305d012009-11-14 10:42:46 +0000352ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000353 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000354 return new ASTConsumer();
355}
356
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000357//===----------------------------------------------------------------------===//
358// Preprocessor Actions
359//===----------------------------------------------------------------------===//
360
361void DumpRawTokensAction::ExecuteAction() {
362 Preprocessor &PP = getCompilerInstance().getPreprocessor();
363 SourceManager &SM = PP.getSourceManager();
364
365 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000366 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikie4e4d0842012-03-11 07:00:24 +0000367 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000368 RawLex.SetKeepWhitespaceMode(true);
369
370 Token RawTok;
371 RawLex.LexFromRawLexer(RawTok);
372 while (RawTok.isNot(tok::eof)) {
373 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000374 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000375 RawLex.LexFromRawLexer(RawTok);
376 }
377}
378
379void DumpTokensAction::ExecuteAction() {
380 Preprocessor &PP = getCompilerInstance().getPreprocessor();
381 // Start preprocessing the specified input file.
382 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000383 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000384 do {
385 PP.Lex(Tok);
386 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000387 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000388 } while (Tok.isNot(tok::eof));
389}
390
391void GeneratePTHAction::ExecuteAction() {
392 CompilerInstance &CI = getCompilerInstance();
393 if (CI.getFrontendOpts().OutputFile.empty() ||
394 CI.getFrontendOpts().OutputFile == "-") {
395 // FIXME: Don't fail this way.
396 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000397 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000398 }
399 llvm::raw_fd_ostream *OS =
400 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000401 if (!OS) return;
402
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000403 CacheTokens(CI.getPreprocessor(), OS);
404}
405
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000406void PreprocessOnlyAction::ExecuteAction() {
407 Preprocessor &PP = getCompilerInstance().getPreprocessor();
408
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000409 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000410 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000411
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000412 Token Tok;
413 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000414 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000415 do {
416 PP.Lex(Tok);
417 } while (Tok.isNot(tok::eof));
418}
419
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000420void PrintPreprocessedAction::ExecuteAction() {
421 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000422 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroffd57d7c02010-01-05 17:33:23 +0000423 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000424 //
425 // Look to see what type of line endings the file uses. If there's a
426 // CRLF, then we won't open the file up in binary mode. If there is
427 // just an LF or CR, then we will open the file up in binary mode.
428 // In this fashion, the output format should match the input format, unless
429 // the input format has inconsistent line endings.
430 //
431 // This should be a relatively fast operation since most files won't have
432 // all of their source code on a single line. However, that is still a
433 // concern, so if we scan for too long, we'll just assume the file should
434 // be opened in binary mode.
435 bool BinaryMode = true;
436 bool InvalidFile = false;
437 const SourceManager& SM = CI.getSourceManager();
438 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
439 &InvalidFile);
440 if (!InvalidFile) {
441 const char *cur = Buffer->getBufferStart();
442 const char *end = Buffer->getBufferEnd();
443 const char *next = (cur != end) ? cur + 1 : end;
444
445 // Limit ourselves to only scanning 256 characters into the source
446 // file. This is mostly a sanity check in case the file has no
447 // newlines whatsoever.
448 if (end - cur > 256) end = cur + 256;
449
450 while (next < end) {
451 if (*cur == 0x0D) { // CR
452 if (*next == 0x0A) // CRLF
453 BinaryMode = false;
454
455 break;
456 } else if (*cur == 0x0A) // LF
457 break;
458
459 ++cur, ++next;
460 }
461 }
462
463 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000464 if (!OS) return;
465
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000466 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
467 CI.getPreprocessorOutputOpts());
468}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000469
470void PrintPreambleAction::ExecuteAction() {
471 switch (getCurrentFileKind()) {
472 case IK_C:
473 case IK_CXX:
474 case IK_ObjC:
475 case IK_ObjCXX:
476 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000477 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000478 break;
479
480 case IK_None:
481 case IK_Asm:
482 case IK_PreprocessedC:
483 case IK_PreprocessedCXX:
484 case IK_PreprocessedObjC:
485 case IK_PreprocessedObjCXX:
486 case IK_AST:
487 case IK_LLVM_IR:
488 // We can't do anything with these.
489 return;
490 }
491
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000492 CompilerInstance &CI = getCompilerInstance();
493 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000494 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000495 if (Buffer) {
Argyrios Kyrtzidis03c107a2011-08-25 20:39:19 +0000496 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000497 llvm::outs().write(Buffer->getBufferStart(), Preamble);
498 delete Buffer;
499 }
500}