xref: /freebsd/contrib/llvm-project/llvm/lib/DWARFLinker/Parallel/DWARFLinkerGlobalData.h (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
1 //===- DWARFLinkerGlobalData.h ----------------------------------*- C++ -*-===//
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 
9 #ifndef LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERGLOBALDATA_H
10 #define LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERGLOBALDATA_H
11 
12 #include "TypePool.h"
13 #include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
14 #include "llvm/DWARFLinker/StringPool.h"
15 #include "llvm/Support/PerThreadBumpPtrAllocator.h"
16 
17 namespace llvm {
18 
19 class DWARFDie;
20 
21 namespace dwarf_linker {
22 namespace parallel {
23 
24 using TranslatorFuncTy = std::function<StringRef(StringRef)>;
25 using MessageHandlerTy = std::function<void(
26     const Twine &Warning, StringRef Context, const DWARFDie *DIE)>;
27 
28 /// linking options
29 struct DWARFLinkerOptions {
30   /// DWARF version for the output.
31   uint16_t TargetDWARFVersion = 0;
32 
33   /// Generate processing log to the standard output.
34   bool Verbose = false;
35 
36   /// Print statistics.
37   bool Statistics = false;
38 
39   /// Verify the input DWARF.
40   bool VerifyInputDWARF = false;
41 
42   /// Do not emit output.
43   bool NoOutput = false;
44 
45   /// Do not unique types according to ODR
46   bool NoODR = false;
47 
48   /// Update index tables.
49   bool UpdateIndexTablesOnly = false;
50 
51   /// Whether we want a static variable to force us to keep its enclosing
52   /// function.
53   bool KeepFunctionForStatic = false;
54 
55   /// Allow to generate valid, but non deterministic output.
56   bool AllowNonDeterministicOutput = false;
57 
58   /// Number of threads.
59   unsigned Threads = 1;
60 
61   /// The accelerator table kinds
62   SmallVector<DWARFLinker::AccelTableKind, 1> AccelTables;
63 
64   /// Prepend path for the clang modules.
65   std::string PrependPath;
66 
67   /// input verification handler(it might be called asynchronously).
68   DWARFLinker::InputVerificationHandlerTy InputVerificationHandler = nullptr;
69 
70   /// A list of all .swiftinterface files referenced by the debug
71   /// info, mapping Module name to path on disk. The entries need to
72   /// be uniqued and sorted and there are only few entries expected
73   /// per compile unit, which is why this is a std::map.
74   /// this is dsymutil specific fag.
75   ///
76   /// (it might be called asynchronously).
77   DWARFLinker::SwiftInterfacesMapTy *ParseableSwiftInterfaces = nullptr;
78 
79   /// A list of remappings to apply to file paths.
80   ///
81   /// (it might be called asynchronously).
82   DWARFLinker::ObjectPrefixMapTy *ObjectPrefixMap = nullptr;
83 };
84 
85 class DWARFLinkerImpl;
86 
87 /// This class keeps data and services common for the whole linking process.
88 class LinkingGlobalData {
89   friend DWARFLinkerImpl;
90 
91 public:
92   /// Returns global per-thread allocator.
93   llvm::parallel::PerThreadBumpPtrAllocator &getAllocator() {
94     return Allocator;
95   }
96 
97   /// Returns global string pool.
98   StringPool &getStringPool() { return Strings; }
99 
100   /// Set translation function.
101   void setTranslator(TranslatorFuncTy Translator) {
102     this->Translator = Translator;
103   }
104 
105   /// Translate specified string.
106   StringRef translateString(StringRef String) {
107     if (Translator)
108       return Translator(String);
109 
110     return String;
111   }
112 
113   /// Returns linking options.
114   const DWARFLinkerOptions &getOptions() const { return Options; }
115 
116   /// Set warning handler.
117   void setWarningHandler(MessageHandlerTy Handler) { WarningHandler = Handler; }
118 
119   /// Set error handler.
120   void setErrorHandler(MessageHandlerTy Handler) { ErrorHandler = Handler; }
121 
122   /// Report warning.
123   void warn(const Twine &Warning, StringRef Context,
124             const DWARFDie *DIE = nullptr) {
125     if (WarningHandler)
126       (WarningHandler)(Warning, Context, DIE);
127   }
128 
129   /// Report warning.
130   void warn(Error Warning, StringRef Context, const DWARFDie *DIE = nullptr) {
131     handleAllErrors(std::move(Warning), [&](ErrorInfoBase &Info) {
132       warn(Info.message(), Context, DIE);
133     });
134   }
135 
136   /// Report error.
137   void error(const Twine &Err, StringRef Context,
138              const DWARFDie *DIE = nullptr) {
139     if (ErrorHandler)
140       (ErrorHandler)(Err, Context, DIE);
141   }
142 
143   /// Report error.
144   void error(Error Err, StringRef Context, const DWARFDie *DIE = nullptr) {
145     handleAllErrors(std::move(Err), [&](ErrorInfoBase &Info) {
146       error(Info.message(), Context, DIE);
147     });
148   }
149 
150 protected:
151   llvm::parallel::PerThreadBumpPtrAllocator Allocator;
152   StringPool Strings;
153   TranslatorFuncTy Translator;
154   DWARFLinkerOptions Options;
155   MessageHandlerTy WarningHandler;
156   MessageHandlerTy ErrorHandler;
157 };
158 
159 } // end of namespace parallel
160 } // end of namespace dwarf_linker
161 } // end of namespace llvm
162 
163 #endif // LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERGLOBALDATA_H
164