clang  9.0.0svn
CodeGenAction.cpp
Go to the documentation of this file.
1 //===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "CodeGenModule.h"
11 #include "CoverageMappingGen.h"
12 #include "MacroPPCallbacks.h"
13 #include "clang/AST/ASTConsumer.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclGroup.h"
19 #include "clang/Basic/TargetInfo.h"
25 #include "clang/Lex/Preprocessor.h"
26 #include "llvm/Bitcode/BitcodeReader.h"
27 #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h"
28 #include "llvm/IR/DebugInfo.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/DiagnosticPrinter.h"
31 #include "llvm/IR/GlobalValue.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/RemarkStreamer.h"
35 #include "llvm/IRReader/IRReader.h"
36 #include "llvm/Linker/Linker.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/SourceMgr.h"
40 #include "llvm/Support/Timer.h"
41 #include "llvm/Support/ToolOutputFile.h"
42 #include "llvm/Support/YAMLTraits.h"
43 #include "llvm/Transforms/IPO/Internalize.h"
44 
45 #include <memory>
46 using namespace clang;
47 using namespace llvm;
48 
49 namespace clang {
50  class BackendConsumer;
52  public:
54  : CodeGenOpts(CGOpts), BackendCon(BCon) {}
55 
56  bool handleDiagnostics(const DiagnosticInfo &DI) override;
57 
58  bool isAnalysisRemarkEnabled(StringRef PassName) const override {
59  return (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
60  CodeGenOpts.OptimizationRemarkAnalysisPattern->match(PassName));
61  }
62  bool isMissedOptRemarkEnabled(StringRef PassName) const override {
63  return (CodeGenOpts.OptimizationRemarkMissedPattern &&
64  CodeGenOpts.OptimizationRemarkMissedPattern->match(PassName));
65  }
66  bool isPassedOptRemarkEnabled(StringRef PassName) const override {
67  return (CodeGenOpts.OptimizationRemarkPattern &&
68  CodeGenOpts.OptimizationRemarkPattern->match(PassName));
69  }
70 
71  bool isAnyRemarkEnabled() const override {
72  return (CodeGenOpts.OptimizationRemarkAnalysisPattern ||
73  CodeGenOpts.OptimizationRemarkMissedPattern ||
74  CodeGenOpts.OptimizationRemarkPattern);
75  }
76 
77  private:
78  const CodeGenOptions &CodeGenOpts;
79  BackendConsumer *BackendCon;
80  };
81 
82  class BackendConsumer : public ASTConsumer {
83  using LinkModule = CodeGenAction::LinkModule;
84 
85  virtual void anchor();
86  DiagnosticsEngine &Diags;
87  BackendAction Action;
88  const HeaderSearchOptions &HeaderSearchOpts;
89  const CodeGenOptions &CodeGenOpts;
90  const TargetOptions &TargetOpts;
91  const LangOptions &LangOpts;
92  std::unique_ptr<raw_pwrite_stream> AsmOutStream;
93  ASTContext *Context;
94 
95  Timer LLVMIRGeneration;
96  unsigned LLVMIRGenerationRefCount;
97 
98  /// True if we've finished generating IR. This prevents us from generating
99  /// additional LLVM IR after emitting output in HandleTranslationUnit. This
100  /// can happen when Clang plugins trigger additional AST deserialization.
101  bool IRGenFinished = false;
102 
103  std::unique_ptr<CodeGenerator> Gen;
104 
105  SmallVector<LinkModule, 4> LinkModules;
106 
107  // This is here so that the diagnostic printer knows the module a diagnostic
108  // refers to.
109  llvm::Module *CurLinkModule = nullptr;
110 
111  public:
113  const HeaderSearchOptions &HeaderSearchOpts,
114  const PreprocessorOptions &PPOpts,
115  const CodeGenOptions &CodeGenOpts,
116  const TargetOptions &TargetOpts,
117  const LangOptions &LangOpts, bool TimePasses,
118  const std::string &InFile,
119  SmallVector<LinkModule, 4> LinkModules,
120  std::unique_ptr<raw_pwrite_stream> OS, LLVMContext &C,
121  CoverageSourceInfo *CoverageInfo = nullptr)
122  : Diags(Diags), Action(Action), HeaderSearchOpts(HeaderSearchOpts),
123  CodeGenOpts(CodeGenOpts), TargetOpts(TargetOpts), LangOpts(LangOpts),
124  AsmOutStream(std::move(OS)), Context(nullptr),
125  LLVMIRGeneration("irgen", "LLVM IR Generation Time"),
126  LLVMIRGenerationRefCount(0),
127  Gen(CreateLLVMCodeGen(Diags, InFile, HeaderSearchOpts, PPOpts,
128  CodeGenOpts, C, CoverageInfo)),
129  LinkModules(std::move(LinkModules)) {
130  FrontendTimesIsEnabled = TimePasses;
131  llvm::TimePassesIsEnabled = TimePasses;
132  }
133  llvm::Module *getModule() const { return Gen->GetModule(); }
134  std::unique_ptr<llvm::Module> takeModule() {
135  return std::unique_ptr<llvm::Module>(Gen->ReleaseModule());
136  }
137 
138  CodeGenerator *getCodeGenerator() { return Gen.get(); }
139 
141  Gen->HandleCXXStaticMemberVarInstantiation(VD);
142  }
143 
144  void Initialize(ASTContext &Ctx) override {
145  assert(!Context && "initialized multiple times");
146 
147  Context = &Ctx;
148 
150  LLVMIRGeneration.startTimer();
151 
152  Gen->Initialize(Ctx);
153 
155  LLVMIRGeneration.stopTimer();
156  }
157 
158  bool HandleTopLevelDecl(DeclGroupRef D) override {
159  PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
160  Context->getSourceManager(),
161  "LLVM IR generation of declaration");
162 
163  // Recurse.
165  LLVMIRGenerationRefCount += 1;
166  if (LLVMIRGenerationRefCount == 1)
167  LLVMIRGeneration.startTimer();
168  }
169 
170  Gen->HandleTopLevelDecl(D);
171 
173  LLVMIRGenerationRefCount -= 1;
174  if (LLVMIRGenerationRefCount == 0)
175  LLVMIRGeneration.stopTimer();
176  }
177 
178  return true;
179  }
180 
182  PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
183  Context->getSourceManager(),
184  "LLVM IR generation of inline function");
186  LLVMIRGeneration.startTimer();
187 
188  Gen->HandleInlineFunctionDefinition(D);
189 
191  LLVMIRGeneration.stopTimer();
192  }
193 
195  // Ignore interesting decls from the AST reader after IRGen is finished.
196  if (!IRGenFinished)
197  HandleTopLevelDecl(D);
198  }
199 
200  // Links each entry in LinkModules into our module. Returns true on error.
201  bool LinkInModules() {
202  for (auto &LM : LinkModules) {
203  if (LM.PropagateAttrs)
204  for (Function &F : *LM.Module)
205  Gen->CGM().AddDefaultFnAttrs(F);
206 
207  CurLinkModule = LM.Module.get();
208 
209  bool Err;
210  if (LM.Internalize) {
211  Err = Linker::linkModules(
212  *getModule(), std::move(LM.Module), LM.LinkFlags,
213  [](llvm::Module &M, const llvm::StringSet<> &GVS) {
214  internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
215  return !GV.hasName() || (GVS.count(GV.getName()) == 0);
216  });
217  });
218  } else {
219  Err = Linker::linkModules(*getModule(), std::move(LM.Module),
220  LM.LinkFlags);
221  }
222 
223  if (Err)
224  return true;
225  }
226  return false; // success
227  }
228 
230  {
231  PrettyStackTraceString CrashInfo("Per-file LLVM IR generation");
233  LLVMIRGenerationRefCount += 1;
234  if (LLVMIRGenerationRefCount == 1)
235  LLVMIRGeneration.startTimer();
236  }
237 
238  Gen->HandleTranslationUnit(C);
239 
241  LLVMIRGenerationRefCount -= 1;
242  if (LLVMIRGenerationRefCount == 0)
243  LLVMIRGeneration.stopTimer();
244  }
245 
246  IRGenFinished = true;
247  }
248 
249  // Silently ignore if we weren't initialized for some reason.
250  if (!getModule())
251  return;
252 
253  // Install an inline asm handler so that diagnostics get printed through
254  // our diagnostics hooks.
255  LLVMContext &Ctx = getModule()->getContext();
256  LLVMContext::InlineAsmDiagHandlerTy OldHandler =
257  Ctx.getInlineAsmDiagnosticHandler();
258  void *OldContext = Ctx.getInlineAsmDiagnosticContext();
259  Ctx.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, this);
260 
261  std::unique_ptr<DiagnosticHandler> OldDiagnosticHandler =
262  Ctx.getDiagnosticHandler();
263  Ctx.setDiagnosticHandler(llvm::make_unique<ClangDiagnosticHandler>(
264  CodeGenOpts, this));
265  Ctx.setDiagnosticsHotnessRequested(CodeGenOpts.DiagnosticsWithHotness);
266  if (CodeGenOpts.DiagnosticsHotnessThreshold != 0)
267  Ctx.setDiagnosticsHotnessThreshold(
268  CodeGenOpts.DiagnosticsHotnessThreshold);
269 
270  std::unique_ptr<llvm::ToolOutputFile> OptRecordFile;
271  if (!CodeGenOpts.OptRecordFile.empty()) {
272  std::error_code EC;
273  OptRecordFile = llvm::make_unique<llvm::ToolOutputFile>(
274  CodeGenOpts.OptRecordFile, EC, sys::fs::F_None);
275  if (EC) {
276  Diags.Report(diag::err_cannot_open_file) <<
277  CodeGenOpts.OptRecordFile << EC.message();
278  return;
279  }
280 
281  Ctx.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
282  CodeGenOpts.OptRecordFile, OptRecordFile->os()));
283 
284  if (!CodeGenOpts.OptRecordPasses.empty())
285  if (Error E = Ctx.getRemarkStreamer()->setFilter(
286  CodeGenOpts.OptRecordPasses))
287  Diags.Report(diag::err_drv_optimization_remark_pattern)
288  << toString(std::move(E)) << CodeGenOpts.OptRecordPasses;
289 
290  if (CodeGenOpts.getProfileUse() != CodeGenOptions::ProfileNone)
291  Ctx.setDiagnosticsHotnessRequested(true);
292  }
293 
294  // Link each LinkModule into our module.
295  if (LinkInModules())
296  return;
297 
298  EmbedBitcode(getModule(), CodeGenOpts, llvm::MemoryBufferRef());
299 
300  EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts,
301  LangOpts, C.getTargetInfo().getDataLayout(),
302  getModule(), Action, std::move(AsmOutStream));
303 
304  Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext);
305 
306  Ctx.setDiagnosticHandler(std::move(OldDiagnosticHandler));
307 
308  if (OptRecordFile)
309  OptRecordFile->keep();
310  }
311 
312  void HandleTagDeclDefinition(TagDecl *D) override {
313  PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
314  Context->getSourceManager(),
315  "LLVM IR generation of declaration");
316  Gen->HandleTagDeclDefinition(D);
317  }
318 
319  void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
320  Gen->HandleTagDeclRequiredDefinition(D);
321  }
322 
324  Gen->CompleteTentativeDefinition(D);
325  }
326 
328  Gen->AssignInheritanceModel(RD);
329  }
330 
331  void HandleVTable(CXXRecordDecl *RD) override {
332  Gen->HandleVTable(RD);
333  }
334 
335  static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context,
336  unsigned LocCookie) {
338  ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc);
339  }
340 
341  /// Get the best possible source location to represent a diagnostic that
342  /// may have associated debug info.
343  const FullSourceLoc
344  getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D,
345  bool &BadDebugInfo, StringRef &Filename,
346  unsigned &Line, unsigned &Column) const;
347 
348  void InlineAsmDiagHandler2(const llvm::SMDiagnostic &,
349  SourceLocation LocCookie);
350 
351  void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
352  /// Specialized handler for InlineAsm diagnostic.
353  /// \return True if the diagnostic has been successfully reported, false
354  /// otherwise.
355  bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
356  /// Specialized handler for StackSize diagnostic.
357  /// \return True if the diagnostic has been successfully reported, false
358  /// otherwise.
359  bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
360  /// Specialized handler for unsupported backend feature diagnostic.
361  void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
362  /// Specialized handlers for optimization remarks.
363  /// Note that these handlers only accept remarks and they always handle
364  /// them.
365  void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
366  unsigned DiagID);
367  void
368  OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
369  void OptimizationRemarkHandler(
370  const llvm::OptimizationRemarkAnalysisFPCommute &D);
371  void OptimizationRemarkHandler(
372  const llvm::OptimizationRemarkAnalysisAliasing &D);
373  void OptimizationFailureHandler(
374  const llvm::DiagnosticInfoOptimizationFailure &D);
375  };
376 
377  void BackendConsumer::anchor() {}
378 }
379 
380 bool ClangDiagnosticHandler::handleDiagnostics(const DiagnosticInfo &DI) {
381  BackendCon->DiagnosticHandlerImpl(DI);
382  return true;
383 }
384 
385 /// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr
386 /// buffer to be a valid FullSourceLoc.
387 static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D,
388  SourceManager &CSM) {
389  // Get both the clang and llvm source managers. The location is relative to
390  // a memory buffer that the LLVM Source Manager is handling, we need to add
391  // a copy to the Clang source manager.
392  const llvm::SourceMgr &LSM = *D.getSourceMgr();
393 
394  // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr
395  // already owns its one and clang::SourceManager wants to own its one.
396  const MemoryBuffer *LBuf =
397  LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc()));
398 
399  // Create the copy and transfer ownership to clang::SourceManager.
400  // TODO: Avoid copying files into memory.
401  std::unique_ptr<llvm::MemoryBuffer> CBuf =
402  llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(),
403  LBuf->getBufferIdentifier());
404  // FIXME: Keep a file ID map instead of creating new IDs for each location.
405  FileID FID = CSM.createFileID(std::move(CBuf));
406 
407  // Translate the offset into the file.
408  unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart();
409  SourceLocation NewLoc =
410  CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset);
411  return FullSourceLoc(NewLoc, CSM);
412 }
413 
414 
415 /// InlineAsmDiagHandler2 - This function is invoked when the backend hits an
416 /// error parsing inline asm. The SMDiagnostic indicates the error relative to
417 /// the temporary memory buffer that the inline asm parser has set up.
418 void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
419  SourceLocation LocCookie) {
420  // There are a couple of different kinds of errors we could get here. First,
421  // we re-format the SMDiagnostic in terms of a clang diagnostic.
422 
423  // Strip "error: " off the start of the message string.
424  StringRef Message = D.getMessage();
425  if (Message.startswith("error: "))
426  Message = Message.substr(7);
427 
428  // If the SMDiagnostic has an inline asm source location, translate it.
429  FullSourceLoc Loc;
430  if (D.getLoc() != SMLoc())
431  Loc = ConvertBackendLocation(D, Context->getSourceManager());
432 
433  unsigned DiagID;
434  switch (D.getKind()) {
435  case llvm::SourceMgr::DK_Error:
436  DiagID = diag::err_fe_inline_asm;
437  break;
438  case llvm::SourceMgr::DK_Warning:
439  DiagID = diag::warn_fe_inline_asm;
440  break;
441  case llvm::SourceMgr::DK_Note:
442  DiagID = diag::note_fe_inline_asm;
443  break;
444  case llvm::SourceMgr::DK_Remark:
445  llvm_unreachable("remarks unexpected");
446  }
447  // If this problem has clang-level source location information, report the
448  // issue in the source with a note showing the instantiated
449  // code.
450  if (LocCookie.isValid()) {
451  Diags.Report(LocCookie, DiagID).AddString(Message);
452 
453  if (D.getLoc().isValid()) {
454  DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
455  // Convert the SMDiagnostic ranges into SourceRange and attach them
456  // to the diagnostic.
457  for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) {
458  unsigned Column = D.getColumnNo();
459  B << SourceRange(Loc.getLocWithOffset(Range.first - Column),
460  Loc.getLocWithOffset(Range.second - Column));
461  }
462  }
463  return;
464  }
465 
466  // Otherwise, report the backend issue as occurring in the generated .s file.
467  // If Loc is invalid, we still need to report the issue, it just gets no
468  // location info.
469  Diags.Report(Loc, DiagID).AddString(Message);
470 }
471 
472 #define ComputeDiagID(Severity, GroupName, DiagID) \
473  do { \
474  switch (Severity) { \
475  case llvm::DS_Error: \
476  DiagID = diag::err_fe_##GroupName; \
477  break; \
478  case llvm::DS_Warning: \
479  DiagID = diag::warn_fe_##GroupName; \
480  break; \
481  case llvm::DS_Remark: \
482  llvm_unreachable("'remark' severity not expected"); \
483  break; \
484  case llvm::DS_Note: \
485  DiagID = diag::note_fe_##GroupName; \
486  break; \
487  } \
488  } while (false)
489 
490 #define ComputeDiagRemarkID(Severity, GroupName, DiagID) \
491  do { \
492  switch (Severity) { \
493  case llvm::DS_Error: \
494  DiagID = diag::err_fe_##GroupName; \
495  break; \
496  case llvm::DS_Warning: \
497  DiagID = diag::warn_fe_##GroupName; \
498  break; \
499  case llvm::DS_Remark: \
500  DiagID = diag::remark_fe_##GroupName; \
501  break; \
502  case llvm::DS_Note: \
503  DiagID = diag::note_fe_##GroupName; \
504  break; \
505  } \
506  } while (false)
507 
508 bool
509 BackendConsumer::InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D) {
510  unsigned DiagID;
511  ComputeDiagID(D.getSeverity(), inline_asm, DiagID);
512  std::string Message = D.getMsgStr().str();
513 
514  // If this problem has clang-level source location information, report the
515  // issue as being a problem in the source with a note showing the instantiated
516  // code.
517  SourceLocation LocCookie =
518  SourceLocation::getFromRawEncoding(D.getLocCookie());
519  if (LocCookie.isValid())
520  Diags.Report(LocCookie, DiagID).AddString(Message);
521  else {
522  // Otherwise, report the backend diagnostic as occurring in the generated
523  // .s file.
524  // If Loc is invalid, we still need to report the diagnostic, it just gets
525  // no location info.
526  FullSourceLoc Loc;
527  Diags.Report(Loc, DiagID).AddString(Message);
528  }
529  // We handled all the possible severities.
530  return true;
531 }
532 
533 bool
534 BackendConsumer::StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D) {
535  if (D.getSeverity() != llvm::DS_Warning)
536  // For now, the only support we have for StackSize diagnostic is warning.
537  // We do not know how to format other severities.
538  return false;
539 
540  if (const Decl *ND = Gen->GetDeclForMangledName(D.getFunction().getName())) {
541  // FIXME: Shouldn't need to truncate to uint32_t
542  Diags.Report(ND->getASTContext().getFullLoc(ND->getLocation()),
543  diag::warn_fe_frame_larger_than)
544  << static_cast<uint32_t>(D.getStackSize()) << Decl::castToDeclContext(ND);
545  return true;
546  }
547 
548  return false;
549 }
550 
552  const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo,
553  StringRef &Filename, unsigned &Line, unsigned &Column) const {
554  SourceManager &SourceMgr = Context->getSourceManager();
555  FileManager &FileMgr = SourceMgr.getFileManager();
556  SourceLocation DILoc;
557 
558  if (D.isLocationAvailable()) {
559  D.getLocation(Filename, Line, Column);
560  if (Line > 0) {
561  const FileEntry *FE = FileMgr.getFile(Filename);
562  if (!FE)
563  FE = FileMgr.getFile(D.getAbsolutePath());
564  if (FE) {
565  // If -gcolumn-info was not used, Column will be 0. This upsets the
566  // source manager, so pass 1 if Column is not set.
567  DILoc = SourceMgr.translateFileLineCol(FE, Line, Column ? Column : 1);
568  }
569  }
570  BadDebugInfo = DILoc.isInvalid();
571  }
572 
573  // If a location isn't available, try to approximate it using the associated
574  // function definition. We use the definition's right brace to differentiate
575  // from diagnostics that genuinely relate to the function itself.
576  FullSourceLoc Loc(DILoc, SourceMgr);
577  if (Loc.isInvalid())
578  if (const Decl *FD = Gen->GetDeclForMangledName(D.getFunction().getName()))
579  Loc = FD->getASTContext().getFullLoc(FD->getLocation());
580 
581  if (DILoc.isInvalid() && D.isLocationAvailable())
582  // If we were not able to translate the file:line:col information
583  // back to a SourceLocation, at least emit a note stating that
584  // we could not translate this location. This can happen in the
585  // case of #line directives.
586  Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
587  << Filename << Line << Column;
588 
589  return Loc;
590 }
591 
593  const llvm::DiagnosticInfoUnsupported &D) {
594  // We only support errors.
595  assert(D.getSeverity() == llvm::DS_Error);
596 
597  StringRef Filename;
598  unsigned Line, Column;
599  bool BadDebugInfo = false;
600  FullSourceLoc Loc =
601  getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
602 
603  Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage().str();
604 
605  if (BadDebugInfo)
606  // If we were not able to translate the file:line:col information
607  // back to a SourceLocation, at least emit a note stating that
608  // we could not translate this location. This can happen in the
609  // case of #line directives.
610  Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
611  << Filename << Line << Column;
612 }
613 
615  const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) {
616  // We only support warnings and remarks.
617  assert(D.getSeverity() == llvm::DS_Remark ||
618  D.getSeverity() == llvm::DS_Warning);
619 
620  StringRef Filename;
621  unsigned Line, Column;
622  bool BadDebugInfo = false;
623  FullSourceLoc Loc =
624  getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
625 
626  std::string Msg;
627  raw_string_ostream MsgStream(Msg);
628  MsgStream << D.getMsg();
629 
630  if (D.getHotness())
631  MsgStream << " (hotness: " << *D.getHotness() << ")";
632 
633  Diags.Report(Loc, DiagID)
634  << AddFlagValue(D.getPassName())
635  << MsgStream.str();
636 
637  if (BadDebugInfo)
638  // If we were not able to translate the file:line:col information
639  // back to a SourceLocation, at least emit a note stating that
640  // we could not translate this location. This can happen in the
641  // case of #line directives.
642  Diags.Report(Loc, diag::note_fe_backend_invalid_loc)
643  << Filename << Line << Column;
644 }
645 
647  const llvm::DiagnosticInfoOptimizationBase &D) {
648  // Without hotness information, don't show noisy remarks.
649  if (D.isVerbose() && !D.getHotness())
650  return;
651 
652  if (D.isPassed()) {
653  // Optimization remarks are active only if the -Rpass flag has a regular
654  // expression that matches the name of the pass name in \p D.
655  if (CodeGenOpts.OptimizationRemarkPattern &&
656  CodeGenOpts.OptimizationRemarkPattern->match(D.getPassName()))
657  EmitOptimizationMessage(D, diag::remark_fe_backend_optimization_remark);
658  } else if (D.isMissed()) {
659  // Missed optimization remarks are active only if the -Rpass-missed
660  // flag has a regular expression that matches the name of the pass
661  // name in \p D.
662  if (CodeGenOpts.OptimizationRemarkMissedPattern &&
663  CodeGenOpts.OptimizationRemarkMissedPattern->match(D.getPassName()))
664  EmitOptimizationMessage(
665  D, diag::remark_fe_backend_optimization_remark_missed);
666  } else {
667  assert(D.isAnalysis() && "Unknown remark type");
668 
669  bool ShouldAlwaysPrint = false;
670  if (auto *ORA = dyn_cast<llvm::OptimizationRemarkAnalysis>(&D))
671  ShouldAlwaysPrint = ORA->shouldAlwaysPrint();
672 
673  if (ShouldAlwaysPrint ||
674  (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
675  CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
676  EmitOptimizationMessage(
677  D, diag::remark_fe_backend_optimization_remark_analysis);
678  }
679 }
680 
682  const llvm::OptimizationRemarkAnalysisFPCommute &D) {
683  // Optimization analysis remarks are active if the pass name is set to
684  // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
685  // regular expression that matches the name of the pass name in \p D.
686 
687  if (D.shouldAlwaysPrint() ||
688  (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
689  CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
690  EmitOptimizationMessage(
691  D, diag::remark_fe_backend_optimization_remark_analysis_fpcommute);
692 }
693 
695  const llvm::OptimizationRemarkAnalysisAliasing &D) {
696  // Optimization analysis remarks are active if the pass name is set to
697  // llvm::DiagnosticInfo::AlwasyPrint or if the -Rpass-analysis flag has a
698  // regular expression that matches the name of the pass name in \p D.
699 
700  if (D.shouldAlwaysPrint() ||
701  (CodeGenOpts.OptimizationRemarkAnalysisPattern &&
702  CodeGenOpts.OptimizationRemarkAnalysisPattern->match(D.getPassName())))
703  EmitOptimizationMessage(
704  D, diag::remark_fe_backend_optimization_remark_analysis_aliasing);
705 }
706 
708  const llvm::DiagnosticInfoOptimizationFailure &D) {
709  EmitOptimizationMessage(D, diag::warn_fe_backend_optimization_failure);
710 }
711 
712 /// This function is invoked when the backend needs
713 /// to report something to the user.
714 void BackendConsumer::DiagnosticHandlerImpl(const DiagnosticInfo &DI) {
715  unsigned DiagID = diag::err_fe_inline_asm;
716  llvm::DiagnosticSeverity Severity = DI.getSeverity();
717  // Get the diagnostic ID based.
718  switch (DI.getKind()) {
719  case llvm::DK_InlineAsm:
720  if (InlineAsmDiagHandler(cast<DiagnosticInfoInlineAsm>(DI)))
721  return;
722  ComputeDiagID(Severity, inline_asm, DiagID);
723  break;
724  case llvm::DK_StackSize:
725  if (StackSizeDiagHandler(cast<DiagnosticInfoStackSize>(DI)))
726  return;
727  ComputeDiagID(Severity, backend_frame_larger_than, DiagID);
728  break;
729  case DK_Linker:
730  assert(CurLinkModule);
731  // FIXME: stop eating the warnings and notes.
732  if (Severity != DS_Error)
733  return;
734  DiagID = diag::err_fe_cannot_link_module;
735  break;
736  case llvm::DK_OptimizationRemark:
737  // Optimization remarks are always handled completely by this
738  // handler. There is no generic way of emitting them.
739  OptimizationRemarkHandler(cast<OptimizationRemark>(DI));
740  return;
741  case llvm::DK_OptimizationRemarkMissed:
742  // Optimization remarks are always handled completely by this
743  // handler. There is no generic way of emitting them.
744  OptimizationRemarkHandler(cast<OptimizationRemarkMissed>(DI));
745  return;
746  case llvm::DK_OptimizationRemarkAnalysis:
747  // Optimization remarks are always handled completely by this
748  // handler. There is no generic way of emitting them.
749  OptimizationRemarkHandler(cast<OptimizationRemarkAnalysis>(DI));
750  return;
751  case llvm::DK_OptimizationRemarkAnalysisFPCommute:
752  // Optimization remarks are always handled completely by this
753  // handler. There is no generic way of emitting them.
754  OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisFPCommute>(DI));
755  return;
756  case llvm::DK_OptimizationRemarkAnalysisAliasing:
757  // Optimization remarks are always handled completely by this
758  // handler. There is no generic way of emitting them.
759  OptimizationRemarkHandler(cast<OptimizationRemarkAnalysisAliasing>(DI));
760  return;
761  case llvm::DK_MachineOptimizationRemark:
762  // Optimization remarks are always handled completely by this
763  // handler. There is no generic way of emitting them.
764  OptimizationRemarkHandler(cast<MachineOptimizationRemark>(DI));
765  return;
766  case llvm::DK_MachineOptimizationRemarkMissed:
767  // Optimization remarks are always handled completely by this
768  // handler. There is no generic way of emitting them.
769  OptimizationRemarkHandler(cast<MachineOptimizationRemarkMissed>(DI));
770  return;
771  case llvm::DK_MachineOptimizationRemarkAnalysis:
772  // Optimization remarks are always handled completely by this
773  // handler. There is no generic way of emitting them.
774  OptimizationRemarkHandler(cast<MachineOptimizationRemarkAnalysis>(DI));
775  return;
776  case llvm::DK_OptimizationFailure:
777  // Optimization failures are always handled completely by this
778  // handler.
779  OptimizationFailureHandler(cast<DiagnosticInfoOptimizationFailure>(DI));
780  return;
781  case llvm::DK_Unsupported:
782  UnsupportedDiagHandler(cast<DiagnosticInfoUnsupported>(DI));
783  return;
784  default:
785  // Plugin IDs are not bound to any value as they are set dynamically.
786  ComputeDiagRemarkID(Severity, backend_plugin, DiagID);
787  break;
788  }
789  std::string MsgStorage;
790  {
791  raw_string_ostream Stream(MsgStorage);
792  DiagnosticPrinterRawOStream DP(Stream);
793  DI.print(DP);
794  }
795 
796  if (DiagID == diag::err_fe_cannot_link_module) {
797  Diags.Report(diag::err_fe_cannot_link_module)
798  << CurLinkModule->getModuleIdentifier() << MsgStorage;
799  return;
800  }
801 
802  // Report the backend message using the usual diagnostic mechanism.
803  FullSourceLoc Loc;
804  Diags.Report(Loc, DiagID).AddString(MsgStorage);
805 }
806 #undef ComputeDiagID
807 
808 CodeGenAction::CodeGenAction(unsigned _Act, LLVMContext *_VMContext)
809  : Act(_Act), VMContext(_VMContext ? _VMContext : new LLVMContext),
810  OwnsVMContext(!_VMContext) {}
811 
813  TheModule.reset();
814  if (OwnsVMContext)
815  delete VMContext;
816 }
817 
818 bool CodeGenAction::hasIRSupport() const { return true; }
819 
821  // If the consumer creation failed, do nothing.
822  if (!getCompilerInstance().hasASTConsumer())
823  return;
824 
825  // Steal the module from the consumer.
826  TheModule = BEConsumer->takeModule();
827 }
828 
829 std::unique_ptr<llvm::Module> CodeGenAction::takeModule() {
830  return std::move(TheModule);
831 }
832 
833 llvm::LLVMContext *CodeGenAction::takeLLVMContext() {
834  OwnsVMContext = false;
835  return VMContext;
836 }
837 
838 static std::unique_ptr<raw_pwrite_stream>
839 GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action) {
840  switch (Action) {
842  return CI.createDefaultOutputFile(false, InFile, "s");
843  case Backend_EmitLL:
844  return CI.createDefaultOutputFile(false, InFile, "ll");
845  case Backend_EmitBC:
846  return CI.createDefaultOutputFile(true, InFile, "bc");
847  case Backend_EmitNothing:
848  return nullptr;
849  case Backend_EmitMCNull:
850  return CI.createNullOutputFile();
851  case Backend_EmitObj:
852  return CI.createDefaultOutputFile(true, InFile, "o");
853  }
854 
855  llvm_unreachable("Invalid action!");
856 }
857 
858 std::unique_ptr<ASTConsumer>
860  BackendAction BA = static_cast<BackendAction>(Act);
861  std::unique_ptr<raw_pwrite_stream> OS = CI.takeOutputStream();
862  if (!OS)
863  OS = GetOutputStream(CI, InFile, BA);
864 
865  if (BA != Backend_EmitNothing && !OS)
866  return nullptr;
867 
868  // Load bitcode modules to link with, if we need to.
869  if (LinkModules.empty())
870  for (const CodeGenOptions::BitcodeFileToLink &F :
872  auto BCBuf = CI.getFileManager().getBufferForFile(F.Filename);
873  if (!BCBuf) {
874  CI.getDiagnostics().Report(diag::err_cannot_open_file)
875  << F.Filename << BCBuf.getError().message();
876  LinkModules.clear();
877  return nullptr;
878  }
879 
881  getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext);
882  if (!ModuleOrErr) {
883  handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) {
884  CI.getDiagnostics().Report(diag::err_cannot_open_file)
885  << F.Filename << EIB.message();
886  });
887  LinkModules.clear();
888  return nullptr;
889  }
890  LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
891  F.Internalize, F.LinkFlags});
892  }
893 
894  CoverageSourceInfo *CoverageInfo = nullptr;
895  // Add the preprocessor callback only when the coverage mapping is generated.
896  if (CI.getCodeGenOpts().CoverageMapping) {
897  CoverageInfo = new CoverageSourceInfo;
899  std::unique_ptr<PPCallbacks>(CoverageInfo));
900  }
901 
902  std::unique_ptr<BackendConsumer> Result(new BackendConsumer(
903  BA, CI.getDiagnostics(), CI.getHeaderSearchOpts(),
905  CI.getLangOpts(), CI.getFrontendOpts().ShowTimers, InFile,
906  std::move(LinkModules), std::move(OS), *VMContext, CoverageInfo));
907  BEConsumer = Result.get();
908 
909  // Enable generating macro debug info only when debug info is not disabled and
910  // also macro debug info is enabled.
911  if (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo &&
912  CI.getCodeGenOpts().MacroDebugInfo) {
913  std::unique_ptr<PPCallbacks> Callbacks =
914  llvm::make_unique<MacroPPCallbacks>(BEConsumer->getCodeGenerator(),
915  CI.getPreprocessor());
916  CI.getPreprocessor().addPPCallbacks(std::move(Callbacks));
917  }
918 
919  return std::move(Result);
920 }
921 
922 static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM,
923  void *Context,
924  unsigned LocCookie) {
925  SM.print(nullptr, llvm::errs());
926 
927  auto Diags = static_cast<DiagnosticsEngine *>(Context);
928  unsigned DiagID;
929  switch (SM.getKind()) {
930  case llvm::SourceMgr::DK_Error:
931  DiagID = diag::err_fe_inline_asm;
932  break;
933  case llvm::SourceMgr::DK_Warning:
934  DiagID = diag::warn_fe_inline_asm;
935  break;
936  case llvm::SourceMgr::DK_Note:
937  DiagID = diag::note_fe_inline_asm;
938  break;
939  case llvm::SourceMgr::DK_Remark:
940  llvm_unreachable("remarks unexpected");
941  }
942 
943  Diags->Report(DiagID).AddString("cannot compile inline asm");
944 }
945 
946 std::unique_ptr<llvm::Module> CodeGenAction::loadModule(MemoryBufferRef MBRef) {
949 
950  // For ThinLTO backend invocations, ensure that the context
951  // merges types based on ODR identifiers. We also need to read
952  // the correct module out of a multi-module bitcode file.
953  if (!CI.getCodeGenOpts().ThinLTOIndexFile.empty()) {
954  VMContext->enableDebugTypeODRUniquing();
955 
956  auto DiagErrors = [&](Error E) -> std::unique_ptr<llvm::Module> {
957  unsigned DiagID =
959  handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
960  CI.getDiagnostics().Report(DiagID) << EIB.message();
961  });
962  return {};
963  };
964 
965  Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
966  if (!BMsOrErr)
967  return DiagErrors(BMsOrErr.takeError());
968  BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr);
969  // We have nothing to do if the file contains no ThinLTO module. This is
970  // possible if ThinLTO compilation was not able to split module. Content of
971  // the file was already processed by indexing and will be passed to the
972  // linker using merged object file.
973  if (!Bm) {
974  auto M = llvm::make_unique<llvm::Module>("empty", *VMContext);
975  M->setTargetTriple(CI.getTargetOpts().Triple);
976  return M;
977  }
979  Bm->parseModule(*VMContext);
980  if (!MOrErr)
981  return DiagErrors(MOrErr.takeError());
982  return std::move(*MOrErr);
983  }
984 
985  llvm::SMDiagnostic Err;
986  if (std::unique_ptr<llvm::Module> M = parseIR(MBRef, Err, *VMContext))
987  return M;
988 
989  // Translate from the diagnostic info to the SourceManager location if
990  // available.
991  // TODO: Unify this with ConvertBackendLocation()
992  SourceLocation Loc;
993  if (Err.getLineNo() > 0) {
994  assert(Err.getColumnNo() >= 0);
996  Err.getLineNo(), Err.getColumnNo() + 1);
997  }
998 
999  // Strip off a leading diagnostic code if there is one.
1000  StringRef Msg = Err.getMessage();
1001  if (Msg.startswith("error: "))
1002  Msg = Msg.substr(7);
1003 
1004  unsigned DiagID =
1006 
1007  CI.getDiagnostics().Report(Loc, DiagID) << Msg;
1008  return {};
1009 }
1010 
1012  // If this is an IR file, we have to treat it specially.
1013  if (getCurrentFileKind().getLanguage() == InputKind::LLVM_IR) {
1014  BackendAction BA = static_cast<BackendAction>(Act);
1016  std::unique_ptr<raw_pwrite_stream> OS =
1017  GetOutputStream(CI, getCurrentFile(), BA);
1018  if (BA != Backend_EmitNothing && !OS)
1019  return;
1020 
1021  bool Invalid;
1023  FileID FID = SM.getMainFileID();
1024  llvm::MemoryBuffer *MainFile = SM.getBuffer(FID, &Invalid);
1025  if (Invalid)
1026  return;
1027 
1028  TheModule = loadModule(*MainFile);
1029  if (!TheModule)
1030  return;
1031 
1032  const TargetOptions &TargetOpts = CI.getTargetOpts();
1033  if (TheModule->getTargetTriple() != TargetOpts.Triple) {
1035  diag::warn_fe_override_module)
1036  << TargetOpts.Triple;
1037  TheModule->setTargetTriple(TargetOpts.Triple);
1038  }
1039 
1040  EmbedBitcode(TheModule.get(), CI.getCodeGenOpts(),
1041  MainFile->getMemBufferRef());
1042 
1043  LLVMContext &Ctx = TheModule->getContext();
1044  Ctx.setInlineAsmDiagnosticHandler(BitcodeInlineAsmDiagHandler,
1045  &CI.getDiagnostics());
1046 
1048  CI.getCodeGenOpts(), TargetOpts, CI.getLangOpts(),
1049  CI.getTarget().getDataLayout(), TheModule.get(), BA,
1050  std::move(OS));
1051  return;
1052  }
1053 
1054  // Otherwise follow the normal AST path.
1056 }
1057 
1058 //
1059 
1060 void EmitAssemblyAction::anchor() { }
1061 EmitAssemblyAction::EmitAssemblyAction(llvm::LLVMContext *_VMContext)
1062  : CodeGenAction(Backend_EmitAssembly, _VMContext) {}
1063 
1064 void EmitBCAction::anchor() { }
1065 EmitBCAction::EmitBCAction(llvm::LLVMContext *_VMContext)
1066  : CodeGenAction(Backend_EmitBC, _VMContext) {}
1067 
1068 void EmitLLVMAction::anchor() { }
1069 EmitLLVMAction::EmitLLVMAction(llvm::LLVMContext *_VMContext)
1070  : CodeGenAction(Backend_EmitLL, _VMContext) {}
1071 
1072 void EmitLLVMOnlyAction::anchor() { }
1073 EmitLLVMOnlyAction::EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext)
1074  : CodeGenAction(Backend_EmitNothing, _VMContext) {}
1075 
1076 void EmitCodeGenOnlyAction::anchor() { }
1077 EmitCodeGenOnlyAction::EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext)
1078  : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
1079 
1080 void EmitObjAction::anchor() { }
1081 EmitObjAction::EmitObjAction(llvm::LLVMContext *_VMContext)
1082  : CodeGenAction(Backend_EmitObj, _VMContext) {}
SourceLocation getLocForStartOfFile(FileID FID) const
Return the source location corresponding to the first byte of the specified file. ...
void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID)
Specialized handlers for optimization remarks.
Defines the clang::ASTContext interface.
LangOptions & getLangOpts()
void HandleInlineFunctionDefinition(FunctionDecl *D) override
This callback is invoked each time an inline (method or friend) function definition in a class is com...
Represents a function declaration or definition.
Definition: Decl.h:1737
PreprocessorOptions & getPreprocessorOpts()
static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM, void *Context, unsigned LocCookie)
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
static DeclContext * castToDeclContext(const Decl *)
Definition: DeclBase.cpp:890
bool isPassedOptRemarkEnabled(StringRef PassName) const override
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:116
bool hasIRSupport() const override
Does this action support use with IR files?
ASTConsumer - This is an abstract interface that should be implemented by clients that read ASTs...
Definition: ASTConsumer.h:33
Defines the clang::FileManager interface and associated types.
Emit human-readable LLVM assembly.
Definition: BackendUtil.h:33
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:29
Run CodeGen, but don&#39;t emit anything.
Definition: BackendUtil.h:35
void EndSourceFileAction() override
Callback at the end of processing a single input.
void ExecuteAction() override
Implement the ExecuteAction interface by running Sema on the already-initialized AST consumer...
Defines the SourceManager interface.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
bool HandleTopLevelDecl(DeclGroupRef D) override
HandleTopLevelDecl - Handle the specified top-level declaration.
friend class BackendConsumer
Definition: CodeGenAction.h:26
Stores additional source code information like skipped ranges which is required by the coverage mappi...
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1296
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:689
void HandleTagDeclRequiredDefinition(const TagDecl *D) override
This callback is invoked the first time each TagDecl is required to be complete.
std::unique_ptr< llvm::Module > takeModule()
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Don&#39;t emit anything (benchmarking mode)
Definition: BackendUtil.h:34
Represents a variable declaration or definition.
Definition: Decl.h:812
Options for controlling the target.
Definition: TargetOptions.h:26
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing)...
Definition: DiagnosticIDs.h:79
void HandleTagDeclDefinition(TagDecl *D) override
HandleTagDeclDefinition - This callback is invoked each time a TagDecl (e.g.
bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D)
Specialized handler for StackSize diagnostic.
FileManager & getFileManager() const
bool isAnyRemarkEnabled() const override
#define ComputeDiagRemarkID(Severity, GroupName, DiagID)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:154
Emit LLVM bitcode files.
Definition: BackendUtil.h:32
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
static SourceLocation getFromRawEncoding(unsigned Encoding)
Turn a raw encoding of a SourceLocation object into a real SourceLocation.
void OptimizationFailureHandler(const llvm::DiagnosticInfoOptimizationFailure &D)
SourceLocation translateFileLineCol(const FileEntry *SourceFile, unsigned Line, unsigned Col) const
Get the source location for the given file:line:col triplet.
Definition: Format.h:2113
void CompleteTentativeDefinition(VarDecl *D) override
CompleteTentativeDefinition - Callback invoked at the end of a translation unit to notify the consume...
CodeGenOptions & getCodeGenOpts()
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:49
BackendAction
Definition: BackendUtil.h:30
CompilerInstance & getCompilerInstance() const
FrontendOptions & getFrontendOpts()
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:148
iterator begin()
Definition: DeclGroup.h:99
void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI)
This function is invoked when the backend needs to report something to the user.
CodeGenerator * CreateLLVMCodeGen(DiagnosticsEngine &Diags, llvm::StringRef ModuleName, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PreprocessorOpts, const CodeGenOptions &CGO, llvm::LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
CreateLLVMCodeGen - Create a CodeGenerator instance.
void HandleInterestingDecl(DeclGroupRef D) override
HandleInterestingDecl - Handle the specified interesting declaration.
bool isMissedOptRemarkEnabled(StringRef PassName) const override
static void BitcodeInlineAsmDiagHandler(const llvm::SMDiagnostic &SM, void *Context, unsigned LocCookie)
The primary public interface to the Clang code generator.
Definition: ModuleBuilder.h:43
HeaderSearchOptions & getHeaderSearchOpts()
bool handleDiagnostics(const DiagnosticInfo &DI) override
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
A little helper class used to produce diagnostics.
Definition: Diagnostic.h:1044
BackendConsumer * BEConsumer
Definition: CodeGenAction.h:80
StringRef Filename
Definition: Format.cpp:1638
EmitLLVMAction(llvm::LLVMContext *_VMContext=nullptr)
unsigned Offset
Definition: Format.cpp:1640
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
const AnnotatedLine * Line
CodeGenAction(unsigned _Act, llvm::LLVMContext *_VMContext=nullptr)
Create a new code generation action.
FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, unsigned LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
unsigned ShowTimers
Show timers for individual actions.
Defines the clang::Preprocessor interface.
Emit native object files.
Definition: BackendUtil.h:36
EmitObjAction(llvm::LLVMContext *_VMContext=nullptr)
void OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D)
InputKind getCurrentFileKind() const
The result type of a method or function.
static std::unique_ptr< raw_pwrite_stream > GetOutputStream(CompilerInstance &CI, StringRef InFile, BackendAction Action)
const SourceManager & SM
Definition: Format.cpp:1499
EmitBCAction(llvm::LLVMContext *_VMContext=nullptr)
llvm::Module * getModule() const
static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, SourceManager &CSM)
ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr buffer to be a valid FullS...
Emit native assembly files.
Definition: BackendUtil.h:31
void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D)
Specialized handler for unsupported backend feature diagnostic.
llvm::MemoryBuffer * getBuffer(FileID FID, SourceLocation Loc, bool *Invalid=nullptr) const
Return the buffer for the specified FileID.
EmitCodeGenOnlyAction(llvm::LLVMContext *_VMContext=nullptr)
void AssignInheritanceModel(CXXRecordDecl *RD) override
Callback invoked when an MSInheritanceAttr has been attached to a CXXRecordDecl.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Encodes a location in the source.
std::unique_ptr< llvm::raw_pwrite_stream > takeOutputStream()
LLVM IR: we accept this so that we can run the optimizer on it, and compile it to assembly or object ...
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3063
Cached information about one file (either on disk or in the virtual file system). ...
Definition: FileManager.h:59
std::unique_ptr< raw_pwrite_stream > createNullOutputFile()
const llvm::DataLayout & getDataLayout() const
Definition: TargetInfo.h:970
#define ComputeDiagID(Severity, GroupName, DiagID)
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Definition: Diagnostic.h:777
std::vector< BitcodeFileToLink > LinkBitcodeFiles
The files specified here are linked in to the module before optimizations.
EmitLLVMOnlyAction(llvm::LLVMContext *_VMContext=nullptr)
std::string ThinLTOIndexFile
Name of the function summary index file to use for ThinLTO function importing.
std::unique_ptr< llvm::Module > takeModule()
Take the generated LLVM module, for use after the action has been run.
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
void EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts, llvm::MemoryBufferRef Buf)
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
FileManager & getFileManager() const
Return the current file manager to the caller.
bool FrontendTimesIsEnabled
If the user specifies the -ftime-report argument on an Clang command line then the value of this bool...
FileID getMainFileID() const
Returns the FileID of the main source file.
StringRef getCurrentFile() const
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.
void EmitBackendOutput(DiagnosticsEngine &Diags, const HeaderSearchOptions &, const CodeGenOptions &CGOpts, const TargetOptions &TOpts, const LangOptions &LOpts, const llvm::DataLayout &TDesc, llvm::Module *M, BackendAction Action, std::unique_ptr< raw_pwrite_stream > OS)
SourceManager & getSourceManager() const
Return the current source manager.
void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, SourceLocation LocCookie)
InlineAsmDiagHandler2 - This function is invoked when the backend hits an error parsing inline asm...
const FullSourceLoc getBestLocationFromDebugLoc(const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo, StringRef &Filename, unsigned &Line, unsigned &Column) const
Get the best possible source location to represent a diagnostic that may have associated debug info...
llvm::Expected< llvm::BitcodeModule > FindThinLTOModule(llvm::MemoryBufferRef MBRef)
void HandleVTable(CXXRecordDecl *RD) override
Callback involved at the end of a translation unit to notify the consumer that a vtable for the given...
void Initialize(ASTContext &Ctx) override
Initialize - This is called to initialize the consumer, providing the ASTContext. ...
TargetInfo & getTarget() const
CodeGenOptions - Track various options which control how the code is optimized and passed to the back...
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
Create the default output file (from the invocation&#39;s options) and add it to the list of tracked outp...
SourceManager & getSourceManager()
Definition: ASTContext.h:661
Preprocessor & getPreprocessor() const
Return the current preprocessor.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
EmitAssemblyAction(llvm::LLVMContext *_VMContext=nullptr)
Represents a C++ struct/union/class.
Definition: DeclCXX.h:299
llvm::LLVMContext * takeLLVMContext()
Take the LLVM context used by this action.
CodeGenerator * getCodeGenerator()
void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override
HandleCXXStaticMemberVarInstantiation - Tell the consumer that this.
ClangDiagnosticHandler(const CodeGenOptions &CGOpts, BackendConsumer *BCon)
Defines the clang::TargetInfo interface.
A SourceLocation and its associated SourceManager.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
HeaderSearchOptions - Helper class for storing options related to the initialization of the HeaderSea...
void HandleTranslationUnit(ASTContext &C) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags, const HeaderSearchOptions &HeaderSearchOpts, const PreprocessorOptions &PPOpts, const CodeGenOptions &CodeGenOpts, const TargetOptions &TargetOpts, const LangOptions &LangOpts, bool TimePasses, const std::string &InFile, SmallVector< LinkModule, 4 > LinkModules, std::unique_ptr< raw_pwrite_stream > OS, LLVMContext &C, CoverageSourceInfo *CoverageInfo=nullptr)
A trivial tuple used to represent a source range.
bool isAnalysisRemarkEnabled(StringRef PassName) const override
std::string Triple
The name of the target triple to compile for.
Definition: TargetOptions.h:29
TargetOptions & getTargetOpts()
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
Definition: Preprocessor.h:906
This class handles loading and caching of source files into memory.
PrettyStackTraceDecl - If a crash occurs, indicate that it happened when doing something to a specifi...
Definition: DeclBase.h:1172