Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 13 | |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Version.h" |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 15 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 16 | #include <cstring> |
| 17 | #include <cstdlib> |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 18 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 19 | using namespace std; |
| 20 | |
| 21 | namespace clang { |
| 22 | |
Ted Kremenek | 517e676 | 2010-01-22 20:55:35 +0000 | [diff] [blame] | 23 | llvm::StringRef getClangRepositoryPath() { |
Benjamin Kramer | e242d5f | 2010-01-30 14:01:39 +0000 | [diff] [blame^] | 24 | static const char URL[] = "$URL$"; |
| 25 | const char *URLEnd = URL + strlen(URL); |
| 26 | |
| 27 | const char *End = strstr(URL, "/lib/Basic"); |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 28 | if (End) |
Benjamin Kramer | e242d5f | 2010-01-30 14:01:39 +0000 | [diff] [blame^] | 29 | URLEnd = End; |
| 30 | |
Douglas Gregor | 1fbf1f0 | 2009-11-05 23:46:05 +0000 | [diff] [blame] | 31 | End = strstr(URL, "/clang/tools/clang"); |
| 32 | if (End) |
Benjamin Kramer | e242d5f | 2010-01-30 14:01:39 +0000 | [diff] [blame^] | 33 | URLEnd = End; |
| 34 | |
| 35 | const char *Begin = strstr(URL, "cfe/"); |
| 36 | if (Begin) |
| 37 | return llvm::StringRef(Begin + 4, URLEnd - Begin - 4); |
| 38 | |
| 39 | return llvm::StringRef(URL, URLEnd - URL); |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 43 | llvm::StringRef getClangRevision() { |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 44 | #ifndef SVN_REVISION |
Douglas Gregor | b8d1191 | 2009-10-05 20:33:49 +0000 | [diff] [blame] | 45 | // Subversion was not available at build time? |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 46 | return llvm::StringRef(); |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 47 | #else |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 48 | static std::string revision; |
| 49 | if (revision.empty()) { |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 50 | llvm::raw_string_ostream OS(revision); |
| 51 | OS << strtol(SVN_REVISION, 0, 10); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 52 | } |
| 53 | return revision; |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 54 | #endif |
| 55 | } |
| 56 | |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 57 | llvm::StringRef getClangFullRepositoryVersion() { |
| 58 | static std::string buf; |
| 59 | if (buf.empty()) { |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 60 | llvm::raw_string_ostream OS(buf); |
| 61 | OS << getClangRepositoryPath(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 62 | llvm::StringRef Revision = getClangRevision(); |
| 63 | if (!Revision.empty()) |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 64 | OS << ' ' << Revision; |
| 65 | } |
| 66 | return buf; |
| 67 | } |
| 68 | |
Ted Kremenek | a18f1b8 | 2010-01-23 02:11:34 +0000 | [diff] [blame] | 69 | const char *getClangFullVersion() { |
Ted Kremenek | 3687a5d | 2010-01-22 22:29:50 +0000 | [diff] [blame] | 70 | static std::string buf; |
| 71 | if (buf.empty()) { |
| 72 | llvm::raw_string_ostream OS(buf); |
| 73 | #ifdef CLANG_VENDOR |
| 74 | OS << CLANG_VENDOR; |
| 75 | #endif |
| 76 | OS << "clang version " CLANG_VERSION_STRING " (" |
| 77 | << getClangFullRepositoryVersion() << ')'; |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 78 | } |
Ted Kremenek | 04bb716 | 2010-01-22 22:44:15 +0000 | [diff] [blame] | 79 | return buf.c_str(); |
Ted Kremenek | f7a96a3 | 2010-01-22 22:12:47 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Douglas Gregor | 60b5d8e | 2009-10-05 18:52:24 +0000 | [diff] [blame] | 82 | } // end namespace clang |