1 //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines several version-related utility functions for Clang.
11 //===----------------------------------------------------------------------===//
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"
20 #ifdef HAVE_VCS_VERSION_INC
21 #include "VCSVersion.inc"
26 std::string getClangRepositoryPath() {
27 #if defined(CLANG_REPOSITORY_STRING)
28 return CLANG_REPOSITORY_STRING;
30 #ifdef CLANG_REPOSITORY
31 StringRef URL(CLANG_REPOSITORY);
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: https://llvm.org/svn/llvm-project/cfe/tags/RELEASE_900/final/lib/Basic/Version.cpp $");
40 URL = SVNRepository.slice(SVNRepository.find(':'),
41 SVNRepository.find("/lib/Basic"));
44 // Strip off version from a build from an integration branch.
45 URL = URL.slice(0, URL.find("/src/tools/clang"));
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);
56 std::string getLLVMRepositoryPath() {
57 #ifdef LLVM_REPOSITORY
58 StringRef URL(LLVM_REPOSITORY);
63 // Trim path prefix off, assuming path came from standard llvm path.
64 // Leave "llvm/" prefix to distinguish the following llvm revision from the
66 size_t Start = URL.find("llvm/");
67 if (Start != StringRef::npos)
68 URL = URL.substr(Start);
73 std::string getClangRevision() {
75 return CLANG_REVISION;
81 std::string getLLVMRevision() {
89 std::string getClangFullRepositoryVersion() {
91 llvm::raw_string_ostream OS(buf);
92 std::string Path = getClangRepositoryPath();
93 std::string Revision = getClangRevision();
94 if (!Path.empty() || !Revision.empty()) {
98 if (!Revision.empty()) {
105 // Support LLVM in a separate repository.
106 std::string LLVMRev = getLLVMRevision();
107 if (!LLVMRev.empty() && LLVMRev != Revision) {
109 std::string LLVMRepo = getLLVMRepositoryPath();
110 if (!LLVMRepo.empty())
111 OS << LLVMRepo << ' ';
112 OS << LLVMRev << ')';
117 std::string getClangFullVersion() {
118 return getClangToolFullVersion("clang");
121 std::string getClangToolFullVersion(StringRef ToolName) {
123 llvm::raw_string_ostream OS(buf);
127 OS << ToolName << " version " CLANG_VERSION_STRING " "
128 << getClangFullRepositoryVersion();
130 // If vendor supplied, include the base LLVM version as well.
132 OS << " (based on " << BACKEND_PACKAGE_STRING << ")";
138 std::string getClangFullCPPVersion() {
139 // The version string we report in __VERSION__ is just a compacted version of
140 // the one we report on the command line.
142 llvm::raw_string_ostream OS(buf);
146 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
150 } // end namespace clang