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"
20#include <optional>
21#include <string>
22#include <tuple>
23
24namespace llvm {
25template <typename HasherT, llvm::endianness Endianness> class HashBuilder;
26class raw_ostream;
27class StringRef;
28
29/// Represents a version number in the form major[.minor[.subminor[.build]]].
31 unsigned Major : 32;
32
33 unsigned Minor : 31;
34 unsigned HasMinor : 1;
35
36 unsigned Subminor : 31;
37 unsigned HasSubminor : 1;
38
39 unsigned Build : 20;
40 unsigned Subbuild : 10;
41 unsigned HasBuild : 1;
42 unsigned HasSubbuild : 1;
43
44public:
45 constexpr VersionTuple()
46 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
47 Build(0), Subbuild(0), HasBuild(false), HasSubbuild(false) {}
48
49 explicit constexpr VersionTuple(unsigned Major)
50 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
51 HasSubminor(false), Build(0), Subbuild(0), HasBuild(false),
52 HasSubbuild(false) {}
53
54 explicit constexpr VersionTuple(unsigned Major, unsigned Minor)
55 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
56 HasSubminor(false), Build(0), Subbuild(0), HasBuild(false),
57 HasSubbuild(false) {}
58
59 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
60 unsigned Subminor)
61 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
62 HasSubminor(true), Build(0), Subbuild(0), HasBuild(false),
63 HasSubbuild(false) {}
64
65 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
66 unsigned Subminor, unsigned Build)
67 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
68 HasSubminor(true), Build(Build), Subbuild(0), HasBuild(true),
69 HasSubbuild(false) {}
70
71 explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
72 unsigned Subminor, unsigned Build,
73 unsigned Subbuild)
74 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
75 HasSubminor(true), Build(Build), Subbuild(Subbuild), HasBuild(true),
76 HasSubbuild(true) {}
77
78 std::tuple<unsigned, unsigned, unsigned, unsigned, unsigned> asTuple() const {
79 return {Major, Minor, Subminor, Build, Subbuild};
80 }
81
82 /// Determine whether this version information is empty
83 /// (e.g., all version components are zero).
84 bool empty() const { return *this == VersionTuple(); }
85
86 /// Retrieve the major version number.
87 unsigned getMajor() const { return Major; }
88
89 /// Retrieve the minor version number, if provided.
90 std::optional<unsigned> getMinor() const {
91 if (!HasMinor)
92 return std::nullopt;
93 return Minor;
94 }
95
96 /// Retrieve the subminor version number, if provided.
97 std::optional<unsigned> getSubminor() const {
98 if (!HasSubminor)
99 return std::nullopt;
100 return Subminor;
101 }
102
103 /// Retrieve the build version number, if provided.
104 std::optional<unsigned> getBuild() const {
105 if (!HasBuild)
106 return std::nullopt;
107 return Build;
108 }
109
110 /// Retrieve the subbuild version number, if provided.
111 std::optional<unsigned> getSubbuild() const {
112 if (!HasSubbuild)
113 return std::nullopt;
114 return Subbuild;
115 }
116
117 /// Return a version tuple that contains only the first 3 version components.
119 if (HasBuild)
120 return VersionTuple(Major, Minor, Subminor);
121 return *this;
122 }
123
124 /// Return a version tuple that contains a different major version but
125 /// everything else is the same.
126 LLVM_ABI VersionTuple withMajorReplaced(unsigned NewMajor) const;
127
128 /// Return a version tuple that contains only components that are non-zero.
130 VersionTuple Result = *this;
131 if (Result.Subbuild == 0) {
132 Result.HasSubbuild = false;
133 if (Result.Build == 0) {
134 Result.HasBuild = false;
135 if (Result.Subminor == 0) {
136 Result.HasSubminor = false;
137 if (Result.Minor == 0)
138 Result.HasMinor = false;
139 }
140 }
141 }
142 return Result;
143 }
144
145 /// Determine if two version numbers are equivalent. If not
146 /// provided, minor and subminor version numbers are considered to be zero.
147 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
148 return X.asTuple() == Y.asTuple();
149 }
150
151 /// Determine if two version numbers are not equivalent.
152 ///
153 /// If not provided, minor and subminor version numbers are considered to be
154 /// zero.
155 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
156 return !(X == Y);
157 }
158
159 /// Determine whether one version number precedes another.
160 ///
161 /// If not provided, minor and subminor version numbers are considered to be
162 /// zero.
163 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
164 return X.asTuple() < Y.asTuple();
165 }
166
167 /// Determine whether one version number follows another.
168 ///
169 /// If not provided, minor and subminor version numbers are considered to be
170 /// zero.
171 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
172 return Y < X;
173 }
174
175 /// Determine whether one version number precedes or is
176 /// equivalent to another.
177 ///
178 /// If not provided, minor and subminor version numbers are considered to be
179 /// zero.
180 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
181 return !(Y < X);
182 }
183
184 /// Determine whether one version number follows or is
185 /// equivalent to another.
186 ///
187 /// If not provided, minor and subminor version numbers are considered to be
188 /// zero.
189 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
190 return !(X < Y);
191 }
192
194 return hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build, VT.Subbuild);
195 }
196
197 template <typename HasherT, llvm::endianness Endianness>
199 const VersionTuple &VT) {
200 HBuilder.add(VT.Major, VT.Minor, VT.Subminor, VT.Build, VT.Subbuild);
201 }
202
203 /// Retrieve a string representation of the version number.
204 LLVM_ABI std::string getAsString() const;
205
206 /// Try to parse the given string as a version number.
207 /// \returns \c true if the string does not match the regular expression
208 /// [0-9]+(\.[0-9]+){0,3}
209 LLVM_ABI bool tryParse(StringRef string);
210};
211
212/// Print a version number.
213LLVM_ABI raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
214
215// Provide DenseMapInfo for version tuples.
216template <> struct DenseMapInfo<VersionTuple> {
217 static inline VersionTuple getEmptyKey() { return VersionTuple(0x7FFFFFFF); }
219 return VersionTuple(0x7FFFFFFE);
220 }
221 static unsigned getHashValue(const VersionTuple &Value) {
222 unsigned Result = Value.getMajor();
223 if (auto Minor = Value.getMinor())
224 Result = detail::combineHashValue(Result, *Minor);
225 if (auto Subminor = Value.getSubminor())
226 Result = detail::combineHashValue(Result, *Subminor);
227 if (auto Build = Value.getBuild())
228 Result = detail::combineHashValue(Result, *Build);
229 if (auto Subbuild = Value.getSubbuild())
230 Result = detail::combineHashValue(Result, *Subbuild);
231
232 return Result;
233 }
234
235 static bool isEqual(const VersionTuple &LHS, const VersionTuple &RHS) {
236 return LHS == RHS;
237 }
238};
239
240} // end namespace llvm
241#endif // LLVM_SUPPORT_VERSIONTUPLE_H
#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")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Value * RHS
Value * LHS
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.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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:76
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.
Definition Types.h:26
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:592
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...