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