blob: 5c7567fa8c02f335befd1168df68c1a569649303 [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"
12#include "clang/Basic/FileManager.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000013#include "clang/Frontend/ASTConsumers.h"
14#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000016#include "clang/Frontend/FrontendDiagnostic.h"
17#include "clang/Frontend/Utils.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000018#include "clang/Lex/HeaderSearch.h"
19#include "clang/Lex/Pragma.h"
20#include "clang/Lex/Preprocessor.h"
21#include "clang/Parse/Parser.h"
Douglas Gregorc544ba02013-03-27 16:47:18 +000022#include "clang/Serialization/ASTReader.h"
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000023#include "clang/Serialization/ASTWriter.h"
Nick Lewyckyba5f6ec2010-04-24 01:30:46 +000024#include "llvm/ADT/OwningPtr.h"
Douglas Gregor77d029f2011-12-08 19:11:24 +000025#include "llvm/Support/FileSystem.h"
Douglas Gregorf033f1d2010-07-20 20:18:03 +000026#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar8305d012009-11-14 10:42:46 +000027#include "llvm/Support/raw_ostream.h"
Douglas Gregor10efbaf2011-09-23 23:43:36 +000028#include "llvm/Support/system_error.h"
29
Daniel Dunbar8305d012009-11-14 10:42:46 +000030using namespace clang;
31
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000032//===----------------------------------------------------------------------===//
Daniel Dunbar27585952010-03-19 19:44:04 +000033// Custom Actions
34//===----------------------------------------------------------------------===//
35
36ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000037 StringRef InFile) {
Daniel Dunbar27585952010-03-19 19:44:04 +000038 return new ASTConsumer();
39}
40
41void InitOnlyAction::ExecuteAction() {
42}
43
44//===----------------------------------------------------------------------===//
Daniel Dunbar5f3b9972009-11-14 10:42:57 +000045// AST Consumer Actions
46//===----------------------------------------------------------------------===//
47
Daniel Dunbar8305d012009-11-14 10:42:46 +000048ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000049 StringRef InFile) {
50 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000051 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
Daniel Dunbar36043592009-12-03 09:13:30 +000052 return 0;
Daniel Dunbar8305d012009-11-14 10:42:46 +000053}
54
Daniel Dunbar8305d012009-11-14 10:42:46 +000055ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000056 StringRef InFile) {
Alexander Kornienkoe34a0522012-07-26 16:01:23 +000057 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter);
Daniel Dunbar8305d012009-11-14 10:42:46 +000058}
59
Alexander Kornienko171af642012-07-31 09:37:40 +000060ASTConsumer *ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI,
61 StringRef InFile) {
62 return CreateASTDeclNodeLister();
63}
64
John McCallf3514242010-11-24 11:21:45 +000065ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000066 StringRef InFile) {
67 raw_ostream *OS;
John McCallf3514242010-11-24 11:21:45 +000068 if (CI.getFrontendOpts().OutputFile.empty())
69 OS = &llvm::outs();
70 else
71 OS = CI.createDefaultOutputFile(false, InFile);
72 if (!OS) return 0;
73 return CreateASTDumperXML(*OS);
74}
75
Daniel Dunbar8305d012009-11-14 10:42:46 +000076ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000077 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000078 return CreateASTViewer();
79}
80
81ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000082 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +000083 return CreateDeclContextPrinter();
84}
85
Daniel Dunbar8305d012009-11-14 10:42:46 +000086ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000087 StringRef InFile) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +000088 std::string Sysroot;
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000089 std::string OutputFile;
Chris Lattner5f9e2722011-07-23 10:55:15 +000090 raw_ostream *OS = 0;
Douglas Gregor9293ba82011-08-25 22:35:51 +000091 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
Daniel Dunbar8305d012009-11-14 10:42:46 +000092 return 0;
Douglas Gregor1d715ac2010-08-03 08:14:03 +000093
Douglas Gregor832d6202011-07-22 16:35:34 +000094 if (!CI.getFrontendOpts().RelocatablePCH)
95 Sysroot.clear();
Douglas Gregora8cc6ce2011-11-30 04:39:39 +000096 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS);
Douglas Gregor1d715ac2010-08-03 08:14:03 +000097}
98
99bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 StringRef InFile,
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000101 std::string &Sysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000102 std::string &OutputFile,
Douglas Gregor9293ba82011-08-25 22:35:51 +0000103 raw_ostream *&OS) {
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000104 Sysroot = CI.getHeaderSearchOpts().Sysroot;
105 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redl11c3dc42010-08-17 17:55:38 +0000106 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000107 return true;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000108 }
109
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000110 // We use createOutputFile here because this is exposed via libclang, and we
111 // must disable the RemoveFileOnSignal behavior.
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000112 // We use a temporary to avoid race conditions.
Daniel Dunbarc7a9cda2011-01-31 22:00:44 +0000113 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
Argyrios Kyrtzidis7e909852011-07-28 00:45:10 +0000114 /*RemoveFileOnSignal=*/false, InFile,
115 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar36043592009-12-03 09:13:30 +0000116 if (!OS)
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000117 return true;
Daniel Dunbar36043592009-12-03 09:13:30 +0000118
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000119 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor1d715ac2010-08-03 08:14:03 +0000120 return false;
Daniel Dunbar8305d012009-11-14 10:42:46 +0000121}
122
Douglas Gregordb1cde72011-11-16 00:09:06 +0000123ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
124 StringRef InFile) {
125 std::string Sysroot;
126 std::string OutputFile;
127 raw_ostream *OS = 0;
128 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
129 return 0;
130
Douglas Gregora8cc6ce2011-11-30 04:39:39 +0000131 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
Douglas Gregordb1cde72011-11-16 00:09:06 +0000132 Sysroot, OS);
133}
134
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000135static SmallVectorImpl<char> &
136operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
137 Includes.append(RHS.begin(), RHS.end());
138 return Includes;
139}
140
141static void addHeaderInclude(StringRef HeaderName,
142 SmallVectorImpl<char> &Includes,
143 const LangOptions &LangOpts) {
144 if (LangOpts.ObjC1)
145 Includes += "#import \"";
146 else
147 Includes += "#include \"";
148 Includes += HeaderName;
149 Includes += "\"\n";
150}
151
152static void addHeaderInclude(const FileEntry *Header,
153 SmallVectorImpl<char> &Includes,
154 const LangOptions &LangOpts) {
155 addHeaderInclude(Header->getName(), Includes, LangOpts);
156}
157
Douglas Gregor261e75b2011-11-16 17:04:00 +0000158/// \brief Collect the set of header includes needed to construct the given
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000159/// module and update the TopHeaders file set of the module.
Douglas Gregor261e75b2011-11-16 17:04:00 +0000160///
161/// \param Module The module we're collecting includes from.
Douglas Gregor8075ce62011-12-06 17:15:11 +0000162///
James Dennett5e9dda62012-06-15 21:48:19 +0000163/// \param Includes Will be augmented with the set of \#includes or \#imports
Douglas Gregor8075ce62011-12-06 17:15:11 +0000164/// needed to load all of the named headers.
Douglas Gregor261e75b2011-11-16 17:04:00 +0000165static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
Douglas Gregor752769f2012-01-05 00:04:05 +0000166 FileManager &FileMgr,
167 ModuleMap &ModMap,
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000168 clang::Module *Module,
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000169 SmallVectorImpl<char> &Includes) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000170 // Don't collect any headers for unavailable modules.
171 if (!Module->isAvailable())
172 return;
173
Douglas Gregor8075ce62011-12-06 17:15:11 +0000174 // Add includes for each of these headers.
Douglas Gregor2f04f182012-02-02 18:42:48 +0000175 for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) {
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000176 const FileEntry *Header = Module->Headers[I];
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000177 Module->addTopHeader(Header);
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000178 addHeaderInclude(Header, Includes, LangOpts);
Douglas Gregor2f04f182012-02-02 18:42:48 +0000179 }
Douglas Gregor8075ce62011-12-06 17:15:11 +0000180
Douglas Gregor10694ce2011-12-08 17:39:04 +0000181 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000182 Module->addTopHeader(UmbrellaHeader);
Douglas Gregor2f04f182012-02-02 18:42:48 +0000183 if (Module->Parent) {
184 // Include the umbrella header for submodules.
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000185 addHeaderInclude(UmbrellaHeader, Includes, LangOpts);
Douglas Gregor2f04f182012-02-02 18:42:48 +0000186 }
Douglas Gregor77d029f2011-12-08 19:11:24 +0000187 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
Douglas Gregor752769f2012-01-05 00:04:05 +0000188 // Add all of the headers we find in this subdirectory.
Douglas Gregor77d029f2011-12-08 19:11:24 +0000189 llvm::error_code EC;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000190 SmallString<128> DirNative;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000191 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
Douglas Gregor3b29bb92011-12-12 19:13:53 +0000192 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
193 DirEnd;
Douglas Gregor77d029f2011-12-08 19:11:24 +0000194 Dir != DirEnd && !EC; Dir.increment(EC)) {
195 // Check whether this entry has an extension typically associated with
196 // headers.
197 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
198 .Cases(".h", ".H", ".hh", ".hpp", true)
199 .Default(false))
200 continue;
201
Douglas Gregor752769f2012-01-05 00:04:05 +0000202 // If this header is marked 'unavailable' in this module, don't include
203 // it.
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000204 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
Douglas Gregor752769f2012-01-05 00:04:05 +0000205 if (ModMap.isHeaderInUnavailableModule(Header))
206 continue;
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000207 Module->addTopHeader(Header);
Argyrios Kyrtzidisc7782d92012-10-05 00:22:33 +0000208 }
Douglas Gregor752769f2012-01-05 00:04:05 +0000209
Douglas Gregor2f04f182012-02-02 18:42:48 +0000210 // Include this header umbrella header for submodules.
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000211 addHeaderInclude(Dir->path(), Includes, LangOpts);
Douglas Gregor77d029f2011-12-08 19:11:24 +0000212 }
Douglas Gregor261e75b2011-11-16 17:04:00 +0000213 }
214
215 // Recurse into submodules.
Douglas Gregorb7a78192012-01-04 23:32:19 +0000216 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
217 SubEnd = Module->submodule_end();
Douglas Gregor8075ce62011-12-06 17:15:11 +0000218 Sub != SubEnd; ++Sub)
Douglas Gregor2f04f182012-02-02 18:42:48 +0000219 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000220}
221
Douglas Gregordb1cde72011-11-16 00:09:06 +0000222bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
223 StringRef Filename) {
224 // Find the module map file.
225 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
226 if (!ModuleMap) {
227 CI.getDiagnostics().Report(diag::err_module_map_not_found)
228 << Filename;
229 return false;
230 }
231
232 // Parse the module map file.
233 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
234 if (HS.loadModuleMapFile(ModuleMap))
235 return false;
236
237 if (CI.getLangOpts().CurrentModule.empty()) {
238 CI.getDiagnostics().Report(diag::err_missing_module_name);
239
240 // FIXME: Eventually, we could consider asking whether there was just
241 // a single module described in the module map, and use that as a
242 // default. Then it would be fairly trivial to just "compile" a module
243 // map with a single module (the common case).
244 return false;
245 }
246
247 // Dig out the module definition.
Douglas Gregore434ec72012-01-29 17:08:11 +0000248 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
249 /*AllowSearch=*/false);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000250 if (!Module) {
251 CI.getDiagnostics().Report(diag::err_missing_module)
252 << CI.getLangOpts().CurrentModule << Filename;
253
254 return false;
255 }
Douglas Gregor51f564f2011-12-31 04:05:44 +0000256
257 // Check whether we can build this module at all.
258 StringRef Feature;
Douglas Gregordc58aa72012-01-30 06:01:29 +0000259 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Feature)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000260 CI.getDiagnostics().Report(diag::err_module_unavailable)
261 << Module->getFullModuleName()
262 << Feature;
263
264 return false;
265 }
266
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000267 FileManager &FileMgr = CI.getFileManager();
268
Douglas Gregor261e75b2011-11-16 17:04:00 +0000269 // Collect the set of #includes we need to build the module.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000270 SmallString<256> HeaderContents;
Argyrios Kyrtzidis2a857182012-10-10 02:12:39 +0000271 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
272 addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts());
273 collectModuleHeaderIncludes(CI.getLangOpts(), FileMgr,
Douglas Gregor752769f2012-01-05 00:04:05 +0000274 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
Douglas Gregor2f04f182012-02-02 18:42:48 +0000275 Module, HeaderContents);
Douglas Gregor261e75b2011-11-16 17:04:00 +0000276
Argyrios Kyrtzidis992d9172012-11-15 18:57:27 +0000277 llvm::MemoryBuffer *InputBuffer =
278 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
279 Module::getModuleInputBufferName());
280 // Ownership of InputBuffer will be transfered to the SourceManager.
281 setCurrentInput(FrontendInputFile(InputBuffer, getCurrentFileKind(),
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000282 Module->IsSystem));
Douglas Gregordb1cde72011-11-16 00:09:06 +0000283 return true;
284}
285
286bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
287 StringRef InFile,
288 std::string &Sysroot,
289 std::string &OutputFile,
290 raw_ostream *&OS) {
291 // If no output file was provided, figure out where this module would go
292 // in the module cache.
293 if (CI.getFrontendOpts().OutputFile.empty()) {
294 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000295 SmallString<256> ModuleFileName(HS.getModuleCachePath());
Douglas Gregordb1cde72011-11-16 00:09:06 +0000296 llvm::sys::path::append(ModuleFileName,
297 CI.getLangOpts().CurrentModule + ".pcm");
298 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
299 }
300
301 // We use createOutputFile here because this is exposed via libclang, and we
302 // must disable the RemoveFileOnSignal behavior.
303 // We use a temporary to avoid race conditions.
304 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
305 /*RemoveFileOnSignal=*/false, InFile,
Daniel Dunbar12f28ab2012-03-03 00:36:02 +0000306 /*Extension=*/"", /*useTemporary=*/true,
307 /*CreateMissingDirectories=*/true);
Douglas Gregordb1cde72011-11-16 00:09:06 +0000308 if (!OS)
309 return true;
310
311 OutputFile = CI.getFrontendOpts().OutputFile;
312 return false;
313}
314
Daniel Dunbar8305d012009-11-14 10:42:46 +0000315ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000316 StringRef InFile) {
Daniel Dunbar8305d012009-11-14 10:42:46 +0000317 return new ASTConsumer();
318}
319
Douglas Gregorc544ba02013-03-27 16:47:18 +0000320ASTConsumer *DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
321 StringRef InFile) {
322 return new ASTConsumer();
323}
324
325namespace {
326 /// \brief AST reader listener that dumps module information for a module
327 /// file.
328 class DumpModuleInfoListener : public ASTReaderListener {
329 llvm::raw_ostream &Out;
330
331 public:
332 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
333
334#define DUMP_BOOLEAN(Value, Text) \
335 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
336
337 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
338 Out.indent(2)
339 << "Generated by "
340 << (FullVersion == getClangFullRepositoryVersion()? "this"
341 : "a different")
342 << " Clang: " << FullVersion << "\n";
343 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
344 }
345
346 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
347 bool Complain) {
348 Out.indent(2) << "Language options:\n";
349#define LANGOPT(Name, Bits, Default, Description) \
350 DUMP_BOOLEAN(LangOpts.Name, Description);
351#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
352 Out.indent(4) << Description << ": " \
353 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
354#define VALUE_LANGOPT(Name, Bits, Default, Description) \
355 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
356#define BENIGN_LANGOPT(Name, Bits, Default, Description)
357#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
358#include "clang/Basic/LangOptions.def"
359 return false;
360 }
361
362 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
363 bool Complain) {
364 Out.indent(2) << "Target options:\n";
365 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
366 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
367 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
368 Out.indent(4) << " C++ ABI: " << TargetOpts.CXXABI << "\n";
369 Out.indent(4) << " Linker version: " << TargetOpts.LinkerVersion << "\n";
370
371 if (!TargetOpts.FeaturesAsWritten.empty()) {
372 Out.indent(4) << "Target features:\n";
373 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
374 I != N; ++I) {
375 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
376 }
377 }
378
379 return false;
380 }
381
382 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
383 bool Complain) {
384 Out.indent(2) << "Header search options:\n";
385 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
386 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
387 "Use builtin include directories [-nobuiltininc]");
388 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
389 "Use standard system include directories [-nostdinc]");
390 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
391 "Use standard C++ include directories [-nostdinc++]");
392 DUMP_BOOLEAN(HSOpts.UseLibcxx,
393 "Use libc++ (rather than libstdc++) [-stdlib=]");
394 return false;
395 }
396
397 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
398 bool Complain,
399 std::string &SuggestedPredefines) {
400 Out.indent(2) << "Preprocessor options:\n";
401 DUMP_BOOLEAN(PPOpts.UsePredefines,
402 "Uses compiler/target-specific predefines [-undef]");
403 DUMP_BOOLEAN(PPOpts.DetailedRecord,
404 "Uses detailed preprocessing record (for indexing)");
405
406 if (!PPOpts.Macros.empty()) {
407 Out.indent(4) << "Predefined macros:\n";
408 }
409
410 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
411 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
412 I != IEnd; ++I) {
413 Out.indent(6);
414 if (I->second)
415 Out << "-U";
416 else
417 Out << "-D";
418 Out << I->first << "\n";
419 }
420 return false;
421 }
422#undef DUMP_BOOLEAN
423 };
424}
425
426void DumpModuleInfoAction::ExecuteAction() {
427 // Set up the output file.
428 llvm::OwningPtr<llvm::raw_fd_ostream> OutFile;
429 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
430 if (!OutputFileName.empty() && OutputFileName != "-") {
431 std::string ErrorInfo;
432 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str().c_str(),
433 ErrorInfo));
434 }
435 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
436
437 Out << "Information for module file '" << getCurrentFile() << "':\n";
438 DumpModuleInfoListener Listener(Out);
439 ASTReader::readASTFileControlBlock(getCurrentFile(),
440 getCompilerInstance().getFileManager(),
441 Listener);
442}
443
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000444//===----------------------------------------------------------------------===//
445// Preprocessor Actions
446//===----------------------------------------------------------------------===//
447
448void DumpRawTokensAction::ExecuteAction() {
449 Preprocessor &PP = getCompilerInstance().getPreprocessor();
450 SourceManager &SM = PP.getSourceManager();
451
452 // Start lexing the specified input file.
Chris Lattner6e290142009-11-30 04:18:44 +0000453 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikie4e4d0842012-03-11 07:00:24 +0000454 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000455 RawLex.SetKeepWhitespaceMode(true);
456
457 Token RawTok;
458 RawLex.LexFromRawLexer(RawTok);
459 while (RawTok.isNot(tok::eof)) {
460 PP.DumpToken(RawTok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000461 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000462 RawLex.LexFromRawLexer(RawTok);
463 }
464}
465
466void DumpTokensAction::ExecuteAction() {
467 Preprocessor &PP = getCompilerInstance().getPreprocessor();
468 // Start preprocessing the specified input file.
469 Token Tok;
Chris Lattnere127a0d2010-04-20 20:35:58 +0000470 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000471 do {
472 PP.Lex(Tok);
473 PP.DumpToken(Tok, true);
Daniel Dunbar33f57f82009-11-25 10:27:48 +0000474 llvm::errs() << "\n";
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000475 } while (Tok.isNot(tok::eof));
476}
477
478void GeneratePTHAction::ExecuteAction() {
479 CompilerInstance &CI = getCompilerInstance();
480 if (CI.getFrontendOpts().OutputFile.empty() ||
481 CI.getFrontendOpts().OutputFile == "-") {
482 // FIXME: Don't fail this way.
483 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner83e7a782010-04-07 22:58:06 +0000484 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000485 }
486 llvm::raw_fd_ostream *OS =
487 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000488 if (!OS) return;
489
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000490 CacheTokens(CI.getPreprocessor(), OS);
491}
492
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000493void PreprocessOnlyAction::ExecuteAction() {
494 Preprocessor &PP = getCompilerInstance().getPreprocessor();
495
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000496 // Ignore unknown pragmas.
Argyrios Kyrtzidis9b36c3f2010-07-13 09:07:17 +0000497 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbarc72cc502010-06-11 20:10:12 +0000498
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000499 Token Tok;
500 // Start parsing the specified input file.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000501 PP.EnterMainSourceFile();
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000502 do {
503 PP.Lex(Tok);
504 } while (Tok.isNot(tok::eof));
505}
506
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000507void PrintPreprocessedAction::ExecuteAction() {
508 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000509 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroffd57d7c02010-01-05 17:33:23 +0000510 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor10efbaf2011-09-23 23:43:36 +0000511 //
512 // Look to see what type of line endings the file uses. If there's a
513 // CRLF, then we won't open the file up in binary mode. If there is
514 // just an LF or CR, then we will open the file up in binary mode.
515 // In this fashion, the output format should match the input format, unless
516 // the input format has inconsistent line endings.
517 //
518 // This should be a relatively fast operation since most files won't have
519 // all of their source code on a single line. However, that is still a
520 // concern, so if we scan for too long, we'll just assume the file should
521 // be opened in binary mode.
522 bool BinaryMode = true;
523 bool InvalidFile = false;
524 const SourceManager& SM = CI.getSourceManager();
525 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
526 &InvalidFile);
527 if (!InvalidFile) {
528 const char *cur = Buffer->getBufferStart();
529 const char *end = Buffer->getBufferEnd();
530 const char *next = (cur != end) ? cur + 1 : end;
531
532 // Limit ourselves to only scanning 256 characters into the source
533 // file. This is mostly a sanity check in case the file has no
534 // newlines whatsoever.
535 if (end - cur > 256) end = cur + 256;
536
537 while (next < end) {
538 if (*cur == 0x0D) { // CR
539 if (*next == 0x0A) // CRLF
540 BinaryMode = false;
541
542 break;
543 } else if (*cur == 0x0A) // LF
544 break;
545
546 ++cur, ++next;
547 }
548 }
549
550 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar36043592009-12-03 09:13:30 +0000551 if (!OS) return;
552
Daniel Dunbar5f3b9972009-11-14 10:42:57 +0000553 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
554 CI.getPreprocessorOutputOpts());
555}
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000556
557void PrintPreambleAction::ExecuteAction() {
558 switch (getCurrentFileKind()) {
559 case IK_C:
560 case IK_CXX:
561 case IK_ObjC:
562 case IK_ObjCXX:
563 case IK_OpenCL:
Peter Collingbourne895fcca2010-12-01 03:15:20 +0000564 case IK_CUDA:
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000565 break;
566
567 case IK_None:
568 case IK_Asm:
569 case IK_PreprocessedC:
570 case IK_PreprocessedCXX:
571 case IK_PreprocessedObjC:
572 case IK_PreprocessedObjCXX:
573 case IK_AST:
574 case IK_LLVM_IR:
575 // We can't do anything with these.
576 return;
577 }
578
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000579 CompilerInstance &CI = getCompilerInstance();
580 llvm::MemoryBuffer *Buffer
Chris Lattner39b49bc2010-11-23 08:35:12 +0000581 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000582 if (Buffer) {
Argyrios Kyrtzidis03c107a2011-08-25 20:39:19 +0000583 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000584 llvm::outs().write(Buffer->getBufferStart(), Preamble);
585 delete Buffer;
586 }
587}