1 //===--- 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"
13 #include "clang/Frontend/ASTConsumers.h"
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/Frontend/MultiplexConsumer.h"
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/Pragma.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Parse/Parser.h"
23 #include "clang/Serialization/ASTReader.h"
24 #include "clang/Serialization/ASTWriter.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <memory>
29 #include <system_error>
30 
31 using namespace clang;
32 
33 //===----------------------------------------------------------------------===//
34 // Custom Actions
35 //===----------------------------------------------------------------------===//
36 
37 std::unique_ptr<ASTConsumer>
38 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
39   return llvm::make_unique<ASTConsumer>();
40 }
41 
42 void InitOnlyAction::ExecuteAction() {
43 }
44 
45 //===----------------------------------------------------------------------===//
46 // AST Consumer Actions
47 //===----------------------------------------------------------------------===//
48 
49 std::unique_ptr<ASTConsumer>
50 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
51   if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
52     return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
53   return nullptr;
54 }
55 
56 std::unique_ptr<ASTConsumer>
57 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
58   return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
59                          CI.getFrontendOpts().ASTDumpDecls,
60                          CI.getFrontendOpts().ASTDumpLookups);
61 }
62 
63 std::unique_ptr<ASTConsumer>
64 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
65   return CreateASTDeclNodeLister();
66 }
67 
68 std::unique_ptr<ASTConsumer>
69 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
70   return CreateASTViewer();
71 }
72 
73 std::unique_ptr<ASTConsumer>
74 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
75                                           StringRef InFile) {
76   return CreateDeclContextPrinter();
77 }
78 
79 std::unique_ptr<ASTConsumer>
80 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
81   std::string Sysroot;
82   std::string OutputFile;
83   raw_pwrite_stream *OS =
84       ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
85   if (!OS)
86     return nullptr;
87 
88   if (!CI.getFrontendOpts().RelocatablePCH)
89     Sysroot.clear();
90 
91   auto Buffer = std::make_shared<PCHBuffer>();
92   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
93   Consumers.push_back(llvm::make_unique<PCHGenerator>(
94                         CI.getPreprocessor(), OutputFile, nullptr, Sysroot,
95                         Buffer, CI.getFrontendOpts().ModuleFileExtensions));
96   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
97       CI, InFile, OutputFile, OS, Buffer));
98 
99   return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
100 }
101 
102 raw_pwrite_stream *GeneratePCHAction::ComputeASTConsumerArguments(
103     CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
104     std::string &OutputFile) {
105   Sysroot = CI.getHeaderSearchOpts().Sysroot;
106   if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
107     CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
108     return nullptr;
109   }
110 
111   // We use createOutputFile here because this is exposed via libclang, and we
112   // must disable the RemoveFileOnSignal behavior.
113   // We use a temporary to avoid race conditions.
114   raw_pwrite_stream *OS =
115       CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
116                           /*RemoveFileOnSignal=*/false, InFile,
117                           /*Extension=*/"", /*useTemporary=*/true);
118   if (!OS)
119     return nullptr;
120 
121   OutputFile = CI.getFrontendOpts().OutputFile;
122   return OS;
123 }
124 
125 std::unique_ptr<ASTConsumer>
126 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
127                                         StringRef InFile) {
128   std::string Sysroot;
129   std::string OutputFile;
130   raw_pwrite_stream *OS =
131       ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
132   if (!OS)
133     return nullptr;
134 
135   auto Buffer = std::make_shared<PCHBuffer>();
136   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
137 
138   Consumers.push_back(llvm::make_unique<PCHGenerator>(
139                         CI.getPreprocessor(), OutputFile, Module, Sysroot,
140                         Buffer, CI.getFrontendOpts().ModuleFileExtensions,
141                         /*AllowASTWithErrors=*/false,
142                         /*IncludeTimestamps=*/
143                           +CI.getFrontendOpts().BuildingImplicitModule));
144   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
145       CI, InFile, OutputFile, OS, Buffer));
146   return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
147 }
148 
149 static SmallVectorImpl<char> &
150 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
151   Includes.append(RHS.begin(), RHS.end());
152   return Includes;
153 }
154 
155 static void addHeaderInclude(StringRef HeaderName,
156                              SmallVectorImpl<char> &Includes,
157                              const LangOptions &LangOpts,
158                              bool IsExternC) {
159   if (IsExternC && LangOpts.CPlusPlus)
160     Includes += "extern \"C\" {\n";
161   if (LangOpts.ObjC1)
162     Includes += "#import \"";
163   else
164     Includes += "#include \"";
165 
166   Includes += HeaderName;
167 
168   Includes += "\"\n";
169   if (IsExternC && LangOpts.CPlusPlus)
170     Includes += "}\n";
171 }
172 
173 /// \brief Collect the set of header includes needed to construct the given
174 /// module and update the TopHeaders file set of the module.
175 ///
176 /// \param Module The module we're collecting includes from.
177 ///
178 /// \param Includes Will be augmented with the set of \#includes or \#imports
179 /// needed to load all of the named headers.
180 static std::error_code
181 collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
182                             ModuleMap &ModMap, clang::Module *Module,
183                             SmallVectorImpl<char> &Includes) {
184   // Don't collect any headers for unavailable modules.
185   if (!Module->isAvailable())
186     return std::error_code();
187 
188   // Add includes for each of these headers.
189   for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
190     for (Module::Header &H : Module->Headers[HK]) {
191       Module->addTopHeader(H.Entry);
192       // Use the path as specified in the module map file. We'll look for this
193       // file relative to the module build directory (the directory containing
194       // the module map file) so this will find the same file that we found
195       // while parsing the module map.
196       addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
197     }
198   }
199   // Note that Module->PrivateHeaders will not be a TopHeader.
200 
201   if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
202     Module->addTopHeader(UmbrellaHeader.Entry);
203     if (Module->Parent)
204       // Include the umbrella header for submodules.
205       addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
206                        Module->IsExternC);
207   } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
208     // Add all of the headers we find in this subdirectory.
209     std::error_code EC;
210     SmallString<128> DirNative;
211     llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
212 
213     vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
214     for (vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
215          Dir != End && !EC; Dir.increment(EC)) {
216       // Check whether this entry has an extension typically associated with
217       // headers.
218       if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName()))
219           .Cases(".h", ".H", ".hh", ".hpp", true)
220           .Default(false))
221         continue;
222 
223       const FileEntry *Header = FileMgr.getFile(Dir->getName());
224       // FIXME: This shouldn't happen unless there is a file system race. Is
225       // that worth diagnosing?
226       if (!Header)
227         continue;
228 
229       // If this header is marked 'unavailable' in this module, don't include
230       // it.
231       if (ModMap.isHeaderUnavailableInModule(Header, Module))
232         continue;
233 
234       // Compute the relative path from the directory to this file.
235       SmallVector<StringRef, 16> Components;
236       auto PathIt = llvm::sys::path::rbegin(Dir->getName());
237       for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
238         Components.push_back(*PathIt);
239       SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
240       for (auto It = Components.rbegin(), End = Components.rend(); It != End;
241            ++It)
242         llvm::sys::path::append(RelativeHeader, *It);
243 
244       // Include this header as part of the umbrella directory.
245       Module->addTopHeader(Header);
246       addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
247     }
248 
249     if (EC)
250       return EC;
251   }
252 
253   // Recurse into submodules.
254   for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
255                                       SubEnd = Module->submodule_end();
256        Sub != SubEnd; ++Sub)
257     if (std::error_code Err = collectModuleHeaderIncludes(
258             LangOpts, FileMgr, ModMap, *Sub, Includes))
259       return Err;
260 
261   return std::error_code();
262 }
263 
264 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
265                                                  StringRef Filename) {
266   CI.getLangOpts().CompilingModule = true;
267 
268   // Find the module map file.
269   const FileEntry *ModuleMap =
270       CI.getFileManager().getFile(Filename, /*openFile*/true);
271   if (!ModuleMap)  {
272     CI.getDiagnostics().Report(diag::err_module_map_not_found)
273       << Filename;
274     return false;
275   }
276 
277   // Set up embedding for any specified files. Do this before we load any
278   // source files, including the primary module map for the compilation.
279   for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
280     if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true))
281       CI.getSourceManager().setFileIsTransient(FE);
282     else
283       CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
284   }
285   if (CI.getFrontendOpts().ModulesEmbedAllFiles)
286     CI.getSourceManager().setAllFilesAreTransient(true);
287 
288   // Parse the module map file.
289   HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
290   if (HS.loadModuleMapFile(ModuleMap, IsSystem))
291     return false;
292 
293   if (CI.getLangOpts().CurrentModule.empty()) {
294     CI.getDiagnostics().Report(diag::err_missing_module_name);
295 
296     // FIXME: Eventually, we could consider asking whether there was just
297     // a single module described in the module map, and use that as a
298     // default. Then it would be fairly trivial to just "compile" a module
299     // map with a single module (the common case).
300     return false;
301   }
302 
303   // If we're being run from the command-line, the module build stack will not
304   // have been filled in yet, so complete it now in order to allow us to detect
305   // module cycles.
306   SourceManager &SourceMgr = CI.getSourceManager();
307   if (SourceMgr.getModuleBuildStack().empty())
308     SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
309                                    FullSourceLoc(SourceLocation(), SourceMgr));
310 
311   // Dig out the module definition.
312   Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
313                            /*AllowSearch=*/false);
314   if (!Module) {
315     CI.getDiagnostics().Report(diag::err_missing_module)
316       << CI.getLangOpts().CurrentModule << Filename;
317 
318     return false;
319   }
320 
321   // Check whether we can build this module at all.
322   clang::Module::Requirement Requirement;
323   clang::Module::UnresolvedHeaderDirective MissingHeader;
324   if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
325                            MissingHeader)) {
326     if (MissingHeader.FileNameLoc.isValid()) {
327       CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
328                                  diag::err_module_header_missing)
329         << MissingHeader.IsUmbrella << MissingHeader.FileName;
330     } else {
331       CI.getDiagnostics().Report(diag::err_module_unavailable)
332         << Module->getFullModuleName()
333         << Requirement.second << Requirement.first;
334     }
335 
336     return false;
337   }
338 
339   if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
340     Module->IsInferred = true;
341     HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
342   } else {
343     ModuleMapForUniquing = ModuleMap;
344   }
345 
346   FileManager &FileMgr = CI.getFileManager();
347 
348   // Collect the set of #includes we need to build the module.
349   SmallString<256> HeaderContents;
350   std::error_code Err = std::error_code();
351   if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader())
352     addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
353                      CI.getLangOpts(), Module->IsExternC);
354   Err = collectModuleHeaderIncludes(
355         CI.getLangOpts(), FileMgr,
356         CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module,
357         HeaderContents);
358 
359   if (Err) {
360     CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
361       << Module->getFullModuleName() << Err.message();
362     return false;
363   }
364 
365   // Inform the preprocessor that includes from within the input buffer should
366   // be resolved relative to the build directory of the module map file.
367   CI.getPreprocessor().setMainFileDir(Module->Directory);
368 
369   std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
370       llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
371                                            Module::getModuleInputBufferName());
372   // Ownership of InputBuffer will be transferred to the SourceManager.
373   setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
374                                     Module->IsSystem));
375   return true;
376 }
377 
378 raw_pwrite_stream *GenerateModuleAction::ComputeASTConsumerArguments(
379     CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
380     std::string &OutputFile) {
381   // If no output file was provided, figure out where this module would go
382   // in the module cache.
383   if (CI.getFrontendOpts().OutputFile.empty()) {
384     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
385     CI.getFrontendOpts().OutputFile =
386         HS.getModuleFileName(CI.getLangOpts().CurrentModule,
387                              ModuleMapForUniquing->getName());
388   }
389 
390   // We use createOutputFile here because this is exposed via libclang, and we
391   // must disable the RemoveFileOnSignal behavior.
392   // We use a temporary to avoid race conditions.
393   raw_pwrite_stream *OS =
394       CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
395                           /*RemoveFileOnSignal=*/false, InFile,
396                           /*Extension=*/"", /*useTemporary=*/true,
397                           /*CreateMissingDirectories=*/true);
398   if (!OS)
399     return nullptr;
400 
401   OutputFile = CI.getFrontendOpts().OutputFile;
402   return OS;
403 }
404 
405 SyntaxOnlyAction::~SyntaxOnlyAction() {
406 }
407 
408 std::unique_ptr<ASTConsumer>
409 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
410   return llvm::make_unique<ASTConsumer>();
411 }
412 
413 std::unique_ptr<ASTConsumer>
414 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
415                                         StringRef InFile) {
416   return llvm::make_unique<ASTConsumer>();
417 }
418 
419 std::unique_ptr<ASTConsumer>
420 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
421   return llvm::make_unique<ASTConsumer>();
422 }
423 
424 void VerifyPCHAction::ExecuteAction() {
425   CompilerInstance &CI = getCompilerInstance();
426   bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
427   const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
428   std::unique_ptr<ASTReader> Reader(new ASTReader(
429       CI.getPreprocessor(), CI.getASTContext(), CI.getPCHContainerReader(),
430       CI.getFrontendOpts().ModuleFileExtensions,
431       Sysroot.empty() ? "" : Sysroot.c_str(),
432       /*DisableValidation*/ false,
433       /*AllowPCHWithCompilerErrors*/ false,
434       /*AllowConfigurationMismatch*/ true,
435       /*ValidateSystemInputs*/ true));
436 
437   Reader->ReadAST(getCurrentFile(),
438                   Preamble ? serialization::MK_Preamble
439                            : serialization::MK_PCH,
440                   SourceLocation(),
441                   ASTReader::ARR_ConfigurationMismatch);
442 }
443 
444 namespace {
445   /// \brief AST reader listener that dumps module information for a module
446   /// file.
447   class DumpModuleInfoListener : public ASTReaderListener {
448     llvm::raw_ostream &Out;
449 
450   public:
451     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
452 
453 #define DUMP_BOOLEAN(Value, Text)                       \
454     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
455 
456     bool ReadFullVersionInformation(StringRef FullVersion) override {
457       Out.indent(2)
458         << "Generated by "
459         << (FullVersion == getClangFullRepositoryVersion()? "this"
460                                                           : "a different")
461         << " Clang: " << FullVersion << "\n";
462       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
463     }
464 
465     void ReadModuleName(StringRef ModuleName) override {
466       Out.indent(2) << "Module name: " << ModuleName << "\n";
467     }
468     void ReadModuleMapFile(StringRef ModuleMapPath) override {
469       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
470     }
471 
472     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
473                              bool AllowCompatibleDifferences) override {
474       Out.indent(2) << "Language options:\n";
475 #define LANGOPT(Name, Bits, Default, Description) \
476       DUMP_BOOLEAN(LangOpts.Name, Description);
477 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
478       Out.indent(4) << Description << ": "                   \
479                     << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
480 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
481       Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
482 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
483 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
484 #include "clang/Basic/LangOptions.def"
485 
486       if (!LangOpts.ModuleFeatures.empty()) {
487         Out.indent(4) << "Module features:\n";
488         for (StringRef Feature : LangOpts.ModuleFeatures)
489           Out.indent(6) << Feature << "\n";
490       }
491 
492       return false;
493     }
494 
495     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
496                            bool AllowCompatibleDifferences) override {
497       Out.indent(2) << "Target options:\n";
498       Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
499       Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
500       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
501 
502       if (!TargetOpts.FeaturesAsWritten.empty()) {
503         Out.indent(4) << "Target features:\n";
504         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
505              I != N; ++I) {
506           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
507         }
508       }
509 
510       return false;
511     }
512 
513     bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
514                                bool Complain) override {
515       Out.indent(2) << "Diagnostic options:\n";
516 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
517 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
518       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
519 #define VALUE_DIAGOPT(Name, Bits, Default) \
520       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
521 #include "clang/Basic/DiagnosticOptions.def"
522 
523       Out.indent(4) << "Diagnostic flags:\n";
524       for (const std::string &Warning : DiagOpts->Warnings)
525         Out.indent(6) << "-W" << Warning << "\n";
526       for (const std::string &Remark : DiagOpts->Remarks)
527         Out.indent(6) << "-R" << Remark << "\n";
528 
529       return false;
530     }
531 
532     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
533                                  StringRef SpecificModuleCachePath,
534                                  bool Complain) override {
535       Out.indent(2) << "Header search options:\n";
536       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
537       Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
538       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
539                    "Use builtin include directories [-nobuiltininc]");
540       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
541                    "Use standard system include directories [-nostdinc]");
542       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
543                    "Use standard C++ include directories [-nostdinc++]");
544       DUMP_BOOLEAN(HSOpts.UseLibcxx,
545                    "Use libc++ (rather than libstdc++) [-stdlib=]");
546       return false;
547     }
548 
549     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
550                                  bool Complain,
551                                  std::string &SuggestedPredefines) override {
552       Out.indent(2) << "Preprocessor options:\n";
553       DUMP_BOOLEAN(PPOpts.UsePredefines,
554                    "Uses compiler/target-specific predefines [-undef]");
555       DUMP_BOOLEAN(PPOpts.DetailedRecord,
556                    "Uses detailed preprocessing record (for indexing)");
557 
558       if (!PPOpts.Macros.empty()) {
559         Out.indent(4) << "Predefined macros:\n";
560       }
561 
562       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
563              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
564            I != IEnd; ++I) {
565         Out.indent(6);
566         if (I->second)
567           Out << "-U";
568         else
569           Out << "-D";
570         Out << I->first << "\n";
571       }
572       return false;
573     }
574 
575     /// Indicates that a particular module file extension has been read.
576     void readModuleFileExtension(
577            const ModuleFileExtensionMetadata &Metadata) override {
578       Out.indent(2) << "Module file extension '"
579                     << Metadata.BlockName << "' " << Metadata.MajorVersion
580                     << "." << Metadata.MinorVersion;
581       if (!Metadata.UserInfo.empty()) {
582         Out << ": ";
583         Out.write_escaped(Metadata.UserInfo);
584       }
585 
586       Out << "\n";
587     }
588 #undef DUMP_BOOLEAN
589   };
590 }
591 
592 void DumpModuleInfoAction::ExecuteAction() {
593   // Set up the output file.
594   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
595   StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
596   if (!OutputFileName.empty() && OutputFileName != "-") {
597     std::error_code EC;
598     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
599                                            llvm::sys::fs::F_Text));
600   }
601   llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
602 
603   Out << "Information for module file '" << getCurrentFile() << "':\n";
604   DumpModuleInfoListener Listener(Out);
605   ASTReader::readASTFileControlBlock(
606       getCurrentFile(), getCompilerInstance().getFileManager(),
607       getCompilerInstance().getPCHContainerReader(),
608       /*FindModuleFileExtensions=*/true, Listener);
609 }
610 
611 //===----------------------------------------------------------------------===//
612 // Preprocessor Actions
613 //===----------------------------------------------------------------------===//
614 
615 void DumpRawTokensAction::ExecuteAction() {
616   Preprocessor &PP = getCompilerInstance().getPreprocessor();
617   SourceManager &SM = PP.getSourceManager();
618 
619   // Start lexing the specified input file.
620   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
621   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
622   RawLex.SetKeepWhitespaceMode(true);
623 
624   Token RawTok;
625   RawLex.LexFromRawLexer(RawTok);
626   while (RawTok.isNot(tok::eof)) {
627     PP.DumpToken(RawTok, true);
628     llvm::errs() << "\n";
629     RawLex.LexFromRawLexer(RawTok);
630   }
631 }
632 
633 void DumpTokensAction::ExecuteAction() {
634   Preprocessor &PP = getCompilerInstance().getPreprocessor();
635   // Start preprocessing the specified input file.
636   Token Tok;
637   PP.EnterMainSourceFile();
638   do {
639     PP.Lex(Tok);
640     PP.DumpToken(Tok, true);
641     llvm::errs() << "\n";
642   } while (Tok.isNot(tok::eof));
643 }
644 
645 void GeneratePTHAction::ExecuteAction() {
646   CompilerInstance &CI = getCompilerInstance();
647   raw_pwrite_stream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
648   if (!OS)
649     return;
650 
651   CacheTokens(CI.getPreprocessor(), OS);
652 }
653 
654 void PreprocessOnlyAction::ExecuteAction() {
655   Preprocessor &PP = getCompilerInstance().getPreprocessor();
656 
657   // Ignore unknown pragmas.
658   PP.IgnorePragmas();
659 
660   Token Tok;
661   // Start parsing the specified input file.
662   PP.EnterMainSourceFile();
663   do {
664     PP.Lex(Tok);
665   } while (Tok.isNot(tok::eof));
666 }
667 
668 void PrintPreprocessedAction::ExecuteAction() {
669   CompilerInstance &CI = getCompilerInstance();
670   // Output file may need to be set to 'Binary', to avoid converting Unix style
671   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
672   //
673   // Look to see what type of line endings the file uses. If there's a
674   // CRLF, then we won't open the file up in binary mode. If there is
675   // just an LF or CR, then we will open the file up in binary mode.
676   // In this fashion, the output format should match the input format, unless
677   // the input format has inconsistent line endings.
678   //
679   // This should be a relatively fast operation since most files won't have
680   // all of their source code on a single line. However, that is still a
681   // concern, so if we scan for too long, we'll just assume the file should
682   // be opened in binary mode.
683   bool BinaryMode = true;
684   bool InvalidFile = false;
685   const SourceManager& SM = CI.getSourceManager();
686   const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
687                                                      &InvalidFile);
688   if (!InvalidFile) {
689     const char *cur = Buffer->getBufferStart();
690     const char *end = Buffer->getBufferEnd();
691     const char *next = (cur != end) ? cur + 1 : end;
692 
693     // Limit ourselves to only scanning 256 characters into the source
694     // file.  This is mostly a sanity check in case the file has no
695     // newlines whatsoever.
696     if (end - cur > 256) end = cur + 256;
697 
698     while (next < end) {
699       if (*cur == 0x0D) {  // CR
700         if (*next == 0x0A)  // CRLF
701           BinaryMode = false;
702 
703         break;
704       } else if (*cur == 0x0A)  // LF
705         break;
706 
707       ++cur;
708       ++next;
709     }
710   }
711 
712   raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
713   if (!OS) return;
714 
715   DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
716                            CI.getPreprocessorOutputOpts());
717 }
718 
719 void PrintPreambleAction::ExecuteAction() {
720   switch (getCurrentFileKind()) {
721   case IK_C:
722   case IK_CXX:
723   case IK_ObjC:
724   case IK_ObjCXX:
725   case IK_OpenCL:
726   case IK_CUDA:
727     break;
728 
729   case IK_None:
730   case IK_Asm:
731   case IK_PreprocessedC:
732   case IK_PreprocessedCuda:
733   case IK_PreprocessedCXX:
734   case IK_PreprocessedObjC:
735   case IK_PreprocessedObjCXX:
736   case IK_AST:
737   case IK_LLVM_IR:
738     // We can't do anything with these.
739     return;
740   }
741 
742   CompilerInstance &CI = getCompilerInstance();
743   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
744   if (Buffer) {
745     unsigned Preamble =
746         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
747     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
748   }
749 }
750