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"
17#include "llvm/Support/Error.h"
19#include "llvm/Support/Path.h"
20
21namespace llvm {
22class DWARFDie;
23
24namespace dwarf_linker {
25
26/// Test if the location of variable \p Die contains a global address
27/// but only uses to compute the variable's value, for example
28/// DW_OP_addr DW_OP_stack_value.
29LLVM_ABI bool hasImplicitAddressLocation(const DWARFDie &Die);
30
31/// Build a map from an input DW_AT_LLVM_stmt_sequence byte offset to
32/// the first-row index (in \p LT.Rows) of the corresponding line-table
33/// sequence. Seeds the map from \p LT.Sequences (the DWARF parser's
34/// discovered sequences), then augments it by walking row boundaries
35/// (DW_LNE_end_sequence markers) and matching them against the sorted
36/// input offsets in \p SortedStmtSeqOffsets, using the parser's results
37/// as ground-truth anchors. This recovers sequences the parser may not
38/// have registered and keeps the classic and parallel DWARFLinkers in
39/// lockstep. Caller passes \p SortedStmtSeqOffsets sorted ascending
40/// and deduplicated.
42 const DWARFDebugLine::LineTable &LT,
43 ArrayRef<uint64_t> SortedStmtSeqOffsets,
44 DenseMap<uint64_t, uint64_t> &SeqOffToFirstRow);
45
46/// This function calls \p Iteration() until it returns false.
47/// If number of iterations exceeds \p MaxCounter then an Error is returned.
48/// This function should be used for loops which assumed to have number of
49/// iterations significantly smaller than \p MaxCounter to avoid infinite
50/// looping in error cases.
52 size_t MaxCounter = 100000) {
53 size_t iterationsCounter = 0;
54 while (iterationsCounter++ < MaxCounter) {
55 Expected<bool> IterationResultOrError = Iteration();
56 if (!IterationResultOrError)
57 return IterationResultOrError.takeError();
58 if (!IterationResultOrError.get())
59 return Error::success();
60 }
61 return createStringError(std::errc::invalid_argument, "Infinite recursion");
62}
63
64/// Make a best effort to guess the
65/// Xcode.app/Contents/Developer path from an SDK path.
67 // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
68 auto it = sys::path::rbegin(SysRoot);
69 auto end = sys::path::rend(SysRoot);
70 if (it == end || !it->ends_with(".sdk"))
71 return {};
72 ++it;
73 // Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
74 if (it == end || *it != "SDKs")
75 return {};
76 auto developerEnd = it;
77 ++it;
78 while (it != end) {
79 // Contents/Developer/Platforms/MacOSX.platform/Developer
80 if (*it != "Developer")
81 return {};
82 ++it;
83 if (it == end)
84 return {};
85 if (*it == "Contents")
86 return StringRef(SysRoot.data(),
87 developerEnd - sys::path::rend(SysRoot) - 1);
88 // Contents/Developer/Platforms/MacOSX.platform
89 if (!it->ends_with(".platform"))
90 return {};
91 ++it;
92 // Contents/Developer/Platforms
93 if (it == end || *it != "Platforms")
94 return {};
95 developerEnd = it;
96 ++it;
97 }
98 return {};
99}
100
101/// Make a best effort to determine whether Path is inside a toolchain.
102inline bool isInToolchainDir(StringRef Path) {
103 // Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2024-05-15-a.xctoolchain/usr/lib/swift/macosx/_StringProcessing.swiftmodule/arm64-apple-macos.private.swiftinterface
104 for (auto it = sys::path::rbegin(Path), end = sys::path::rend(Path);
105 it != end; ++it) {
106 if (it->ends_with(".xctoolchain")) {
107 ++it;
108 if (it == end)
109 return false;
110 if (*it != "Toolchains")
111 return false;
112 ++it;
113 if (it == end)
114 return false;
115 if (*it != "Developer")
116 return false;
117 return true;
118 }
119 }
120 return false;
121}
122
123inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
124 // Debug info can contain paths from any OS, not necessarily
125 // an OS we're currently running on. Moreover different compilation units can
126 // be compiled on different operating systems and linked together later.
129}
130
131} // end of namespace dwarf_linker
132} // end of namespace llvm
133
134#endif // LLVM_DWARFLINKER_UTILS_H
#define LLVM_ABI
Definition Compiler.h:215
This file defines the DenseMap class.
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.
StringRef guessDeveloperDir(StringRef SysRoot)
Make a best effort to guess the Xcode.app/Contents/Developer path from an SDK path.
Definition Utils.h:66
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:41
bool isInToolchainDir(StringRef Path)
Make a best effort to determine whether Path is inside a toolchain.
Definition Utils.h:102
Error finiteLoop(function_ref< Expected< bool >()> Iteration, size_t MaxCounter=100000)
This function calls Iteration() until it returns false.
Definition Utils.h:51
bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path)
Definition Utils.h:123
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