Version.cpp revision 1.1.1.1
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
24namespace clang {
25
26std::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
56std::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
73std::string getClangRevision() {
74#ifdef CLANG_REVISION
75  return CLANG_REVISION;
76#else
77  return "";
78#endif
79}
80
81std::string getLLVMRevision() {
82#ifdef LLVM_REVISION
83  return LLVM_REVISION;
84#else
85  return "";
86#endif
87}
88
89std::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
117std::string getClangFullVersion() {
118  return getClangToolFullVersion("clang");
119}
120
121std::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  // If vendor supplied, include the base LLVM version as well.
131#ifdef CLANG_VENDOR
132  OS << " (based on " << BACKEND_PACKAGE_STRING << ")";
133#endif
134
135  return OS.str();
136}
137
138std::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.
141  std::string buf;
142  llvm::raw_string_ostream OS(buf);
143#ifdef CLANG_VENDOR
144  OS << CLANG_VENDOR;
145#endif
146  OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
147  return OS.str();
148}
149
150} // end namespace clang
151