LLVM 23.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
18#include "llvm/ADT/Hashing.h"
19#include "llvm/ADT/bit.h"
21#include <optional>
22#include <string>
23#include <tuple>
24
25namespace llvm {
26template <typename HasherT, llvm::endianness Endianness> class HashBuilder;
27class raw_ostream;
28class StringRef;
29
30/// Represents a version number in the form major[.minor[.subminor[.build]]].
32 unsigned Major : 32;
33
34 unsigned Minor : 31;
35 unsigned HasMinor : 1;
36
37 unsigned Subminor : 31;
38 unsigned HasSubminor : 1;
39
40 unsigned Build : 20;
41 unsigned Subbuild : 10;
42 unsigned HasBuild : 1;
43 unsigned HasSubbuild : 1;
44
45public:
46 constexpr VersionTuple()
47 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
48 Build(0), Subbuild(0), HasBuild(false), HasSubbuild(false) {}
49
50 explicit constexpr VersionTuple(unsigned Major)
51 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
52 HasSubminor(false), Build(0), Subbuild(0), HasBuild(false),
53 HasSubbuild(false) {}
54
55 explicit constexpr VersionTuple(unsigned Major, unsigned Minor)
56 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
57 HasSubminor(false), Build(0), Subbuild(0), HasBuild(false),
58 HasSubbuild(false) {}
59
60 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
61 unsigned Subminor)
62 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
63 HasSubminor(true), Build(0), Subbuild(0), HasBuild(false),
64 HasSubbuild(false) {}
65
66 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
67 unsigned Subminor, unsigned Build)
68 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
69 HasSubminor(true), Build(Build), Subbuild(0), HasBuild(true),
70 HasSubbuild(false) {}
71
72 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
73 unsigned Subminor, unsigned Build,
74 unsigned Subbuild)
75 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
76 HasSubminor(true), Build(Build), Subbuild(Subbuild), HasBuild(true),
77 HasSubbuild(true) {}
78
79 std::tuple<unsigned, unsigned, unsigned, unsigned, unsigned> asTuple() const {
80 return {Major, Minor, Subminor, Build, Subbuild};
81 }
82
83 /// Determine whether this version information is empty
84 /// (e.g., all version components are zero).
85 bool empty() const { return *this == VersionTuple(); }
86
87 /// Retrieve the major version number.
88 unsigned getMajor() const { return Major; }
89
90 /// Retrieve the minor version number, if provided.
91 std::optional<unsigned> getMinor() const {
92 if (!HasMinor)
93 return std::nullopt;
94 return Minor;
95 }
96
97 /// Retrieve the subminor version number, if provided.
98 std::optional<unsigned> getSubminor() const {
99 if (!HasSubminor)
100 return std::nullopt;
101 return Subminor;
102 }
103
104 /// Retrieve the build version number, if provided.
105 std::optional<unsigned> getBuild() const {
106 if (!HasBuild)
107 return std::nullopt;
108 return Build;
109 }
110
111 /// Retrieve the subbuild version number, if provided.
112 std::optional<unsigned> getSubbuild() const {
113 if (!HasSubbuild)
114 return std::nullopt;
115 return Subbuild;
116 }
117
118 /// Return a version tuple that contains only the first 3 version components.
120 if (HasBuild)
121 return VersionTuple(Major, Minor, Subminor);
122 return *this;
123 }
124
125 /// Return a version tuple that contains a different major version but
126 /// everything else is the same.
127 LLVM_ABI VersionTuple withMajorReplaced(unsigned NewMajor) const;
128
129 /// Return a version tuple that contains only components that are non-zero.
131 VersionTuple Result = *this;
132 if (Result.Subbuild == 0) {
133 Result.HasSubbuild = false;
134 if (Result.Build == 0) {
135 Result.HasBuild = false;
136 if (Result.Subminor == 0) {
137 Result.HasSubminor = false;
138 if (Result.Minor == 0)
139 Result.HasMinor = false;
140 }
141 }
142 }
143 return Result;
144 }
145
146 /// Determine if two version numbers are equivalent. If not
147 /// provided, minor and subminor version numbers are considered to be zero.
148 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
149 return X.asTuple() == Y.asTuple();
150 }
151
152 /// Determine if two version numbers are not equivalent.
153 ///
154 /// If not provided, minor and subminor version numbers are considered to be
155 /// zero.
156 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
157 return !(X == Y);
158 }
159
160 /// Determine whether one version number precedes another.
161 ///
162 /// If not provided, minor and subminor version numbers are considered to be
163 /// zero.
164 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
165 return X.asTuple() < Y.asTuple();
166 }
167
168 /// Determine whether one version number follows another.
169 ///
170 /// If not provided, minor and subminor version numbers are considered to be
171 /// zero.
172 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
173 return Y < X;
174 }
175
176 /// Determine whether one version number precedes or is
177 /// equivalent to another.
178 ///
179 /// If not provided, minor and subminor version numbers are considered to be
180 /// zero.
181 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
182 return !(Y < X);
183 }
184
185 /// Determine whether one version number follows or is
186 /// equivalent to another.
187 ///
188 /// If not provided, minor and subminor version numbers are considered to be
189 /// zero.
190 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
191 return !(X < Y);
192 }
193
195 return hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build, VT.Subbuild);
196 }
197
198 template <typename HasherT, llvm::endianness Endianness>
200 const VersionTuple &VT) {
201 HBuilder.add(VT.Major, VT.Minor, VT.Subminor, VT.Build, VT.Subbuild);
202 }
203
204 /// Retrieve a string representation of the version number.
205 LLVM_ABI std::string getAsString() const;
206
207 /// Try to parse the given string as a version number.
208 /// \returns \c true if the string does not match the regular expression
209 /// [0-9]+(\.[0-9]+){0,3}
210 LLVM_ABI bool tryParse(StringRef string);
211};
212
213/// Print a version number.
214LLVM_ABI raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
215
216// Provide DenseMapInfo for version tuples.
217template <> struct DenseMapInfo<VersionTuple> {
218 static inline VersionTuple getEmptyKey() { return VersionTuple(0x7FFFFFFF); }
220 return VersionTuple(0x7FFFFFFE);
221 }
222 static unsigned getHashValue(const VersionTuple &Value) {
223 unsigned Result = Value.getMajor();
224 if (auto Minor = Value.getMinor())
225 Result = detail::combineHashValue(Result, *Minor);
226 if (auto Subminor = Value.getSubminor())
227 Result = detail::combineHashValue(Result, *Subminor);
228 if (auto Build = Value.getBuild())
229 Result = detail::combineHashValue(Result, *Build);
230 if (auto Subbuild = Value.getSubbuild())
231 Result = detail::combineHashValue(Result, *Subbuild);
232
233 return Result;
234 }
235
236 static bool isEqual(const VersionTuple &LHS, const VersionTuple &RHS) {
237 return LHS == RHS;
238 }
239};
240
241} // end namespace llvm
242#endif // LLVM_SUPPORT_VERSIONTUPLE_H
#define X(NUM, ENUM, NAME)
Definition ELF.h:851
#define LLVM_ABI
Definition Compiler.h:213
This file defines DenseMapInfo traits for DenseMap.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Value * RHS
Value * LHS
This file implements the C++20 <bit> header.
Interface to help hash various types through a hasher type.
std::enable_if_t< hashbuilder_detail::IsHashableData< T >::value, HashBuilder & > add(T Value)
Implement hashing for hashable data types, e.g. integral or enum values.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
Represents a version number in the form major[.minor[.subminor[.build]]].
constexpr VersionTuple(unsigned Major)
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
std::optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
friend bool operator!=(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are not equivalent.
std::tuple< unsigned, unsigned, unsigned, unsigned, unsigned > asTuple() const
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
constexpr VersionTuple(unsigned Major, unsigned Minor)
unsigned getMajor() const
Retrieve the major version number.
friend hash_code hash_value(const VersionTuple &VT)
LLVM_ABI bool tryParse(StringRef string)
Try to parse the given string as a version number.
constexpr VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build, unsigned Subbuild)
std::optional< unsigned > getSubbuild() const
Retrieve the subbuild version number, if provided.
LLVM_ABI std::string getAsString() const
Retrieve a string representation of the version number.
LLVM_ABI VersionTuple withMajorReplaced(unsigned NewMajor) const
Return a version tuple that contains a different major version but everything else is the same.
VersionTuple normalize() const
Return a version tuple that contains only components that are non-zero.
std::optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
constexpr VersionTuple()
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
std::optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
friend void addHash(HashBuilder< HasherT, Endianness > &HBuilder, const VersionTuple &VT)
An opaque object representing a hash code.
Definition Hashing.h:78
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
unsigned combineHashValue(unsigned a, unsigned b)
Simplistic combination of 32-bit hash values into 32-bit hash values.
This is an optimization pass for GlobalISel generic memory operations.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:325
static VersionTuple getEmptyKey()
static bool isEqual(const VersionTuple &LHS, const VersionTuple &RHS)
static VersionTuple getTombstoneKey()
static unsigned getHashValue(const VersionTuple &Value)
An information struct used to provide DenseMap with the various necessary components for a given valu...