[llvm] f9fe603 - [TextAPI] Support more constructors for PackedVersions

Cyndy Ishida via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 7 12:25:56 PDT 2023


Author: Cyndy Ishida
Date: 2023-09-07T12:23:12-07:00
New Revision: f9fe6032cd6fd0814a15dfddf3116494ffa46ac7

URL: https://github.com/llvm/llvm-project/commit/f9fe6032cd6fd0814a15dfddf3116494ffa46ac7
DIFF: https://github.com/llvm/llvm-project/commit/f9fe6032cd6fd0814a15dfddf3116494ffa46ac7.diff

LOG: [TextAPI] Support more constructors for PackedVersions

TBD files now record minimum deplyoment versions and tapi interfaces
with apple system linker by a packed version encoding. Support mapping
between that and `VersionTuple`s.

Added: 
    

Modified: 
    llvm/include/llvm/TextAPI/PackedVersion.h
    llvm/lib/TextAPI/PackedVersion.cpp
    llvm/unittests/TextAPI/TextStubV5Tests.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/TextAPI/PackedVersion.h b/llvm/include/llvm/TextAPI/PackedVersion.h
index eafa5089673521b..e680d40c71044b2 100644
--- a/llvm/include/llvm/TextAPI/PackedVersion.h
+++ b/llvm/include/llvm/TextAPI/PackedVersion.h
@@ -13,6 +13,7 @@
 #ifndef LLVM_TEXTAPI_PACKEDVERSION_H
 #define LLVM_TEXTAPI_PACKEDVERSION_H
 
+#include "llvm/Support/VersionTuple.h"
 #include <cstdint>
 #include <string>
 #include <utility>
@@ -28,10 +29,19 @@ class PackedVersion {
 
 public:
   constexpr PackedVersion() = default;
-  explicit constexpr PackedVersion(uint32_t RawVersion) : Version(RawVersion) {}
+  constexpr PackedVersion(uint32_t RawVersion) : Version(RawVersion) {}
   PackedVersion(unsigned Major, unsigned Minor, unsigned Subminor)
       : Version((Major << 16) | ((Minor & 0xff) << 8) | (Subminor & 0xff)) {}
 
+  PackedVersion(VersionTuple VT) {
+    unsigned Minor = 0, Subminor = 0;
+    if (auto VTMinor = VT.getMinor())
+      Minor = *VTMinor;
+    if (auto VTSub = VT.getSubminor())
+      Subminor = *VTSub;
+    *this = PackedVersion(VT.getMajor(), Minor, Subminor);
+  }
+
   bool empty() const { return Version == 0; }
 
   /// Retrieve the major version number.

diff  --git a/llvm/lib/TextAPI/PackedVersion.cpp b/llvm/lib/TextAPI/PackedVersion.cpp
index 22960c33e9ee8e5..4742be79f45786a 100644
--- a/llvm/lib/TextAPI/PackedVersion.cpp
+++ b/llvm/lib/TextAPI/PackedVersion.cpp
@@ -28,7 +28,7 @@ bool PackedVersion::parse32(StringRef Str) {
   SmallVector<StringRef, 3> Parts;
   SplitString(Str, Parts, ".");
 
-  if (Parts.size() > 3)
+  if (Parts.size() > 3 || Parts.empty())
     return false;
 
   unsigned long long Num;
@@ -63,7 +63,7 @@ std::pair<bool, bool> PackedVersion::parse64(StringRef Str) {
   SmallVector<StringRef, 5> Parts;
   SplitString(Str, Parts, ".");
 
-  if (Parts.size() > 5)
+  if (Parts.size() > 5 || Parts.empty())
     return std::make_pair(false, Truncated);
 
   unsigned long long Num;

diff  --git a/llvm/unittests/TextAPI/TextStubV5Tests.cpp b/llvm/unittests/TextAPI/TextStubV5Tests.cpp
index e6b87fa5592728e..54ca21b91c4d568 100644
--- a/llvm/unittests/TextAPI/TextStubV5Tests.cpp
+++ b/llvm/unittests/TextAPI/TextStubV5Tests.cpp
@@ -197,8 +197,18 @@ TEST(TBDv5, ReadFile) {
       Target(AK_arm64, PLATFORM_MACOS, VersionTuple(11, 0, 0)),
       Target(AK_arm64, PLATFORM_MACCATALYST, VersionTuple(14, 0)),
   };
+  std::set<Target> FileTargets{File->targets().begin(), File->targets().end()};
   EXPECT_EQ(mapToPlatformSet(AllTargets), File->getPlatforms());
   EXPECT_EQ(mapToArchitectureSet(AllTargets), File->getArchitectures());
+  EXPECT_EQ(FileTargets.size(), AllTargets.size());
+  for (const auto &Targ : AllTargets) {
+    auto FileTarg = FileTargets.find(Targ);
+    EXPECT_FALSE(FileTarg == FileTargets.end());
+    EXPECT_EQ(*FileTarg, Targ);
+    PackedVersion MD = Targ.MinDeployment;
+    PackedVersion FileMD = FileTarg->MinDeployment;
+    EXPECT_EQ(MD, FileMD);
+  }
 
   EXPECT_EQ(PackedVersion(1, 2, 0), File->getCurrentVersion());
   EXPECT_EQ(PackedVersion(1, 1, 0), File->getCompatibilityVersion());


        


More information about the llvm-commits mailing list