aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/clang/lib/CodeGen/BackendConsumer.h
blob: 72a814cd43d738d8bc1675ed87faa22856d73f74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//===--- BackendConsumer.h - LLVM BackendConsumer Header File -------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LIB_CODEGEN_BACKENDCONSUMER_H
#define LLVM_CLANG_LIB_CODEGEN_BACKENDCONSUMER_H

#include "clang/CodeGen/BackendUtil.h"
#include "clang/CodeGen/CodeGenAction.h"

#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/Support/Timer.h"

namespace llvm {
  class DiagnosticInfoDontCall;
}

namespace clang {
class ASTContext;
class CodeGenAction;
class CoverageSourceInfo;

class BackendConsumer : public ASTConsumer {
  using LinkModule = CodeGenAction::LinkModule;

  virtual void anchor();
  DiagnosticsEngine &Diags;
  BackendAction Action;
  const HeaderSearchOptions &HeaderSearchOpts;
  const CodeGenOptions &CodeGenOpts;
  const TargetOptions &TargetOpts;
  const LangOptions &LangOpts;
  std::unique_ptr<raw_pwrite_stream> AsmOutStream;
  ASTContext *Context;
  IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;

  llvm::Timer LLVMIRGeneration;
  unsigned LLVMIRGenerationRefCount;

  /// True if we've finished generating IR. This prevents us from generating
  /// additional LLVM IR after emitting output in HandleTranslationUnit. This
  /// can happen when Clang plugins trigger additional AST deserialization.
  bool IRGenFinished = false;

  bool TimerIsEnabled = false;

  std::unique_ptr<CodeGenerator> Gen;

  SmallVector<LinkModule, 4> LinkModules;

  // A map from mangled names to their function's source location, used for
  // backend diagnostics as the Clang AST may be unavailable. We actually use
  // the mangled name's hash as the key because mangled names can be very
  // long and take up lots of space. Using a hash can cause name collision,
  // but that is rare and the consequences are pointing to a wrong source
  // location which is not severe. This is a vector instead of an actual map
  // because we optimize for time building this map rather than time
  // retrieving an entry, as backend diagnostics are uncommon.
  std::vector<std::pair<llvm::hash_code, FullSourceLoc>>
    ManglingFullSourceLocs;


  // This is here so that the diagnostic printer knows the module a diagnostic
  // refers to.
  llvm::Module *CurLinkModule = nullptr;

public:
  BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
                  IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
                  const HeaderSearchOptions &HeaderSearchOpts,
                  const PreprocessorOptions &PPOpts,
                  const CodeGenOptions &CodeGenOpts,
                  const TargetOptions &TargetOpts,
                  const LangOptions &LangOpts, const std::string &InFile,
                  SmallVector<LinkModule, 4> LinkModules,
                  std::unique_ptr<raw_pwrite_stream> OS, llvm::LLVMContext &C,
                  CoverageSourceInfo *CoverageInfo = nullptr);

  // This constructor is used in installing an empty BackendConsumer
  // to use the clang diagnostic handler for IR input files. It avoids
  // initializing the OS field.
  BackendConsumer(BackendAction Action, DiagnosticsEngine &Diags,
                  IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
                  const HeaderSearchOptions &HeaderSearchOpts,
                  const PreprocessorOptions &PPOpts,
                  const CodeGenOptions &CodeGenOpts,
                  const TargetOptions &TargetOpts,
                  const LangOptions &LangOpts, llvm::Module *Module,
                  SmallVector<LinkModule, 4> LinkModules, llvm::LLVMContext &C,
                  CoverageSourceInfo *CoverageInfo = nullptr);

  llvm::Module *getModule() const;
  std::unique_ptr<llvm::Module> takeModule();

  CodeGenerator *getCodeGenerator();

  void HandleCXXStaticMemberVarInstantiation(VarDecl *VD) override;
  void Initialize(ASTContext &Ctx) override;
  bool HandleTopLevelDecl(DeclGroupRef D) override;
  void HandleInlineFunctionDefinition(FunctionDecl *D) override;
  void HandleInterestingDecl(DeclGroupRef D) override;
  void HandleTranslationUnit(ASTContext &C) override;
  void HandleTagDeclDefinition(TagDecl *D) override;
  void HandleTagDeclRequiredDefinition(const TagDecl *D) override;
  void CompleteTentativeDefinition(VarDecl *D) override;
  void CompleteExternalDeclaration(VarDecl *D) override;
  void AssignInheritanceModel(CXXRecordDecl *RD) override;
  void HandleVTable(CXXRecordDecl *RD) override;


 // Links each entry in LinkModules into our module.  Returns true on error.
  bool LinkInModules(llvm::Module *M, bool ShouldLinkFiles = true);

  /// Get the best possible source location to represent a diagnostic that
  /// may have associated debug info.
  const FullSourceLoc getBestLocationFromDebugLoc(
    const llvm::DiagnosticInfoWithLocationBase &D,
    bool &BadDebugInfo, StringRef &Filename,
    unsigned &Line, unsigned &Column) const;

  std::optional<FullSourceLoc> getFunctionSourceLocation(
    const llvm::Function &F) const;

  void DiagnosticHandlerImpl(const llvm::DiagnosticInfo &DI);
  /// Specialized handler for InlineAsm diagnostic.
  /// \return True if the diagnostic has been successfully reported, false
  /// otherwise.
  bool InlineAsmDiagHandler(const llvm::DiagnosticInfoInlineAsm &D);
  /// Specialized handler for diagnostics reported using SMDiagnostic.
  void SrcMgrDiagHandler(const llvm::DiagnosticInfoSrcMgr &D);
  /// Specialized handler for StackSize diagnostic.
  /// \return True if the diagnostic has been successfully reported, false
  /// otherwise.
  bool StackSizeDiagHandler(const llvm::DiagnosticInfoStackSize &D);
  /// Specialized handler for ResourceLimit diagnostic.
  /// \return True if the diagnostic has been successfully reported, false
  /// otherwise.
  bool ResourceLimitDiagHandler(const llvm::DiagnosticInfoResourceLimit &D);

  /// Specialized handler for unsupported backend feature diagnostic.
  void UnsupportedDiagHandler(const llvm::DiagnosticInfoUnsupported &D);
  /// Specialized handlers for optimization remarks.
  /// Note that these handlers only accept remarks and they always handle
  /// them.
  void EmitOptimizationMessage(const llvm::DiagnosticInfoOptimizationBase &D,
                               unsigned DiagID);
  void
    OptimizationRemarkHandler(const llvm::DiagnosticInfoOptimizationBase &D);
  void OptimizationRemarkHandler(
    const llvm::OptimizationRemarkAnalysisFPCommute &D);
  void OptimizationRemarkHandler(
    const llvm::OptimizationRemarkAnalysisAliasing &D);
  void OptimizationFailureHandler(
    const llvm::DiagnosticInfoOptimizationFailure &D);
  void DontCallDiagHandler(const llvm::DiagnosticInfoDontCall &D);
  /// Specialized handler for misexpect warnings.
  /// Note that misexpect remarks are emitted through ORE
  void MisExpectDiagHandler(const llvm::DiagnosticInfoMisExpect &D);
};

} // namespace clang
#endif