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 "llvm/Support/raw_ostream.h"
16 #include "llvm/Config/config.h"
17 #include <cstring>
18 #include <cstdlib>
19 
20 using namespace std;
21 
22 namespace clang {
23 
24 std::string getClangRepositoryPath() {
25 #if defined(CLANG_REPOSITORY_STRING)
26   return CLANG_REPOSITORY_STRING;
27 #else
28 #ifdef SVN_REPOSITORY
29   llvm::StringRef URL(SVN_REPOSITORY);
30 #else
31   llvm::StringRef URL("");
32 #endif
33 
34   // If the SVN_REPOSITORY is empty, try to use the SVN keyword. This helps us
35   // pick up a tag in an SVN export, for example.
36   static llvm::StringRef SVNRepository("$URL$");
37   if (URL.empty()) {
38     URL = SVNRepository.slice(SVNRepository.find(':'),
39                               SVNRepository.find("/lib/Basic"));
40   }
41 
42   // Strip off version from a build from an integration branch.
43   URL = URL.slice(0, URL.find("/src/tools/clang"));
44 
45   // Trim path prefix off, assuming path came from standard cfe path.
46   size_t Start = URL.find("cfe/");
47   if (Start != llvm::StringRef::npos)
48     URL = URL.substr(Start + 4);
49 
50   return URL;
51 #endif
52 }
53 
54 std::string getClangRevision() {
55 #ifdef SVN_REVISION
56   return SVN_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())
68     OS << Path;
69   if (!Revision.empty()) {
70     if (!Path.empty())
71       OS << ' ';
72     OS << Revision;
73   }
74   return OS.str();
75 }
76 
77 std::string getClangFullVersion() {
78   std::string buf;
79   llvm::raw_string_ostream OS(buf);
80 #ifdef CLANG_VENDOR
81   OS << CLANG_VENDOR;
82 #endif
83   OS << "clang version " CLANG_VERSION_STRING " ("
84      << getClangFullRepositoryVersion() << ')';
85 
86   // If vendor supplied, include the base LLVM version as well.
87 #ifdef CLANG_VENDOR
88   OS << " (based on LLVM " << PACKAGE_VERSION << ")";
89 #endif
90 
91   return OS.str();
92 }
93 
94 std::string getClangFullCPPVersion() {
95   // The version string we report in __VERSION__ is just a compacted version of
96   // the one we report on the command line.
97   std::string buf;
98   llvm::raw_string_ostream OS(buf);
99 #ifdef CLANG_VENDOR
100   OS << CLANG_VENDOR;
101 #endif
102   OS << "Clang " CLANG_VERSION_STRING " ("
103      << getClangFullRepositoryVersion() << ')';
104   return OS.str();
105 }
106 
107 } // end namespace clang
108