Skip to content

[DWARFLinker][NFC] Move common code into the base library: Utils.h #77604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_UTILS_H
#define LLVM_LIB_DWARFLINKER_PARALLEL_UTILS_H
#ifndef LLVM_DWARFLINKER_UTILS_H
#define LLVM_DWARFLINKER_UTILS_H

#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

namespace llvm {
namespace dwarf_linker {
namespace parallel {

/// This function calls \p Iteration() until it returns false.
/// If number of iterations exceeds \p MaxCounter then an Error is returned.
Expand All @@ -27,16 +30,35 @@ inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
Expected<bool> IterationResultOrError = Iteration();
if (!IterationResultOrError)
return IterationResultOrError.takeError();

if (!IterationResultOrError.get())
return Error::success();
}

return createStringError(std::errc::invalid_argument, "Infinite recursion");
}

} // end of namespace parallel
/// Make a best effort to guess the
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
inline SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
SmallString<128> Result;
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
StringRef Base = sys::path::parent_path(SysRoot);
if (sys::path::filename(Base) != "SDKs")
return Result;
Base = sys::path::parent_path(Base);
Result = Base;
Result += "/Toolchains";
return Result;
}

inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
// Debug info can contain paths from any OS, not necessarily
// an OS we're currently running on. Moreover different compilation units can
// be compiled on different operating systems and linked together later.
return sys::path::is_absolute(Path, sys::path::Style::posix) ||
sys::path::is_absolute(Path, sys::path::Style::windows);
}

} // end of namespace dwarf_linker
} // end of namespace llvm

#endif // LLVM_LIB_DWARFLINKER_PARALLEL_UTILS_H
#endif // LLVM_DWARFLINKER_UTILS_H
15 changes: 1 addition & 14 deletions llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/CodeGen/NonRelocatableStringpool.h"
#include "llvm/DWARFLinker/Classic/DWARFLinkerDeclContext.h"
#include "llvm/DWARFLinker/Classic/DWARFStreamer.h"
#include "llvm/DWARFLinker/Utils.h"
#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Expand Down Expand Up @@ -176,20 +177,6 @@ static void resolveRelativeObjectPath(SmallVectorImpl<char> &Buf, DWARFDie CU) {
sys::path::append(Buf, dwarf::toString(CU.find(dwarf::DW_AT_comp_dir), ""));
}

/// Make a best effort to guess the
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
static SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
SmallString<128> Result;
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
StringRef Base = sys::path::parent_path(SysRoot);
if (sys::path::filename(Base) != "SDKs")
return Result;
Base = sys::path::parent_path(Base);
Result = Base;
Result += "/Toolchains";
return Result;
}

/// Collect references to parseable Swift interfaces in imported
/// DW_TAG_module blocks.
static void analyzeImportedModule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//===----------------------------------------------------------------------===//

#include "AcceleratorRecordsSaver.h"
#include "Utils.h"
#include "llvm/DWARFLinker/Utils.h"
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/Support/DJB.h"

Expand Down
23 changes: 1 addition & 22 deletions llvm/lib/DWARFLinker/Parallel/DWARFLinkerCompileUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "DIEGenerator.h"
#include "DependencyTracker.h"
#include "SyntheticTypeNameBuilder.h"
#include "llvm/DWARFLinker/Utils.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
#include "llvm/Support/DJB.h"
Expand Down Expand Up @@ -247,20 +248,6 @@ void CompileUnit::cleanupDataAfterClonning() {
getOrigUnit().clear();
}

/// Make a best effort to guess the
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
static SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
SmallString<128> Result;
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk
StringRef Base = sys::path::parent_path(SysRoot);
if (sys::path::filename(Base) != "SDKs")
return Result;
Base = sys::path::parent_path(Base);
Result = Base;
Result += "/Toolchains";
return Result;
}

/// Collect references to parseable Swift interfaces in imported
/// DW_TAG_module blocks.
void CompileUnit::analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry) {
Expand Down Expand Up @@ -1698,14 +1685,6 @@ CompileUnit::getDirAndFilenameFromLineTable(
return getDirAndFilenameFromLineTable(FileIdx);
}

static bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {
// Debug info can contain paths from any OS, not necessarily
// an OS we're currently running on. Moreover different compilation units can
// be compiled on different operating systems and linked together later.
return sys::path::is_absolute(Path, sys::path::Style::posix) ||
sys::path::is_absolute(Path, sys::path::Style::windows);
}

std::optional<std::pair<StringRef, StringRef>>
CompileUnit::getDirAndFilenameFromLineTable(uint64_t FileIdx) {
FileNamesCache::iterator FileData = FileNames.find(FileIdx);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "DWARFLinkerImpl.h"
#include "DIEGenerator.h"
#include "DependencyTracker.h"
#include "Utils.h"
#include "llvm/DWARFLinker/Utils.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Parallel.h"
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/DWARFLinker/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/DWARFLinker/Utils.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you intend to add any code to this file? If it's empty, we can delete it

Copy link
Collaborator Author

@avl-llvm avl-llvm Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I am going to add code to this file in the future. Another thing is that currently this is the only .cpp file in the library. Without any .cpp file it would be necessary to write more complex CMakeLists.txt. It is just easier to have empty .cpp.