LLVM 24.0.0git
Utils.h
Go to the documentation of this file.
1//===- Utils.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_DWARFLINKER_UTILS_H
10#define LLVM_DWARFLINKER_UTILS_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/Twine.h"
18#include "llvm/Support/Error.h"
20#include "llvm/Support/Path.h"
21
22namespace llvm {
23class DWARFDie;
24
25namespace dwarf_linker {
26
27/// Test if the location of variable \p Die contains a global address
28/// but only uses to compute the variable's value, for example
29/// DW_OP_addr DW_OP_stack_value.
30LLVM_ABI bool hasImplicitAddressLocation(const DWARFDie &Die);
31
32/// Test if the DW_TAG_member \p Die is a DWARF 2-4 style static data member: a
33/// declaration with no storage in the record. DWARF 5 uses a DW_TAG_variable
34/// instead.
35LLVM_ABI bool isStaticDataMember(const DWARFDie &Die);
36
37/// Test if an unnamed type with tag \p Tag is identified by its children, which
38/// tell it apart from other unnamed types declared on the same line. Other
39/// unnamed types, like pointer types, have no such children.
41
42/// Test if \p Die is a child that identifies its unnamed parent type.
43LLVM_ABI bool isIdentifyingChild(const DWARFDie &Die);
44
45/// Build a map from an input DW_AT_LLVM_stmt_sequence byte offset to
46/// the first-row index (in \p LT.Rows) of the corresponding line-table
47/// sequence. Seeds the map from \p LT.Sequences (the DWARF parser's
48/// discovered sequences), then augments it by walking row boundaries
49/// (DW_LNE_end_sequence markers) and matching them against the sorted
50/// input offsets in \p SortedStmtSeqOffsets, using the parser's results
51/// as ground-truth anchors. This recovers sequences the parser may not
52/// have registered and keeps the classic and parallel DWARFLinkers in
53/// lockstep. Caller passes \p SortedStmtSeqOffsets sorted ascending
54/// and deduplicated.
56 const DWARFDebugLine::LineTable &LT,
57 ArrayRef<uint64_t> SortedStmtSeqOffsets,
58 DenseMap<uint64_t, uint64_t> &SeqOffToFirstRow);
59
60/// This function calls \p Iteration() until it returns false.
61/// If number of iterations exceeds \p MaxCounter then an Error is returned.
62/// This function should be used for loops which assumed to have number of
63/// iterations significantly smaller than \p MaxCounter to avoid infinite
64/// looping in error cases.
66 size_t MaxCounter = 100000) {
67 size_t iterationsCounter = 0;
68 while (iterationsCounter++ < MaxCounter) {
69 Expected<bool> IterationResultOrError = Iteration();
70 if (!IterationResultOrError)
71 return IterationResultOrError.takeError();
72 if (!IterationResultOrError.get())
73 return Error::success();
74 }
75 return createStringError(std::errc::invalid_argument, "Infinite recursion");
76}
77
78/// Make a best effort to guess the
79/// Xcode.app/Contents/Developer path from an SDK path.
81 // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
82 auto it = sys::path::rbegin(SysRoot);
83 auto end = sys::path::rend(SysRoot);
84 if (it == end || !it->ends_with(".sdk"))
85 return {};
86 ++it;
87 // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
88 if (it == end || *it != "SDKs")
89 return {};
90 auto developerEnd = it;
91 ++it;
92 while (it != end) {
93 // Contents/Developer/Platforms/MacOSX.platform/Developer
94 if (*it != "Developer")
95 return {};
96 ++it;
97 if (it == end)
98 return {};
99 if (*it == "Contents")
100 return StringRef(SysRoot.data(),
101 developerEnd - sys::path::rend(SysRoot) - 1);
102 // Contents/Developer/Platforms/MacOSX.platform
103 if (!it->ends_with(".platform"))
104 return {};
105 ++it;
106 // Contents/Developer/Platforms
107 if (it == end || *it != "Platforms")
108 return {};
109 developerEnd = it;
110 ++it;
111 }
112 return {};
113}
114
115/// Make a best effort to determine whether Path is inside a toolchain.
116inline bool isInToolchainDir(StringRef Path) {
117 // Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-05-15-a.xctoolchain/usr/lib/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.private.swiftinterface
118 for (auto it = sys::path::rbegin(Path), end = sys::path::rend(Path);
119 it != end; ++it) {
120 if (it->ends_with(".xctoolchain")) {
121 ++it;
122 if (it == end)
123 return false;
124 if (*it != "Toolchains")
125 return false;
126 ++it;
127 if (it == end)
128 return false;
129 if (*it != "Developer")
130 return false;
131 return true;
132 }
133 }
134 return false;
135}
136
137inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
138 // Debug info can contain paths from any OS, not necessarily
139 // an OS we're currently running on. Moreover different compilation units can
140 // be compiled on different operating systems and linked together later.
143}
144
145} // end of namespace dwarf_linker
146} // end of namespace llvm
147
148#endif // LLVM_DWARFLINKER_UTILS_H
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
This file contains constants used for implementing Dwarf debug support.
This file defines the SmallString class.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:138
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
An efficient, type-erasing, non-owning reference to a callable.
LLVM_ABI bool isIdentifiedByChildren(dwarf::Tag Tag)
Test if an unnamed type with tag Tag is identified by its children, which tell it apart from other un...
Definition Utils.cpp:55
StringRef guessDeveloperDir(StringRef SysRoot)
Make a best effort to guess the Xcode.app/Contents/Developer path from an SDK path.
Definition Utils.h:80
LLVM_ABI bool hasImplicitAddressLocation(const DWARFDie &Die)
Test if the location of variable Die contains a global address but only uses to compute the variable'...
Definition Utils.cpp:21
LLVM_ABI void buildStmtSeqOffsetToFirstRowIndex(const DWARFDebugLine::LineTable &LT, ArrayRef< uint64_t > SortedStmtSeqOffsets, DenseMap< uint64_t, uint64_t > &SeqOffToFirstRow)
Build a map from an input DW_AT_LLVM_stmt_sequence byte offset to the first-row index (in LT....
Definition Utils.cpp:82
bool isInToolchainDir(StringRef Path)
Make a best effort to determine whether Path is inside a toolchain.
Definition Utils.h:116
Error finiteLoop(function_ref< Expected< bool >()> Iteration, size_t MaxCounter=100000)
This function calls Iteration() until it returns false.
Definition Utils.h:65
LLVM_ABI bool isStaticDataMember(const DWARFDie &Die)
Test if the DW_TAG_member Die is a DWARF 2-4 style static data member: a declaration with no storage ...
Definition Utils.cpp:41
bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path)
Definition Utils.h:137
LLVM_ABI bool isIdentifyingChild(const DWARFDie &Die)
Test if Die is a child that identifies its unnamed parent type.
Definition Utils.cpp:68
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:688
LLVM_ABI reverse_iterator rend(StringRef path LLVM_LIFETIME_BOUND)
Get reverse end iterator over path.
LLVM_ABI reverse_iterator rbegin(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get reverse begin iterator over path.
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321