1 //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines several version-related utility functions for Clang. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/Version.h" 14 #include "clang/Basic/LLVM.h" 15 #include "clang/Config/config.h" 16 #include "llvm/Support/raw_ostream.h" 17 #include <cstdlib> 18 #include <cstring> 19 20 #ifdef HAVE_VCS_VERSION_INC 21 #include "VCSVersion.inc" 22 #endif 23 24 namespace clang { 25 26 std::string getClangRepositoryPath() { 27 #if defined(CLANG_REPOSITORY_STRING) 28 return CLANG_REPOSITORY_STRING; 29 #else 30 #ifdef CLANG_REPOSITORY 31 StringRef URL(CLANG_REPOSITORY); 32 #else 33 StringRef URL(""); 34 #endif 35 36 // If the CLANG_REPOSITORY is empty, try to use the SVN keyword. This helps us 37 // pick up a tag in an SVN export, for example. 38 StringRef SVNRepository("$URL$"); 39 if (URL.empty()) { 40 URL = SVNRepository.slice(SVNRepository.find(':'), 41 SVNRepository.find("/lib/Basic")); 42 } 43 44 // Strip off version from a build from an integration branch. 45 URL = URL.slice(0, URL.find("/src/tools/clang")); 46 47 // Trim path prefix off, assuming path came from standard cfe path. 48 size_t Start = URL.find("cfe/"); 49 if (Start != StringRef::npos) 50 URL = URL.substr(Start + 4); 51 52 return URL; 53 #endif 54 } 55 56 std::string getLLVMRepositoryPath() { 57 #ifdef LLVM_REPOSITORY 58 StringRef URL(LLVM_REPOSITORY); 59 #else 60 StringRef URL(""); 61 #endif 62 63 // Trim path prefix off, assuming path came from standard llvm path. 64 // Leave "llvm/" prefix to distinguish the following llvm revision from the 65 // clang revision. 66 size_t Start = URL.find("llvm/"); 67 if (Start != StringRef::npos) 68 URL = URL.substr(Start); 69 70 return URL; 71 } 72 73 std::string getClangRevision() { 74 #ifdef CLANG_REVISION 75 return CLANG_REVISION; 76 #else 77 return ""; 78 #endif 79 } 80 81 std::string getLLVMRevision() { 82 #ifdef LLVM_REVISION 83 return LLVM_REVISION; 84 #else 85 return ""; 86 #endif 87 } 88 89 std::string getClangFullRepositoryVersion() { 90 std::string buf; 91 llvm::raw_string_ostream OS(buf); 92 std::string Path = getClangRepositoryPath(); 93 std::string Revision = getClangRevision(); 94 if (!Path.empty() || !Revision.empty()) { 95 OS << '('; 96 if (!Path.empty()) 97 OS << Path; 98 if (!Revision.empty()) { 99 if (!Path.empty()) 100 OS << ' '; 101 OS << Revision; 102 } 103 OS << ')'; 104 } 105 // Support LLVM in a separate repository. 106 std::string LLVMRev = getLLVMRevision(); 107 if (!LLVMRev.empty() && LLVMRev != Revision) { 108 OS << " ("; 109 std::string LLVMRepo = getLLVMRepositoryPath(); 110 if (!LLVMRepo.empty()) 111 OS << LLVMRepo << ' '; 112 OS << LLVMRev << ')'; 113 } 114 return OS.str(); 115 } 116 117 std::string getClangFullVersion() { 118 return getClangToolFullVersion("clang"); 119 } 120 121 std::string getClangToolFullVersion(StringRef ToolName) { 122 std::string buf; 123 llvm::raw_string_ostream OS(buf); 124 #ifdef CLANG_VENDOR 125 OS << CLANG_VENDOR; 126 #endif 127 OS << ToolName << " version " CLANG_VERSION_STRING " " 128 << getClangFullRepositoryVersion(); 129 130 return OS.str(); 131 } 132 133 std::string getClangFullCPPVersion() { 134 // The version string we report in __VERSION__ is just a compacted version of 135 // the one we report on the command line. 136 std::string buf; 137 llvm::raw_string_ostream OS(buf); 138 #ifdef CLANG_VENDOR 139 OS << CLANG_VENDOR; 140 #endif 141 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion(); 142 return OS.str(); 143 } 144 145 } // end namespace clang 146