VersionTuple.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "llvm/ADT/Hashing.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include <string>
  19. #include <tuple>
  20. namespace llvm {
  21. class raw_ostream;
  22. class StringRef;
  23. /// Represents a version number in the form major[.minor[.subminor[.build]]].
  24. class VersionTuple {
  25. unsigned Major : 32;
  26. unsigned Minor : 31;
  27. unsigned HasMinor : 1;
  28. unsigned Subminor : 31;
  29. unsigned HasSubminor : 1;
  30. unsigned Build : 31;
  31. unsigned HasBuild : 1;
  32. public:
  33. VersionTuple()
  34. : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
  35. Build(0), HasBuild(false) {}
  36. explicit VersionTuple(unsigned Major)
  37. : Major(Major), Minor(0), HasMinor(false), Subminor(0),
  38. HasSubminor(false), Build(0), HasBuild(false) {}
  39. explicit VersionTuple(unsigned Major, unsigned Minor)
  40. : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
  41. HasSubminor(false), Build(0), HasBuild(false) {}
  42. explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
  43. : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
  44. HasSubminor(true), Build(0), HasBuild(false) {}
  45. explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor,
  46. unsigned Build)
  47. : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
  48. HasSubminor(true), Build(Build), HasBuild(true) {}
  49. /// Determine whether this version information is empty
  50. /// (e.g., all version components are zero).
  51. bool empty() const {
  52. return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
  53. }
  54. /// Retrieve the major version number.
  55. unsigned getMajor() const { return Major; }
  56. /// Retrieve the minor version number, if provided.
  57. Optional<unsigned> getMinor() const {
  58. if (!HasMinor)
  59. return None;
  60. return Minor;
  61. }
  62. /// Retrieve the subminor version number, if provided.
  63. Optional<unsigned> getSubminor() const {
  64. if (!HasSubminor)
  65. return None;
  66. return Subminor;
  67. }
  68. /// Retrieve the build version number, if provided.
  69. Optional<unsigned> getBuild() const {
  70. if (!HasBuild)
  71. return None;
  72. return Build;
  73. }
  74. /// Return a version tuple that contains only the first 3 version components.
  75. VersionTuple withoutBuild() const {
  76. if (HasBuild)
  77. return VersionTuple(Major, Minor, Subminor);
  78. return *this;
  79. }
  80. /// Determine if two version numbers are equivalent. If not
  81. /// provided, minor and subminor version numbers are considered to be zero.
  82. friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
  83. return X.Major == Y.Major && X.Minor == Y.Minor &&
  84. X.Subminor == Y.Subminor && X.Build == Y.Build;
  85. }
  86. /// Determine if two version numbers are not equivalent.
  87. ///
  88. /// If not provided, minor and subminor version numbers are considered to be
  89. /// zero.
  90. friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
  91. return !(X == Y);
  92. }
  93. /// Determine whether one version number precedes another.
  94. ///
  95. /// If not provided, minor and subminor version numbers are considered to be
  96. /// zero.
  97. friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
  98. return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
  99. std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
  100. }
  101. /// Determine whether one version number follows another.
  102. ///
  103. /// If not provided, minor and subminor version numbers are considered to be
  104. /// zero.
  105. friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
  106. return Y < X;
  107. }
  108. /// Determine whether one version number precedes or is
  109. /// equivalent to another.
  110. ///
  111. /// If not provided, minor and subminor version numbers are considered to be
  112. /// zero.
  113. friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
  114. return !(Y < X);
  115. }
  116. /// Determine whether one version number follows or is
  117. /// equivalent to another.
  118. ///
  119. /// If not provided, minor and subminor version numbers are considered to be
  120. /// zero.
  121. friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
  122. return !(X < Y);
  123. }
  124. friend llvm::hash_code hash_value(const VersionTuple &VT) {
  125. return llvm::hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build);
  126. }
  127. /// Retrieve a string representation of the version number.
  128. std::string getAsString() const;
  129. /// Try to parse the given string as a version number.
  130. /// \returns \c true if the string does not match the regular expression
  131. /// [0-9]+(\.[0-9]+){0,3}
  132. bool tryParse(StringRef string);
  133. };
  134. /// Print a version number.
  135. raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
  136. } // end namespace llvm
  137. #endif // LLVM_SUPPORT_VERSIONTUPLE_H