LLVM  10.0.0
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/Optional.h"
18 #include "llvm/ADT/StringRef.h"
20 #include <string>
21 #include <tuple>
22 
23 namespace llvm {
24 
25 /// Represents a version number in the form major[.minor[.subminor[.build]]].
26 class VersionTuple {
27  unsigned Major : 32;
28 
29  unsigned Minor : 31;
30  unsigned HasMinor : 1;
31 
32  unsigned Subminor : 31;
33  unsigned HasSubminor : 1;
34 
35  unsigned Build : 31;
36  unsigned HasBuild : 1;
37 
38 public:
40  : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
41  Build(0), HasBuild(false) {}
42 
43  explicit VersionTuple(unsigned Major)
44  : Major(Major), Minor(0), HasMinor(false), Subminor(0),
45  HasSubminor(false), Build(0), HasBuild(false) {}
46 
47  explicit VersionTuple(unsigned Major, unsigned Minor)
48  : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
49  HasSubminor(false), Build(0), HasBuild(false) {}
50 
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) {}
54 
55  explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
56  unsigned Build)
57  : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
58  HasSubminor(true), Build(Build), HasBuild(true) {}
59 
60  /// Determine whether this version information is empty
61  /// (e.g., all version components are zero).
62  bool empty() const {
63  return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
64  }
65 
66  /// Retrieve the major version number.
67  unsigned getMajor() const { return Major; }
68 
69  /// Retrieve the minor version number, if provided.
71  if (!HasMinor)
72  return None;
73  return Minor;
74  }
75 
76  /// Retrieve the subminor version number, if provided.
78  if (!HasSubminor)
79  return None;
80  return Subminor;
81  }
82 
83  /// Retrieve the build version number, if provided.
85  if (!HasBuild)
86  return None;
87  return Build;
88  }
89 
90  /// Return a version tuple that contains only the first 3 version components.
92  if (HasBuild)
93  return VersionTuple(Major, Minor, Subminor);
94  return *this;
95  }
96 
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;
102  }
103 
104  /// Determine if two version numbers are not equivalent.
105  ///
106  /// If not provided, minor and subminor version numbers are considered to be
107  /// zero.
108  friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
109  return !(X == Y);
110  }
111 
112  /// Determine whether one version number precedes another.
113  ///
114  /// If not provided, minor and subminor version numbers are considered to be
115  /// zero.
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);
119  }
120 
121  /// Determine whether one version number follows another.
122  ///
123  /// If not provided, minor and subminor version numbers are considered to be
124  /// zero.
125  friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
126  return Y < X;
127  }
128 
129  /// Determine whether one version number precedes or is
130  /// equivalent to another.
131  ///
132  /// If not provided, minor and subminor version numbers are considered to be
133  /// zero.
134  friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
135  return !(Y < X);
136  }
137 
138  /// Determine whether one version number follows or is
139  /// equivalent to another.
140  ///
141  /// If not provided, minor and subminor version numbers are considered to be
142  /// zero.
143  friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
144  return !(X < Y);
145  }
146 
147  /// Retrieve a string representation of the version number.
148  std::string getAsString() const;
149 
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);
154 };
155 
156 /// Print a version number.
158 
159 } // end namespace llvm
160 #endif // LLVM_SUPPORT_VERSIONTUPLE_H
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, unsigned Build)
Definition: VersionTuple.h:55
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This class represents lattice values for constants.
Definition: AllocatorList.h:23
VersionTuple(unsigned Major, unsigned Minor)
Definition: VersionTuple.h:47
block Block Frequency true
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
friend bool operator!=(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are not equivalent.
Definition: VersionTuple.h:108
friend bool operator>(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows another.
Definition: VersionTuple.h:125
VersionTuple(unsigned Major)
Definition: VersionTuple.h:43
VersionTuple withoutBuild() const
Return a version tuple that contains only the first 3 version components.
Definition: VersionTuple.h:91
friend bool operator==(const VersionTuple &X, const VersionTuple &Y)
Determine if two version numbers are equivalent.
Definition: VersionTuple.h:99
friend bool operator>=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number follows or is equivalent to another.
Definition: VersionTuple.h:143
friend bool operator<=(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes or is equivalent to another.
Definition: VersionTuple.h:134
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:67
friend bool operator<(const VersionTuple &X, const VersionTuple &Y)
Determine whether one version number precedes another.
Definition: VersionTuple.h:116
VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
Definition: VersionTuple.h:51
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:70
bool tryParse(StringRef string)
Try to parse the given string as a version number.
Optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition: VersionTuple.h:84
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
std::string getAsString() const
Retrieve a string representation of the version number.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2096
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition: VersionTuple.h:77
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero)...
Definition: VersionTuple.h:62