Fix the dsymutil heuristic for excluding system interfaces. (#93745)

The function was meant to find the Developer/ dir, but it found a
Developer directory nested deep inside the top-level Developer dir.

The new implementation rejects everything in Xcode.app/Developer in
broad strokes.

rdar://128571037
This commit is contained in:
Adrian Prantl 2024-05-30 12:23:20 -07:00 committed by GitHub
parent 0a93e9f2e2
commit f8cc183ea2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 13 deletions

View File

@ -37,17 +37,41 @@ inline Error finiteLoop(function_ref<Expected<bool>()> Iteration,
}
/// Make a best effort to guess the
/// Xcode.app/Contents/Developer/Toolchains/ path from an SDK path.
inline SmallString<128> guessToolchainBaseDir(StringRef SysRoot) {
/// Xcode.app/Contents/Developer path from an SDK path.
inline StringRef guessDeveloperDir(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;
auto it = sys::path::rbegin(SysRoot);
auto end = sys::path::rend(SysRoot);
if (it == end || !it->ends_with(".sdk"))
return {};
++it;
// Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs
if (it == end || *it != "SDKs")
return {};
auto developerEnd = it;
++it;
while (it != end) {
// Contents/Developer/Platforms/MacOSX.platform/Developer
if (*it != "Developer")
return {};
++it;
if (it == end)
return {};
if (*it == "Contents")
return StringRef(SysRoot.data(),
developerEnd - sys::path::rend(SysRoot) - 1);
// Contents/Developer/Platforms/MacOSX.platform
if (!it->ends_with(".platform"))
return {};
++it;
// Contents/Developer/Platforms
if (it == end || *it != "Platforms")
return {};
developerEnd = it;
++it;
}
return {};
}
inline bool isPathAbsoluteOnWindowsOrPosix(const Twine &Path) {

View File

@ -201,8 +201,8 @@ static void analyzeImportedModule(
return;
// Don't track interfaces that are part of the toolchain.
// For example: Swift, _Concurrency, ...
SmallString<128> Toolchain = guessToolchainBaseDir(SysRoot);
if (!Toolchain.empty() && Path.starts_with(Toolchain))
StringRef DeveloperDir = guessDeveloperDir(SysRoot);
if (!DeveloperDir.empty() && Path.starts_with(DeveloperDir))
return;
std::optional<const char *> Name =
dwarf::toString(DIE.find(dwarf::DW_AT_name));

View File

@ -270,8 +270,8 @@ void CompileUnit::analyzeImportedModule(const DWARFDebugInfoEntry *DieEntry) {
return;
// Don't track interfaces that are part of the toolchain.
// For example: Swift, _Concurrency, ...
SmallString<128> Toolchain = guessToolchainBaseDir(SysRoot);
if (!Toolchain.empty() && Path.starts_with(Toolchain))
StringRef DeveloperDir = guessDeveloperDir(SysRoot);
if (!DeveloperDir.empty() && Path.starts_with(DeveloperDir))
return;
if (std::optional<DWARFFormValue> Val = find(DieEntry, dwarf::DW_AT_name)) {
Expected<const char *> Name = Val->getAsCString();

View File

@ -5,3 +5,25 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/DWARFLinker/Utils.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace dwarf_linker;
#define DEVELOPER_DIR "/Applications/Xcode.app/Contents/Developer"
namespace {
TEST(DWARFLinker, PathTest) {
EXPECT_EQ(guessDeveloperDir("/Foo"), "");
EXPECT_EQ(guessDeveloperDir("Foo.sdk"), "");
EXPECT_EQ(guessDeveloperDir(
DEVELOPER_DIR
"/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk"),
DEVELOPER_DIR);
EXPECT_EQ(guessDeveloperDir(DEVELOPER_DIR "/SDKs/MacOSX.sdk"), DEVELOPER_DIR);
}
} // anonymous namespace