LLVM  14.0.0git
VersionTuple.h
Go to the documentation of this file.
1 //===- VersionTuple.h - Version Number Handling -----------------*- 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 /// \file
10 /// Defines the llvm::VersionTuple class, which represents a version in
11 /// the form major[.minor[.subminor]].
12 ///
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_VERSIONTUPLE_H
15 #define LLVM_SUPPORT_VERSIONTUPLE_H
16 
17 #include "llvm/ADT/DenseMapInfo.h"
18 #include "llvm/ADT/Hashing.h"
19 #include "llvm/ADT/Optional.h"
21 #include <string>
22 #include <tuple>
23 
24 namespace llvm {
25 class raw_ostream;
26 class StringRef;
27 
28 /// Represents a version number in the form major[.minor[.subminor[.build]]].
29 class VersionTuple {
30  unsigned Major : 32;
31 
32  unsigned Minor : 31;
33  unsigned HasMinor : 1;
34 
35  unsigned Subminor : 31;
36  unsigned HasSubminor : 1;
37 
38  unsigned Build : 31;
39  unsigned HasBuild : 1;
40 
41 public:
43  : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
44  Build(0), HasBuild(false) {}
45 
46  explicit VersionTuple(unsigned Major)
47  : Major(Major), Minor(0), HasMinor(false), Subminor(0),
48  HasSubminor(false), Build(0), HasBuild(false) {}
49 
50  explicit VersionTuple(unsigned Major, unsigned Minor)
51  : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
52  HasSubminor(false), Build(0), HasBuild(false) {}
53 
54  explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
55  : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
56  HasSubminor(true), Build(0), HasBuild(false) {}
57 
58  explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
59  unsigned Build)
60  : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
61  HasSubminor(true), Build(Build), HasBuild(true) {}
62 
63  /// Determine whether this version information is empty
64  /// (e.g., all version components are zero).
65  bool empty() const {
66  return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
67  }
68 
69  /// Retrieve the major version number.
70  unsigned getMajor() const { return Major; }
71 
72  /// Retrieve the minor version number, if provided.
74  if (!HasMinor)
75  return None;
76  return Minor;
77  }
78 
79  /// Retrieve the subminor version number, if provided.
81  if (!HasSubminor)
82  return None;
83  return Subminor;
84  }
85 
86  /// Retrieve the build version number, if provided.
88  if (!HasBuild)
89  return None;
90  return Build;
91  }
92 
93  /// Return a version tuple that contains only the first 3 version components.
95  if (HasBuild)
96  return VersionTuple(Major, Minor, Subminor);
97  return *this;
98  }
99 
100  /// Return a version tuple that contains only components that are non-zero.
102  VersionTuple Result = *this;
103  if (Result.Build == 0) {
104  Result.HasBuild = false;
105  if (Result.Subminor == 0) {
106  Result.HasSubminor = false;
107  if (Result.Minor == 0)
108  Result.HasMinor = false;
109  }
110  }
111  return Result;
112  }
113 
114  /// Determine if two version numbers are equivalent. If not
115  /// provided, minor and subminor version numbers are considered to be zero.
116  friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
117  return X.Major == Y.Major && X.Minor == Y.Minor &&
118  X.Subminor == Y.Subminor && X.Build == Y.Build;
119  }
120 
121  /// Determine if two version numbers are not equivalent.
122  ///
123  /// If not provided, minor and subminor version numbers are considered to be
124  /// zero.
125  friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
126  return !(X == Y);
127  }
128 
129  /// Determine whether one version number precedes another.
130  ///
131  /// If not provided, minor and subminor version numbers are considered to be
132  /// zero.
133  friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
134  return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
135  std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
136  }
137 
138  /// Determine whether one version number follows another.
139  ///
140  /// If not provided, minor and subminor version numbers are considered to be
141  /// zero.
142  friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
143  return Y < X;
144  }
145 
146  /// Determine whether one version number precedes or is
147  /// equivalent to another.
148  ///
149  /// If not provided, minor and subminor version numbers are considered to be
150  /// zero.
151  friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
152  return !(Y < X);
153  }
154 
155  /// Determine whether one version number follows or is
156  /// equivalent to another.
157  ///
158  /// If not provided, minor and subminor version numbers are considered to be
159  /// zero.
160  friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
161  return !(X < Y);
162  }
163 
165  return llvm::hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build);
166  }
167 
168  template <typename HasherT, llvm::support::endianness Endianness>
170  const VersionTuple &VT) {
171  HBuilder.add(VT.Major, VT.Minor, VT.Subminor, VT.Build);
172  }
173 
174  /// Retrieve a string representation of the version number.
175  std::string getAsString() const;
176 
177  /// Try to parse the given string as a version number.
178  /// \returns \c true if the string does not match the regular expression
179  /// [0-9]+(\.[0-9]+){0,3}
180  bool tryParse(StringRef string);
181 };
182 
183 /// Print a version number.
184 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
185 
186 // Provide DenseMapInfo for version tuples.
187 template <> struct DenseMapInfo<VersionTuple> {
188  static inline VersionTuple getEmptyKey() { return VersionTuple(0x7FFFFFFF); }
189  static inline VersionTuple getTombstoneKey() {
190  return VersionTuple(0x7FFFFFFE);
191  }
192  static unsigned getHashValue(const VersionTuple &Value) {
193  unsigned Result = Value.getMajor();
194  if (auto Minor = Value.getMinor())
195  Result = detail::combineHashValue(Result, *Minor);
196  if (auto Subminor = Value.getSubminor())
197  Result = detail::combineHashValue(Result, *Subminor);
198  if (auto Build = Value.getBuild())
199  Result = detail::combineHashValue(Result, *Build);
200 
201  return Result;
202  }
203 
204  static bool isEqual(const VersionTuple &LHS, const VersionTuple &RHS) {
205  return LHS == RHS;
206  }
207 };
208 
209 } // end namespace llvm
210 #endif // LLVM_SUPPORT_VERSIONTUPLE_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::VersionTuple::operator>=
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
Definition: VersionTuple.h:160
llvm::VersionTuple::tryParse
bool tryParse(StringRef string)
Try to parse the given string as a version number.
Definition: VersionTuple.cpp:63
llvm::VersionTuple::operator<=
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
Definition: VersionTuple.h:151
Optional.h
llvm::Optional< unsigned >
Hashing.h
llvm::VersionTuple::operator!=
friend bool operator!=(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are not equivalent.
Definition: VersionTuple.h:125
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major)
Definition: VersionTuple.h:46
llvm::VersionTuple::withoutBuild
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
Definition: VersionTuple.h:94
llvm::VersionTuple::getMajor
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:70
llvm::DenseMapInfo
Definition: APInt.h:34
llvm::VersionTuple::addHash
friend void addHash(HashBuilderImpl< HasherT, Endianness > &HBuilder, const VersionTuple &VT)
Definition: VersionTuple.h:169
llvm::DenseMapInfo< VersionTuple >::getEmptyKey
static VersionTuple getEmptyKey()
Definition: VersionTuple.h:188
llvm::VersionTuple::operator>
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
Definition: VersionTuple.h:142
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
false
Definition: StackSlotColoring.cpp:142
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
llvm::HashBuilderImpl::add
std::enable_if_t< hashbuilder_detail::IsHashableData< T >::value, HashBuilderImpl & > add(T Value)
Implement hashing for hashable data types, e.g. integral or enum values.
Definition: HashBuilder.h:105
llvm::VersionTuple
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:29
HashBuilder.h
llvm::None
const NoneType None
Definition: None.h:23
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
Definition: VersionTuple.h:58
llvm::detail::combineHashValue
static unsigned combineHashValue(unsigned a, unsigned b)
Simplistic combination of 32-bit hash values into 32-bit hash values.
Definition: DenseMapInfo.h:27
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::DenseMapInfo< VersionTuple >::isEqual
static bool isEqual(const VersionTuple &LHS, const VersionTuple &RHS)
Definition: VersionTuple.h:204
llvm::VersionTuple::getSubminor
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition: VersionTuple.h:80
llvm::VersionTuple::normalize
VersionTuple normalize() const
Return a version tuple that contains only components that are non-zero.
Definition: VersionTuple.h:101
llvm::VersionTuple::hash_value
friend llvm::hash_code hash_value(const VersionTuple &VT)
Definition: VersionTuple.h:164
llvm::VersionTuple::getMinor
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:73
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major, unsigned Minor)
Definition: VersionTuple.h:50
llvm::VersionTuple::getBuild
Optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition: VersionTuple.h:87
llvm::HashBuilderImpl
Implementation of the HashBuilder interface.
Definition: HashBuilder.h:90
llvm::VersionTuple::operator==
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
Definition: VersionTuple.h:116
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::VersionTuple::getAsString
std::string getAsString() const
Retrieve a string representation of the version number.
Definition: VersionTuple.cpp:21
llvm::DenseMapInfo< VersionTuple >::getTombstoneKey
static VersionTuple getTombstoneKey()
Definition: VersionTuple.h:189
llvm::DenseMapInfo< VersionTuple >::getHashValue
static unsigned getHashValue(const VersionTuple &Value)
Definition: VersionTuple.h:192
llvm::hash_combine
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:604
llvm::VersionTuple::VersionTuple
VersionTuple()
Definition: VersionTuple.h:42
llvm::VersionTuple::operator<
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
Definition: VersionTuple.h:133
llvm::VersionTuple::empty
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
Definition: VersionTuple.h:65
DenseMapInfo.h
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
Definition: VersionTuple.h:54
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1815
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::hash_code
An opaque object representing a hash code.
Definition: Hashing.h:72