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