LLVM  13.0.0git
VersionTuple.h
Go to the documentation of this file.
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:
41  : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
42  Build(0), HasBuild(false) {}
43 
44  explicit VersionTuple(unsigned Major)
45  : Major(Major), Minor(0), HasMinor(false), Subminor(0),
46  HasSubminor(false), Build(0), HasBuild(false) {}
47 
48  explicit VersionTuple(unsigned Major, unsigned Minor)
49  : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
50  HasSubminor(false), Build(0), HasBuild(false) {}
51 
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 
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).
63  bool empty() const {
64  return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
65  }
66 
67  /// Retrieve the major version number.
68  unsigned getMajor() const { return Major; }
69 
70  /// Retrieve the minor version number, if provided.
72  if (!HasMinor)
73  return None;
74  return Minor;
75  }
76 
77  /// Retrieve the subminor version number, if provided.
79  if (!HasSubminor)
80  return None;
81  return Subminor;
82  }
83 
84  /// Retrieve the build version number, if provided.
86  if (!HasBuild)
87  return None;
88  return Build;
89  }
90 
91  /// Return a version tuple that contains only the first 3 version components.
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 
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
llvm
Definition: AllocatorList.h:23
llvm::VersionTuple::operator>=
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
Definition: VersionTuple.h:144
llvm::VersionTuple::tryParse
bool tryParse(StringRef string)
Try to parse the given string as a version number.
Definition: VersionTuple.cpp:63
llvm::VersionTuple::operator<=
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
Definition: VersionTuple.h:135
Optional.h
llvm::Optional< unsigned >
Hashing.h
llvm::VersionTuple::operator!=
friend bool operator!=(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are not equivalent.
Definition: VersionTuple.h:109
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major)
Definition: VersionTuple.h:44
llvm::VersionTuple::withoutBuild
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
Definition: VersionTuple.h:92
llvm::VersionTuple::getMajor
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:68
llvm::VersionTuple::operator>
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
Definition: VersionTuple.h:126
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
false
Definition: StackSlotColoring.cpp:142
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
llvm::VersionTuple
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:27
llvm::None
const NoneType None
Definition: None.h:23
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
Definition: VersionTuple.h:56
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::VersionTuple::getSubminor
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition: VersionTuple.h:78
llvm::VersionTuple::hash_value
friend llvm::hash_code hash_value(const VersionTuple &VT)
Definition: VersionTuple.h:148
llvm::VersionTuple::getMinor
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:71
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major, unsigned Minor)
Definition: VersionTuple.h:48
llvm::VersionTuple::getBuild
Optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition: VersionTuple.h:85
llvm::VersionTuple::operator==
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
Definition: VersionTuple.h:100
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::VersionTuple::getAsString
std::string getAsString() const
Retrieve a string representation of the version number.
Definition: VersionTuple.cpp:21
llvm::hash_combine
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition: Hashing.h:604
llvm::VersionTuple::VersionTuple
VersionTuple()
Definition: VersionTuple.h:40
llvm::VersionTuple::operator<
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
Definition: VersionTuple.h:117
llvm::VersionTuple::empty
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
Definition: VersionTuple.h:63
llvm::VersionTuple::VersionTuple
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
Definition: VersionTuple.h:52
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1799
llvm::hash_code
An opaque object representing a hash code.
Definition: Hashing.h:72