Skip to content

Commit b861b12

Browse files
committed
[TextAPI] Implement TBDv5 Reader
[TextAPI] Implement TBDv5 Reader Introduce initial reader for TBDv5 which is in JSON. This captures all the currently understood fields within the internal structure `InterfaceFile`. New fields & follow up tests will be followed up in future PRs. Reviewed By: pete Differential Revision: https://reviews.llvm.org/D144156
1 parent b309bc0 commit b861b12

File tree

19 files changed

+1270
-27
lines changed

19 files changed

+1270
-27
lines changed

lld/test/MachO/Inputs/libStubLink.tbd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ current-version: 1.0.0
2222
exports:
2323
- targets: [ arm64-ios-simulator ]
2424
symbols: [ _arm64_sim_only ]
25+
...

lld/test/MachO/invalid/invalid-stub.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
# RUN: not %lld -L%t -linvalidYAML %t/test.o -o %t/test 2>&1 | FileCheck %s -DDIR=%t
88
# RUN: not %lld -F%t -framework invalidYAML %t/test.o -o %t/test 2>&1 | FileCheck %s -DDIR=%t --check-prefix=CHECK-FRAMEWORK
99

10-
# CHECK: could not load TAPI file at [[DIR]]{{[\\/]}}libinvalidYAML.tbd: malformed file
11-
# CHECK-FRAMEWORK: could not load TAPI file at [[DIR]]{{[\\/]}}invalidYAML.framework{{[\\/]}}invalidYAML.tbd: malformed file
12-
10+
# CHECK: could not load TAPI file at [[DIR]]{{[\\/]}}libinvalidYAML.tbd: unsupported file type
11+
# CHECK-FRAMEWORK: could not load TAPI file at [[DIR]]{{[\\/]}}invalidYAML.framework{{[\\/]}}invalidYAML.tbd: unsupported file type
1312
.globl _main
1413
_main:
1514
ret

lld/test/MachO/tapi-link.s

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ exports:
121121
re-exports: [ 'libNested.dylib' ]
122122
...
123123

124-
## This tests that weak and thread-local symbols are imported as such.
125124
#--- libTlvWeak.tbd
126125
--- !tapi-tbd
127126
tbd-version: 4
@@ -131,8 +130,8 @@ uuids:
131130
value: 00000000-0000-0000-0000-000000000000
132131
install-name: '/usr/lib/libTlvWeak.dylib'
133132
current-version: 0001.001.1
134-
exports:
133+
exports: # Validate weak & thread-local symbols
135134
- targets: [ x86_64-macos ]
136135
weak-symbols: [ _weak ]
137136
thread-local-symbols: [ _tlv ]
138-
---
137+
...

llvm/include/llvm/TextAPI/InterfaceFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ enum FileType : unsigned {
6666
/// Text-based stub file (.tbd) version 4.0
6767
TBD_V4 = 1U << 3,
6868

69+
/// Text-based stub file (.tbd) version 5.0
70+
TBD_V5 = 1U << 4,
71+
6972
All = ~0U,
7073

7174
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),

llvm/lib/TextAPI/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_llvm_component_library(LLVMTextAPI
22
Architecture.cpp
33
ArchitectureSet.cpp
44
InterfaceFile.cpp
5+
TextStubV5.cpp
56
PackedVersion.cpp
67
Platform.cpp
78
Symbol.cpp

llvm/lib/TextAPI/TextStub.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,6 @@ struct UUIDv4 {
258258
UUIDv4(const Target &TargetID, const std::string &Value)
259259
: TargetID(TargetID), Value(Value) {}
260260
};
261-
262-
// clang-format off
263-
enum TBDFlags : unsigned {
264-
None = 0U,
265-
FlatNamespace = 1U << 0,
266-
NotApplicationExtensionSafe = 1U << 1,
267-
InstallAPI = 1U << 2,
268-
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/InstallAPI),
269-
};
270-
// clang-format on
271261
} // end anonymous namespace.
272262

