1 //===- PackedVersion.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implements the Mach-O packed version.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/TextAPI/PackedVersion.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
22 bool PackedVersion::parse32(StringRef Str
) {
28 SmallVector
<StringRef
, 3> Parts
;
29 SplitString(Str
, Parts
, ".");
34 unsigned long long Num
;
35 if (getAsUnsignedInteger(Parts
[0], 10, Num
))
43 for (unsigned i
= 1, ShiftNum
= 8; i
< Parts
.size(); ++i
, ShiftNum
-= 8) {
44 if (getAsUnsignedInteger(Parts
[i
], 10, Num
))
50 Version
|= (Num
<< ShiftNum
);
56 std::pair
<bool, bool> PackedVersion::parse64(StringRef Str
) {
57 bool Truncated
= false;
61 return std::make_pair(false, Truncated
);
63 SmallVector
<StringRef
, 5> Parts
;
64 SplitString(Str
, Parts
, ".");
67 return std::make_pair(false, Truncated
);
69 unsigned long long Num
;
70 if (getAsUnsignedInteger(Parts
[0], 10, Num
))
71 return std::make_pair(false, Truncated
);
73 if (Num
> 0xFFFFFFULL
)
74 return std::make_pair(false, Truncated
);
76 if (Num
> 0xFFFFULL
) {
82 for (unsigned i
= 1, ShiftNum
= 8; i
< Parts
.size() && i
< 3;
84 if (getAsUnsignedInteger(Parts
[i
], 10, Num
))
85 return std::make_pair(false, Truncated
);
88 return std::make_pair(false, Truncated
);
94 Version
|= (Num
<< ShiftNum
);
100 return std::make_pair(true, Truncated
);
103 void PackedVersion::print(raw_ostream
&OS
) const {
104 OS
<< format("%d", getMajor());
105 if (getMinor() || getSubminor())
106 OS
<< format(".%d", getMinor());
108 OS
<< format(".%d", getSubminor());
111 } // end namespace MachO.
112 } // end namespace llvm.