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 /// \brief Defines the clang::VersionTuple class, which represents a version in
12 /// the form major[.minor[.subminor]].
14 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_BASIC_VERSIONTUPLE_H
16 #define LLVM_CLANG_BASIC_VERSIONTUPLE_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/ADT/Optional.h"
25 /// \brief Represents a version number in the form major[.minor[.subminor[.build]]].
29 unsigned UsesUnderscores : 1;
32 unsigned HasMinor : 1;
34 unsigned Subminor : 31;
35 unsigned HasSubminor : 1;
38 unsigned HasBuild : 1;
42 : Major(0), UsesUnderscores(false), Minor(0), HasMinor(false),
43 Subminor(0), HasSubminor(false), Build(0), HasBuild(false) {}
45 explicit VersionTuple(unsigned Major)
46 : Major(Major), UsesUnderscores(false), Minor(0), HasMinor(false),
47 Subminor(0), HasSubminor(false), Build(0), HasBuild(false) {}
49 explicit VersionTuple(unsigned Major, unsigned Minor,
50 bool UsesUnderscores = false)
51 : Major(Major), UsesUnderscores(UsesUnderscores), Minor(Minor),
52 HasMinor(true), Subminor(0), HasSubminor(false), Build(0),
55 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
56 bool UsesUnderscores = false)
57 : Major(Major), UsesUnderscores(UsesUnderscores), Minor(Minor),
58 HasMinor(true), Subminor(Subminor), HasSubminor(true), Build(0),
61 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
62 unsigned Build, bool UsesUnderscores = false)
63 : Major(Major), UsesUnderscores(UsesUnderscores), Minor(Minor),
64 HasMinor(true), Subminor(Subminor), HasSubminor(true), Build(Build),
67 /// \brief Determine whether this version information is empty
68 /// (e.g., all version components are zero).
70 return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
73 /// \brief Retrieve the major version number.
74 unsigned getMajor() const { return Major; }
76 /// \brief Retrieve the minor version number, if provided.
77 Optional<unsigned> getMinor() const {
83 /// \brief Retrieve the subminor version number, if provided.
84 Optional<unsigned> getSubminor() const {
90 /// \brief Retrieve the build version number, if provided.
91 Optional<unsigned> getBuild() const {
97 bool usesUnderscores() const {
98 return UsesUnderscores;
101 void UseDotAsSeparator() {
102 UsesUnderscores = false;
105 /// \brief Determine if two version numbers are equivalent. If not
106 /// provided, minor and subminor version numbers are considered to be zero.
107 friend bool operator==(const VersionTuple& X, const VersionTuple &Y) {
108 return X.Major == Y.Major && X.Minor == Y.Minor &&
109 X.Subminor == Y.Subminor && X.Build == Y.Build;
112 /// \brief Determine if two version numbers are not equivalent.
114 /// If not provided, minor and subminor version numbers are considered to be
116 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
120 /// \brief Determine whether one version number precedes another.
122 /// If not provided, minor and subminor version numbers are considered to be
124 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
125 return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
126 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
129 /// \brief Determine whether one version number follows another.
131 /// If not provided, minor and subminor version numbers are considered to be
133 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
137 /// \brief Determine whether one version number precedes or is
138 /// equivalent to another.
140 /// If not provided, minor and subminor version numbers are considered to be
142 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
146 /// \brief Determine whether one version number follows or is
147 /// equivalent to another.
149 /// If not provided, minor and subminor version numbers are considered to be
151 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
155 /// \brief Retrieve a string representation of the version number.
156 std::string getAsString() const;
158 /// \brief Try to parse the given string as a version number.
159 /// \returns \c true if the string does not match the regular expression
160 /// [0-9]+(\.[0-9]+){0,3}
161 bool tryParse(StringRef string);
164 /// \brief Print a version number.
165 raw_ostream& operator<<(raw_ostream &Out, const VersionTuple &V);
167 } // end namespace clang
168 #endif // LLVM_CLANG_BASIC_VERSIONTUPLE_H