llvm.org GIT mirror llvm / 802e2f3
Move VersionTuple from clang/Basic to llvm/Support Summary: This kind of functionality is useful to other project apart from clang. LLDB works with version numbers a lot, but it does not have a convenient abstraction for this. Moving this class to a lower level library allows it to be freely used within LLDB. Since this class is used in a lot of places in clang, and it used to be in the clang namespace, it seemed appropriate to add it to the list of adopted classes in LLVM.h to avoid prefixing all uses with "llvm::". Also, I didn't find any tests specific for this class, so I wrote a couple of quick ones for the more interesting bits of functionality. Reviewers: zturner, erik.pilkington Subscribers: mgorny, cfe-commits, llvm-commits Differential Revision: https://reviews.llvm.org/D47887 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@334399 91177308-0d34-0410-b5e6-96231b3b80d8 Pavel Labath 1 year, 4 months ago
5 changed file(s) with 316 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
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"
19 #include "llvm/Support/raw_ostream.h"
20 #include
21 #include
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:
39 VersionTuple()
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.
70 Optional getMinor() const {
71 if (!HasMinor)
72 return None;
73 return Minor;
74 }
75
76 /// Retrieve the subminor version number, if provided.
77 Optional getSubminor() const {
78 if (!HasSubminor)
79 return None;
80 return Subminor;
81 }
82
83 /// Retrieve the build version number, if provided.
84 Optional getBuild() const {
85 if (!HasBuild)
86 return None;
87 return Build;
88 }
89
90 /// Determine if two version numbers are equivalent. If not
91 /// provided, minor and subminor version numbers are considered to be zero.
92 friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
93 return X.Major == Y.Major && X.Minor == Y.Minor &&
94 X.Subminor == Y.Subminor && X.Build == Y.Build;
95 }
96
97 /// Determine if two version numbers are not equivalent.
98 ///
99 /// If not provided, minor and subminor version numbers are considered to be
100 /// zero.
101 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
102 return !(X == Y);
103 }
104
105 /// Determine whether one version number precedes another.
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 std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
111 std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
112 }
113
114 /// Determine whether one version number follows another.
115 ///
116 /// If not provided, minor and subminor version numbers are considered to be
117 /// zero.
118 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
119 return Y < X;
120 }
121
122 /// Determine whether one version number precedes or is
123 /// equivalent to another.
124 ///
125 /// If not provided, minor and subminor version numbers are considered to be
126 /// zero.
127 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
128 return !(Y < X);
129 }
130
131 /// Determine whether one version number follows or is
132 /// equivalent to another.
133 ///
134 /// If not provided, minor and subminor version numbers are considered to be
135 /// zero.
136 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
137 return !(X < Y);
138 }
139
140 /// Retrieve a string representation of the version number.
141 std::string getAsString() const;
142
143 /// Try to parse the given string as a version number.
144 /// \returns \c true if the string does not match the regular expression
145 /// [0-9]+(\.[0-9]+){0,3}
146 bool tryParse(StringRef string);
147 };
148
149 /// Print a version number.
150 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
151
152 } // end namespace llvm
153 #endif // LLVM_SUPPORT_VERSIONTUPLE_H
123123 Twine.cpp
124124 Unicode.cpp
125125 UnicodeCaseFold.cpp
126 VersionTuple.cpp
126127 WithColor.cpp
127128 YAMLParser.cpp
128129 YAMLTraits.cpp
0 //===- VersionTuple.cpp - Version Number Handling ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the VersionTuple class, which represents a version in
10 // the form major[.minor[.subminor]].
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/VersionTuple.h"
14 #include "llvm/Support/raw_ostream.h"
15
16 using namespace llvm;
17
18 std::string VersionTuple::getAsString() const {
19 std::string Result;
20 {
21 llvm::raw_string_ostream Out(Result);
22 Out << *this;
23 }
24 return Result;
25 }
26
27 raw_ostream &llvm::operator<<(raw_ostream &Out, const VersionTuple &V) {
28 Out << V.getMajor();
29 if (Optional Minor = V.getMinor())
30 Out << '.' << *Minor;
31 if (Optional Subminor = V.getSubminor())
32 Out << '.' << *Subminor;
33 if (Optional Build = V.getBuild())
34 Out << '.' << *Build;
35 return Out;
36 }
37
38 static bool parseInt(StringRef &input, unsigned &value) {
39 assert(value == 0);
40 if (input.empty())
41 return true;
42
43 char next = input[0];
44 input = input.substr(1);
45 if (next < '0' || next > '9')
46 return true;
47 value = (unsigned)(next - '0');
48
49 while (!input.empty()) {
50 next = input[0];
51 if (next < '0' || next > '9')
52 return false;
53 input = input.substr(1);
54 value = value * 10 + (unsigned)(next - '0');
55 }
56
57 return false;
58 }
59
60 bool VersionTuple::tryParse(StringRef input) {
61 unsigned major = 0, minor = 0, micro = 0, build = 0;
62
63 // Parse the major version, [0-9]+
64 if (parseInt(input, major))
65 return true;
66
67 if (input.empty()) {
68 *this = VersionTuple(major);
69 return false;
70 }
71
72 // If we're not done, parse the minor version, \.[0-9]+
73 if (input[0] != '.')
74 return true;
75 input = input.substr(1);
76 if (parseInt(input, minor))
77 return true;
78
79 if (input.empty()) {
80 *this = VersionTuple(major, minor);
81 return false;
82 }
83
84 // If we're not done, parse the micro version, \.[0-9]+
85 if (input[0] != '.')
86 return true;
87 input = input.substr(1);
88 if (parseInt(input, micro))
89 return true;
90
91 if (input.empty()) {
92 *this = VersionTuple(major, minor, micro);
93 return false;
94 }
95
96 // If we're not done, parse the micro version, \.[0-9]+
97 if (input[0] != '.')
98 return true;
99 input = input.substr(1);
100 if (parseInt(input, build))
101 return true;
102
103 // If we have characters left over, it's an error.
104 if (!input.empty())
105 return true;
106
107 *this = VersionTuple(major, minor, micro, build);
108 return false;
109 }
6060 TrailingObjectsTest.cpp
6161 TrigramIndexTest.cpp
6262 UnicodeTest.cpp
63 VersionTupleTest.cpp
6364 YAMLIOTest.cpp
6465 YAMLParserTest.cpp
6566 formatted_raw_ostream_test.cpp
0 //===- VersionTupleTests.cpp - Version Number Handling Tests --------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Support/VersionTuple.h"
10 #include "gtest/gtest.h"
11
12 using namespace llvm;
13
14 TEST(VersionTuple, getAsString) {
15 EXPECT_EQ("0", VersionTuple().getAsString());
16 EXPECT_EQ("1", VersionTuple(1).getAsString());
17 EXPECT_EQ("1.2", VersionTuple(1, 2).getAsString());
18 EXPECT_EQ("1.2.3", VersionTuple(1, 2, 3).getAsString());
19 EXPECT_EQ("1.2.3.4", VersionTuple(1, 2, 3, 4).getAsString());
20 }
21
22 TEST(VersionTuple, tryParse) {
23 VersionTuple VT;
24
25 EXPECT_FALSE(VT.tryParse("1"));
26 EXPECT_EQ("1", VT.getAsString());
27
28 EXPECT_FALSE(VT.tryParse("1.2"));
29 EXPECT_EQ("1.2", VT.getAsString());
30
31 EXPECT_FALSE(VT.tryParse("1.2.3"));
32 EXPECT_EQ("1.2.3", VT.getAsString());
33
34 EXPECT_FALSE(VT.tryParse("1.2.3.4"));
35 EXPECT_EQ("1.2.3.4", VT.getAsString());
36
37 EXPECT_TRUE(VT.tryParse(""));
38 EXPECT_TRUE(VT.tryParse("1."));
39 EXPECT_TRUE(VT.tryParse("1.2."));
40 EXPECT_TRUE(VT.tryParse("1.2.3."));
41 EXPECT_TRUE(VT.tryParse("1.2.3.4."));
42 EXPECT_TRUE(VT.tryParse("1.2.3.4.5"));
43 EXPECT_TRUE(VT.tryParse("1-2"));
44 EXPECT_TRUE(VT.tryParse("1+2"));
45 EXPECT_TRUE(VT.tryParse(".1"));
46 EXPECT_TRUE(VT.tryParse(" 1"));
47 EXPECT_TRUE(VT.tryParse("1 "));
48 EXPECT_TRUE(VT.tryParse("."));
49 }