blob: eda21c5f3f78cf301b09cc553d6b5437839625ad [file] [log] [blame]
Daniel Dunbar02bd2d72009-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 Dunbar02bd2d72009-11-14 10:42:46 +000013#include "clang/Frontend/ASTConsumers.h"
14#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/CompilerInstance.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000016#include "clang/Frontend/FrontendDiagnostic.h"
17#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-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 Gregorbf7fc9c2013-03-27 16:47:18 +000022#include "clang/Serialization/ASTReader.h"
Sebastian Redl1914c6f2010-08-18 23:56:37 +000023#include "clang/Serialization/ASTWriter.h"
Nick Lewycky784fad72010-04-24 01:30:46 +000024#include "llvm/ADT/OwningPtr.h"
Douglas Gregor524e33e2011-12-08 19:11:24 +000025#include "llvm/Support/FileSystem.h"
Douglas Gregoraf82e352010-07-20 20:18:03 +000026#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000027#include "llvm/Support/raw_ostream.h"
Douglas Gregor52da28b2011-09-23 23:43:36 +000028#include "llvm/Support/system_error.h"
29
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000030using namespace clang;
31
Daniel Dunbar88357802009-11-14 10:42:57 +000032//===----------------------------------------------------------------------===//
Daniel Dunbar1c201fb2010-03-19 19:44:04 +000033// Custom Actions
34//===----------------------------------------------------------------------===//
35
36ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000037 StringRef InFile) {
Daniel Dunbar1c201fb2010-03-19 19:44:04 +000038 return new ASTConsumer();
39}
40
41void InitOnlyAction::ExecuteAction() {
42}
43
44//===----------------------------------------------------------------------===//
Daniel Dunbar88357802009-11-14 10:42:57 +000045// AST Consumer Actions
46//===----------------------------------------------------------------------===//
47
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000048ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000049 StringRef InFile) {
50 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
Alexander Kornienko3db68ee2012-07-26 16:01:23 +000051 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
Daniel Dunbar75546992009-12-03 09:13:30 +000052 return 0;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000053}
54
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000055ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056 StringRef InFile) {
Richard Smith6ea05822013-06-24 01:45:33 +000057 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
58 CI.getFrontendOpts().ASTDumpLookups);
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000059}
60
Alexander Kornienko4de03592012-07-31 09:37:40 +000061ASTConsumer *ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI,
62 StringRef InFile) {
63 return CreateASTDeclNodeLister();
64}
65
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000066ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000067 StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000068 return CreateASTViewer();
69}
70
71ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000072 StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000073 return CreateDeclContextPrinter();
74}
75
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000076ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000077 StringRef InFile) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +000078 std::string Sysroot;
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +000079 std::string OutputFile;
Chris Lattner0e62c1c2011-07-23 10:55:15 +000080 raw_ostream *OS = 0;
Douglas Gregor36db4f92011-08-25 22:35:51 +000081 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000082 return 0;
Douglas Gregor48c8cd32010-08-03 08:14:03 +000083
Douglas Gregorc567ba22011-07-22 16:35:34 +000084 if (!CI.getFrontendOpts().RelocatablePCH)
85 Sysroot.clear();
Douglas Gregorf7a700fd2011-11-30 04:39:39 +000086 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS);
Douglas Gregor48c8cd32010-08-03 08:14:03 +000087}
88
89bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000090 StringRef InFile,
Douglas Gregor48c8cd32010-08-03 08:14:03 +000091 std::string &Sysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +000092 std::string &OutputFile,
Douglas Gregor36db4f92011-08-25 22:35:51 +000093 raw_ostream *&OS) {
Douglas Gregor48c8cd32010-08-03 08:14:03 +000094 Sysroot = CI.getHeaderSearchOpts().Sysroot;
95 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
Sebastian Redlea68af42010-08-17 17:55:38 +000096 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
Douglas Gregor48c8cd32010-08-03 08:14:03 +000097 return true;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +000098 }
99
Daniel Dunbara2867c72011-01-31 22:00:44 +0000100 // We use createOutputFile here because this is exposed via libclang, and we
101 // must disable the RemoveFileOnSignal behavior.
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000102 // We use a temporary to avoid race conditions.
Daniel Dunbara2867c72011-01-31 22:00:44 +0000103 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
Argyrios Kyrtzidis08a2bfd2011-07-28 00:45:10 +0000104 /*RemoveFileOnSignal=*/false, InFile,
105 /*Extension=*/"", /*useTemporary=*/true);
Daniel Dunbar75546992009-12-03 09:13:30 +0000106 if (!OS)
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000107 return true;
Daniel Dunbar75546992009-12-03 09:13:30 +0000108
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000109 OutputFile = CI.getFrontendOpts().OutputFile;
Douglas Gregor48c8cd32010-08-03 08:14:03 +0000110 return false;
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000111}
112
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000113ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
114 StringRef InFile) {
115 std::string Sysroot;
116 std::string OutputFile;
117 raw_ostream *OS = 0;
118 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
119 return 0;
120
Douglas Gregorf7a700fd2011-11-30 04:39:39 +0000121 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module,
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000122 Sysroot, OS);
123}
124
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000125static SmallVectorImpl<char> &
126operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
127 Includes.append(RHS.begin(), RHS.end());
128 return Includes;
129}
130
131static void addHeaderInclude(StringRef HeaderName,
132 SmallVectorImpl<char> &Includes,
133 const LangOptions &LangOpts) {
134 if (LangOpts.ObjC1)
135 Includes += "#import \"";
136 else
137 Includes += "#include \"";
138 Includes += HeaderName;
139 Includes += "\"\n";
140}
141
142static void addHeaderInclude(const FileEntry *Header,
143 SmallVectorImpl<char> &Includes,
144 const LangOptions &LangOpts) {
145 addHeaderInclude(Header->getName(), Includes, LangOpts);
146}
147
Douglas Gregor4332b322011-11-16 17:04:00 +0000148/// \brief Collect the set of header includes needed to construct the given
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +0000149/// module and update the TopHeaders file set of the module.
Douglas Gregor4332b322011-11-16 17:04:00 +0000150///
151/// \param Module The module we're collecting includes from.
Douglas Gregore5626c42011-12-06 17:15:11 +0000152///
James Dennettc423c532012-06-15 21:48:19 +0000153/// \param Includes Will be augmented with the set of \#includes or \#imports
Douglas Gregore5626c42011-12-06 17:15:11 +0000154/// needed to load all of the named headers.
Douglas Gregor4332b322011-11-16 17:04:00 +0000155static void collectModuleHeaderIncludes(const LangOptions &LangOpts,
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000156 FileManager &FileMgr,
157 ModuleMap &ModMap,
Douglas Gregorde3ef502011-11-30 23:21:26 +0000158 clang::Module *Module,
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000159 SmallVectorImpl<char> &Includes) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000160 // Don't collect any headers for unavailable modules.
161 if (!Module->isAvailable())
162 return;
163
Douglas Gregore5626c42011-12-06 17:15:11 +0000164 // Add includes for each of these headers.
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000165 for (unsigned I = 0, N = Module->NormalHeaders.size(); I != N; ++I) {
166 const FileEntry *Header = Module->NormalHeaders[I];
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +0000167 Module->addTopHeader(Header);
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000168 addHeaderInclude(Header, Includes, LangOpts);
Douglas Gregor3ec66632012-02-02 18:42:48 +0000169 }
Lawrence Crowlb53e5482013-06-20 21:14:14 +0000170 // Note that Module->PrivateHeaders will not be a TopHeader.
Douglas Gregore5626c42011-12-06 17:15:11 +0000171
Douglas Gregor73141fa2011-12-08 17:39:04 +0000172 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +0000173 Module->addTopHeader(UmbrellaHeader);
Douglas Gregor3ec66632012-02-02 18:42:48 +0000174 if (Module->Parent) {
175 // Include the umbrella header for submodules.
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000176 addHeaderInclude(UmbrellaHeader, Includes, LangOpts);
Douglas Gregor3ec66632012-02-02 18:42:48 +0000177 }
Douglas Gregor524e33e2011-12-08 19:11:24 +0000178 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000179 // Add all of the headers we find in this subdirectory.
Douglas Gregor524e33e2011-12-08 19:11:24 +0000180 llvm::error_code EC;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000181 SmallString<128> DirNative;
Douglas Gregor524e33e2011-12-08 19:11:24 +0000182 llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
Douglas Gregorc1aaf8c2011-12-12 19:13:53 +0000183 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
184 DirEnd;
Douglas Gregor524e33e2011-12-08 19:11:24 +0000185 Dir != DirEnd && !EC; Dir.increment(EC)) {
186 // Check whether this entry has an extension typically associated with
187 // headers.
188 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
189 .Cases(".h", ".H", ".hh", ".hpp", true)
190 .Default(false))
191 continue;
192
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000193 // If this header is marked 'unavailable' in this module, don't include
194 // it.
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +0000195 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) {
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000196 if (ModMap.isHeaderInUnavailableModule(Header))
197 continue;
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +0000198 Module->addTopHeader(Header);
Argyrios Kyrtzidisc597c8c2012-10-05 00:22:33 +0000199 }
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000200
Douglas Gregor3ec66632012-02-02 18:42:48 +0000201 // Include this header umbrella header for submodules.
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000202 addHeaderInclude(Dir->path(), Includes, LangOpts);
Douglas Gregor524e33e2011-12-08 19:11:24 +0000203 }
Douglas Gregor4332b322011-11-16 17:04:00 +0000204 }
205
206 // Recurse into submodules.
Douglas Gregoreb90e832012-01-04 23:32:19 +0000207 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
208 SubEnd = Module->submodule_end();
Douglas Gregore5626c42011-12-06 17:15:11 +0000209 Sub != SubEnd; ++Sub)
Douglas Gregor3ec66632012-02-02 18:42:48 +0000210 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes);
Douglas Gregor4332b322011-11-16 17:04:00 +0000211}
212
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000213bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
214 StringRef Filename) {
215 // Find the module map file.
216 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
217 if (!ModuleMap) {
218 CI.getDiagnostics().Report(diag::err_module_map_not_found)
219 << Filename;
220 return false;
221 }
222
223 // Parse the module map file.
224 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Douglas Gregor963c5532013-06-21 16:28:10 +0000225 if (HS.loadModuleMapFile(ModuleMap, IsSystem))
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000226 return false;
227
228 if (CI.getLangOpts().CurrentModule.empty()) {
229 CI.getDiagnostics().Report(diag::err_missing_module_name);
230
231 // FIXME: Eventually, we could consider asking whether there was just
232 // a single module described in the module map, and use that as a
233 // default. Then it would be fairly trivial to just "compile" a module
234 // map with a single module (the common case).
235 return false;
236 }
237
238 // Dig out the module definition.
Douglas Gregor279a6c32012-01-29 17:08:11 +0000239 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
240 /*AllowSearch=*/false);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000241 if (!Module) {
242 CI.getDiagnostics().Report(diag::err_missing_module)
243 << CI.getLangOpts().CurrentModule << Filename;
244
245 return false;
246 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000247
248 // Check whether we can build this module at all.
Richard Smitha3feee22013-10-28 22:18:19 +0000249 clang::Module::Requirement Requirement;
Daniel Jasper0761a8a2013-12-17 10:31:37 +0000250 clang::Module::HeaderDirective MissingHeader;
251 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
252 MissingHeader)) {
253 if (MissingHeader.FileNameLoc.isValid()) {
254 CI.getDiagnostics().Report(diag::err_module_header_missing)
255 << MissingHeader.IsUmbrella << MissingHeader.FileName;
256 } else {
257 CI.getDiagnostics().Report(diag::err_module_unavailable)
258 << Module->getFullModuleName()
259 << Requirement.second << Requirement.first;
260 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000261
262 return false;
263 }
264
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000265 FileManager &FileMgr = CI.getFileManager();
266
Douglas Gregor4332b322011-11-16 17:04:00 +0000267 // Collect the set of #includes we need to build the module.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000268 SmallString<256> HeaderContents;
Argyrios Kyrtzidis130190f2012-10-10 02:12:39 +0000269 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
270 addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts());
271 collectModuleHeaderIncludes(CI.getLangOpts(), FileMgr,
Douglas Gregord2a8fe12012-01-05 00:04:05 +0000272 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(),
Douglas Gregor3ec66632012-02-02 18:42:48 +0000273 Module, HeaderContents);
Douglas Gregor4332b322011-11-16 17:04:00 +0000274
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000275 llvm::MemoryBuffer *InputBuffer =
276 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
277 Module::getModuleInputBufferName());
Alp Tokerf6a24ce2013-12-05 16:25:25 +0000278 // Ownership of InputBuffer will be transferred to the SourceManager.
Argyrios Kyrtzidis52595242012-11-15 18:57:27 +0000279 setCurrentInput(FrontendInputFile(InputBuffer, getCurrentFileKind(),
Douglas Gregora686e1b2012-01-27 19:52:33 +0000280 Module->IsSystem));
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000281 return true;
282}
283
284bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
285 StringRef InFile,
286 std::string &Sysroot,
287 std::string &OutputFile,
288 raw_ostream *&OS) {
289 // If no output file was provided, figure out where this module would go
290 // in the module cache.
291 if (CI.getFrontendOpts().OutputFile.empty()) {
292 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000293 SmallString<256> ModuleFileName(HS.getModuleCachePath());
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000294 llvm::sys::path::append(ModuleFileName,
295 CI.getLangOpts().CurrentModule + ".pcm");
296 CI.getFrontendOpts().OutputFile = ModuleFileName.str();
297 }
298
299 // We use createOutputFile here because this is exposed via libclang, and we
300 // must disable the RemoveFileOnSignal behavior.
301 // We use a temporary to avoid race conditions.
302 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
303 /*RemoveFileOnSignal=*/false, InFile,
Daniel Dunbarb9c62c02012-03-03 00:36:02 +0000304 /*Extension=*/"", /*useTemporary=*/true,
305 /*CreateMissingDirectories=*/true);
Douglas Gregor2b20cb82011-11-16 00:09:06 +0000306 if (!OS)
307 return true;
308
309 OutputFile = CI.getFrontendOpts().OutputFile;
310 return false;
311}
312
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000313ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000314 StringRef InFile) {
Daniel Dunbar02bd2d72009-11-14 10:42:46 +0000315 return new ASTConsumer();
316}
317
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000318ASTConsumer *DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
319 StringRef InFile) {
320 return new ASTConsumer();
321}
322
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000323ASTConsumer *VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI,
324 StringRef InFile) {
325 return new ASTConsumer();
326}
327
328void VerifyPCHAction::ExecuteAction() {
Ben Langmuir3d4417c2014-02-07 17:31:11 +0000329 CompilerInstance &CI = getCompilerInstance();
330 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
331 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
332 OwningPtr<ASTReader> Reader(new ASTReader(
333 CI.getPreprocessor(), CI.getASTContext(),
334 Sysroot.empty() ? "" : Sysroot.c_str(),
335 /*DisableValidation*/false,
336 /*AllowPCHWithCompilerErrors*/false,
337 /*AllowConfigurationMismatch*/true,
338 /*ValidateSystemInputs*/true));
339
340 Reader->ReadAST(getCurrentFile(),
341 Preamble ? serialization::MK_Preamble
342 : serialization::MK_PCH,
343 SourceLocation(),
344 ASTReader::ARR_ConfigurationMismatch);
Ben Langmuir2cb4a782014-02-05 22:21:15 +0000345}
346
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000347namespace {
348 /// \brief AST reader listener that dumps module information for a module
349 /// file.
350 class DumpModuleInfoListener : public ASTReaderListener {
351 llvm::raw_ostream &Out;
352
353 public:
354 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
355
356#define DUMP_BOOLEAN(Value, Text) \
357 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
358
359 virtual bool ReadFullVersionInformation(StringRef FullVersion) {
360 Out.indent(2)
361 << "Generated by "
362 << (FullVersion == getClangFullRepositoryVersion()? "this"
363 : "a different")
364 << " Clang: " << FullVersion << "\n";
365 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
366 }
367
368 virtual bool ReadLanguageOptions(const LangOptions &LangOpts,
369 bool Complain) {
370 Out.indent(2) << "Language options:\n";
371#define LANGOPT(Name, Bits, Default, Description) \
372 DUMP_BOOLEAN(LangOpts.Name, Description);
373#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
374 Out.indent(4) << Description << ": " \
375 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
376#define VALUE_LANGOPT(Name, Bits, Default, Description) \
377 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
378#define BENIGN_LANGOPT(Name, Bits, Default, Description)
379#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
380#include "clang/Basic/LangOptions.def"
381 return false;
382 }
383
384 virtual bool ReadTargetOptions(const TargetOptions &TargetOpts,
385 bool Complain) {
386 Out.indent(2) << "Target options:\n";
387 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
388 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
389 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000390 Out.indent(4) << " Linker version: " << TargetOpts.LinkerVersion << "\n";
391
392 if (!TargetOpts.FeaturesAsWritten.empty()) {
393 Out.indent(4) << "Target features:\n";
394 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
395 I != N; ++I) {
396 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
397 }
398 }
399
400 return false;
401 }
402
403 virtual bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
404 bool Complain) {
405 Out.indent(2) << "Header search options:\n";
406 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
407 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
408 "Use builtin include directories [-nobuiltininc]");
409 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
410 "Use standard system include directories [-nostdinc]");
411 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
412 "Use standard C++ include directories [-nostdinc++]");
413 DUMP_BOOLEAN(HSOpts.UseLibcxx,
414 "Use libc++ (rather than libstdc++) [-stdlib=]");
415 return false;
416 }
417
418 virtual bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
419 bool Complain,
420 std::string &SuggestedPredefines) {
421 Out.indent(2) << "Preprocessor options:\n";
422 DUMP_BOOLEAN(PPOpts.UsePredefines,
423 "Uses compiler/target-specific predefines [-undef]");
424 DUMP_BOOLEAN(PPOpts.DetailedRecord,
425 "Uses detailed preprocessing record (for indexing)");
426
427 if (!PPOpts.Macros.empty()) {
428 Out.indent(4) << "Predefined macros:\n";
429 }
430
431 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
432 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
433 I != IEnd; ++I) {
434 Out.indent(6);
435 if (I->second)
436 Out << "-U";
437 else
438 Out << "-D";
439 Out << I->first << "\n";
440 }
441 return false;
442 }
443#undef DUMP_BOOLEAN
444 };
445}
446
447void DumpModuleInfoAction::ExecuteAction() {
448 // Set up the output file.
449 llvm::OwningPtr<llvm::raw_fd_ostream> OutFile;
450 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
451 if (!OutputFileName.empty() && OutputFileName != "-") {
452 std::string ErrorInfo;
453 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str().c_str(),
Rafael Espindola4fbd3732014-02-24 18:20:21 +0000454 ErrorInfo, llvm::sys::fs::F_Text));
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +0000455 }
456 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
457
458 Out << "Information for module file '" << getCurrentFile() << "':\n";
459 DumpModuleInfoListener Listener(Out);
460 ASTReader::readASTFileControlBlock(getCurrentFile(),
461 getCompilerInstance().getFileManager(),
462 Listener);
463}
464
Daniel Dunbar88357802009-11-14 10:42:57 +0000465//===----------------------------------------------------------------------===//
466// Preprocessor Actions
467//===----------------------------------------------------------------------===//
468
469void DumpRawTokensAction::ExecuteAction() {
470 Preprocessor &PP = getCompilerInstance().getPreprocessor();
471 SourceManager &SM = PP.getSourceManager();
472
473 // Start lexing the specified input file.
Chris Lattner710bb872009-11-30 04:18:44 +0000474 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
David Blaikiebbafb8a2012-03-11 07:00:24 +0000475 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
Daniel Dunbar88357802009-11-14 10:42:57 +0000476 RawLex.SetKeepWhitespaceMode(true);
477
478 Token RawTok;
479 RawLex.LexFromRawLexer(RawTok);
480 while (RawTok.isNot(tok::eof)) {
481 PP.DumpToken(RawTok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000482 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000483 RawLex.LexFromRawLexer(RawTok);
484 }
485}
486
487void DumpTokensAction::ExecuteAction() {
488 Preprocessor &PP = getCompilerInstance().getPreprocessor();
489 // Start preprocessing the specified input file.
490 Token Tok;
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000491 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000492 do {
493 PP.Lex(Tok);
494 PP.DumpToken(Tok, true);
Daniel Dunbar75024622009-11-25 10:27:48 +0000495 llvm::errs() << "\n";
Daniel Dunbar88357802009-11-14 10:42:57 +0000496 } while (Tok.isNot(tok::eof));
497}
498
499void GeneratePTHAction::ExecuteAction() {
500 CompilerInstance &CI = getCompilerInstance();
501 if (CI.getFrontendOpts().OutputFile.empty() ||
502 CI.getFrontendOpts().OutputFile == "-") {
503 // FIXME: Don't fail this way.
504 // FIXME: Verify that we can actually seek in the given file.
Chris Lattner4b73cfa2010-04-07 22:58:06 +0000505 llvm::report_fatal_error("PTH requires a seekable file for output!");
Daniel Dunbar88357802009-11-14 10:42:57 +0000506 }
507 llvm::raw_fd_ostream *OS =
508 CI.createDefaultOutputFile(true, getCurrentFile());
Daniel Dunbar75546992009-12-03 09:13:30 +0000509 if (!OS) return;
510
Daniel Dunbar88357802009-11-14 10:42:57 +0000511 CacheTokens(CI.getPreprocessor(), OS);
512}
513
Daniel Dunbar88357802009-11-14 10:42:57 +0000514void PreprocessOnlyAction::ExecuteAction() {
515 Preprocessor &PP = getCompilerInstance().getPreprocessor();
516
Daniel Dunbard839e772010-06-11 20:10:12 +0000517 // Ignore unknown pragmas.
NAKAMURA Takumiddc86792013-12-04 11:12:26 +0000518 PP.AddPragmaHandler(new EmptyPragmaHandler());
Daniel Dunbard839e772010-06-11 20:10:12 +0000519
Daniel Dunbar88357802009-11-14 10:42:57 +0000520 Token Tok;
521 // Start parsing the specified input file.
Chris Lattnerfb24a3a2010-04-20 20:35:58 +0000522 PP.EnterMainSourceFile();
Daniel Dunbar88357802009-11-14 10:42:57 +0000523 do {
524 PP.Lex(Tok);
525 } while (Tok.isNot(tok::eof));
526}
527
Daniel Dunbar88357802009-11-14 10:42:57 +0000528void PrintPreprocessedAction::ExecuteAction() {
529 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor52da28b2011-09-23 23:43:36 +0000530 // Output file may need to be set to 'Binary', to avoid converting Unix style
Steve Naroff3d38a682010-01-05 17:33:23 +0000531 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
Douglas Gregor52da28b2011-09-23 23:43:36 +0000532 //
533 // Look to see what type of line endings the file uses. If there's a
534 // CRLF, then we won't open the file up in binary mode. If there is
535 // just an LF or CR, then we will open the file up in binary mode.
536 // In this fashion, the output format should match the input format, unless
537 // the input format has inconsistent line endings.
538 //
539 // This should be a relatively fast operation since most files won't have
540 // all of their source code on a single line. However, that is still a
541 // concern, so if we scan for too long, we'll just assume the file should
542 // be opened in binary mode.
543 bool BinaryMode = true;
544 bool InvalidFile = false;
545 const SourceManager& SM = CI.getSourceManager();
546 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
547 &InvalidFile);
548 if (!InvalidFile) {
549 const char *cur = Buffer->getBufferStart();
550 const char *end = Buffer->getBufferEnd();
551 const char *next = (cur != end) ? cur + 1 : end;
552
553 // Limit ourselves to only scanning 256 characters into the source
554 // file. This is mostly a sanity check in case the file has no
555 // newlines whatsoever.
556 if (end - cur > 256) end = cur + 256;
557
558 while (next < end) {
559 if (*cur == 0x0D) { // CR
560 if (*next == 0x0A) // CRLF
561 BinaryMode = false;
562
563 break;
564 } else if (*cur == 0x0A) // LF
565 break;
566
567 ++cur, ++next;
568 }
569 }
570
571 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
Daniel Dunbar75546992009-12-03 09:13:30 +0000572 if (!OS) return;
573
Daniel Dunbar88357802009-11-14 10:42:57 +0000574 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
575 CI.getPreprocessorOutputOpts());
576}
Douglas Gregoraf82e352010-07-20 20:18:03 +0000577
578void PrintPreambleAction::ExecuteAction() {
579 switch (getCurrentFileKind()) {
580 case IK_C:
581 case IK_CXX:
582 case IK_ObjC:
583 case IK_ObjCXX:
584 case IK_OpenCL:
Peter Collingbourne62089b82010-12-01 03:15:20 +0000585 case IK_CUDA:
Douglas Gregoraf82e352010-07-20 20:18:03 +0000586 break;
587
588 case IK_None:
589 case IK_Asm:
590 case IK_PreprocessedC:
591 case IK_PreprocessedCXX:
592 case IK_PreprocessedObjC:
593 case IK_PreprocessedObjCXX:
594 case IK_AST:
595 case IK_LLVM_IR:
596 // We can't do anything with these.
597 return;
598 }
599
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000600 CompilerInstance &CI = getCompilerInstance();
601 llvm::MemoryBuffer *Buffer
Chris Lattner5159f612010-11-23 08:35:12 +0000602 = CI.getFileManager().getBufferForFile(getCurrentFile());
Douglas Gregoraf82e352010-07-20 20:18:03 +0000603 if (Buffer) {
Argyrios Kyrtzidis7aecbc72011-08-25 20:39:19 +0000604 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first;
Douglas Gregoraf82e352010-07-20 20:18:03 +0000605 llvm::outs().write(Buffer->getBufferStart(), Preamble);
606 delete Buffer;
607 }
608}