Skip to content

[TextAPI] Refactor BinaryAttrs to InterfaceFile assignment #81551

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 1 commit into from
Feb 13, 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
49 changes: 49 additions & 0 deletions llvm/include/llvm/TextAPI/FileTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===- llvm/TextAPI/FileTypes.h - TAPI Interface File -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_TEXTAPI_FILETYPES_H
#define LLVM_TEXTAPI_FILETYPES_H

#include "llvm/ADT/BitmaskEnum.h"
namespace llvm::MachO {
/// Defines the file type TextAPI files can represent.
enum FileType : unsigned {
/// Invalid file type.
Invalid = 0U,

/// \brief MachO Dynamic Library file.
MachO_DynamicLibrary = 1U << 0,

/// \brief MachO Dynamic Library Stub file.
MachO_DynamicLibrary_Stub = 1U << 1,

/// \brief MachO Bundle file.
MachO_Bundle = 1U << 2,

/// Text-based stub file (.tbd) version 1.0
TBD_V1 = 1U << 3,

/// Text-based stub file (.tbd) version 2.0
TBD_V2 = 1U << 4,

/// Text-based stub file (.tbd) version 3.0
TBD_V3 = 1U << 5,

/// Text-based stub file (.tbd) version 4.0
TBD_V4 = 1U << 6,

/// Text-based stub file (.tbd) version 5.0
TBD_V5 = 1U << 7,

All = ~0U,

LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
};

} // namespace llvm::MachO
#endif // LLVM_TEXTAPI_FILETYPES_H
49 changes: 10 additions & 39 deletions llvm/include/llvm/TextAPI/InterfaceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
#ifndef LLVM_TEXTAPI_INTERFACEFILE_H
#define LLVM_TEXTAPI_INTERFACEFILE_H

#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Support/Allocator.h"
#include "llvm/TextAPI/ArchitectureSet.h"
#include "llvm/TextAPI/FileTypes.h"
#include "llvm/TextAPI/PackedVersion.h"
#include "llvm/TextAPI/Platform.h"
#include "llvm/TextAPI/RecordsSlice.h"
#include "llvm/TextAPI/Symbol.h"
#include "llvm/TextAPI/SymbolSet.h"
#include "llvm/TextAPI/Target.h"
Expand All @@ -47,44 +48,6 @@ enum class ObjCConstraintType : unsigned {
GC = 4,
};

// clang-format off

/// Defines the file type this file represents.
enum FileType : unsigned {
/// Invalid file type.
Invalid = 0U,

/// \brief MachO Dynamic Library file.
MachO_DynamicLibrary = 1U << 0,

/// \brief MachO Dynamic Library Stub file.
MachO_DynamicLibrary_Stub = 1U << 1,

/// \brief MachO Bundle file.
MachO_Bundle = 1U << 2,

/// Text-based stub file (.tbd) version 1.0
TBD_V1 = 1U << 3,

/// Text-based stub file (.tbd) version 2.0
TBD_V2 = 1U << 4,

/// Text-based stub file (.tbd) version 3.0
TBD_V3 = 1U << 5,

/// Text-based stub file (.tbd) version 4.0
TBD_V4 = 1U << 6,

/// Text-based stub file (.tbd) version 5.0
TBD_V5 = 1U << 7,

All = ~0U,

LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
};

// clang-format on

