[InstCombine] Signed saturation tests. NFC
[llvm-complete.git] / include / llvm / Support / VersionTuple.h
blob14736d6b28f0537e927475e6f42f5c6d11690928
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
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <string>
21 #include <tuple>
23 namespace llvm {
25 /// Represents a version number in the form major[.minor[.subminor[.build]]].
26 class VersionTuple {
27 unsigned Major : 32;
29 unsigned Minor : 31;
30 unsigned HasMinor : 1;
32 unsigned Subminor : 31;
33 unsigned HasSubminor : 1;
35 unsigned Build : 31;
36 unsigned HasBuild : 1;
38 public:
39 VersionTuple()
40 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
41 Build(0), HasBuild(false) {}
43 explicit VersionTuple(unsigned Major)
44 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
45 HasSubminor(false), Build(0), HasBuild(false) {}
47 explicit VersionTuple(unsigned Major, unsigned Minor)
48 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
49 HasSubminor(false), Build(0), HasBuild(false) {}
51 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
52 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
53 HasSubminor(true), Build(0), HasBuild(false) {}
55 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
56 unsigned Build)
57 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
58 HasSubminor(true), Build(Build), HasBuild(true) {}
60 /// Determine whether this version information is empty
61 /// (e.g., all version components are zero).
62 bool empty() const {
63 return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
66 /// Retrieve the major version number.
67 unsigned getMajor() const { return Major; }
69 /// Retrieve the minor version number, if provided.
70 Optional<unsigned> getMinor() const {
71 if (!HasMinor)
72 return None;
73 return Minor;
76 /// Retrieve the subminor version number, if provided.
77 Optional<unsigned> getSubminor() const {
78 if (!HasSubminor)
79 return None;
80 return Subminor;
83 /// Retrieve the build version number, if provided.
84 Optional<unsigned> getBuild() const {
85 if (!HasBuild)
86 return None;
87 return Build;
90 /// Determine if two version numbers are equivalent. If not
91 /// provided, minor and subminor version numbers are considered to be zero.
92 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
93 return X.Major == Y.Major && X.Minor == Y.Minor &&
94 X.Subminor == Y.Subminor && X.Build == Y.Build;
97 /// Determine if two version numbers are not equivalent.
98 ///
99 /// If not provided, minor and subminor version numbers are considered to be
100 /// zero.
101 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
102 return !(X == Y);
105 /// Determine whether one version number precedes another.
107 /// If not provided, minor and subminor version numbers are considered to be
108 /// zero.
109 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
110 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
111 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
114 /// Determine whether one version number follows another.
116 /// If not provided, minor and subminor version numbers are considered to be
117 /// zero.
118 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
119 return Y < X;
122 /// Determine whether one version number precedes or is
123 /// equivalent to another.
125 /// If not provided, minor and subminor version numbers are considered to be
126 /// zero.
127 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
128 return !(Y < X);
131 /// Determine whether one version number follows or is
132 /// equivalent to another.
134 /// If not provided, minor and subminor version numbers are considered to be
135 /// zero.
136 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
137 return !(X < Y);
140 /// Retrieve a string representation of the version number.
141 std::string getAsString() const;
143 /// Try to parse the given string as a version number.
144 /// \returns \c true if the string does not match the regular expression
145 /// [0-9]+(\.[0-9]+){0,3}
146 bool tryParse(StringRef string);
149 /// Print a version number.
150 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
152 } // end namespace llvm
153 #endif // LLVM_SUPPORT_VERSIONTUPLE_H