273263
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(Architecture)
@@ -1105,10 +1095,49 @@ static void DiagHandler(const SMDiagnostic &Diag, void *Context) {
11051095
File->ErrorMessage = ("malformed file\n" + Message).str();
11061096
}
11071097

1098+
namespace {
1099+
1100+
Expected<FileType> canReadFileType(MemoryBufferRef InputBuffer) {
1101+
auto TAPIFile = InputBuffer.getBuffer().trim();
1102+
if (TAPIFile.startswith("{") && TAPIFile.endswith("}"))
1103+
return FileType::TBD_V5;
1104+
1105+
if (!TAPIFile.endswith("..."))
1106+
return createStringError(std::errc::not_supported, "unsupported file type");
1107+
1108+
if (TAPIFile.startswith("--- !tapi-tbd\n"))
1109+
return FileType::TBD_V4;
1110+
1111+
if (TAPIFile.startswith("--- !tapi-tbd-v3\n"))
1112+
return FileType::TBD_V3;
1113+
1114+
if (TAPIFile.startswith("--- !tapi-tbd-v2\n"))
1115+
return FileType::TBD_V2;
1116+
1117+
if (TAPIFile.startswith("--- !tapi-tbd-v1\n") ||
1118+
TAPIFile.startswith("---\narchs:"))
1119+
return FileType::TBD_V1;
1120+
1121+
return createStringError(std::errc::not_supported, "unsupported file type");
1122+
}
1123+
} // namespace
1124+
11081125
Expected<std::unique_ptr<InterfaceFile>>
11091126
TextAPIReader::get(MemoryBufferRef InputBuffer) {
11101127
TextAPIContext Ctx;
11111128
Ctx.Path = std::string(InputBuffer.getBufferIdentifier());
1129+
if (auto FTOrErr = canReadFileType(InputBuffer))
1130+
Ctx.FileKind = *FTOrErr;
1131+
else
1132+
return FTOrErr.takeError();
1133+
1134+
// Handle JSON Format.
1135+
if (Ctx.FileKind >= FileType::TBD_V5) {
1136+
auto FileOrErr = getInterfaceFileFromJSON(InputBuffer.getBuffer());
1137+
if (!FileOrErr)
1138+
return FileOrErr.takeError();
1139+
return std::move(*FileOrErr);
1140+
}
11121141
yaml::Input YAMLIn(InputBuffer.getBuffer(), &Ctx, DiagHandler, &Ctx);
11131142

11141143
// Fill vector with interface file objects created by parsing the YAML file.

llvm/lib/TextAPI/TextStubCommon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Implememts common Text Stub YAML mappings.
9+
// Implements common Text Stub YAML mappings.
1010
//
1111
//===----------------------------------------------------------------------===//
1212

llvm/lib/TextAPI/TextStubCommon.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222

2323
using UUID = std::pair<llvm::MachO::Target, std::string>;
2424

25+
// clang-format off
26+
enum TBDFlags : unsigned {
27+
None = 0U,
28+
FlatNamespace = 1U << 0,
29+
NotApplicationExtensionSafe = 1U << 1,
30+
InstallAPI = 1U << 2,
31+
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/InstallAPI),
32+
};
33+
// clang-format on
34+
2535
LLVM_YAML_STRONG_TYPEDEF(llvm::StringRef, FlowStringRef)
2636
LLVM_YAML_STRONG_TYPEDEF(uint8_t, SwiftVersion)
2737
LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(UUID)
@@ -30,9 +40,13 @@ LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(FlowStringRef)
3040
namespace llvm {
3141

3242
namespace MachO {
33-
class ArchitectureSet;
34-
class PackedVersion;
35-
}
43+
class ArchitectureSet;
44+
class PackedVersion;
45+
46+
Expected<std::unique_ptr<InterfaceFile>>
47+
getInterfaceFileFromJSON(StringRef JSON);
48+
} // namespace MachO
49+
3650
namespace yaml {
3751

3852
template <> struct ScalarTraits<FlowStringRef> {

0 commit comments

Comments
 (0)