/// Reference to an interface file.
class InterfaceFileRef {
public:
Expand Down Expand Up @@ -437,6 +400,14 @@ class InterfaceFile {
void inlineLibrary(std::shared_ptr<InterfaceFile> Library,
bool Overwrite = false);

/// Set InterfaceFile properties from pre-gathered binary attributes,
/// if they are not set already.
///
/// \param BA Attributes typically represented in load commands.
/// \param Targ MachO Target slice to add attributes to.
void setFromBinaryAttrs(const RecordsSlice::BinaryAttrs &BA,
const Target &Targ);

/// The equality is determined by attributes that impact linking
/// compatibilities. Path, & FileKind are irrelevant since these by
/// itself should not impact linking.
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/TextAPI/RecordsSlice.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define LLVM_TEXTAPI_RECORDSLICE_H

#include "llvm/Support/Allocator.h"
#include "llvm/TextAPI/InterfaceFile.h"
#include "llvm/TextAPI/FileTypes.h"
#include "llvm/TextAPI/PackedVersion.h"
#include "llvm/TextAPI/Record.h"
#include "llvm/TextAPI/RecordVisitor.h"
Expand Down Expand Up @@ -191,6 +191,7 @@ class RecordsSlice {
};

using Records = llvm::SmallVector<std::shared_ptr<RecordsSlice>, 4>;
class InterfaceFile;
std::unique_ptr<InterfaceFile> convertToInterfaceFile(const Records &Slices);

} // namespace MachO
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/TextAPI/BinaryReader/DylibReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/Object/MachOUniversal.h"
#include "llvm/Support/Endian.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/TextAPI/InterfaceFile.h"
#include "llvm/TextAPI/RecordsSlice.h"
#include "llvm/TextAPI/TextAPIError.h"
#include <iomanip>
Expand Down
29 changes: 29 additions & 0 deletions llvm/lib/TextAPI/InterfaceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/TextAPI/InterfaceFile.h"
#include "llvm/TextAPI/RecordsSlice.h"
#include "llvm/TextAPI/TextAPIError.h"
#include <iomanip>
#include <sstream>
Expand Down Expand Up @@ -351,6 +352,34 @@ InterfaceFile::extract(Architecture Arch) const {
return std::move(IF);
}

void InterfaceFile::setFromBinaryAttrs(const RecordsSlice::BinaryAttrs &BA,
const Target &Targ) {
if (getFileType() != BA.File)
setFileType(BA.File);
if (getInstallName().empty())
setInstallName(BA.InstallName);
if (BA.AppExtensionSafe && !isApplicationExtensionSafe())
setApplicationExtensionSafe();
if (BA.TwoLevelNamespace && !isTwoLevelNamespace())
setTwoLevelNamespace();
if (BA.OSLibNotForSharedCache && !isOSLibNotForSharedCache())
setOSLibNotForSharedCache();
if (getCurrentVersion().empty())
setCurrentVersion(BA.CurrentVersion);
if (getCompatibilityVersion().empty())
setCompatibilityVersion(BA.CompatVersion);
if (getSwiftABIVersion() == 0)
setSwiftABIVersion(BA.SwiftABI);
if (getPath().empty())
setPath(BA.Path);
if (!BA.ParentUmbrella.empty())
addParentUmbrella(Targ, BA.ParentUmbrella);
for (const auto &Client : BA.AllowableClients)
addAllowableClient(Client, Targ);
for (const auto &Lib : BA.RexportedLibraries)
addReexportedLibrary(Lib, Targ);
}

static bool isYAMLTextStub(const FileType &Kind) {
return (Kind >= FileType::TBD_V1) && (Kind < FileType::TBD_V5);
}
Expand Down
24 changes: 2 additions & 22 deletions llvm/lib/TextAPI/RecordsSlice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "llvm/TextAPI/RecordsSlice.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/TextAPI/InterfaceFile.h"
#include "llvm/TextAPI/Record.h"
#include "llvm/TextAPI/Symbol.h"
#include <utility>
Expand Down Expand Up @@ -325,28 +326,7 @@ createInterfaceFile(const Records &Slices, StringRef InstallName) {
continue;
const Target &Targ = S->getTarget();
File->addTarget(Targ);
if (File->getFileType() == FileType::Invalid)
File->setFileType(BA.File);
if (BA.AppExtensionSafe && !File->isApplicationExtensionSafe())
File->setApplicationExtensionSafe();
if (BA.TwoLevelNamespace && !File->isTwoLevelNamespace())
File->setTwoLevelNamespace();
if (BA.OSLibNotForSharedCache && !File->isOSLibNotForSharedCache())
File->setOSLibNotForSharedCache();
if (File->getCurrentVersion().empty())
File->setCurrentVersion(BA.CurrentVersion);
if (File->getCompatibilityVersion().empty())
File->setCompatibilityVersion(BA.CompatVersion);
if (File->getSwiftABIVersion() == 0)
File->setSwiftABIVersion(BA.SwiftABI);
if (File->getPath().empty())
File->setPath(BA.Path);
if (!BA.ParentUmbrella.empty())
File->addParentUmbrella(Targ, BA.ParentUmbrella);
for (const auto &Client : BA.AllowableClients)
File->addAllowableClient(Client, Targ);
for (const auto &Lib : BA.RexportedLibraries)
File->addReexportedLibrary(Lib, Targ);
File->setFromBinaryAttrs(BA, Targ);
}

return File;
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/TextAPI/TextAPIContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
#ifndef LLVM_TEXTAPI_MACHO_CONTEXT_H
#define LLVM_TEXTAPI_MACHO_CONTEXT_H

#include "llvm/TextAPI/FileTypes.h"
#include <string>

namespace llvm {
namespace MachO {

enum FileType : unsigned;

struct TextAPIContext {
std::string ErrorMessage;
std::string Path;
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/TextAPI/TextStubCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef LLVM_TEXTAPI_TEXT_STUB_COMMON_H
#define LLVM_TEXTAPI_TEXT_STUB_COMMON_H

#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/TextAPI/Architecture.h"
Expand Down