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/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/raw_ostream.h"
25 /// Represents a version number in the form major[.minor[.subminor[.build]]].
30 unsigned HasMinor : 1;
32 unsigned Subminor : 31;
33 unsigned HasSubminor : 1;
36 unsigned HasBuild : 1;
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,
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).
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 {
76 /// Retrieve the subminor version number, if provided.
77 Optional<unsigned> getSubminor() const {
83 /// Retrieve the build version number, if provided.
84 Optional<unsigned> getBuild() const {
90 /// Return a version tuple that contains only the first 3 version components.
91 VersionTuple withoutBuild() const {
93 return VersionTuple(Major, Minor, Subminor);
97 /// Determine if two version numbers are equivalent. If not
98 /// provided, minor and subminor version numbers are considered to be zero.
99 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
100 return X.Major == Y.Major && X.Minor == Y.Minor &&
101 X.Subminor == Y.Subminor && X.Build == Y.Build;
104 /// Determine if two version numbers are not equivalent.
106 /// If not provided, minor and subminor version numbers are considered to be
108 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
112 /// Determine whether one version number precedes another.
114 /// If not provided, minor and subminor version numbers are considered to be
116 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
117 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
118 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
121 /// Determine whether one version number follows another.
123 /// If not provided, minor and subminor version numbers are considered to be
125 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
129 /// Determine whether one version number precedes or is
130 /// equivalent to another.
132 /// If not provided, minor and subminor version numbers are considered to be
134 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
138 /// Determine whether one version number follows or is
139 /// equivalent to another.
141 /// If not provided, minor and subminor version numbers are considered to be
143 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
147 /// Retrieve a string representation of the version number.
148 std::string getAsString() const;
150 /// Try to parse the given string as a version number.
151 /// \returns \c true if the string does not match the regular expression
152 /// [0-9]+(\.[0-9]+){0,3}
153 bool tryParse(StringRef string);
156 /// Print a version number.
157 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
159 } // end namespace llvm
160 #endif // LLVM_SUPPORT_VERSIONTUPLE_H