Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:35

0001 //===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// Defines the llvm::VersionTuple class, which represents a version in
0011 /// the form major[.minor[.subminor]].
0012 ///
0013 //===----------------------------------------------------------------------===//
0014 #ifndef LLVM_SUPPORT_VERSIONTUPLE_H
0015 #define LLVM_SUPPORT_VERSIONTUPLE_H
0016 
0017 #include "llvm/ADT/DenseMapInfo.h"
0018 #include "llvm/ADT/Hashing.h"
0019 #include <optional>
0020 #include <string>
0021 #include <tuple>
0022 
0023 namespace llvm {
0024 template <typename HasherT, llvm::endianness Endianness> class HashBuilder;
0025 class raw_ostream;
0026 class StringRef;
0027 
0028 /// Represents a version number in the form major[.minor[.subminor[.build]]].
0029 class VersionTuple {
0030   unsigned Major : 32;
0031 
0032   unsigned Minor : 31;
0033   unsigned HasMinor : 1;
0034 
0035   unsigned Subminor : 31;
0036   unsigned HasSubminor : 1;
0037 
0038   unsigned Build : 31;
0039   unsigned HasBuild : 1;
0040 
0041 public:
0042   constexpr VersionTuple()
0043       : Major(0), Minor(0), HasMinor(false), Subminor(0), HasSubminor(false),
0044         Build(0), HasBuild(false) {}
0045 
0046   explicit constexpr VersionTuple(unsigned Major)
0047       : Major(Major), Minor(0), HasMinor(false), Subminor(0),
0048         HasSubminor(false), Build(0), HasBuild(false) {}
0049 
0050   explicit constexpr VersionTuple(unsigned Major, unsigned Minor)
0051       : Major(Major), Minor(Minor), HasMinor(true), Subminor(0),
0052         HasSubminor(false), Build(0), HasBuild(false) {}
0053 
0054   explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
0055                                   unsigned Subminor)
0056       : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
0057         HasSubminor(true), Build(0), HasBuild(false) {}
0058 
0059   explicit constexpr VersionTuple(unsigned Major, unsigned Minor,
0060                                   unsigned Subminor, unsigned Build)
0061       : Major(Major), Minor(Minor), HasMinor(true), Subminor(Subminor),
0062         HasSubminor(true), Build(Build), HasBuild(true) {}
0063 
0064   /// Determine whether this version information is empty
0065   /// (e.g., all version components are zero).
0066   bool empty() const {
0067     return Major == 0 && Minor == 0 && Subminor == 0 && Build == 0;
0068   }
0069 
0070   /// Retrieve the major version number.
0071   unsigned getMajor() const { return Major; }
0072 
0073   /// Retrieve the minor version number, if provided.
0074   std::optional<unsigned> getMinor() const {
0075     if (!HasMinor)
0076       return std::nullopt;
0077     return Minor;
0078   }
0079 
0080   /// Retrieve the subminor version number, if provided.
0081   std::optional<unsigned> getSubminor() const {
0082     if (!HasSubminor)
0083       return std::nullopt;
0084     return Subminor;
0085   }
0086 
0087   /// Retrieve the build version number, if provided.
0088   std::optional<unsigned> getBuild() const {
0089     if (!HasBuild)
0090       return std::nullopt;
0091     return Build;
0092   }
0093 
0094   /// Return a version tuple that contains only the first 3 version components.
0095   VersionTuple withoutBuild() const {
0096     if (HasBuild)
0097       return VersionTuple(Major, Minor, Subminor);
0098     return *this;
0099   }
0100 
0101   /// Return a version tuple that contains a different major version but
0102   /// everything else is the same.
0103   VersionTuple withMajorReplaced(unsigned NewMajor) const {
0104     return VersionTuple(NewMajor, Minor, Subminor, Build);
0105   }
0106 
0107   /// Return a version tuple that contains only components that are non-zero.
0108   VersionTuple normalize() const {
0109     VersionTuple Result = *this;
0110     if (Result.Build == 0) {
0111       Result.HasBuild = false;
0112       if (Result.Subminor == 0) {
0113         Result.HasSubminor = false;
0114         if (Result.Minor == 0)
0115           Result.HasMinor = false;
0116       }
0117     }
0118     return Result;
0119   }
0120 
0121   /// Determine if two version numbers are equivalent. If not
0122   /// provided, minor and subminor version numbers are considered to be zero.
0123   friend bool operator==(const VersionTuple &X, const VersionTuple &Y) {
0124     return X.Major == Y.Major && X.Minor == Y.Minor &&
0125            X.Subminor == Y.Subminor && X.Build == Y.Build;
0126   }
0127 
0128   /// Determine if two version numbers are not equivalent.
0129   ///
0130   /// If not provided, minor and subminor version numbers are considered to be
0131   /// zero.
0132   friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
0133     return !(X == Y);
0134   }
0135 
0136   /// Determine whether one version number precedes another.
0137   ///
0138   /// If not provided, minor and subminor version numbers are considered to be
0139   /// zero.
0140   friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
0141     return std::tie(X.Major, X.Minor, X.Subminor, X.Build) <
0142            std::tie(Y.Major, Y.Minor, Y.Subminor, Y.Build);
0143   }
0144 
0145   /// Determine whether one version number follows another.
0146   ///
0147   /// If not provided, minor and subminor version numbers are considered to be
0148   /// zero.
0149   friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
0150     return Y < X;
0151   }
0152 
0153   /// Determine whether one version number precedes or is
0154   /// equivalent to another.
0155   ///
0156   /// If not provided, minor and subminor version numbers are considered to be
0157   /// zero.
0158   friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
0159     return !(Y < X);
0160   }
0161 
0162   /// Determine whether one version number follows or is
0163   /// equivalent to another.
0164   ///
0165   /// If not provided, minor and subminor version numbers are considered to be
0166   /// zero.
0167   friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) {
0168     return !(X < Y);
0169   }
0170 
0171   friend hash_code hash_value(const VersionTuple &VT) {
0172     return hash_combine(VT.Major, VT.Minor, VT.Subminor, VT.Build);
0173   }
0174 
0175   template <typename HasherT, llvm::endianness Endianness>
0176   friend void addHash(HashBuilder<HasherT, Endianness> &HBuilder,
0177                       const VersionTuple &VT) {
0178     HBuilder.add(VT.Major, VT.Minor, VT.Subminor, VT.Build);
0179   }
0180 
0181   /// Retrieve a string representation of the version number.
0182   std::string getAsString() const;
0183 
0184   /// Try to parse the given string as a version number.
0185   /// \returns \c true if the string does not match the regular expression
0186   ///   [0-9]+(\.[0-9]+){0,3}
0187   bool tryParse(StringRef string);
0188 };
0189 
0190 /// Print a version number.
0191 raw_ostream &operator<<(raw_ostream &Out, const VersionTuple &V);
0192 
0193 // Provide DenseMapInfo for version tuples.
0194 template <> struct DenseMapInfo<VersionTuple> {
0195   static inline VersionTuple getEmptyKey() { return VersionTuple(0x7FFFFFFF); }
0196   static inline VersionTuple getTombstoneKey() {
0197     return VersionTuple(0x7FFFFFFE);
0198   }
0199   static unsigned getHashValue(const VersionTuple &Value) {
0200     unsigned Result = Value.getMajor();
0201     if (auto Minor = Value.getMinor())
0202       Result = detail::combineHashValue(Result, *Minor);
0203     if (auto Subminor = Value.getSubminor())
0204       Result = detail::combineHashValue(Result, *Subminor);
0205     if (auto Build = Value.getBuild())
0206       Result = detail::combineHashValue(Result, *Build);
0207 
0208     return Result;
0209   }
0210 
0211   static bool isEqual(const VersionTuple &LHS, const VersionTuple &RHS) {
0212     return LHS == RHS;
0213   }
0214 };
0215 
0216 } // end namespace llvm
0217 #endif // LLVM_SUPPORT_VERSIONTUPLE_H