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 17 #include "llvm/ADT/Hashing.h" 18 #include "llvm/ADT/Optional.h" 19 #include <string> 20 #include <tuple> 21 22 namespace llvm { 23 class raw_ostream; 24 class StringRef; 25 26 /// Represents a version number in the form major[.minor[.subminor[.build]]]. 27 class VersionTuple { 28 unsigned Major : 32; 29 30 unsigned Minor : 31; 31 unsigned HasMinor : 1; 32 33 unsigned Subminor : 31; 34 unsigned HasSubminor : 1; 35 36 unsigned Build : 31; 37 unsigned HasBuild : 1; 38 39 public: VersionTuple()40 VersionTuple() 41 : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false), 42 Build(0), HasBuild(false) {} 43 VersionTuple(unsigned Major)44 explicit VersionTuple(unsigned Major) 45 : Major(Major), Minor(0), HasMinor(false), Subminor(0), 46 HasSubminor(false), Build(0), HasBuild(false) {} 47 VersionTuple(unsigned Major,unsigned Minor)48 explicit VersionTuple(unsigned Major, unsigned Minor) 49 : Major(Major), Minor(Minor), HasMinor(true), Subminor(0), 50 HasSubminor(false), Build(0), HasBuild(false) {} 51 VersionTuple(unsigned Major,unsigned Minor,unsigned Subminor)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) {} 55 VersionTuple(unsigned Major,unsigned Minor,unsigned Subminor,unsigned Build)56 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, 57 unsigned Build) 58 : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor), 59 HasSubminor(true), Build(Build), HasBuild(true) {} 60 61 /// Determine whether this version information is empty 62 /// (e.g., all version components are zero). empty()63 bool empty() const { 64 return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0; 65 } 66 67 /// Retrieve the major version number. getMajor()68 unsigned getMajor() const { return Major; } 69 70 /// Retrieve the minor version number, if provided. getMinor()71 Optional<unsigned> getMinor() const { 72 if (!HasMinor) 73 return None; 74 return Minor; 75 } 76 77 /// Retrieve the subminor version number, if provided. getSubminor()78 Optional<unsigned> getSubminor() const { 79 if (!HasSubminor) 80 return None; 81 return Subminor; 82 } 83 84 /// Retrieve the build version number, if provided. getBuild()85 Optional<unsigned> getBuild() const { 86 if (!HasBuild) 87 return None; 88 return Build; 89 } 90 91 /// Return a version tuple that contains only the first 3 version components. withoutBuild()92 VersionTuple withoutBuild() const { 93 if (HasBuild) 94 return VersionTuple(Major, Minor, Subminor); 95 return *this; 96 } 97 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; 103 } 104 105 /// Determine if two version numbers are not equivalent. 106 /// 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 !(X == Y); 111 } 112 113 /// Determine whether one version number precedes another. 114 /// 115 /// If not provided, minor and subminor version numbers are considered to be 116 /// zero. 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); 120 } 121 122 /// Determine whether one version number follows another. 123 /// 124 /// If not provided, minor and subminor version numbers are considered to be 125 /// zero. 126 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) { 127 return Y < X; 128 } 129 130 /// Determine whether one version number precedes or is 131 /// equivalent to another. 132 /// 133 /// If not provided, minor and subminor version numbers are considered to be 134 /// zero. 135 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) { 136 return !(Y < X); 137 } 138 139 /// Determine whether one version number follows or is 140 /// equivalent to another. 141 /// 142 /// If not provided, minor and subminor version numbers are considered to be 143 /// zero. 144 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) { 145 return !(X < Y); 146 } 147 hash_value(const VersionTuple & VT)148 friend llvm::hash_code hash_value(const VersionTuple &VT) { 149 return llvm::hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build); 150 } 151 152 /// Retrieve a string representation of the version number. 153 std::string getAsString() const; 154 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); 159 }; 160 161 /// Print a version number. 162 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V); 163 164 } // end namespace llvm 165 #endif // LLVM_SUPPORT_VERSIONTUPLE_H 166