1 //===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Defines the llvm::VersionTuple class, which represents a version in
11 /// the form major[.minor[.subminor]].
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_VERSIONTUPLE_H
15 #define LLVM_SUPPORT_VERSIONTUPLE_H
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/Optional.h"
26 /// Represents a version number in the form major[.minor[.subminor[.build]]].
31 unsigned HasMinor : 1;
33 unsigned Subminor : 31;
34 unsigned HasSubminor : 1;
37 unsigned HasBuild : 1;
41 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
42 Build(0), HasBuild(false) {}
44 explicit VersionTuple(unsigned Major)
45 : Major(Major), Minor(0), HasMinor(false), Subminor(0),
46 HasSubminor(false), Build(0), HasBuild(false) {}
48 explicit VersionTuple(unsigned Major, unsigned Minor)
49 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
50 HasSubminor(false), Build(0), HasBuild(false) {}
52 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
53 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
54 HasSubminor(true), Build(0), HasBuild(false) {}
56 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
58 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
59 HasSubminor(true), Build(Build), HasBuild(true) {}
61 /// Determine whether this version information is empty
62 /// (e.g., all version components are zero).
64 return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
67 /// Retrieve the major version number.
68 unsigned getMajor() const { return Major; }
70 /// Retrieve the minor version number, if provided.
71 Optional<unsigned> getMinor() const {
77 /// Retrieve the subminor version number, if provided.
78 Optional<unsigned> getSubminor() const {
84 /// Retrieve the build version number, if provided.
85 Optional<unsigned> getBuild() const {
91 /// Return a version tuple that contains only the first 3 version components.
92 VersionTuple withoutBuild() const {
94 return VersionTuple(Major, Minor, Subminor);
98 /// Determine if two version numbers are equivalent. If not
99 /// provided, minor and subminor version numbers are considered to be zero.
100 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
101 return X.Major == Y.Major && X.Minor == Y.Minor &&
102 X.Subminor == Y.Subminor && X.Build == Y.Build;
105 /// Determine if two version numbers are not equivalent.
107 /// If not provided, minor and subminor version numbers are considered to be
109 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
113 /// Determine whether one version number precedes another.
115 /// If not provided, minor and subminor version numbers are considered to be
117 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
118 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
119 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
122 /// Determine whether one version number follows another.
124 /// If not provided, minor and subminor version numbers are considered to be
126 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
130 /// Determine whether one version number precedes or is
131 /// equivalent to another.
133 /// If not provided, minor and subminor version numbers are considered to be
135 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
139 /// Determine whether one version number follows or is
140 /// equivalent to another.
142 /// If not provided, minor and subminor version numbers are considered to be
144 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
148 friend llvm::hash_code hash_value(const VersionTuple &VT) {
149 return llvm::hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build);
152 /// Retrieve a string representation of the version number.
153 std::string getAsString() const;
155 /// Try to parse the given string as a version number.
156 /// \returns \c true if the string does not match the regular expression
157 /// [0-9]+(\.[0-9]+){0,3}
158 bool tryParse(StringRef string);
161 /// Print a version number.
162 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
164 } // end namespace llvm
165 #endif // LLVM_SUPPORT_VERSIONTUPLE_H