]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Frontend/FrontendActions.cpp
Import tzdata 2018e
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Frontend / FrontendActions.cpp
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/CompilerInstance.h"
15 #include "clang/Frontend/FrontendDiagnostic.h"
16 #include "clang/Frontend/MultiplexConsumer.h"
17 #include "clang/Frontend/Utils.h"
18 #include "clang/Lex/HeaderSearch.h"
19 #include "clang/Lex/Preprocessor.h"
20 #include "clang/Lex/PreprocessorOptions.h"
21 #include "clang/Serialization/ASTReader.h"
22 #include "clang/Serialization/ASTWriter.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <memory>
28 #include <system_error>
29
30 using namespace clang;
31
32 //===----------------------------------------------------------------------===//
33 // Custom Actions
34 //===----------------------------------------------------------------------===//
35
36 std::unique_ptr<ASTConsumer>
37 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
38   return llvm::make_unique<ASTConsumer>();
39 }
40
41 void InitOnlyAction::ExecuteAction() {
42 }
43
44 //===----------------------------------------------------------------------===//
45 // AST Consumer Actions
46 //===----------------------------------------------------------------------===//
47
48 std::unique_ptr<ASTConsumer>
49 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
50   if (std::unique_ptr<raw_ostream> OS =
51           CI.createDefaultOutputFile(false, InFile))
52     return CreateASTPrinter(std::move(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().ASTDumpAll,
61                          CI.getFrontendOpts().ASTDumpLookups);
62 }
63
64 std::unique_ptr<ASTConsumer>
65 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
66   return CreateASTDeclNodeLister();
67 }
68
69 std::unique_ptr<ASTConsumer>
70 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
71   return CreateASTViewer();
72 }
73
74 std::unique_ptr<ASTConsumer>
75 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
76                                           StringRef InFile) {
77   return CreateDeclContextPrinter();
78 }
79
80 std::unique_ptr<ASTConsumer>
81 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
82   std::string Sysroot;
83   if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
84     return nullptr;
85
86   std::string OutputFile;
87   std::unique_ptr<raw_pwrite_stream> OS =
88       CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
89   if (!OS)
90     return nullptr;
91
92   if (!CI.getFrontendOpts().RelocatablePCH)
93     Sysroot.clear();
94
95   auto Buffer = std::make_shared<PCHBuffer>();
96   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
97   Consumers.push_back(llvm::make_unique<PCHGenerator>(
98                         CI.getPreprocessor(), OutputFile, Sysroot,
99                         Buffer, CI.getFrontendOpts().ModuleFileExtensions,
100       /*AllowASTWithErrors*/CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
101                         /*IncludeTimestamps*/
102                           +CI.getFrontendOpts().IncludeTimestamps));
103   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
104       CI, InFile, OutputFile, std::move(OS), Buffer));
105
106   return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
107 }
108
109 bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
110                                                     std::string &Sysroot) {
111   Sysroot = CI.getHeaderSearchOpts().Sysroot;
112   if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
113     CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
114     return false;
115   }
116
117   return true;
118 }
119
120 std::unique_ptr<llvm::raw_pwrite_stream>
121 GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
122                                     std::string &OutputFile) {
123   // We use createOutputFile here because this is exposed via libclang, and we
124   // must disable the RemoveFileOnSignal behavior.
125   // We use a temporary to avoid race conditions.
126   std::unique_ptr<raw_pwrite_stream> OS =
127       CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
128                           /*RemoveFileOnSignal=*/false, InFile,
129                           /*Extension=*/"", /*useTemporary=*/true);
130   if (!OS)
131     return nullptr;
132
133   OutputFile = CI.getFrontendOpts().OutputFile;
134   return OS;
135 }
136
137 bool GeneratePCHAction::shouldEraseOutputFiles() {
138   if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
139     return false;
140   return ASTFrontendAction::shouldEraseOutputFiles();
141 }
142
143 bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
144   CI.getLangOpts().CompilingPCH = true;
145   return true;
146 }
147
148 std::unique_ptr<ASTConsumer>
149 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
150                                         StringRef InFile) {
151   std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
152   if (!OS)
153     return nullptr;
154
155   std::string OutputFile = CI.getFrontendOpts().OutputFile;
156   std::string Sysroot;
157
158   auto Buffer = std::make_shared<PCHBuffer>();
159   std::vector<std::unique_ptr<ASTConsumer>> Consumers;
160
161   Consumers.push_back(llvm::make_unique<PCHGenerator>(
162                         CI.getPreprocessor(), OutputFile, Sysroot,
163                         Buffer, CI.getFrontendOpts().ModuleFileExtensions,
164                         /*AllowASTWithErrors=*/false,
165                         /*IncludeTimestamps=*/
166                           +CI.getFrontendOpts().BuildingImplicitModule));
167   Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
168       CI, InFile, OutputFile, std::move(OS), Buffer));
169   return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
170 }
171
172 bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
173     CompilerInstance &CI) {
174   if (!CI.getLangOpts().Modules) {
175     CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
176     return false;
177   }
178
179   return GenerateModuleAction::BeginSourceFileAction(CI);
180 }
181
182 std::unique_ptr<raw_pwrite_stream>
183 GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
184                                                     StringRef InFile) {
185   // If no output file was provided, figure out where this module would go
186   // in the module cache.
187   if (CI.getFrontendOpts().OutputFile.empty()) {
188     StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
189     if (ModuleMapFile.empty())
190       ModuleMapFile = InFile;
191
192     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
193     CI.getFrontendOpts().OutputFile =
194         HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
195                                    ModuleMapFile);
196   }
197
198   // We use createOutputFile here because this is exposed via libclang, and we
199   // must disable the RemoveFileOnSignal behavior.
200   // We use a temporary to avoid race conditions.
201   return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
202                              /*RemoveFileOnSignal=*/false, InFile,
203                              /*Extension=*/"", /*useTemporary=*/true,
204                              /*CreateMissingDirectories=*/true);
205 }
206
207 bool GenerateModuleInterfaceAction::BeginSourceFileAction(
208     CompilerInstance &CI) {
209   if (!CI.getLangOpts().ModulesTS) {
210     CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts);
211     return false;
212   }
213
214   CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
215
216   return GenerateModuleAction::BeginSourceFileAction(CI);
217 }
218
219 std::unique_ptr<raw_pwrite_stream>
220 GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
221                                                 StringRef InFile) {
222   return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
223 }
224
225 SyntaxOnlyAction::~SyntaxOnlyAction() {
226 }
227
228 std::unique_ptr<ASTConsumer>
229 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
230   return llvm::make_unique<ASTConsumer>();
231 }
232
233 std::unique_ptr<ASTConsumer>
234 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
235                                         StringRef InFile) {
236   return llvm::make_unique<ASTConsumer>();
237 }
238
239 std::unique_ptr<ASTConsumer>
240 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
241   return llvm::make_unique<ASTConsumer>();
242 }
243
244 void VerifyPCHAction::ExecuteAction() {
245   CompilerInstance &CI = getCompilerInstance();
246   bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
247   const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
248   std::unique_ptr<ASTReader> Reader(new ASTReader(
249       CI.getPreprocessor(), &CI.getASTContext(), CI.getPCHContainerReader(),
250       CI.getFrontendOpts().ModuleFileExtensions,
251       Sysroot.empty() ? "" : Sysroot.c_str(),
252       /*DisableValidation*/ false,
253       /*AllowPCHWithCompilerErrors*/ false,
254       /*AllowConfigurationMismatch*/ true,
255       /*ValidateSystemInputs*/ true));
256
257   Reader->ReadAST(getCurrentFile(),
258                   Preamble ? serialization::MK_Preamble
259                            : serialization::MK_PCH,
260                   SourceLocation(),
261                   ASTReader::ARR_ConfigurationMismatch);
262 }
263
264 namespace {
265   /// \brief AST reader listener that dumps module information for a module
266   /// file.
267   class DumpModuleInfoListener : public ASTReaderListener {
268     llvm::raw_ostream &Out;
269
270   public:
271     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
272
273 #define DUMP_BOOLEAN(Value, Text)                       \
274     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
275
276     bool ReadFullVersionInformation(StringRef FullVersion) override {
277       Out.indent(2)
278         << "Generated by "
279         << (FullVersion == getClangFullRepositoryVersion()? "this"
280                                                           : "a different")
281         << " Clang: " << FullVersion << "\n";
282       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
283     }
284
285     void ReadModuleName(StringRef ModuleName) override {
286       Out.indent(2) << "Module name: " << ModuleName << "\n";
287     }
288     void ReadModuleMapFile(StringRef ModuleMapPath) override {
289       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
290     }
291
292     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
293                              bool AllowCompatibleDifferences) override {
294       Out.indent(2) << "Language options:\n";
295 #define LANGOPT(Name, Bits, Default, Description) \
296       DUMP_BOOLEAN(LangOpts.Name, Description);
297 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
298       Out.indent(4) << Description << ": "                   \
299                     << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
300 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
301       Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
302 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
303 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
304 #include "clang/Basic/LangOptions.def"
305
306       if (!LangOpts.ModuleFeatures.empty()) {
307         Out.indent(4) << "Module features:\n";
308         for (StringRef Feature : LangOpts.ModuleFeatures)
309           Out.indent(6) << Feature << "\n";
310       }
311
312       return false;
313     }
314
315     bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
316                            bool AllowCompatibleDifferences) override {
317       Out.indent(2) << "Target options:\n";
318       Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
319       Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
320       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
321
322       if (!TargetOpts.FeaturesAsWritten.empty()) {
323         Out.indent(4) << "Target features:\n";
324         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
325              I != N; ++I) {
326           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
327         }
328       }
329
330       return false;
331     }
332
333     bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
334                                bool Complain) override {
335       Out.indent(2) << "Diagnostic options:\n";
336 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
337 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
338       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
339 #define VALUE_DIAGOPT(Name, Bits, Default) \
340       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
341 #include "clang/Basic/DiagnosticOptions.def"
342
343       Out.indent(4) << "Diagnostic flags:\n";
344       for (const std::string &Warning : DiagOpts->Warnings)
345         Out.indent(6) << "-W" << Warning << "\n";
346       for (const std::string &Remark : DiagOpts->Remarks)
347         Out.indent(6) << "-R" << Remark << "\n";
348
349       return false;
350     }
351
352     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
353                                  StringRef SpecificModuleCachePath,
354                                  bool Complain) override {
355       Out.indent(2) << "Header search options:\n";
356       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
357       Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
358       Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
359       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
360                    "Use builtin include directories [-nobuiltininc]");
361       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
362                    "Use standard system include directories [-nostdinc]");
363       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
364                    "Use standard C++ include directories [-nostdinc++]");
365       DUMP_BOOLEAN(HSOpts.UseLibcxx,
366                    "Use libc++ (rather than libstdc++) [-stdlib=]");
367       return false;
368     }
369
370     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
371                                  bool Complain,
372                                  std::string &SuggestedPredefines) override {
373       Out.indent(2) << "Preprocessor options:\n";
374       DUMP_BOOLEAN(PPOpts.UsePredefines,
375                    "Uses compiler/target-specific predefines [-undef]");
376       DUMP_BOOLEAN(PPOpts.DetailedRecord,
377                    "Uses detailed preprocessing record (for indexing)");
378
379       if (!PPOpts.Macros.empty()) {
380         Out.indent(4) << "Predefined macros:\n";
381       }
382
383       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
384              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
385            I != IEnd; ++I) {
386         Out.indent(6);
387         if (I->second)
388           Out << "-U";
389         else
390           Out << "-D";
391         Out << I->first << "\n";
392       }
393       return false;
394     }
395
396     /// Indicates that a particular module file extension has been read.
397     void readModuleFileExtension(
398            const ModuleFileExtensionMetadata &Metadata) override {
399       Out.indent(2) << "Module file extension '"
400                     << Metadata.BlockName << "' " << Metadata.MajorVersion
401                     << "." << Metadata.MinorVersion;
402       if (!Metadata.UserInfo.empty()) {
403         Out << ": ";
404         Out.write_escaped(Metadata.UserInfo);
405       }
406
407       Out << "\n";
408     }
409 #undef DUMP_BOOLEAN
410   };
411 }
412
413 bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
414   // The Object file reader also supports raw ast files and there is no point in
415   // being strict about the module file format in -module-file-info mode.
416   CI.getHeaderSearchOpts().ModuleFormat = "obj";
417   return true;
418 }
419
420 void DumpModuleInfoAction::ExecuteAction() {
421   // Set up the output file.
422   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
423   StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
424   if (!OutputFileName.empty() && OutputFileName != "-") {
425     std::error_code EC;
426     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
427                                            llvm::sys::fs::F_Text));
428   }
429   llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
430
431   Out << "Information for module file '" << getCurrentFile() << "':\n";
432   auto &FileMgr = getCompilerInstance().getFileManager();
433   auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
434   StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
435   bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
436                 Magic[2] == 'C' && Magic[3] == 'H');
437   Out << "  Module format: " << (IsRaw ? "raw" : "obj") << "\n";
438
439   Preprocessor &PP = getCompilerInstance().getPreprocessor();
440   DumpModuleInfoListener Listener(Out);
441   HeaderSearchOptions &HSOpts =
442       PP.getHeaderSearchInfo().getHeaderSearchOpts();
443   ASTReader::readASTFileControlBlock(
444       getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
445       /*FindModuleFileExtensions=*/true, Listener,
446       HSOpts.ModulesValidateDiagnosticOptions);
447 }
448
449 //===----------------------------------------------------------------------===//
450 // Preprocessor Actions
451 //===----------------------------------------------------------------------===//
452
453 void DumpRawTokensAction::ExecuteAction() {
454   Preprocessor &PP = getCompilerInstance().getPreprocessor();
455   SourceManager &SM = PP.getSourceManager();
456
457   // Start lexing the specified input file.
458   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
459   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
460   RawLex.SetKeepWhitespaceMode(true);
461
462   Token RawTok;
463   RawLex.LexFromRawLexer(RawTok);
464   while (RawTok.isNot(tok::eof)) {
465     PP.DumpToken(RawTok, true);
466     llvm::errs() << "\n";
467     RawLex.LexFromRawLexer(RawTok);
468   }
469 }
470
471 void DumpTokensAction::ExecuteAction() {
472   Preprocessor &PP = getCompilerInstance().getPreprocessor();
473   // Start preprocessing the specified input file.
474   Token Tok;
475   PP.EnterMainSourceFile();
476   do {
477     PP.Lex(Tok);
478     PP.DumpToken(Tok, true);
479     llvm::errs() << "\n";
480   } while (Tok.isNot(tok::eof));
481 }
482
483 void GeneratePTHAction::ExecuteAction() {
484   CompilerInstance &CI = getCompilerInstance();
485   std::unique_ptr<raw_pwrite_stream> OS =
486       CI.createDefaultOutputFile(true, getCurrentFile());
487   if (!OS)
488     return;
489
490   CacheTokens(CI.getPreprocessor(), OS.get());
491 }
492
493 void PreprocessOnlyAction::ExecuteAction() {
494   Preprocessor &PP = getCompilerInstance().getPreprocessor();
495
496   // Ignore unknown pragmas.
497   PP.IgnorePragmas();
498
499   Token Tok;
500   // Start parsing the specified input file.
501   PP.EnterMainSourceFile();
502   do {
503     PP.Lex(Tok);
504   } while (Tok.isNot(tok::eof));
505 }
506
507 void PrintPreprocessedAction::ExecuteAction() {
508   CompilerInstance &CI = getCompilerInstance();
509   // Output file may need to be set to 'Binary', to avoid converting Unix style
510   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
511   //
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;
547       ++next;
548     }
549   }
550
551   std::unique_ptr<raw_ostream> OS =
552       CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
553   if (!OS) return;
554
555   // If we're preprocessing a module map, start by dumping the contents of the
556   // module itself before switching to the input buffer.
557   auto &Input = getCurrentInput();
558   if (Input.getKind().getFormat() == InputKind::ModuleMap) {
559     if (Input.isFile()) {
560       (*OS) << "# 1 \"";
561       OS->write_escaped(Input.getFile());
562       (*OS) << "\"\n";
563     }
564     // FIXME: Include additional information here so that we don't need the
565     // original source files to exist on disk.
566     getCurrentModule()->print(*OS);
567     (*OS) << "#pragma clang module contents\n";
568   }
569
570   DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
571                            CI.getPreprocessorOutputOpts());
572 }
573
574 void PrintPreambleAction::ExecuteAction() {
575   switch (getCurrentFileKind().getLanguage()) {
576   case InputKind::C:
577   case InputKind::CXX:
578   case InputKind::ObjC:
579   case InputKind::ObjCXX:
580   case InputKind::OpenCL:
581   case InputKind::CUDA:
582     break;
583       
584   case InputKind::Unknown:
585   case InputKind::Asm:
586   case InputKind::LLVM_IR:
587   case InputKind::RenderScript:
588     // We can't do anything with these.
589     return;
590   }
591
592   // We don't expect to find any #include directives in a preprocessed input.
593   if (getCurrentFileKind().isPreprocessed())
594     return;
595
596   CompilerInstance &CI = getCompilerInstance();
597   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
598   if (Buffer) {
599     unsigned Preamble =
600         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
601     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
602   }
603 }