clang  3.7.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 (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
53  return nullptr;
54 }
55 
56 std::unique_ptr<ASTConsumer>
61 }
62 
63 std::unique_ptr<ASTConsumer>
65  return CreateASTDeclNodeLister();
66 }
67 
68 std::unique_ptr<ASTConsumer>
70  return CreateASTViewer();
71 }
72 
73 std::unique_ptr<ASTConsumer>
75  StringRef InFile) {
76  return CreateDeclContextPrinter();
77 }
78 
79 std::unique_ptr<ASTConsumer>
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 
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, Buffer));
95  Consumers.push_back(
99  InFile, OutputFile, OS, Buffer));
100 
101  return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
102 }
103 
105  CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
106  std::string &OutputFile) {
107  Sysroot = CI.getHeaderSearchOpts().Sysroot;
108  if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
109  CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
110  return nullptr;
111  }
112 
113  // We use createOutputFile here because this is exposed via libclang, and we
114  // must disable the RemoveFileOnSignal behavior.
115  // We use a temporary to avoid race conditions.
116  raw_pwrite_stream *OS =
117  CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
118  /*RemoveFileOnSignal=*/false, InFile,
119  /*Extension=*/"", /*useTemporary=*/true);
120  if (!OS)
121  return nullptr;
122 
123  OutputFile = CI.getFrontendOpts().OutputFile;
124  return OS;
125 }
126 
127 std::unique_ptr<ASTConsumer>
129  StringRef InFile) {
130  std::string Sysroot;
131  std::string OutputFile;
132  raw_pwrite_stream *OS =
133  ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
134  if (!OS)
135  return nullptr;
136 
137  auto Buffer = std::make_shared<PCHBuffer>();
138  std::vector<std::unique_ptr<ASTConsumer>> Consumers;
139  Consumers.push_back(llvm::make_unique<PCHGenerator>(
140  CI.getPreprocessor(), OutputFile, Module, Sysroot, Buffer));
141  Consumers.push_back(
145  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 std::error_code 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  return std::error_code();
172 }
173 
174 /// \brief Collect the set of header includes needed to construct the given
175 /// module and update the TopHeaders file set of the module.
176 ///
177 /// \param Module The module we're collecting includes from.
178 ///
179 /// \param Includes Will be augmented with the set of \#includes or \#imports
180 /// needed to load all of the named headers.
181 static std::error_code
183  ModuleMap &ModMap, clang::Module *Module,
184  SmallVectorImpl<char> &Includes) {
185  // Don't collect any headers for unavailable modules.
186  if (!Module->isAvailable())
187  return std::error_code();
188 
189  // Add includes for each of these headers.
190  for (Module::Header &H : Module->Headers[Module::HK_Normal]) {
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  if (std::error_code Err = addHeaderInclude(H.NameAsWritten, Includes,
197  LangOpts, Module->IsExternC))
198  return Err;
199  }
200  // Note that Module->PrivateHeaders will not be a TopHeader.
201 
202  if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
203  Module->addTopHeader(UmbrellaHeader.Entry);
204  if (Module->Parent) {
205  // Include the umbrella header for submodules.
206  if (std::error_code Err = addHeaderInclude(UmbrellaHeader.NameAsWritten,
207  Includes, LangOpts,
208  Module->IsExternC))
209  return Err;
210  }
211  } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
212  // Add all of the headers we find in this subdirectory.
213  std::error_code EC;
214  SmallString<128> DirNative;
215  llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
216  for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC),
217  DirEnd;
218  Dir != DirEnd && !EC; Dir.increment(EC)) {
219  // Check whether this entry has an extension typically associated with
220  // headers.
221  if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
222  .Cases(".h", ".H", ".hh", ".hpp", true)
223  .Default(false))
224  continue;
225 
226  const FileEntry *Header = FileMgr.getFile(Dir->path());
227  // FIXME: This shouldn't happen unless there is a file system race. Is
228  // that worth diagnosing?
229  if (!Header)
230  continue;
231 
232  // If this header is marked 'unavailable' in this module, don't include
233  // it.
234  if (ModMap.isHeaderUnavailableInModule(Header, Module))
235  continue;
236 
237  // Compute the relative path from the directory to this file.
238  SmallVector<StringRef, 16> Components;
239  auto PathIt = llvm::sys::path::rbegin(Dir->path());
240  for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
241  Components.push_back(*PathIt);
242  SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
243  for (auto It = Components.rbegin(), End = Components.rend(); It != End;
244  ++It)
245  llvm::sys::path::append(RelativeHeader, *It);
246 
247  // Include this header as part of the umbrella directory.
248  Module->addTopHeader(Header);
249  if (std::error_code Err = addHeaderInclude(RelativeHeader, Includes,
250  LangOpts, Module->IsExternC))
251  return Err;
252  }
253 
254  if (EC)
255  return EC;
256  }
257 
258  // Recurse into submodules.
260  SubEnd = Module->submodule_end();
261  Sub != SubEnd; ++Sub)
262  if (std::error_code Err = collectModuleHeaderIncludes(
263  LangOpts, FileMgr, ModMap, *Sub, Includes))
264  return Err;
265 
266  return std::error_code();
267 }
268 
270  StringRef Filename) {
271  // Find the module map file.
272  const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
273  if (!ModuleMap) {
274  CI.getDiagnostics().Report(diag::err_module_map_not_found)
275  << Filename;
276  return false;
277  }
278 
279  // Parse the module map file.
281  if (HS.loadModuleMapFile(ModuleMap, IsSystem))
282  return false;
283 
284  if (CI.getLangOpts().CurrentModule.empty()) {
285  CI.getDiagnostics().Report(diag::err_missing_module_name);
286 
287  // FIXME: Eventually, we could consider asking whether there was just
288  // a single module described in the module map, and use that as a
289  // default. Then it would be fairly trivial to just "compile" a module
290  // map with a single module (the common case).
291  return false;
292  }
293 
294  // If we're being run from the command-line, the module build stack will not
295  // have been filled in yet, so complete it now in order to allow us to detect
296  // module cycles.
298  if (SourceMgr.getModuleBuildStack().empty())
300  FullSourceLoc(SourceLocation(), SourceMgr));
301 
302  // Dig out the module definition.
304  /*AllowSearch=*/false);
305  if (!Module) {
306  CI.getDiagnostics().Report(diag::err_missing_module)
307  << CI.getLangOpts().CurrentModule << Filename;
308 
309  return false;
310  }
311 
312  // Check whether we can build this module at all.
313  clang::Module::Requirement Requirement;
315  if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
316  MissingHeader)) {
317  if (MissingHeader.FileNameLoc.isValid()) {
318  CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
319  diag::err_module_header_missing)
320  << MissingHeader.IsUmbrella << MissingHeader.FileName;
321  } else {
322  CI.getDiagnostics().Report(diag::err_module_unavailable)
324  << Requirement.second << Requirement.first;
325  }
326 
327  return false;
328  }
329 
330  if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
331  Module->IsInferred = true;
332  HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
333  } else {
334  ModuleMapForUniquing = ModuleMap;
335  }
336 
337  FileManager &FileMgr = CI.getFileManager();
338 
339  // Collect the set of #includes we need to build the module.
340  SmallString<256> HeaderContents;
341  std::error_code Err = std::error_code();
342  if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader())
343  Err = addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
344  CI.getLangOpts(), Module->IsExternC);
345  if (!Err)
347  CI.getLangOpts(), FileMgr,
349  HeaderContents);
350 
351  if (Err) {
352  CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
353  << Module->getFullModuleName() << Err.message();
354  return false;
355  }
356 
357  // Inform the preprocessor that includes from within the input buffer should
358  // be resolved relative to the build directory of the module map file.
360 
361  std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
362  llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
364  // Ownership of InputBuffer will be transferred to the SourceManager.
365  setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
366  Module->IsSystem));
367  return true;
368 }
369 
371  CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
372  std::string &OutputFile) {
373  // If no output file was provided, figure out where this module would go
374  // in the module cache.
375  if (CI.getFrontendOpts().OutputFile.empty()) {
379  ModuleMapForUniquing->getName());
380  }
381 
382  // We use createOutputFile here because this is exposed via libclang, and we
383  // must disable the RemoveFileOnSignal behavior.
384  // We use a temporary to avoid race conditions.
385  raw_pwrite_stream *OS =
386  CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
387  /*RemoveFileOnSignal=*/false, InFile,
388  /*Extension=*/"", /*useTemporary=*/true,
389  /*CreateMissingDirectories=*/true);
390  if (!OS)
391  return nullptr;
392 
393  OutputFile = CI.getFrontendOpts().OutputFile;
394  return OS;
395 }
396 
397 std::unique_ptr<ASTConsumer>
399  return llvm::make_unique<ASTConsumer>();
400 }
401 
402 std::unique_ptr<ASTConsumer>
404  StringRef InFile) {
405  return llvm::make_unique<ASTConsumer>();
406 }
407 
408 std::unique_ptr<ASTConsumer>
410  return llvm::make_unique<ASTConsumer>();
411 }
412 
415  bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
416  const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
417  std::unique_ptr<ASTReader> Reader(new ASTReader(
419  Sysroot.empty() ? "" : Sysroot.c_str(),
420  /*DisableValidation*/ false,
421  /*AllowPCHWithCompilerErrors*/ false,
422  /*AllowConfigurationMismatch*/ true,
423  /*ValidateSystemInputs*/ true));
424 
425  Reader->ReadAST(getCurrentFile(),
426  Preamble ? serialization::MK_Preamble
428  SourceLocation(),
430 }
431 
432 namespace {
433  /// \brief AST reader listener that dumps module information for a module
434  /// file.
435  class DumpModuleInfoListener : public ASTReaderListener {
436  llvm::raw_ostream &Out;
437 
438  public:
439  DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
440 
441 #define DUMP_BOOLEAN(Value, Text) \
442  Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
443 
444  bool ReadFullVersionInformation(StringRef FullVersion) override {
445  Out.indent(2)
446  << "Generated by "
447  << (FullVersion == getClangFullRepositoryVersion()? "this"
448  : "a different")
449  << " Clang: " << FullVersion << "\n";
451  }
452 
453  void ReadModuleName(StringRef ModuleName) override {
454  Out.indent(2) << "Module name: " << ModuleName << "\n";
455  }
456  void ReadModuleMapFile(StringRef ModuleMapPath) override {
457  Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
458  }
459 
460  bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
461  bool AllowCompatibleDifferences) override {
462  Out.indent(2) << "Language options:\n";
463 #define LANGOPT(Name, Bits, Default, Description) \
464  DUMP_BOOLEAN(LangOpts.Name, Description);
465 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
466  Out.indent(4) << Description << ": " \
467  << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
468 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
469  Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
470 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
471 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
472 #include "clang/Basic/LangOptions.def"
473 
474  if (!LangOpts.ModuleFeatures.empty()) {
475  Out.indent(4) << "Module features:\n";
476  for (StringRef Feature : LangOpts.ModuleFeatures)
477  Out.indent(6) << Feature << "\n";
478  }
479 
480  return false;
481  }
482 
483  bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
484  bool AllowCompatibleDifferences) override {
485  Out.indent(2) << "Target options:\n";
486  Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
487  Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
488  Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
489 
490  if (!TargetOpts.FeaturesAsWritten.empty()) {
491  Out.indent(4) << "Target features:\n";
492  for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
493  I != N; ++I) {
494  Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
495  }
496  }
497 
498  return false;
499  }
500 
501  bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
502  bool Complain) override {
503  Out.indent(2) << "Diagnostic options:\n";
504 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
505 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
506  Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
507 #define VALUE_DIAGOPT(Name, Bits, Default) \
508  Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
509 #include "clang/Basic/DiagnosticOptions.def"
510 
511  Out.indent(4) << "Diagnostic flags:\n";
512  for (const std::string &Warning : DiagOpts->Warnings)
513  Out.indent(6) << "-W" << Warning << "\n";
514  for (const std::string &Remark : DiagOpts->Remarks)
515  Out.indent(6) << "-R" << Remark << "\n";
516 
517  return false;
518  }
519 
520  bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
521  StringRef SpecificModuleCachePath,
522  bool Complain) override {
523  Out.indent(2) << "Header search options:\n";
524  Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
525  Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
527  "Use builtin include directories [-nobuiltininc]");
529  "Use standard system include directories [-nostdinc]");
531  "Use standard C++ include directories [-nostdinc++]");
532  DUMP_BOOLEAN(HSOpts.UseLibcxx,
533  "Use libc++ (rather than libstdc++) [-stdlib=]");
534  return false;
535  }
536 
537  bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
538  bool Complain,
539  std::string &SuggestedPredefines) override {
540  Out.indent(2) << "Preprocessor options:\n";
542  "Uses compiler/target-specific predefines [-undef]");
544  "Uses detailed preprocessing record (for indexing)");
545 
546  if (!PPOpts.Macros.empty()) {
547  Out.indent(4) << "Predefined macros:\n";
548  }
549 
550  for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
551  I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
552  I != IEnd; ++I) {
553  Out.indent(6);
554  if (I->second)
555  Out << "-U";
556  else
557  Out << "-D";
558  Out << I->first << "\n";
559  }
560  return false;
561  }
562 #undef DUMP_BOOLEAN
563  };
564 }
565 
567  // Set up the output file.
568  std::unique_ptr<llvm::raw_fd_ostream> OutFile;
569  StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
570  if (!OutputFileName.empty() && OutputFileName != "-") {
571  std::error_code EC;
572  OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
573  llvm::sys::fs::F_Text));
574  }
575  llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
576 
577  Out << "Information for module file '" << getCurrentFile() << "':\n";
578  DumpModuleInfoListener Listener(Out);
580  getCurrentFile(), getCompilerInstance().getFileManager(),
581  getCompilerInstance().getPCHContainerReader(), Listener);
582 }
583 
584 //===----------------------------------------------------------------------===//
585 // Preprocessor Actions
586 //===----------------------------------------------------------------------===//
587 
591 
592  // Start lexing the specified input file.
593  const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
594  Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
595  RawLex.SetKeepWhitespaceMode(true);
596 
597  Token RawTok;
598  RawLex.LexFromRawLexer(RawTok);
599  while (RawTok.isNot(tok::eof)) {
600  PP.DumpToken(RawTok, true);
601  llvm::errs() << "\n";
602  RawLex.LexFromRawLexer(RawTok);
603  }
604 }
605 
608  // Start preprocessing the specified input file.
609  Token Tok;
610  PP.EnterMainSourceFile();
611  do {
612  PP.Lex(Tok);
613  PP.DumpToken(Tok, true);
614  llvm::errs() << "\n";
615  } while (Tok.isNot(tok::eof));
616 }
617 
620  raw_pwrite_stream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
621  if (!OS)
622  return;
623 
624  CacheTokens(CI.getPreprocessor(), OS);
625 }
626 
629 
630  // Ignore unknown pragmas.
631  PP.IgnorePragmas();
632 
633  Token Tok;
634  // Start parsing the specified input file.
635  PP.EnterMainSourceFile();
636  do {
637  PP.Lex(Tok);
638  } while (Tok.isNot(tok::eof));
639 }
640 
643  // Output file may need to be set to 'Binary', to avoid converting Unix style
644  // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
645  //
646  // Look to see what type of line endings the file uses. If there's a
647  // CRLF, then we won't open the file up in binary mode. If there is
648  // just an LF or CR, then we will open the file up in binary mode.
649  // In this fashion, the output format should match the input format, unless
650  // the input format has inconsistent line endings.
651  //
652  // This should be a relatively fast operation since most files won't have
653  // all of their source code on a single line. However, that is still a
654  // concern, so if we scan for too long, we'll just assume the file should
655  // be opened in binary mode.
656  bool BinaryMode = true;
657  bool InvalidFile = false;
658  const SourceManager& SM = CI.getSourceManager();
659  const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
660  &InvalidFile);
661  if (!InvalidFile) {
662  const char *cur = Buffer->getBufferStart();
663  const char *end = Buffer->getBufferEnd();
664  const char *next = (cur != end) ? cur + 1 : end;
665 
666  // Limit ourselves to only scanning 256 characters into the source
667  // file. This is mostly a sanity check in case the file has no
668  // newlines whatsoever.
669  if (end - cur > 256) end = cur + 256;
670 
671  while (next < end) {
672  if (*cur == 0x0D) { // CR
673  if (*next == 0x0A) // CRLF
674  BinaryMode = false;
675 
676  break;
677  } else if (*cur == 0x0A) // LF
678  break;
679 
680  ++cur, ++next;
681  }
682  }
683 
684  raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
685  if (!OS) return;
686 
689 }
690 
692  switch (getCurrentFileKind()) {
693  case IK_C:
694  case IK_CXX:
695  case IK_ObjC:
696  case IK_ObjCXX:
697  case IK_OpenCL:
698  case IK_CUDA:
699  break;
700 
701  case IK_None:
702  case IK_Asm:
703  case IK_PreprocessedC:
704  case IK_PreprocessedCuda:
705  case IK_PreprocessedCXX:
706  case IK_PreprocessedObjC:
708  case IK_AST:
709  case IK_LLVM_IR:
710  // We can't do anything with these.
711  return;
712  }
713 
715  auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
716  if (Buffer) {
717  unsigned Preamble =
718  Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
719  llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
720  }
721 }
std::string OutputFile
The output file, if any.
SourceManager & getSourceManager() const
Definition: Preprocessor.h:682
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:394
std::vector< std::pair< std::string, bool > > Macros
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:115
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:467
unsigned IsExternC
Whether this is an 'extern "C"' module (which implicitly puts all headers in it within an 'extern "...
Definition: Basic/Module.h:177
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.
static bool readASTFileControlBlock(StringRef Filename, FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, ASTReaderListener &Listener)
Read the control block for the named AST file.
Definition: ASTReader.cpp:3968
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1118
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:1467
Options for controlling the target.
Definition: TargetOptions.h:24
InputKind getCurrentFileKind() const
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:585
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:439
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.
static StringRef getModuleInputBufferName()
Definition: Basic/Module.h:478
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
const LangOptions & getLangOpts() const
Definition: Preprocessor.h:679
unsigned DetailedRecord
Whether we should maintain a detailed record of all macro definitions and expansions.
The client can handle an AST file that cannot load because it's compiled configuration doesn't match ...
Definition: ASTReader.h:1324
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.
Describes a module or submodule.
Definition: Basic/Module.h:49
bool isAvailable() const
Determine whether this module is available for use within the current translation unit...
Definition: Basic/Module.h:313
FrontendOptions & getFrontendOpts()
PreprocessorOutputOptions & getPreprocessorOutputOpts()
HeaderSearch & getHeaderSearchInfo() const
Definition: Preprocessor.h:683
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. This will be NULL for the top-level module.
Definition: Basic/Module.h:59
unsigned IsInferred
Whether this is an inferred submodule (module * { ... }).
Definition: Basic/Module.h:180
submodule_iterator submodule_end()
Definition: Basic/Module.h:469
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)
HeaderSearchOptions & getHeaderSearchOpts()
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...
Preprocessor & getPreprocessor() const
Return the current preprocessor.
std::vector< Module * >::iterator submodule_iterator
Definition: Basic/Module.h:464
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:107
Encapsulates the information needed to find the file referenced by a #include or #include_next, (sub-)framework lookup, etc.
Definition: HeaderSearch.h:151
unsigned IsSystem
Whether this is a "system" module (which assumes that all headers in it are system headers)...
Definition: Basic/Module.h:172
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.
Definition: LangOptions.h:96
static std::error_code addHeaderInclude(StringRef HeaderName, SmallVectorImpl< char > &Includes, const LangOptions &LangOpts, bool IsExternC)
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
void setCurrentInput(const FrontendInputFile &CurrentInput, std::unique_ptr< ASTUnit > AST=nullptr)
SourceManager & SM
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)
SourceManager & SourceMgr
Definition: Format.cpp:1205
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.
bool isNot(tok::TokenKind K) const
Definition: Token.h:96
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:111
Module * lookupModule(StringRef ModuleName, bool AllowSearch=true)
Lookup a module Search for a module with the given name.
std::string CPU
If given, the name of the target CPU to generate code for.
Definition: TargetOptions.h:31
const DirectoryEntry * Directory
The build directory of this module. This is the directory in which the module is notionally built...
Definition: Basic/Module.h:64
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:37
File is a PCH file treated as the preamble.
const char * getName() const
Definition: FileManager.h:84
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
unsigned UseStandardSystemIncludes
Include the system standard include search directories.
const PCHContainerReader & getPCHContainerReader() const
File is a PCH file treated as such.
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 ...
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:120
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:53
SmallVector< Header, 2 > Headers[5]
The headers that are part of this module.
Definition: Basic/Module.h:128
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:109
std::vector< std::string > FeaturesAsWritten
The list of target specific features to enable or disable, as written on the command line...
Definition: TargetOptions.h:43
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.
std::unique_ptr< ASTConsumer > CreateASTPrinter(raw_ostream *OS, StringRef FilterString)
Stored information about a header directive that was found in the module map file but has not been re...
Definition: Basic/Module.h:132
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:302
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.
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:407
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:101
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:144
std::unique_ptr< ASTConsumer > CreateASTViewer()
const PCHContainerWriter & getPCHContainerWriter() const
raw_pwrite_stream * createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
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...
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...
A SourceLocation and its associated SourceManager.
virtual std::unique_ptr< ASTConsumer > CreatePCHContainerGenerator(DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO, const PreprocessorOptions &PPO, const TargetOptions &TO, const LangOptions &LO, const std::string &MainFileName, const std::string &OutputFileName, llvm::raw_pwrite_stream *OS, std::shared_ptr< PCHBuffer > Buffer) const =0
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
static 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 ...
std::unique_ptr< ASTConsumer > CreateASTDumper(StringRef FilterString, bool DumpDecls, bool DumpLookups)
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)
Definition: Lexer.h:172
raw_pwrite_stream * createOutputFile(StringRef OutputPath, bool Binary, bool RemoveFileOnSignal, StringRef BaseInput, StringRef Extension, bool UseTemporary, bool CreateMissingDirectories=false)
void setInferredModuleAllowedBy(Module *M, const FileEntry *ModuleMap)
Definition: ModuleMap.cpp:826
TargetOptions & getTargetOpts()
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:96
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...