xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Basic/Version.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines several version-related utility functions for Clang.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg 
137330f729Sjoerg #include "clang/Basic/Version.h"
147330f729Sjoerg #include "clang/Basic/LLVM.h"
157330f729Sjoerg #include "clang/Config/config.h"
167330f729Sjoerg #include "llvm/Support/raw_ostream.h"
177330f729Sjoerg #include <cstdlib>
187330f729Sjoerg #include <cstring>
197330f729Sjoerg 
207330f729Sjoerg #include "VCSVersion.inc"
217330f729Sjoerg 
227330f729Sjoerg namespace clang {
237330f729Sjoerg 
getClangRepositoryPath()247330f729Sjoerg std::string getClangRepositoryPath() {
257330f729Sjoerg #if defined(CLANG_REPOSITORY_STRING)
267330f729Sjoerg   return CLANG_REPOSITORY_STRING;
277330f729Sjoerg #else
287330f729Sjoerg #ifdef CLANG_REPOSITORY
29*e038c9c4Sjoerg   return CLANG_REPOSITORY;
307330f729Sjoerg #else
31*e038c9c4Sjoerg   return "";
327330f729Sjoerg #endif
337330f729Sjoerg #endif
347330f729Sjoerg }
357330f729Sjoerg 
getLLVMRepositoryPath()367330f729Sjoerg std::string getLLVMRepositoryPath() {
377330f729Sjoerg #ifdef LLVM_REPOSITORY
38*e038c9c4Sjoerg   return LLVM_REPOSITORY;
397330f729Sjoerg #else
40*e038c9c4Sjoerg   return "";
417330f729Sjoerg #endif
427330f729Sjoerg }
437330f729Sjoerg 
getClangRevision()447330f729Sjoerg std::string getClangRevision() {
457330f729Sjoerg #ifdef CLANG_REVISION
467330f729Sjoerg   return CLANG_REVISION;
477330f729Sjoerg #else
487330f729Sjoerg   return "";
497330f729Sjoerg #endif
507330f729Sjoerg }
517330f729Sjoerg 
getLLVMRevision()527330f729Sjoerg std::string getLLVMRevision() {
537330f729Sjoerg #ifdef LLVM_REVISION
547330f729Sjoerg   return LLVM_REVISION;
557330f729Sjoerg #else
567330f729Sjoerg   return "";
577330f729Sjoerg #endif
587330f729Sjoerg }
597330f729Sjoerg 
getClangFullRepositoryVersion()607330f729Sjoerg std::string getClangFullRepositoryVersion() {
617330f729Sjoerg   std::string buf;
627330f729Sjoerg   llvm::raw_string_ostream OS(buf);
637330f729Sjoerg   std::string Path = getClangRepositoryPath();
647330f729Sjoerg   std::string Revision = getClangRevision();
657330f729Sjoerg   if (!Path.empty() || !Revision.empty()) {
667330f729Sjoerg     OS << '(';
677330f729Sjoerg     if (!Path.empty())
687330f729Sjoerg       OS << Path;
697330f729Sjoerg     if (!Revision.empty()) {
707330f729Sjoerg       if (!Path.empty())
717330f729Sjoerg         OS << ' ';
727330f729Sjoerg       OS << Revision;
737330f729Sjoerg     }
747330f729Sjoerg     OS << ')';
757330f729Sjoerg   }
767330f729Sjoerg   // Support LLVM in a separate repository.
777330f729Sjoerg   std::string LLVMRev = getLLVMRevision();
787330f729Sjoerg   if (!LLVMRev.empty() && LLVMRev != Revision) {
797330f729Sjoerg     OS << " (";
807330f729Sjoerg     std::string LLVMRepo = getLLVMRepositoryPath();
817330f729Sjoerg     if (!LLVMRepo.empty())
827330f729Sjoerg       OS << LLVMRepo << ' ';
837330f729Sjoerg     OS << LLVMRev << ')';
847330f729Sjoerg   }
857330f729Sjoerg   return OS.str();
867330f729Sjoerg }
877330f729Sjoerg 
getClangFullVersion()887330f729Sjoerg std::string getClangFullVersion() {
897330f729Sjoerg   return getClangToolFullVersion("clang");
907330f729Sjoerg }
917330f729Sjoerg 
getClangToolFullVersion(StringRef ToolName)927330f729Sjoerg std::string getClangToolFullVersion(StringRef ToolName) {
937330f729Sjoerg   std::string buf;
947330f729Sjoerg   llvm::raw_string_ostream OS(buf);
957330f729Sjoerg #ifdef CLANG_VENDOR
967330f729Sjoerg   OS << CLANG_VENDOR;
977330f729Sjoerg #endif
98*e038c9c4Sjoerg   OS << ToolName << " version " CLANG_VERSION_STRING;
997330f729Sjoerg 
100*e038c9c4Sjoerg   std::string repo = getClangFullRepositoryVersion();
101*e038c9c4Sjoerg   if (!repo.empty()) {
102*e038c9c4Sjoerg     OS << " " << repo;
103*e038c9c4Sjoerg   }
1047330f729Sjoerg 
1057330f729Sjoerg   return OS.str();
1067330f729Sjoerg }
1077330f729Sjoerg 
getClangFullCPPVersion()1087330f729Sjoerg std::string getClangFullCPPVersion() {
1097330f729Sjoerg   // The version string we report in __VERSION__ is just a compacted version of
1107330f729Sjoerg   // the one we report on the command line.
1117330f729Sjoerg   std::string buf;
1127330f729Sjoerg   llvm::raw_string_ostream OS(buf);
1137330f729Sjoerg #ifdef CLANG_VENDOR
1147330f729Sjoerg   OS << CLANG_VENDOR;
1157330f729Sjoerg #endif
116*e038c9c4Sjoerg   OS << "Clang " CLANG_VERSION_STRING;
117*e038c9c4Sjoerg 
118*e038c9c4Sjoerg   std::string repo = getClangFullRepositoryVersion();
119*e038c9c4Sjoerg   if (!repo.empty()) {
120*e038c9c4Sjoerg     OS << " " << repo;
121*e038c9c4Sjoerg   }
122*e038c9c4Sjoerg 
1237330f729Sjoerg   return OS.str();
1247330f729Sjoerg }
1257330f729Sjoerg 
1267330f729Sjoerg } // end namespace clang
127