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 return CLANG_REPOSITORY; 32 #else 33 return ""; 34 #endif 35 #endif 36 } 37 38 std::string getLLVMRepositoryPath() { 39 #ifdef LLVM_REPOSITORY 40 return LLVM_REPOSITORY; 41 #else 42 return ""; 43 #endif 44 } 45 46 std::string getClangRevision() { 47 #ifdef CLANG_REVISION 48 return CLANG_REVISION; 49 #else 50 return ""; 51 #endif 52 } 53 54 std::string getLLVMRevision() { 55 #ifdef LLVM_REVISION 56 return LLVM_REVISION; 57 #else 58 return ""; 59 #endif 60 } 61 62 std::string getClangFullRepositoryVersion() { 63 std::string buf; 64 llvm::raw_string_ostream OS(buf); 65 std::string Path = getClangRepositoryPath(); 66 std::string Revision = getClangRevision(); 67 if (!Path.empty() || !Revision.empty()) { 68 OS << '('; 69 if (!Path.empty()) 70 OS << Path; 71 if (!Revision.empty()) { 72 if (!Path.empty()) 73 OS << ' '; 74 OS << Revision; 75 } 76 OS << ')'; 77 } 78 // Support LLVM in a separate repository. 79 std::string LLVMRev = getLLVMRevision(); 80 if (!LLVMRev.empty() && LLVMRev != Revision) { 81 OS << " ("; 82 std::string LLVMRepo = getLLVMRepositoryPath(); 83 if (!LLVMRepo.empty()) 84 OS << LLVMRepo << ' '; 85 OS << LLVMRev << ')'; 86 } 87 return OS.str(); 88 } 89 90 std::string getClangFullVersion() { 91 return getClangToolFullVersion("clang"); 92 } 93 94 std::string getClangToolFullVersion(StringRef ToolName) { 95 std::string buf; 96 llvm::raw_string_ostream OS(buf); 97 #ifdef CLANG_VENDOR 98 OS << CLANG_VENDOR; 99 #endif 100 OS << ToolName << " version " CLANG_VERSION_STRING; 101 102 std::string repo = getClangFullRepositoryVersion(); 103 if (!repo.empty()) { 104 OS << " " << repo; 105 } 106 107 return OS.str(); 108 } 109 110 std::string getClangFullCPPVersion() { 111 // The version string we report in __VERSION__ is just a compacted version of 112 // the one we report on the command line. 113 std::string buf; 114 llvm::raw_string_ostream OS(buf); 115 #ifdef CLANG_VENDOR 116 OS << CLANG_VENDOR; 117 #endif 118 OS << "Clang " CLANG_VERSION_STRING; 119 120 std::string repo = getClangFullRepositoryVersion(); 121 if (!repo.empty()) { 122 OS << " " << repo; 123 } 124 125 return OS.str(); 126 } 127 128 } // end namespace clang 129