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