clang API Documentation
00001 //===- VersionTuple.h - Version Number Handling -----------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief Defines the clang::VersionTuple class, which represents a version in 00012 /// the form major[.minor[.subminor]]. 00013 /// 00014 //===----------------------------------------------------------------------===// 00015 #ifndef LLVM_CLANG_BASIC_VERSIONTUPLE_H 00016 #define LLVM_CLANG_BASIC_VERSIONTUPLE_H 00017 00018 #include "clang/Basic/LLVM.h" 00019 #include "llvm/ADT/Optional.h" 00020 #include <string> 00021 #include <tuple> 00022 00023 namespace clang { 00024 00025 /// \brief Represents a version number in the form major[.minor[.subminor]]. 00026 class VersionTuple { 00027 unsigned Major : 31; 00028 unsigned Minor : 31; 00029 unsigned Subminor : 31; 00030 unsigned HasMinor : 1; 00031 unsigned HasSubminor : 1; 00032 unsigned UsesUnderscores : 1; 00033 00034 public: 00035 VersionTuple() 00036 : Major(0), Minor(0), Subminor(0), HasMinor(false), HasSubminor(false), 00037 UsesUnderscores(false) { } 00038 00039 explicit VersionTuple(unsigned Major) 00040 : Major(Major), Minor(0), Subminor(0), HasMinor(false), HasSubminor(false), 00041 UsesUnderscores(false) 00042 { } 00043 00044 explicit VersionTuple(unsigned Major, unsigned Minor, 00045 bool UsesUnderscores = false) 00046 : Major(Major), Minor(Minor), Subminor(0), HasMinor(true), 00047 HasSubminor(false), UsesUnderscores(UsesUnderscores) 00048 { } 00049 00050 explicit VersionTuple(unsigned Major, unsigned Minor, unsigned Subminor, 00051 bool UsesUnderscores = false) 00052 : Major(Major), Minor(Minor), Subminor(Subminor), HasMinor(true), 00053 HasSubminor(true), UsesUnderscores(UsesUnderscores) 00054 { } 00055 00056 /// \brief Determine whether this version information is empty 00057 /// (e.g., all version components are zero). 00058 bool empty() const { return Major == 0 && Minor == 0 && Subminor == 0; } 00059 00060 /// \brief Retrieve the major version number. 00061 unsigned getMajor() const { return Major; } 00062 00063 /// \brief Retrieve the minor version number, if provided. 00064 Optional<unsigned> getMinor() const { 00065 if (!HasMinor) 00066 return None; 00067 return Minor; 00068 } 00069 00070 /// \brief Retrieve the subminor version number, if provided. 00071 Optional<unsigned> getSubminor() const { 00072 if (!HasSubminor) 00073 return None; 00074 return Subminor; 00075 } 00076 00077 bool usesUnderscores() const { 00078 return UsesUnderscores; 00079 } 00080 00081 void UseDotAsSeparator() { 00082 UsesUnderscores = false; 00083 } 00084 00085 /// \brief Determine if two version numbers are equivalent. If not 00086 /// provided, minor and subminor version numbers are considered to be zero. 00087 friend bool operator==(const VersionTuple& X, const VersionTuple &Y) { 00088 return X.Major == Y.Major && X.Minor == Y.Minor && X.Subminor == Y.Subminor; 00089 } 00090 00091 /// \brief Determine if two version numbers are not equivalent. 00092 /// 00093 /// If not provided, minor and subminor version numbers are considered to be 00094 /// zero. 00095 friend bool operator!=(const VersionTuple &X, const VersionTuple &Y) { 00096 return !(X == Y); 00097 } 00098 00099 /// \brief Determine whether one version number precedes another. 00100 /// 00101 /// If not provided, minor and subminor version numbers are considered to be 00102 /// zero. 00103 friend bool operator<(const VersionTuple &X, const VersionTuple &Y) { 00104 return std::tie(X.Major, X.Minor, X.Subminor) < 00105 std::tie(Y.Major, Y.Minor, Y.Subminor); 00106 } 00107 00108 /// \brief Determine whether one version number follows another. 00109 /// 00110 /// If not provided, minor and subminor version numbers are considered to be 00111 /// zero. 00112 friend bool operator>(const VersionTuple &X, const VersionTuple &Y) { 00113 return Y < X; 00114 } 00115 00116 /// \brief Determine whether one version number precedes or is 00117 /// equivalent to another. 00118 /// 00119 /// If not provided, minor and subminor version numbers are considered to be 00120 /// zero. 00121 friend bool operator<=(const VersionTuple &X, const VersionTuple &Y) { 00122 return !(Y < X); 00123 } 00124 00125 /// \brief Determine whether one version number follows or is 00126 /// equivalent to another. 00127 /// 00128 /// If not provided, minor and subminor version numbers are considered to be 00129 /// zero. 00130 friend bool operator>=(const VersionTuple &X, const VersionTuple &Y) { 00131 return !(X < Y); 00132 } 00133 00134 /// \brief Retrieve a string representation of the version number. 00135 std::string getAsString() const; 00136 00137 /// \brief Try to parse the given string as a version number. 00138 /// \returns \c true if the string does not match the regular expression 00139 /// [0-9]+(\.[0-9]+(\.[0-9]+)) 00140 bool tryParse(StringRef string); 00141 }; 00142 00143 /// \brief Print a version number. 00144 raw_ostream& operator<<(raw_ostream &Out, const VersionTuple &V); 00145 00146 } // end namespace clang 00147 #endif // LLVM_CLANG_BASIC_VERSIONTUPLE_H