clang API Documentation
00001 //===- Version.cpp - Clang Version Number -----------------------*- 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 // This file defines several version-related utility functions for Clang. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/Basic/Version.h" 00015 #include "clang/Basic/LLVM.h" 00016 #include "llvm/Support/raw_ostream.h" 00017 #include "llvm/Config/config.h" 00018 #include <cstring> 00019 #include <cstdlib> 00020 00021 namespace clang { 00022 00023 std::string getClangRepositoryPath() { 00024 #if defined(CLANG_REPOSITORY_STRING) 00025 return CLANG_REPOSITORY_STRING; 00026 #else 00027 #ifdef SVN_REPOSITORY 00028 StringRef URL(SVN_REPOSITORY); 00029 #else 00030 StringRef URL(""); 00031 #endif 00032 00033 // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us 00034 // pick up a tag in an SVN export, for example. 00035 static StringRef SVNRepository("$URL: file:///opt/svn-repos/llvm-project/cfe/trunk/lib/Basic/Version.cpp $"); 00036 if (URL.empty()) { 00037 URL = SVNRepository.slice(SVNRepository.find(':'), 00038 SVNRepository.find("/lib/Basic")); 00039 } 00040 00041 // Strip off version from a build from an integration branch. 00042 URL = URL.slice(0, URL.find("/src/tools/clang")); 00043 00044 // Trim path prefix off, assuming path came from standard cfe path. 00045 size_t Start = URL.find("cfe/"); 00046 if (Start != StringRef::npos) 00047 URL = URL.substr(Start + 4); 00048 00049 return URL; 00050 #endif 00051 } 00052 00053 std::string getLLVMRepositoryPath() { 00054 #ifdef LLVM_REPOSITORY 00055 StringRef URL(LLVM_REPOSITORY); 00056 #else 00057 StringRef URL(""); 00058 #endif 00059 00060 // Trim path prefix off, assuming path came from standard llvm path. 00061 // Leave "llvm/" prefix to distinguish the following llvm revision from the 00062 // clang revision. 00063 size_t Start = URL.find("llvm/"); 00064 if (Start != StringRef::npos) 00065 URL = URL.substr(Start); 00066 00067 return URL; 00068 } 00069 00070 std::string getClangRevision() { 00071 #ifdef SVN_REVISION 00072 return SVN_REVISION; 00073 #else 00074 return ""; 00075 #endif 00076 } 00077 00078 std::string getLLVMRevision() { 00079 #ifdef LLVM_REVISION 00080 return LLVM_REVISION; 00081 #else 00082 return ""; 00083 #endif 00084 } 00085 00086 std::string getClangFullRepositoryVersion() { 00087 std::string buf; 00088 llvm::raw_string_ostream OS(buf); 00089 std::string Path = getClangRepositoryPath(); 00090 std::string Revision = getClangRevision(); 00091 if (!Path.empty() || !Revision.empty()) { 00092 OS << '('; 00093 if (!Path.empty()) 00094 OS << Path; 00095 if (!Revision.empty()) { 00096 if (!Path.empty()) 00097 OS << ' '; 00098 OS << Revision; 00099 } 00100 OS << ')'; 00101 } 00102 // Support LLVM in a separate repository. 00103 std::string LLVMRev = getLLVMRevision(); 00104 if (!LLVMRev.empty() && LLVMRev != Revision) { 00105 OS << " ("; 00106 std::string LLVMRepo = getLLVMRepositoryPath(); 00107 if (!LLVMRepo.empty()) 00108 OS << LLVMRepo << ' '; 00109 OS << LLVMRev << ')'; 00110 } 00111 return OS.str(); 00112 } 00113 00114 std::string getClangFullVersion() { 00115 std::string buf; 00116 llvm::raw_string_ostream OS(buf); 00117 #ifdef CLANG_VENDOR 00118 OS << CLANG_VENDOR; 00119 #endif 00120 OS << "clang version " CLANG_VERSION_STRING " " 00121 << getClangFullRepositoryVersion(); 00122 00123 // If vendor supplied, include the base LLVM version as well. 00124 #ifdef CLANG_VENDOR 00125 OS << " (based on LLVM " << PACKAGE_VERSION << ")"; 00126 #endif 00127 00128 return OS.str(); 00129 } 00130 00131 std::string getClangFullCPPVersion() { 00132 // The version string we report in __VERSION__ is just a compacted version of 00133 // the one we report on the command line. 00134 std::string buf; 00135 llvm::raw_string_ostream OS(buf); 00136 #ifdef CLANG_VENDOR 00137 OS << CLANG_VENDOR; 00138 #endif 00139 OS << "Clang " CLANG_VERSION_STRING " (" 00140 << getClangFullRepositoryVersion() << ')'; 00141 return OS.str(); 00142 } 00143 00144 } // end namespace clang