Skip to content

Commit 3a080a0

Browse files
authored
[TextAPI] Refactor BinaryAttrs to InterfaceFile assignment (#81551)
Create a helper method for this operation, so it can be reused in multiple places. Additonally move FileType enum into its own header to avoid include cycles.
1 parent 644ac2a commit 3a080a0

File tree

8 files changed

+95
-64
lines changed

8 files changed

+95
-64
lines changed

llvm/include/llvm/TextAPI/FileTypes.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===- llvm/TextAPI/FileTypes.h - TAPI Interface File -----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TEXTAPI_FILETYPES_H
10+
#define LLVM_TEXTAPI_FILETYPES_H
11+
12+
#include "llvm/ADT/BitmaskEnum.h"
13+
namespace llvm::MachO {
14+
/// Defines the file type TextAPI files can represent.
15+
enum FileType : unsigned {
16+
/// Invalid file type.
17+
Invalid = 0U,
18+
19+
/// \brief MachO Dynamic Library file.
20+
MachO_DynamicLibrary = 1U << 0,
21+
22+
/// \brief MachO Dynamic Library Stub file.
23+
MachO_DynamicLibrary_Stub = 1U << 1,
24+
25+
/// \brief MachO Bundle file.
26+
MachO_Bundle = 1U << 2,
27+
28+
/// Text-based stub file (.tbd) version 1.0
29+
TBD_V1 = 1U << 3,
30+
31+
/// Text-based stub file (.tbd) version 2.0
32+
TBD_V2 = 1U << 4,
33+
34+
/// Text-based stub file (.tbd) version 3.0
35+
TBD_V3 = 1U << 5,
36+
37+
/// Text-based stub file (.tbd) version 4.0
38+
TBD_V4 = 1U << 6,
39+
40+
/// Text-based stub file (.tbd) version 5.0
41+
TBD_V5 = 1U << 7,
42+
43+
All = ~0U,
44+
45+
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
46+
};
47+
48+
} // namespace llvm::MachO
49+
#endif // LLVM_TEXTAPI_FILETYPES_H

llvm/include/llvm/TextAPI/InterfaceFile.h

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
#ifndef LLVM_TEXTAPI_INTERFACEFILE_H
1515
#define LLVM_TEXTAPI_INTERFACEFILE_H
1616

17-
#include "llvm/ADT/BitmaskEnum.h"
1817
#include "llvm/ADT/Hashing.h"
1918
#include "llvm/ADT/StringRef.h"
2019
#include "llvm/ADT/iterator.h"
2120
#include "llvm/Support/Allocator.h"
2221
#include "llvm/TextAPI/ArchitectureSet.h"
22+
#include "llvm/TextAPI/FileTypes.h"
2323
#include "llvm/TextAPI/PackedVersion.h"
2424
#include "llvm/TextAPI/Platform.h"
25+
#include "llvm/TextAPI/RecordsSlice.h"
2526
#include "llvm/TextAPI/Symbol.h"
2627
#include "llvm/TextAPI/SymbolSet.h"
2728
#include "llvm/TextAPI/Target.h"
@@ -47,44 +48,6 @@ enum class ObjCConstraintType : unsigned {
4748
GC = 4,
4849
};
4950

50-
// clang-format off
51-
52-
/// Defines the file type this file represents.
53-
enum FileType : unsigned {
54-
/// Invalid file type.
55-
Invalid = 0U,
56-
57-
/// \brief MachO Dynamic Library file.
58-
MachO_DynamicLibrary = 1U << 0,
59-
60-
/// \brief MachO Dynamic Library Stub file.
61-
MachO_DynamicLibrary_Stub = 1U << 1,
62-
63-
/// \brief MachO Bundle file.
64-
MachO_Bundle = 1U << 2,
65-
66-
/// Text-based stub file (.tbd) version 1.0
67-
TBD_V1 = 1U << 3,
68-
69-
/// Text-based stub file (.tbd) version 2.0
70-
TBD_V2 = 1U << 4,
71-
72-
/// Text-based stub file (.tbd) version 3.0
73-
TBD_V3 = 1U << 5,
74-
75-
/// Text-based stub file (.tbd) version 4.0
76-
TBD_V4 = 1U << 6,
77-
78-
/// Text-based stub file (.tbd) version 5.0
79-
TBD_V5 = 1U << 7,
80-
81-
All = ~0U,
82-
83-
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/All),
84-
};
85-
86-
// clang-format on
87-
8851
/// Reference to an interface file.
8952
class InterfaceFileRef {
9053
public:
@@ -437,6 +400,14 @@ class InterfaceFile {
437400
void inlineLibrary(std::shared_ptr<InterfaceFile> Library,
438401
bool Overwrite = false);
439402

403+
/// Set InterfaceFile properties from pre-gathered binary attributes,
404+
/// if they are not set already.
405+
///
406+
/// \param BA Attributes typically represented in load commands.
407+
/// \param Targ MachO Target slice to add attributes to.
408+
void setFromBinaryAttrs(const RecordsSlice::BinaryAttrs &BA,
409+
const Target &Targ);
410+
440411
/// The equality is determined by attributes that impact linking
441412
/// compatibilities. Path, & FileKind are irrelevant since these by
442413
/// itself should not impact linking.

llvm/include/llvm/TextAPI/RecordsSlice.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define LLVM_TEXTAPI_RECORDSLICE_H
1616

1717
#include "llvm/Support/Allocator.h"
18-
#include "llvm/TextAPI/InterfaceFile.h"
18+
#include "llvm/TextAPI/FileTypes.h"
1919
#include "llvm/TextAPI/PackedVersion.h"
2020
#include "llvm/TextAPI/Record.h"
2121
#include "llvm/TextAPI/RecordVisitor.h"
@@ -191,6 +191,7 @@ class RecordsSlice {
191191
};
192192

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

196197
} // namespace MachO

llvm/lib/TextAPI/BinaryReader/DylibReader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Object/MachOUniversal.h"
1818
#include "llvm/Support/Endian.h"
1919
#include "llvm/TargetParser/Triple.h"
20+
#include "llvm/TextAPI/InterfaceFile.h"
2021
#include "llvm/TextAPI/RecordsSlice.h"
2122
#include "llvm/TextAPI/TextAPIError.h"
2223
#include <iomanip>

llvm/lib/TextAPI/InterfaceFile.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/TextAPI/InterfaceFile.h"
14+
#include "llvm/TextAPI/RecordsSlice.h"
1415
#include "llvm/TextAPI/TextAPIError.h"
1516
#include <iomanip>
1617
#include <sstream>
@@ -351,6 +352,34 @@ InterfaceFile::extract(Architecture Arch) const {
351352
return std::move(IF);
352353
}
353354

355+
void InterfaceFile::setFromBinaryAttrs(const RecordsSlice::BinaryAttrs &BA,
356+
const Target &Targ) {
357+
if (getFileType() != BA.File)
358+
setFileType(BA.File);
359+
if (getInstallName().empty())
360+
setInstallName(BA.InstallName);
361+
if (BA.AppExtensionSafe && !isApplicationExtensionSafe())
362+
setApplicationExtensionSafe();
363+
if (BA.TwoLevelNamespace && !isTwoLevelNamespace())
364+
setTwoLevelNamespace();
365+
if (BA.OSLibNotForSharedCache && !isOSLibNotForSharedCache())
366+
setOSLibNotForSharedCache();
367+
if (getCurrentVersion().empty())
368+
setCurrentVersion(BA.CurrentVersion);
369+
if (getCompatibilityVersion().empty())
370+
setCompatibilityVersion(BA.CompatVersion);
371+
if (getSwiftABIVersion() == 0)
372+
setSwiftABIVersion(BA.SwiftABI);
373+
if (getPath().empty())
374+
setPath(BA.Path);
375+
if (!BA.ParentUmbrella.empty())
376+
addParentUmbrella(Targ, BA.ParentUmbrella);
377+
for (const auto &Client : BA.AllowableClients)
378+
addAllowableClient(Client, Targ);
379+
for (const auto &Lib : BA.RexportedLibraries)
380+
addReexportedLibrary(Lib, Targ);
381+
}
382+
354383
static bool isYAMLTextStub(const FileType &Kind) {
355384
return (Kind >= FileType::TBD_V1) && (Kind < FileType::TBD_V5);
356385
}

llvm/lib/TextAPI/RecordsSlice.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/TextAPI/RecordsSlice.h"
1414
#include "llvm/ADT/SetVector.h"
15+
#include "llvm/TextAPI/InterfaceFile.h"
1516
#include "llvm/TextAPI/Record.h"
1617
#include "llvm/TextAPI/Symbol.h"
1718
#include <utility>
@@ -325,28 +326,7 @@ createInterfaceFile(const Records &Slices, StringRef InstallName) {
325326
continue;
326327
const Target &Targ = S->getTarget();
327328
File->addTarget(Targ);
328-
if (File->getFileType() == FileType::Invalid)
329-
File->setFileType(BA.File);
330-
if (BA.AppExtensionSafe && !File->isApplicationExtensionSafe())
331-
File->setApplicationExtensionSafe();
332-
if (BA.TwoLevelNamespace && !File->isTwoLevelNamespace())
333-
File->setTwoLevelNamespace();
334-
if (BA.OSLibNotForSharedCache && !File->isOSLibNotForSharedCache())
335-
File->setOSLibNotForSharedCache();
336-
if (File->getCurrentVersion().empty())
337-
File->setCurrentVersion(BA.CurrentVersion);
338-
if (File->getCompatibilityVersion().empty())
339-
File->setCompatibilityVersion(BA.CompatVersion);
340-
if (File->getSwiftABIVersion() == 0)
341-
File->setSwiftABIVersion(BA.SwiftABI);
342-
if (File->getPath().empty())
343-
File->setPath(BA.Path);
344-
if (!BA.ParentUmbrella.empty())
345-
File->addParentUmbrella(Targ, BA.ParentUmbrella);
346-
for (const auto &Client : BA.AllowableClients)
347-
File->addAllowableClient(Client, Targ);
348-
for (const auto &Lib : BA.RexportedLibraries)
349-
File->addReexportedLibrary(Lib, Targ);
329+
File->setFromBinaryAttrs(BA, Targ);
350330
}
351331

352332
return File;

llvm/lib/TextAPI/TextAPIContext.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
#ifndef LLVM_TEXTAPI_MACHO_CONTEXT_H
1414
#define LLVM_TEXTAPI_MACHO_CONTEXT_H
1515

16+
#include "llvm/TextAPI/FileTypes.h"
1617
#include <string>
1718

1819
namespace llvm {
1920
namespace MachO {
2021

21-
enum FileType : unsigned;
22-
2322
struct TextAPIContext {
2423
std::string ErrorMessage;
2524
std::string Path;

llvm/lib/TextAPI/TextStubCommon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_TEXTAPI_TEXT_STUB_COMMON_H
1414
#define LLVM_TEXTAPI_TEXT_STUB_COMMON_H
1515

16+
#include "llvm/ADT/BitmaskEnum.h"
1617
#include "llvm/ADT/StringRef.h"
1718
#include "llvm/Support/YAMLTraits.h"
1819
#include "llvm/TextAPI/Architecture.h"

0 commit comments

Comments
 (0)