Home | History | Annotate | Download | only in Basic
      1 //===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 ///
     10 /// \file
     11 /// \brief Defines the clang::VersionTuple class, which represents a version in
     12 /// the form major[.minor[.subminor]].
     13 ///
     14 //===----------------------------------------------------------------------===//
     15 #ifndef LLVM_CLANG_BASIC_VERSIONTUPLE_H
     16 #define LLVM_CLANG_BASIC_VERSIONTUPLE_H
     17 
     18 #include "clang/Basic/LLVM.h"
     19 #include "llvm/ADT/Optional.h"
     20 #include <string>
     21 
     22 namespace clang {
     23 
     24 /// \brief Represents a version number in the form major[.minor[.subminor]].
     25 class VersionTuple {
     26   unsigned Major;
     27   unsigned Minor : 31;
     28   unsigned Subminor : 31;
     29   unsigned HasMinor : 1;
     30   unsigned HasSubminor : 1;
     31 
     32 public:
     33   VersionTuple()
     34     : Major(0), Minor(0), Subminor(0), HasMinor(false), HasSubminor(false) { }
     35 
     36   explicit VersionTuple(unsigned Major)
     37     : Major(Major), Minor(0), Subminor(0), HasMinor(false), HasSubminor(false)
     38   { }
     39 
     40   explicit VersionTuple(unsigned Major, unsigned Minor)
     41     : Major(Major), Minor(Minor), Subminor(0), HasMinor(true),
     42       HasSubminor(false)
     43   { }
     44 
     45   explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor)
     46     : Major(Major), Minor(Minor), Subminor(Subminor), HasMinor(true),
     47       HasSubminor(true)
     48   { }
     49 
     50   /// \brief Determine whether this version information is empty
     51   /// (e.g., all version components are zero).
     52   bool empty() const { return Major == 0 && Minor == 0 && Subminor == 0; }
     53 
     54   /// \brief Retrieve the major version number.
     55   unsigned getMajor() const { return Major; }
     56 
     57   /// \brief Retrieve the minor version number, if provided.
     58   Optional<unsigned> getMinor() const {
     59     if (!HasMinor)
     60       return None;
     61     return Minor;
     62   }
     63 
     64   /// \brief Retrieve the subminor version number, if provided.
     65   Optional<unsigned> getSubminor() const {
     66     if (!HasSubminor)
     67       return None;
     68     return Subminor;
     69   }
     70 
     71   /// \brief Determine if two version numbers are equivalent. If not
     72   /// provided, minor and subminor version numbers are considered to be zero.
     73   friend bool operator==(const VersionTuple& X, const VersionTuple &Y) {
     74     return X.Major == Y.Major && X.Minor == Y.Minor && X.Subminor == Y.Subminor;
     75   }
     76 
     77   /// \brief Determine if two version numbers are not equivalent.
     78   ///
     79   /// If not provided, minor and subminor version numbers are considered to be
     80   /// zero.
     81   friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) {
     82     return !(X == Y);
     83   }
     84 
     85   /// \brief Determine whether one version number precedes another.
     86   ///
     87   /// If not provided, minor and subminor version numbers are considered to be
     88   /// zero.
     89   friend bool operator<(const VersionTuple &X, const VersionTuple &Y) {
     90     if (X.Major != Y.Major)
     91       return X.Major < Y.Major;
     92 
     93     if (X.Minor != Y.Minor)
     94       return X.Minor < Y.Minor;
     95 
     96     return X.Subminor < Y.Subminor;
     97   }
     98 
     99   /// \brief Determine whether one version number follows another.
    100   ///
    101   /// If not provided, minor and subminor version numbers are considered to be
    102   /// zero.
    103   friend bool operator>(const VersionTuple &X, const VersionTuple &Y) {
    104     return Y < X;
    105   }
    106 
    107   /// \brief Determine whether one version number precedes or is
    108   /// equivalent to another.
    109   ///
    110   /// If not provided, minor and subminor version numbers are considered to be
    111   /// zero.
    112   friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) {
    113     return !(Y < X);
    114   }
    115 
    116   /// \brief 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 
    125   /// \brief Retrieve a string representation of the version number.
    126   std::string getAsString() const;
    127 
    128   /// \brief Try to parse the given string as a version number.
    129   /// \returns \c true if the string does not match the regular expression
    130   ///   [0-9]+(\.[0-9]+(\.[0-9]+))
    131   bool tryParse(StringRef string);
    132 };
    133 
    134 /// \brief Print a version number.
    135 raw_ostream& operator<<(raw_ostream &Out, const VersionTuple &V);
    136 
    137 } // end namespace clang
    138 #endif // LLVM_CLANG_BASIC_VERSIONTUPLE_H
    139