Skip to content

Commit a02c0d9

Browse files
authored
[DWARFLinker][NFC] Move common code into the base library: Utils.h (#77604)
This patch is extracted from #74725. Put some usefull routines into the common Utils.h.
1 parent 4b0314d commit a02c0d9

File tree

6 files changed

+35
-45
lines changed

6 files changed

+35
-45
lines changed

llvm/lib/DWARFLinker/Parallel/Utils.h renamed to llvm/include/llvm/DWARFLinker/Utils.h

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_UTILS_H
10-
#define LLVM_LIB_DWARFLINKER_PARALLEL_UTILS_H
9+
#ifndef LLVM_DWARFLINKER_UTILS_H
10+
#define LLVM_DWARFLINKER_UTILS_H
1111

12+
#include "llvm/ADT/SmallString.h"
13+
#include "llvm/ADT/Twine.h"
1214
#include "llvm/Support/Error.h"
15+
#include "llvm/Support/FileSystem.h"
16+
#include "llvm/Support/Path.h"
1317

1418
namespace llvm {
1519
namespace dwarf_linker {
16-
namespace parallel {
1720

1821
/// This function calls \p Iteration() until it returns false.
1922
/// If number of iterations exceeds \p MaxCounter then an Error is returned.
@@ -27,16 +30,35 @@ inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
2730
Expected<bool> IterationResultOrError = Iteration();
2831
if (!IterationResultOrError)
2932
return IterationResultOrError.takeError();
30-
3133
if (!IterationResultOrError.get())
3234
return Error::success();
3335
}
34-
3536
return createStringError(std::errc::invalid_argument, "Infinite recursion");
3637
}
3738

38-
} // end of namespace parallel
39+
/// Make a best effort to guess the
40+
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
41+
inline SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
42+
SmallString<128> Result;
43+
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
44+
StringRef Base = sys::path::parent_path(SysRoot);
45+
if (sys::path::filename(Base) != "SDKs")
46+
return Result;
47+
Base = sys::path::parent_path(Base);
48+
Result = Base;
49+
Result += "/Toolchains";
50+
return Result;
51+
}
52+
53+
inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
54+
// Debug info can contain paths from any OS, not necessarily
55+
// an OS we're currently running on. Moreover different compilation units can
56+
// be compiled on different operating systems and linked together later.
57+
return sys::path::is_absolute(Path, sys::path::Style::posix) ||
58+
sys::path::is_absolute(Path, sys::path::Style::windows);
59+
}
60+
3961
} // end of namespace dwarf_linker
4062
} // end of namespace llvm
4163

42-
#endif // LLVM_LIB_DWARFLINKER_PARALLEL_UTILS_H
64+
#endif // LLVM_DWARFLINKER_UTILS_H

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/CodeGen/NonRelocatableStringpool.h"
1515
#include "llvm/DWARFLinker/Classic/DWARFLinkerDeclContext.h"
1616
#include "llvm/DWARFLinker/Classic/DWARFStreamer.h"
17+
#include "llvm/DWARFLinker/Utils.h"
1718
#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
1819
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
1920
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
@@ -176,20 +177,6 @@ static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
176177
sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
177178
}
178179

179-
/// Make a best effort to guess the
180-
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
181-
static SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
182-
SmallString<128> Result;
183-
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
184-
StringRef Base = sys::path::parent_path(SysRoot);
185-
if (sys::path::filename(Base) != "SDKs")
186-
return Result;
187-
Base = sys::path::parent_path(Base);
188-
Result = Base;
189-
Result += "/Toolchains";
190-
return Result;
191-
}
192-
193180
/// Collect references to parseable Swift interfaces in imported
194181
/// DW_TAG_module blocks.
195182
static void analyzeImportedModule(

llvm/lib/DWARFLinker/Parallel/AcceleratorRecordsSaver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "AcceleratorRecordsSaver.h"
10-
#include "Utils.h"
10+
#include "llvm/DWARFLinker/Utils.h"
1111
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
1212
#include "llvm/Support/DJB.h"
1313

llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "DIEGenerator.h"
1313
#include "DependencyTracker.h"
1414
#include "SyntheticTypeNameBuilder.h"
15+
#include "llvm/DWARFLinker/Utils.h"
1516
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
1617
#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
1718
#include "llvm/Support/DJB.h"
@@ -247,20 +248,6 @@ void CompileUnit::cleanupDataAfterClonning() {
247248
getOrigUnit().clear();
248249
}
249250

250-
/// Make a best effort to guess the
251-
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
252-
static SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
253-
SmallString<128> Result;
254-
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
255-
StringRef Base = sys::path::parent_path(SysRoot);
256-
if (sys::path::filename(Base) != "SDKs")
257-
return Result;
258-
Base = sys::path::parent_path(Base);
259-
Result = Base;
260-
Result += "/Toolchains";
261-
return Result;
262-
}
263-
264251
/// Collect references to parseable Swift interfaces in imported
265252
/// DW_TAG_module blocks.
266253
void CompileUnit::analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry) {
@@ -1698,14 +1685,6 @@ CompileUnit::getDirAndFilenameFromLineTable(
16981685
return getDirAndFilenameFromLineTable(FileIdx);
16991686
}
17001687

1701-
static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
1702-
// Debug info can contain paths from any OS, not necessarily
1703-
// an OS we're currently running on. Moreover different compilation units can
1704-
// be compiled on different operating systems and linked together later.
1705-
return sys::path::is_absolute(Path, sys::path::Style::posix) ||
1706-
sys::path::is_absolute(Path, sys::path::Style::windows);
1707-
}
1708-
17091688
std::optional<std::pair<StringRef, StringRef>>
17101689
CompileUnit::getDirAndFilenameFromLineTable(uint64_t FileIdx) {
17111690
FileNamesCache::iterator FileData = FileNames.find(FileIdx);

llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "DWARFLinkerImpl.h"
1010
#include "DIEGenerator.h"
1111
#include "DependencyTracker.h"
12-
#include "Utils.h"
12+
#include "llvm/DWARFLinker/Utils.h"
1313
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
1414
#include "llvm/Support/FormatVariadic.h"
1515
#include "llvm/Support/Parallel.h"

llvm/lib/DWARFLinker/Utils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/DWARFLinker/Utils.h"

0 commit comments

Comments
 (0)