clang  3.9.0
Frontend/FrontendActions.cpp
Go to the documentation of this file.
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 
11 #include "clang/AST/ASTConsumer.h"
14 #include "clang/Frontend/ASTUnit.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"
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>
51  if (std::unique_ptr<raw_ostream> OS =
52  CI.createDefaultOutputFile(false, InFile))
53  return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
54  return nullptr;
55 }
56 
57 std::unique_ptr<ASTConsumer>
62 }
63 
64 std::unique_ptr<ASTConsumer>
66  return CreateASTDeclNodeLister();
67 }
68 
69 std::unique_ptr<ASTConsumer>
71  return CreateASTViewer();
72 }
73 
74 std::unique_ptr<ASTConsumer>
76  StringRef InFile) {
77  return CreateDeclContextPrinter();
78 }
79 
80 std::unique_ptr<ASTConsumer>
82  std::string Sysroot;
83  std::string OutputFile;
84  std::unique_ptr<raw_pwrite_stream> OS =
85  ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
86  if (!OS)
87  return nullptr;
88 
90  Sysroot.clear();
91 
92  auto Buffer = std::make_shared<PCHBuffer>();
93  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
94  Consumers.push_back(llvm::make_unique<PCHGenerator>(
95  CI.getPreprocessor(), OutputFile, nullptr, Sysroot,
97  /*AllowASTWithErrors*/false,
98  /*IncludeTimestamps*/
100  Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
101  CI, InFile, OutputFile, std::move(OS), Buffer));
102 
103  return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
104 }
105 
106 std::unique_ptr<raw_pwrite_stream>
108  StringRef InFile,
109  std::string &Sysroot,
110  std::string &OutputFile) {
111  Sysroot = CI.getHeaderSearchOpts().Sysroot;
112  if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
113  CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
114  return nullptr;
115  }
116 
117  // We use createOutputFile here because this is exposed via libclang, and we
118  // must disable the RemoveFileOnSignal behavior.
119  // We use a temporary to avoid race conditions.
120  std::unique_ptr<raw_pwrite_stream> OS =
121  CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
122  /*RemoveFileOnSignal=*/false, InFile,
123  /*Extension=*/"", /*useTemporary=*/true);
124  if (!OS)
125  return nullptr;
126 
127  OutputFile = CI.getFrontendOpts().OutputFile;
128  return OS;
129 }
130 
131 std::unique_ptr<ASTConsumer>
133  StringRef InFile) {
134  std::string Sysroot;
135  std::string OutputFile;
136  std::unique_ptr<raw_pwrite_stream> OS =
137  ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
138  if (!OS)
139  return nullptr;
140 
141  auto Buffer = std::make_shared<PCHBuffer>();
142  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
143 
144  Consumers.push_back(llvm::make_unique<PCHGenerator>(
145  CI.getPreprocessor(), OutputFile, Module, Sysroot,
147  /*AllowASTWithErrors=*/false,
148  /*IncludeTimestamps=*/
150  Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
151  CI, InFile, OutputFile, std::move(OS), Buffer));
152  return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
153 }
154 
155 static SmallVectorImpl<char> &
156 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
157  Includes.append(RHS.begin(), RHS.end());
158  return Includes;
159 }
160 
161 static void addHeaderInclude(StringRef HeaderName,
162  SmallVectorImpl<char> &Includes,
163  const LangOptions &LangOpts,
164  bool IsExternC) {
165  if (IsExternC && LangOpts.CPlusPlus)
166  Includes += "extern \"C\" {\n";
167  if (LangOpts.ObjC1)
168  Includes += "#import \"";
169  else
170  Includes += "#include \"";
171 
172  Includes += HeaderName;
173 
174  Includes += "\"\n";
175  if (IsExternC && LangOpts.CPlusPlus)
176  Includes += "}\n";
177 }
178 
179 /// \brief Collect the set of header includes needed to construct the given
180 /// module and update the TopHeaders file set of the module.
181 ///
182 /// \param Module The module we're collecting includes from.
183 ///
184 /// \param Includes Will be augmented with the set of \#includes or \#imports
185 /// needed to load all of the named headers.
186 static std::error_code
188  ModuleMap &ModMap, clang::Module *Module,
189  SmallVectorImpl<char> &Includes) {
190  // Don't collect any headers for unavailable modules.
191  if (!Module->isAvailable())
192  return std::error_code();
193 
194  // Add includes for each of these headers.
195  for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
196  for (Module::Header &H : Module->Headers[HK]) {
197  Module->addTopHeader(H.Entry);
198  // Use the path as specified in the module map file. We'll look for this
199  // file relative to the module build directory (the directory containing
200  // the module map file) so this will find the same file that we found
201  // while parsing the module map.
202  addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
203  }
204  }
205  // Note that Module->PrivateHeaders will not be a TopHeader.
206 
207  if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
208  Module->addTopHeader(UmbrellaHeader.Entry);
209  if (Module->Parent)
210  // Include the umbrella header for submodules.
211  addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
212  Module->IsExternC);
213  } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
214  // Add all of the headers we find in this subdirectory.
215  std::error_code EC;
216  SmallString<128> DirNative;
217  llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
218 
219  vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
220  for (vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
221  Dir != End && !EC; Dir.increment(EC)) {
222  // Check whether this entry has an extension typically associated with
223  // headers.
224  if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName()))
225  .Cases(".h", ".H", ".hh", ".hpp", true)
226  .Default(false))
227  continue;
228 
229  const FileEntry *Header = FileMgr.getFile(Dir->getName());
230  // FIXME: This shouldn't happen unless there is a file system race. Is
231  // that worth diagnosing?
232  if (!Header)
233  continue;
234 
235  // If this header is marked 'unavailable' in this module, don't include
236  // it.
237  if (ModMap.isHeaderUnavailableInModule(Header, Module))
238  continue;
239 
240  // Compute the relative path from the directory to this file.
241  SmallVector<StringRef, 16> Components;
242  auto PathIt = llvm::sys::path::rbegin(Dir->getName());
243  for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
244  Components.push_back(*PathIt);
245  SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
246  for (auto It = Components.rbegin(), End = Components.rend(); It != End;
247  ++It)
248  llvm::sys::path::append(RelativeHeader, *It);
249 
250  // Include this header as part of the umbrella directory.
251  Module->addTopHeader(Header);
252  addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
253  }
254 
255  if (EC)
256  return EC;
257  }
258 
259  // Recurse into submodules.
261  SubEnd = Module->submodule_end();
262  Sub != SubEnd; ++Sub)
263  if (std::error_code Err = collectModuleHeaderIncludes(
264  LangOpts, FileMgr, ModMap, *Sub, Includes))
265  return Err;
266 
267  return std::error_code();
268 }
269 
271  StringRef Filename) {
272  CI.getLangOpts().CompilingModule = true;
273 
274  // Find the module map file.
275  const FileEntry *ModuleMap =
276  CI.getFileManager().getFile(Filename, /*openFile*/true);
277  if (!ModuleMap) {
278  CI.getDiagnostics().Report(diag::err_module_map_not_found)
279  << Filename;
280  return false;
281  }
282 
283  // Set up embedding for any specified files. Do this before we load any
284  // source files, including the primary module map for the compilation.
285  for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
286  if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true))
288  else
289  CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
290  }
293 
294  // Parse the module map file.
296  if (HS.loadModuleMapFile(ModuleMap, IsSystem))
297  return false;
298 
299  if (CI.getLangOpts().CurrentModule.empty()) {
300  CI.getDiagnostics().Report(diag::err_missing_module_name);
301 
302  // FIXME: Eventually, we could consider asking whether there was just
303  // a single module described in the module map, and use that as a
304  // default. Then it would be fairly trivial to just "compile" a module
305  // map with a single module (the common case).
306  return false;
307  }
308 
309  // If we're being run from the command-line, the module build stack will not
310  // have been filled in yet, so complete it now in order to allow us to detect
311  // module cycles.
312  SourceManager &SourceMgr = CI.getSourceManager();
313  if (SourceMgr.getModuleBuildStack().empty())
315  FullSourceLoc(SourceLocation(), SourceMgr));
316 
317  // Dig out the module definition.
319  /*AllowSearch=*/false);
320  if (!Module) {
321  CI.getDiagnostics().Report(diag::err_missing_module)
323 
324  return false;
325  }
326 
327  // Check whether we can build this module at all.
328  clang::Module::Requirement Requirement;
330  if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
331  MissingHeader)) {
332  if (MissingHeader.FileNameLoc.isValid()) {
333  CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
334  diag::err_module_header_missing)
335  << MissingHeader.IsUmbrella << MissingHeader.FileName;
336  } else {
337  CI.getDiagnostics().Report(diag::err_module_unavailable)
339  << Requirement.second << Requirement.first;
340  }
341 
342  return false;
343  }
344 
345  if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
346  Module->IsInferred = true;
347  HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
348  } else {
349  ModuleMapForUniquing = ModuleMap;
350  }
351 
352  FileManager &FileMgr = CI.getFileManager();
353 
354  // Collect the set of #includes we need to build the module.
355  SmallString<256> HeaderContents;
356  std::error_code Err = std::error_code();
357  if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader())
358  addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
359  CI.getLangOpts(), Module->IsExternC);
361  CI.getLangOpts(), FileMgr,
363  HeaderContents);
364 
365  if (Err) {
366  CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
367  << Module->getFullModuleName() << Err.message();
368  return false;
369  }
370 
371  // Inform the preprocessor that includes from within the input buffer should
372  // be resolved relative to the build directory of the module map file.
374 
375  std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
376  llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
378  // Ownership of InputBuffer will be transferred to the SourceManager.
379  setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
380  Module->IsSystem));
381  return true;
382 }
383 
384 std::unique_ptr<raw_pwrite_stream>
386  StringRef InFile,
387  std::string &Sysroot,
388  std::string &OutputFile) {
389  // If no output file was provided, figure out where this module would go
390  // in the module cache.
391  if (CI.getFrontendOpts().OutputFile.empty()) {
395  ModuleMapForUniquing->getName());
396  }
397 
398  // We use createOutputFile here because this is exposed via libclang, and we
399  // must disable the RemoveFileOnSignal behavior.
400  // We use a temporary to avoid race conditions.
401  std::unique_ptr<raw_pwrite_stream> OS =
402  CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
403  /*RemoveFileOnSignal=*/false, InFile,
404  /*Extension=*/"", /*useTemporary=*/true,
405  /*CreateMissingDirectories=*/true);
406  if (!OS)
407  return nullptr;
408 
409  OutputFile = CI.getFrontendOpts().OutputFile;
410  return OS;
411 }
412 
414 }
415 
416 std::unique_ptr<ASTConsumer>
418  return llvm::make_unique<ASTConsumer>();
419 }
420 
421 std::unique_ptr<ASTConsumer>
423  StringRef InFile) {
424  return llvm::make_unique<ASTConsumer>();
425 }
426 
427 std::unique_ptr<ASTConsumer>
429  return llvm::make_unique<ASTConsumer>();
430 }
431 
434  bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
435  const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
436  std::unique_ptr<ASTReader> Reader(new ASTReader(
439  Sysroot.empty() ? "" : Sysroot.c_str(),
440  /*DisableValidation*/ false,
441  /*AllowPCHWithCompilerErrors*/ false,
442  /*AllowConfigurationMismatch*/ true,
443  /*ValidateSystemInputs*/ true));
444 
445  Reader->ReadAST(getCurrentFile(),
446  Preamble ? serialization::MK_Preamble
448  SourceLocation(),
450 }
451 
452 namespace {
453  /// \brief AST reader listener that dumps module information for a module
454  /// file.
455  class DumpModuleInfoListener : public ASTReaderListener {
456  llvm::raw_ostream &Out;
457 
458  public:
459  DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
460 
461 #define DUMP_BOOLEAN(Value, Text) \
462  Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
463 
464  bool ReadFullVersionInformation(StringRef FullVersion) override {
465  Out.indent(2)
466  << "Generated by "
467  << (FullVersion == getClangFullRepositoryVersion()? "this"
468  : "a different")
469  << " Clang: " << FullVersion << "\n";
471  }
472 
473  void ReadModuleName(StringRef ModuleName) override {
474  Out.indent(2) << "Module name: " << ModuleName << "\n";
475  }
476  void ReadModuleMapFile(StringRef ModuleMapPath) override {
477  Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
478  }
479 
480  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
481  bool AllowCompatibleDifferences) override {
482  Out.indent(2) << "Language options:\n";
483 #define LANGOPT(Name, Bits, Default, Description) \
484  DUMP_BOOLEAN(LangOpts.Name, Description);
485 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
486  Out.indent(4) << Description << ": " \
487  << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
488 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
489  Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
490 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
491 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
492 #include "clang/Basic/LangOptions.def"
493 
494  if (!LangOpts.ModuleFeatures.empty()) {
495  Out.indent(4) << "Module features:\n";
496  for (StringRef Feature : LangOpts.ModuleFeatures)
497  Out.indent(6) << Feature << "\n";
498  }
499 
500  return false;
501  }
502 
503  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
504  bool AllowCompatibleDifferences) override {
505  Out.indent(2) << "Target options:\n";
506  Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
507  Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
508  Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
509 
510  if (!TargetOpts.FeaturesAsWritten.empty()) {
511  Out.indent(4) << "Target features:\n";
512  for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
513  I != N; ++I) {
514  Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
515  }
516  }
517 
518  return false;
519  }
520 
521  bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
522  bool Complain) override {
523  Out.indent(2) << "Diagnostic options:\n";
524 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
525 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
526  Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
527 #define VALUE_DIAGOPT(Name, Bits, Default) \
528  Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
529 #include "clang/Basic/DiagnosticOptions.def"
530 
531  Out.indent(4) << "Diagnostic flags:\n";
532  for (const std::string &Warning : DiagOpts->Warnings)
533  Out.indent(6) << "-W" << Warning << "\n";
534  for (const std::string &Remark : DiagOpts->Remarks)
535  Out.indent(6) << "-R" << Remark << "\n";
536 
537  return false;
538  }
539 
540  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
541  StringRef SpecificModuleCachePath,
542  bool Complain) override {
543  Out.indent(2) << "Header search options:\n";
544  Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
545  Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
547  "Use builtin include directories [-nobuiltininc]");
549  "Use standard system include directories [-nostdinc]");
551  "Use standard C++ include directories [-nostdinc++]");
552  DUMP_BOOLEAN(HSOpts.UseLibcxx,
553  "Use libc++ (rather than libstdc++) [-stdlib=]");
554  return false;
555  }
556 
557  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
558  bool Complain,
559  std::string &SuggestedPredefines) override {
560  Out.indent(2) << "Preprocessor options:\n";
562  "Uses compiler/target-specific predefines [-undef]");
564  "Uses detailed preprocessing record (for indexing)");
565 
566  if (!PPOpts.Macros.empty()) {
567  Out.indent(4) << "Predefined macros:\n";
568  }
569 
570  for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
571  I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
572  I != IEnd; ++I) {
573  Out.indent(6);
574  if (I->second)
575  Out << "-U";
576  else
577  Out << "-D";
578  Out << I->first << "\n";
579  }
580  return false;
581  }
582 
583  /// Indicates that a particular module file extension has been read.
584  void readModuleFileExtension(
585  const ModuleFileExtensionMetadata &Metadata) override {
586  Out.indent(2) << "Module file extension '"
587  << Metadata.BlockName << "' " << Metadata.MajorVersion
588  << "." << Metadata.MinorVersion;
589  if (!Metadata.UserInfo.empty()) {
590  Out << ": ";
591  Out.write_escaped(Metadata.UserInfo);
592  }
593 
594  Out << "\n";
595  }
596 #undef DUMP_BOOLEAN
597  };
598 }
599 
601  // Set up the output file.
602  std::unique_ptr<llvm::raw_fd_ostream> OutFile;
603  StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
604  if (!OutputFileName.empty() && OutputFileName != "-") {
605  std::error_code EC;
606  OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
607  llvm::sys::fs::F_Text));
608  }
609  llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
610 
611  Out << "Information for module file '" << getCurrentFile() << "':\n";
612  DumpModuleInfoListener Listener(Out);
614  getCurrentFile(), getCompilerInstance().getFileManager(),
615  getCompilerInstance().getPCHContainerReader(),
616  /*FindModuleFileExtensions=*/true, Listener);
617 }
618 
619 //===----------------------------------------------------------------------===//
620 // Preprocessor Actions
621 //===----------------------------------------------------------------------===//
622 
626 
627  // Start lexing the specified input file.
628  const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
629  Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
630  RawLex.SetKeepWhitespaceMode(true);
631 
632  Token RawTok;
633  RawLex.LexFromRawLexer(RawTok);
634  while (RawTok.isNot(tok::eof)) {
635  PP.DumpToken(RawTok, true);
636  llvm::errs() << "\n";
637  RawLex.LexFromRawLexer(RawTok);
638  }
639 }
640 
643  // Start preprocessing the specified input file.
644  Token Tok;
645  PP.EnterMainSourceFile();
646  do {
647  PP.Lex(Tok);
648  PP.DumpToken(Tok, true);
649  llvm::errs() << "\n";
650  } while (Tok.isNot(tok::eof));
651 }
652 
655  std::unique_ptr<raw_pwrite_stream> OS =
657  if (!OS)
658  return;
659 
660  CacheTokens(CI.getPreprocessor(), OS.get());
661 }
662 
665 
666  // Ignore unknown pragmas.
667  PP.IgnorePragmas();
668 
669  Token Tok;
670  // Start parsing the specified input file.
671  PP.EnterMainSourceFile();
672  do {
673  PP.Lex(Tok);
674  } while (Tok.isNot(tok::eof));
675 }
676 
679  // Output file may need to be set to 'Binary', to avoid converting Unix style
680  // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
681  //
682  // Look to see what type of line endings the file uses. If there's a
683  // CRLF, then we won't open the file up in binary mode. If there is
684  // just an LF or CR, then we will open the file up in binary mode.
685  // In this fashion, the output format should match the input format, unless
686  // the input format has inconsistent line endings.
687  //
688  // This should be a relatively fast operation since most files won't have
689  // all of their source code on a single line. However, that is still a
690  // concern, so if we scan for too long, we'll just assume the file should
691  // be opened in binary mode.
692  bool BinaryMode = true;
693  bool InvalidFile = false;
694  const SourceManager& SM = CI.getSourceManager();
695  const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
696  &InvalidFile);
697  if (!InvalidFile) {
698  const char *cur = Buffer->getBufferStart();
699  const char *end = Buffer->getBufferEnd();
700  const char *next = (cur != end) ? cur + 1 : end;
701 
702  // Limit ourselves to only scanning 256 characters into the source
703  // file. This is mostly a sanity check in case the file has no
704  // newlines whatsoever.
705  if (end - cur > 256) end = cur + 256;
706 
707  while (next < end) {
708  if (*cur == 0x0D) { // CR
709  if (*next == 0x0A) // CRLF
710  BinaryMode = false;
711 
712  break;
713  } else if (*cur == 0x0A) // LF
714  break;
715 
716  ++cur;
717  ++next;
718  }
719  }
720 
721  std::unique_ptr<raw_ostream> OS =
722  CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
723  if (!OS) return;
724 
727 }
728 
730  switch (getCurrentFileKind()) {
731  case IK_C:
732  case IK_CXX:
733  case IK_ObjC:
734  case IK_ObjCXX:
735  case IK_OpenCL:
736  case IK_CUDA:
737  break;
738 
739  case IK_None:
740  case IK_Asm:
741  case IK_PreprocessedC:
742  case IK_PreprocessedCuda:
743  case IK_PreprocessedCXX:
744  case IK_PreprocessedObjC:
746  case IK_AST:
747  case IK_LLVM_IR:
748  case IK_RenderScript:
749  // We can't do anything with these.
750  return;
751  }
752 
755  if (Buffer) {
756  unsigned Preamble =
757  Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
758  llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
759  }
760 }
std::string OutputFile
The output file, if any.
SourceManager & getSourceManager() const
Definition: Preprocessor.h:694
LangOptions & getLangOpts()
ASTContext & getASTContext() const
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
PreprocessorOptions & getPreprocessorOpts()
Header getUmbrellaHeader() const
Retrieve the header that serves as the umbrella header for this module.
Definition: Basic/Module.h:401
std::vector< std::pair< std::string, bool > > Macros
Lexer - This provides a simple interface that turns a text buffer into a stream of tokens...
Definition: Lexer.h:46
bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override
Callback at the start of processing a single input.
unsigned UseLibcxx
Use libc++ instead of the default libstdc++.
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:117
static std::pair< unsigned, bool > ComputePreamble(StringRef Buffer, const LangOptions &LangOpts, unsigned MaxLines=0)
Compute the preamble of the given file.
Definition: Lexer.cpp:537
Defines the clang::FileManager interface and associated types.
submodule_iterator submodule_begin()
Definition: Basic/Module.h:474
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "...
Definition: Basic/Module.h:178
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
CompilerInstance & getCompilerInstance() const
llvm::MemoryBuffer * getBuffer(FileID FID, SourceLocation Loc, bool *Invalid=nullptr) const
Return the buffer for the specified FileID.
std::string getModuleFileName(Module *Module)
Retrieve the name of the module file that should be used to load the given module.
std::unique_ptr< llvm::MemoryBuffer > Buffer
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1124
static std::unique_ptr< raw_pwrite_stream > ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile, std::string &Sysroot, std::string &OutputFile)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
std::string ASTDumpFilter
If given, filter dumped AST Decl nodes by this substring.
TargetInfo & getTarget() const
SourceManager & getSourceManager() const
Return the current source manager.
void IgnorePragmas()
Install empty handlers for all pragmas (making them ignored).
Definition: Pragma.cpp:1506
Options for controlling the target.
Definition: TargetOptions.h:25
InputKind getCurrentFileKind() const
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:590
bool isHeaderUnavailableInModule(const FileEntry *Header, const Module *RequestingModule) const
Determine whether the given header is unavailable as part of the specified module.
Definition: ModuleMap.cpp:437
void DumpToken(const Token &Tok, bool DumpFlags=false) const
Print the token to stderr, used for debugging.
std::string getFullModuleName() const
Retrieve the full name of this module, including the path from its top-level module.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
The virtual file system interface.
static StringRef getModuleInputBufferName()
Definition: Basic/Module.h:492
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, bool FindModuleFileExtensions, ASTReaderListener &Listener)
Read the control block for the named AST file.
Definition: ASTReader.cpp:4194
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:690
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
unsigned DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
An input iterator over the recursive contents of a virtual path, similar to llvm::sys::fs::recursive_...
The client can handle an AST file that cannot load because it's compiled configuration doesn't match ...
Definition: ASTReader.h:1373
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
unsigned BuildingImplicitModule
Whether we are performing an implicit module build.
std::vector< std::string > ModulesEmbedFiles
The list of files to embed into the compiled module file.
Describes a module or submodule.
Definition: Basic/Module.h:47
unsigned RelocatablePCH
When generating PCH files, instruct the AST writer to create relocatable PCH files.
std::string UserInfo
A string containing additional user information that will be stored with the metadata.
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
Definition: Basic/Module.h:314
FrontendOptions & getFrontendOpts()
IntrusiveRefCntPtr< vfs::FileSystem > getVirtualFileSystem() const
Definition: FileManager.h:226
PreprocessorOutputOptions & getPreprocessorOutputOpts()
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:695
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Module * Parent
The parent of this module.
Definition: Basic/Module.h:57
unsigned IsInferred
Whether this is an inferred submodule (module * { ... }).
Definition: Basic/Module.h:181
submodule_iterator submodule_end()
Definition: Basic/Module.h:476
void CacheTokens(Preprocessor &PP, raw_pwrite_stream *OS)
Cache tokens for use with PCH. Note that this requires a seekable stream.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
#define DUMP_BOOLEAN(Value, Text)
iterator end() const
HeaderSearchOptions & getHeaderSearchOpts()
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...
detail::InMemoryDirectory::const_iterator I
Preprocessor & getPreprocessor() const
Return the current preprocessor.
std::vector< Module * >::iterator submodule_iterator
Definition: Basic/Module.h:471
std::vector< std::string > ModuleFeatures
The names of any features to enable in module 'requires' decls in addition to the hard-coded list in ...
Definition: LangOptions.h:113
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:137
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers)...
Definition: Basic/Module.h:173
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
std::string CurrentModule
The name of the current module, of which the main source file is a part.
Definition: LangOptions.h:107
StringRef Filename
Definition: Format.cpp:1194
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
void setCurrentInput(const FrontendInputFile &CurrentInput, std::unique_ptr< ASTUnit > AST=nullptr)
void setFileIsTransient(const FileEntry *SourceFile)
Specify that a file is transient.
unsigned UsePredefines
Initialize the preprocessor with the compiler and target specific predefines.
void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
DoPrintPreprocessedInput - Implement -E mode.
ModuleBuildStack getModuleBuildStack() const
Retrieve the module build stack.
static SmallVectorImpl< char > & operator+=(SmallVectorImpl< char > &Includes, StringRef RHS)
bool loadModuleMapFile(const FileEntry *File, bool IsSystem)
Read the contents of the given module map file.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc...
Defines the clang::Preprocessor interface.
unsigned UseBuiltinIncludes
Include the compiler builtin includes.
unsigned ModulesEmbedAllFiles
Whether we should embed all used files into the PCM file.
bool isNot(tok::TokenKind K) const
Definition: Token.h:95
An input file for the front end.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
DirectoryName getUmbrellaDir() const
Retrieve the directory for which this module serves as the umbrella.
static std::error_code collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr, ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl< char > &Includes)
Collect the set of header includes needed to construct the given module and update the TopHeaders fil...
Information about a header directive as found in the module map file.
Definition: Basic/Module.h:109
Module * lookupModule(StringRef ModuleName, bool AllowSearch=true)
Lookup a module Search for a module with the given name.
const SourceManager & SM
Definition: Format.cpp:1184
unsigned MajorVersion
The major version of the extension data.
std::unique_ptr< raw_pwrite_stream > createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, StringRef BaseInput, StringRef Extension, bool UseTemporary, bool CreateMissingDirectories=false)
Create a new output file and add it to the list of tracked output files, optionally deriving the outp...
unsigned MinorVersion
The minor version of the extension data.
std::string CPU
If given, the name of the target CPU to generate code for.
Definition: TargetOptions.h:36
const DirectoryEntry * Directory
The build directory of this module.
Definition: Basic/Module.h:62
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
std::string ABI
If given, the name of the target ABI to use.
Definition: TargetOptions.h:42
File is a PCH file treated as the preamble.
const char * getName() const
Definition: FileManager.h:85
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
unsigned UseStandardSystemIncludes
Include the system standard include search directories.
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
File is a PCH file treated as such.
bool isValid() const
Return true if this is a valid SourceLocation object.
const StringRef getCurrentFile() const
Information about a directory name as found in the module map file.
Definition: Basic/Module.h:118
std::unique_ptr< raw_pwrite_stream > ComputeASTConsumerArguments(CompilerInstance &CI, StringRef InFile, std::string &Sysroot, std::string &OutputFile)
Compute the AST consumer arguments that will be used to create the PCHGenerator instance returned by ...
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
unsigned IncludeTimestamps
Whether timestamps should be written to the produced PCH file.
SmallVector< Header, 2 > Headers[5]
The headers that are part of this module.
Definition: Basic/Module.h:126
std::string getClangFullRepositoryVersion()
Retrieves the full repository version that is an amalgamation of the information in getClangRepositor...
Definition: Version.cpp:90
void Lex(Token &Result)
Lex the next token for this preprocessor.
virtual bool ReadFullVersionInformation(StringRef FullVersion)
Receives the full Clang version information.
Definition: ASTReader.h:112
unsigned ASTDumpLookups
Whether we include lookup table dumps in AST dumps.
std::vector< std::string > FeaturesAsWritten
The list of target specific features to enable or disable, as written on the command line...
Definition: TargetOptions.h:51
void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc)
Push an entry to the module build stack.
FileID getMainFileID() const
Returns the FileID of the main source file.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Basic/Module.h:130
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:312
FileManager & getFileManager() const
Return the current file manager to the caller.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool ShouldCloseOpenFile=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
virtual std::unique_ptr< ASTConsumer > CreatePCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName, const std::string &OutputFileName, std::unique_ptr< llvm::raw_pwrite_stream > OS, std::shared_ptr< PCHBuffer > Buffer) const =0
Return an ASTConsumer that can be chained with a PCHGenerator that produces a wrapper file format con...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
void addTopHeader(const FileEntry *File)
Add a top-level header associated with this module.
Definition: Basic/Module.h:414
static void addHeaderInclude(StringRef HeaderName, SmallVectorImpl< char > &Includes, const LangOptions &LangOpts, bool IsExternC)
std::string BlockName
The name used to identify this particular extension block within the resulting module file...
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:104
std::unique_ptr< ASTConsumer > CreateDeclContextPrinter()
std::pair< std::string, bool > Requirement
An individual requirement: a feature name and a flag indicating the required state of that feature...
Definition: Basic/Module.h:142
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
std::unique_ptr< ASTConsumer > CreateASTViewer()
Metadata for a module file extension.
const PCHContainerWriter & getPCHContainerWriter() const
Return the appropriate PCHContainerWriter depending on the current CodeGenOptions.
std::unique_ptr< ASTConsumer > CreateASTDeclNodeLister()
void setMainFileDir(const DirectoryEntry *Dir)
Set the directory in which the main file should be considered to have been found, if it is not a real...
unsigned ASTDumpDecls
Whether we include declaration dumps in AST dumps.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::pair< unsigned, bool > PrecompiledPreambleBytes
If non-zero, the implicit PCH include is actually a precompiled preamble that covers this number of b...
std::vector< IntrusiveRefCntPtr< ModuleFileExtension > > ModuleFileExtensions
The list of module file extensions.
A SourceLocation and its associated SourceManager.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
std::string Sysroot
If non-empty, the directory to use as a "virtual system root" for include paths.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< ASTConsumer > CreateASTDumper(StringRef FilterString, bool DumpDecls, bool DumpLookups)
void setAllFilesAreTransient(bool Transient)
Specify that all files that are read during this compilation are transient.
std::unique_ptr< ASTConsumer > CreateASTPrinter(std::unique_ptr< raw_ostream > OS, StringRef FilterString)
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
unsigned UseStandardCXXIncludes
Include the system standard C++ library include search directories.
void SetKeepWhitespaceMode(bool Val)
SetKeepWhitespaceMode - This method lets clients enable or disable whitespace retention mode...
Definition: Lexer.h:172
std::string Triple
If given, the name of the target triple to compile for.
Definition: TargetOptions.h:29
void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap)
Definition: ModuleMap.cpp:848
This class handles loading and caching of source files into memory.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:97
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...