LLVM  14.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 
19 using namespace llvm;
20 
21 std::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 (Optional<unsigned> Minor = V.getMinor())
33  Out << '.' << *Minor;
34  if (Optional<unsigned> Subminor = V.getSubminor())
35  Out << '.' << *Subminor;
36  if (Optional<unsigned> Build = V.getBuild())
37  Out << '.' << *Build;
38  return Out;
39 }
40 
41 static bool parseInt(StringRef &input, unsigned &value) {
42  assert(value == 0);
43  if (input.empty())
44  return true;
45 
46  char next = input[0];
47  input = input.substr(1);
48  if (next < '0' || next > '9')
49  return true;
50  value = (unsigned)(next - '0');
51 
52  while (!input.empty()) {
53  next = input[0];
54  if (next < '0' || next > '9')
55  return false;
56  input = input.substr(1);
57  value = value * 10 + (unsigned)(next - '0');
58  }
59 
60  return false;
61 }
62 
64  unsigned major = 0, minor = 0, micro = 0, build = 0;
65 
66  // Parse the major version, [0-9]+
67  if (parseInt(input, major))
68  return true;
69 
70  if (input.empty()) {
71  *this = VersionTuple(major);
72  return false;
73  }
74 
75  // If we're not done, parse the minor version, \.[0-9]+
76  if (input[0] != '.')
77  return true;
78  input = input.substr(1);
79  if (parseInt(input, minor))
80  return true;
81 
82  if (input.empty()) {
83  *this = VersionTuple(major, minor);
84  return false;
85  }
86 
87  // If we're not done, parse the micro version, \.[0-9]+
88  if (input[0] != '.')
89  return true;
90  input = input.substr(1);
91  if (parseInt(input, micro))
92  return true;
93 
94  if (input.empty()) {
95  *this = VersionTuple(major, minor, micro);
96  return false;
97  }
98 
99  // If we're not done, parse the micro version, \.[0-9]+
100  if (input[0] != '.')
101  return true;
102  input = input.substr(1);
103  if (parseInt(input, build))
104  return true;
105 
106  // If we have characters left over, it's an error.
107  if (!input.empty())
108  return true;
109 
110  *this = VersionTuple(major, minor, micro, build);
111  return false;
112 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::VersionTuple::tryParse
bool tryParse(StringRef string)
Try to parse the given string as a version number.
Definition: VersionTuple.cpp:63
StringRef.h
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:625
VersionTuple.h
llvm::Optional< unsigned >
llvm::VersionTuple::getMajor
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:70
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
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:29
input
The initial backend is deliberately restricted to z10 We should add support for later architectures at some point If an asm ties an i32 r result to an i64 input
Definition: README.txt:10
llvm::VersionTuple::getSubminor
Optional< unsigned > getSubminor() const
Retrieve the subminor version number, if provided.
Definition: VersionTuple.h:80
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::VersionTuple::getMinor
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:73
llvm::VersionTuple::getBuild
Optional< unsigned > getBuild() const
Retrieve the build version number, if provided.
Definition: VersionTuple.h:87
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
parseInt
static bool parseInt(StringRef &input, unsigned &value)
Definition: VersionTuple.cpp:41
llvm::VersionTuple::getAsString
std::string getAsString() const
Retrieve a string representation of the version number.
Definition: VersionTuple.cpp:21
llvm::VersionTuple::VersionTuple
VersionTuple()
Definition: VersionTuple.h:42
raw_ostream.h
llvm::irsymtab::build
Error build(ArrayRef< Module * > Mods, SmallVector< char, 0 > &Symtab, StringTableBuilder &StrtabBuilder, BumpPtrAllocator &Alloc)
Fills in Symtab and StrtabBuilder with a valid symbol and string table for Mods.
Definition: IRSymtab.cpp:361