blob: c4b7d34ed1686a5808d16a5074196847fc1b2ded [file] [log] [blame]
Douglas Gregor7550a6c2009-10-05 18:52:241//===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Douglas Gregor7550a6c2009-10-05 18:52:246//
7//===----------------------------------------------------------------------===//
8//
9// This file defines several version-related utility functions for Clang.
10//
11//===----------------------------------------------------------------------===//
Ted Kremenek2377a0e2010-01-22 20:55:3512
Ted Kremenek51b8bc92010-01-22 22:29:5013#include "clang/Basic/Version.h"
Chris Lattner0e62c1c2011-07-23 10:55:1514#include "clang/Basic/LLVM.h"
Alp Tokerf988d002014-06-06 10:36:2215#include "clang/Config/config.h"
Chandler Carruth3a022472012-12-04 09:13:3316#include "llvm/Support/raw_ostream.h"
Douglas Gregor7550a6c2009-10-05 18:52:2417#include <cstdlib>
Chandler Carruth3a022472012-12-04 09:13:3318#include <cstring>
Ted Kremenek2377a0e2010-01-22 20:55:3519
Petr Hosek23fdd5a2019-02-06 03:51:0020#ifdef HAVE_VCS_VERSION_INC
21#include "VCSVersion.inc"
Petr Hosek12062e02019-01-31 07:12:4322#endif
23
Douglas Gregor7550a6c2009-10-05 18:52:2424namespace clang {
Jia Liu5c302482012-03-02 14:37:4125
Daniel Dunbar181ca582010-09-29 19:15:2926std::string getClangRepositoryPath() {
Daniel Dunbarb1798f72011-03-31 00:32:5027#if defined(CLANG_REPOSITORY_STRING)
28 return CLANG_REPOSITORY_STRING;
29#else
Petr Hosek23fdd5a2019-02-06 03:51:0030#ifdef CLANG_REPOSITORY
Nico Weberfb9413c2020-01-16 15:27:2531 return CLANG_REPOSITORY;
Daniel Dunbar181ca582010-09-29 19:15:2932#else
Nico Weberfb9413c2020-01-16 15:27:2533 return "";
Daniel Dunbarb800fdb2010-09-29 17:57:1034#endif
Daniel Dunbarb1798f72011-03-31 00:32:5035#endif
Douglas Gregor7550a6c2009-10-05 18:52:2436}
37
Jia Liu5c302482012-03-02 14:37:4138std::string getLLVMRepositoryPath() {
39#ifdef LLVM_REPOSITORY
Nico Weberfb9413c2020-01-16 15:27:2540 return LLVM_REPOSITORY;
Jia Liu5c302482012-03-02 14:37:4141#else
Nico Weberfb9413c2020-01-16 15:27:2542 return "";
Jia Liu5c302482012-03-02 14:37:4143#endif
Jia Liu5c302482012-03-02 14:37:4144}
45
Daniel Dunbar181ca582010-09-29 19:15:2946std::string getClangRevision() {
Petr Hosek23fdd5a2019-02-06 03:51:0047#ifdef CLANG_REVISION
48 return CLANG_REVISION;
Daniel Dunbar181ca582010-09-29 19:15:2949#else
Ted Kremenek47307292010-03-03 01:02:4850 return "";
Daniel Dunbar181ca582010-09-29 19:15:2951#endif
Douglas Gregor7550a6c2009-10-05 18:52:2452}
53
Jia Liu5c302482012-03-02 14:37:4154std::string getLLVMRevision() {
55#ifdef LLVM_REVISION
56 return LLVM_REVISION;
57#else
58 return "";
59#endif
60}
61
Ted Kremeneka3e65702010-02-12 22:54:4062std::string getClangFullRepositoryVersion() {
63 std::string buf;
64 llvm::raw_string_ostream OS(buf);
Daniel Dunbar181ca582010-09-29 19:15:2965 std::string Path = getClangRepositoryPath();
66 std::string Revision = getClangRevision();
Andrew Trick9179e8a2012-03-07 00:44:2467 if (!Path.empty() || !Revision.empty()) {
68 OS << '(';
Daniel Dunbarb800fdb2010-09-29 17:57:1069 if (!Path.empty())
Andrew Trick9179e8a2012-03-07 00:44:2470 OS << Path;
71 if (!Revision.empty()) {
72 if (!Path.empty())
73 OS << ' ';
74 OS << Revision;
75 }
76 OS << ')';
Sylvestre Ledru096d6e42014-01-14 10:25:2677 }
Jia Liu5c302482012-03-02 14:37:4178 // Support LLVM in a separate repository.
79 std::string LLVMRev = getLLVMRevision();
80 if (!LLVMRev.empty() && LLVMRev != Revision) {
Sylvestre Ledru096d6e42014-01-14 10:25:2681 OS << " (";
Jia Liu5c302482012-03-02 14:37:4182 std::string LLVMRepo = getLLVMRepositoryPath();
83 if (!LLVMRepo.empty())
Andrew Trick9179e8a2012-03-07 00:44:2484 OS << LLVMRepo << ' ';
85 OS << LLVMRev << ')';
Jia Liu5c302482012-03-02 14:37:4186 }
Benjamin Kramerd48707002010-03-05 15:39:2087 return OS.str();
Ted Kremenek51b8bc92010-01-22 22:29:5088}
Jia Liu5c302482012-03-02 14:37:4189
Ted Kremeneka3e65702010-02-12 22:54:4090std::string getClangFullVersion() {
Nico Weberb00d66e2014-01-07 16:27:3591 return getClangToolFullVersion("clang");
92}
93
94std::string getClangToolFullVersion(StringRef ToolName) {
Ted Kremeneka3e65702010-02-12 22:54:4095 std::string buf;
96 llvm::raw_string_ostream OS(buf);
Ted Kremenek51b8bc92010-01-22 22:29:5097#ifdef CLANG_VENDOR
Ted Kremeneka3e65702010-02-12 22:54:4098 OS << CLANG_VENDOR;
Ted Kremenek51b8bc92010-01-22 22:29:5099#endif
Nico Weberb00d66e2014-01-07 16:27:35100 OS << ToolName << " version " CLANG_VERSION_STRING " "
Andrew Trick9179e8a2012-03-07 00:44:24101 << getClangFullRepositoryVersion();
Daniel Dunbar60362642010-10-07 15:00:30102
Benjamin Kramerd48707002010-03-05 15:39:20103 return OS.str();
Ted Kremenek18e066f2010-01-22 22:12:47104}
Ted Kremeneka3e65702010-02-12 22:54:40105
Daniel Dunbar3b17a862011-03-31 00:53:51106std::string getClangFullCPPVersion() {
Sylvestre Ledru21a92a82019-07-13 06:27:35107 // The version string we report in __VERSION__ is just a compacted version of
108 // the one we report on the command line.
Daniel Dunbar3b17a862011-03-31 00:53:51109 std::string buf;
110 llvm::raw_string_ostream OS(buf);
111#ifdef CLANG_VENDOR
112 OS << CLANG_VENDOR;
113#endif
Benjamin Kramer7b01b572012-05-26 19:39:52114 OS << "Clang " CLANG_VERSION_STRING " " << getClangFullRepositoryVersion();
Daniel Dunbar3b17a862011-03-31 00:53:51115 return OS.str();
116}
117
Douglas Gregor7550a6c2009-10-05 18:52:24118} // end namespace clang