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