1 //===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Defines the llvm::VersionTuple class, which represents a version in
12 /// the form major[.minor[.subminor]].
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_VERSIONTUPLE_H
16 #define LLVM_SUPPORT_VERSIONTUPLE_H
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/raw_ostream.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 /// Determine if two version numbers are equivalent. If not
92 /// provided, minor and subminor version numbers are considered to be zero.
93 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
94 return X.Major == Y.Major && X.Minor == Y.Minor &&
95 X.Subminor == Y.Subminor && X.Build == Y.Build;
98 /// Determine if two version numbers are not equivalent.
100 /// If not provided, minor and subminor version numbers are considered to be
102 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
106 /// Determine whether one version number precedes another.
108 /// If not provided, minor and subminor version numbers are considered to be
110 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
111 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
112 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
115 /// Determine whether one version number follows another.
117 /// If not provided, minor and subminor version numbers are considered to be
119 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
123 /// Determine whether one version number precedes or is
124 /// equivalent to another.
126 /// If not provided, minor and subminor version numbers are considered to be
128 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
132 /// Determine whether one version number follows or is
133 /// equivalent to another.
135 /// If not provided, minor and subminor version numbers are considered to be
137 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
141 /// Retrieve a string representation of the version number.
142 std::string getAsString() const;
144 /// Try to parse the given string as a version number.
145 /// \returns \c true if the string does not match the regular expression
146 /// [0-9]+(\.[0-9]+){0,3}
147 bool tryParse(StringRef string);
150 /// Print a version number.
151 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
153 } // end namespace llvm
154 #endif // LLVM_SUPPORT_VERSIONTUPLE_H