LLVM 23.0.0git
VersionTuple.cpp
Go to the documentation of this file.
1//===- VersionTuple.cpp - 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// This file implements the VersionTuple class, which represents a version in
10// the form major[.minor[.subminor]].
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/StringRef.h"
17#include <cassert>
18
19using namespace llvm;
20
21std::string VersionTuple::getAsString() const {
22 std::string Result;
23 {
24 llvm::raw_string_ostream Out(Result);
25 Out << *this;
26 }
27 return Result;
28}
29
31 Out << V.getMajor();
32 if (std::optional<unsigned> Minor = V.getMinor())
33 Out << '.' << *Minor;
34 if (std::optional<unsigned> Subminor = V.getSubminor())
35 Out << '.' << *Subminor;
36 if (std::optional<unsigned> Build = V.getBuild())
37 Out << '.' << *Build;
38 if (std::optional<unsigned> Subbuild = V.getSubbuild())
39 Out << '.' << *Subbuild;
40 return Out;
41}
42
43static bool parseInt(StringRef &input, unsigned &value) {
44 assert(value == 0);
45 if (input.empty())
46 return true;
47
48 char next = input[0];
49 input = input.substr(1);
50 if (next < '0' || next > '9')
51 return true;
52 value = (unsigned)(next - '0');
53
54 while (!input.empty()) {
55 next = input[0];
56 if (next < '0' || next > '9')
57 return false;
58 input = input.substr(1);
59 value = value * 10 + (unsigned)(next - '0');
60 }
61
62 return false;
63}
64
66 unsigned major = 0, minor = 0, subminor = 0, build = 0, subbuild = 0;
67
68 // Parse the major version, [0-9]+
69 if (parseInt(input, major))
70 return true;
71
72 if (input.empty()) {
73 *this = VersionTuple(major);
74 return false;
75 }
76
77 // If we're not done, parse the minor version, \.[0-9]+
78 if (input[0] != '.')
79 return true;
80 input = input.substr(1);
81 if (parseInt(input, minor))
82 return true;
83
84 if (input.empty()) {
85 *this = VersionTuple(major, minor);
86 return false;
87 }
88
89 // If we're not done, parse the subminor version, \.[0-9]+
90 if (!input.consume_front("."))
91 return true;
92 if (parseInt(input, subminor))
93 return true;
94
95 if (input.empty()) {
96 *this = VersionTuple(major, minor, subminor);
97 return false;
98 }
99
100 // If we're not done, parse the build version, \.[0-9]+
101 if (!input.consume_front("."))
102 return true;
103 if (parseInt(input, build))
104 return true;
105 if (build >= 1024 * 1024)
106 return true;
107
108 if (input.empty()) {
109 *this = VersionTuple(major, minor, subminor, build);
110 return false;
111 }
112
113 // And the subbuild version, \.[0-9]+
114 if (!input.consume_front("."))
115 return true;
116 if (parseInt(input, subbuild))
117 return true;
118 if (subbuild >= 1024)
119 return true;
120
121 // If we have characters left over, it's an error.
122 if (!input.empty())
123 return true;
124
125 *this = VersionTuple(major, minor, subminor, build, subbuild);
126 return false;
127}
128
130 if (HasSubbuild)
131 return VersionTuple(NewMajor, Minor, Subminor, Build, Subbuild);
132 if (HasBuild)
133 return VersionTuple(NewMajor, Minor, Subminor, Build);
134 if (HasSubminor)
135 return VersionTuple(NewMajor, Minor, Subminor);
136 if (HasMinor)
137 return VersionTuple(NewMajor, Minor);
138 return VersionTuple(NewMajor);
139}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static bool parseInt(StringRef &input, unsigned &value)
Defines the llvm::VersionTuple class, which represents a version in the form major[....
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:591
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:655
Represents a version number in the form major[.minor[.subminor[.build]]].
LLVM_ABI bool tryParse(StringRef string)
Try to parse the given string as a version number.
LLVM_ABI std::string getAsString() const
Retrieve a string representation of the version number.
LLVM_ABI VersionTuple withMajorReplaced(unsigned NewMajor) const
Return a version tuple that contains a different major version but everything else is the same.
constexpr VersionTuple()
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)