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