LLVM  16.0.0git
PackedVersion.cpp
Go to the documentation of this file.
1 //===- PackedVersion.cpp --------------------------------------------------===//
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 // Implements the Mach-O packed version.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/Format.h"
18 
19 namespace llvm {
20 namespace MachO {
21 
23  Version = 0;
24 
25  if (Str.empty())
26  return false;
27 
29  SplitString(Str, Parts, ".");
30 
31  if (Parts.size() > 3)
32  return false;
33 
34  unsigned long long Num;
35  if (getAsUnsignedInteger(Parts[0], 10, Num))
36  return false;
37 
38  if (Num > UINT16_MAX)
39  return false;
40 
41  Version = Num << 16;
42 
43  for (unsigned i = 1, ShiftNum = 8; i < Parts.size(); ++i, ShiftNum -= 8) {
44  if (getAsUnsignedInteger(Parts[i], 10, Num))
45  return false;
46 
47  if (Num > UINT8_MAX)
48  return false;
49 
50  Version |= (Num << ShiftNum);
51  }
52 
53  return true;
54 }
55 
56 std::pair<bool, bool> PackedVersion::parse64(StringRef Str) {
57  bool Truncated = false;
58  Version = 0;
59 
60  if (Str.empty())
61  return std::make_pair(false, Truncated);
62 
64  SplitString(Str, Parts, ".");
65 
66  if (Parts.size() > 5)
67  return std::make_pair(false, Truncated);
68 
69  unsigned long long Num;
70  if (getAsUnsignedInteger(Parts[0], 10, Num))
71  return std::make_pair(false, Truncated);
72 
73  if (Num > 0xFFFFFFULL)
74  return std::make_pair(false, Truncated);
75 
76  if (Num > 0xFFFFULL) {
77  Num = 0xFFFFULL;
78  Truncated = true;
79  }
80  Version = Num << 16;
81 
82  for (unsigned i = 1, ShiftNum = 8; i < Parts.size() && i < 3;
83  ++i, ShiftNum -= 8) {
84  if (getAsUnsignedInteger(Parts[i], 10, Num))
85  return std::make_pair(false, Truncated);
86 
87  if (Num > 0x3FFULL)
88  return std::make_pair(false, Truncated);
89 
90  if (Num > 0xFFULL) {
91  Num = 0xFFULL;
92  Truncated = true;
93  }
94  Version |= (Num << ShiftNum);
95  }
96 
97  if (Parts.size() > 3)
98  Truncated = true;
99 
100  return std::make_pair(true, Truncated);
101 }
102 
104  OS << format("%d", getMajor());
105  if (getMinor() || getSubminor())
106  OS << format(".%d", getMinor());
107  if (getSubminor())
108  OS << format(".%d", getSubminor());
109 }
110 
111 } // end namespace MachO.
112 } // end namespace llvm.
i
i
Definition: README.txt:29
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MachO::PackedVersion::getMajor
unsigned getMajor() const
Retrieve the major version number.
Definition: PackedVersion.h:37
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1182
llvm::MachO::PackedVersion::getMinor
unsigned getMinor() const
Retrieve the minor version number, if provided.
Definition: PackedVersion.h:40
Format.h
llvm::MachO::PackedVersion::parse64
std::pair< bool, bool > parse64(StringRef Str)
Definition: PackedVersion.cpp:56
llvm::MachO::PackedVersion::getSubminor
unsigned getSubminor() const
Retrieve the subminor version number, if provided.
Definition: PackedVersion.h:43
llvm::getAsUnsignedInteger
bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
Definition: StringRef.cpp:492
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
StringExtras.h
llvm::MachO::PackedVersion::parse32
bool parse32(StringRef Str)
Definition: PackedVersion.cpp:22
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
PackedVersion.h
SmallVector.h
raw_ostream.h
llvm::MachO::PackedVersion::print
void print(raw_ostream &OS) const
Definition: PackedVersion.cpp:103