Version.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===- Version.cpp - Clang Version Number -----------------------*- C++ -*-===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines several version-related utility functions for Clang.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/Version.h"
  13. #include "clang/Basic/LLVM.h"
  14. #include "clang/Config/config.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cstdlib>
  17. #include <cstring>
  18. #include "VCSVersion.inc"
  19. namespace clang {
  20. std::string getClangRepositoryPath() {
  21. #if defined(CLANG_REPOSITORY_STRING)
  22. return CLANG_REPOSITORY_STRING;
  23. #else
  24. #ifdef CLANG_REPOSITORY
  25. return CLANG_REPOSITORY;
  26. #else
  27. return "";
  28. #endif
  29. #endif
  30. }
  31. std::string getLLVMRepositoryPath() {
  32. #ifdef LLVM_REPOSITORY
  33. return LLVM_REPOSITORY;
  34. #else
  35. return "";
  36. #endif
  37. }
  38. std::string getClangRevision() {
  39. #ifdef CLANG_REVISION
  40. return CLANG_REVISION;
  41. #else
  42. return "";
  43. #endif
  44. }
  45. std::string getLLVMRevision() {
  46. #ifdef LLVM_REVISION
  47. return LLVM_REVISION;
  48. #else
  49. return "";
  50. #endif
  51. }
  52. std::string getClangFullRepositoryVersion() {
  53. std::string buf;
  54. llvm::raw_string_ostream OS(buf);
  55. std::string Path = getClangRepositoryPath();
  56. std::string Revision = getClangRevision();
  57. if (!Path.empty() || !Revision.empty()) {
  58. OS << '(';
  59. if (!Path.empty())
  60. OS << Path;
  61. if (!Revision.empty()) {
  62. if (!Path.empty())
  63. OS << ' ';
  64. OS << Revision;
  65. }
  66. OS << ')';
  67. }
  68. // Support LLVM in a separate repository.
  69. std::string LLVMRev = getLLVMRevision();
  70. if (!LLVMRev.empty() && LLVMRev != Revision) {
  71. OS << " (";
  72. std::string LLVMRepo = getLLVMRepositoryPath();
  73. if (!LLVMRepo.empty())
  74. OS << LLVMRepo << ' ';
  75. OS << LLVMRev << ')';
  76. }
  77. return buf;
  78. }
  79. std::string getClangFullVersion() {
  80. return getClangToolFullVersion("clang");
  81. }
  82. std::string getClangToolFullVersion(StringRef ToolName) {
  83. std::string buf;
  84. llvm::raw_string_ostream OS(buf);
  85. #ifdef CLANG_VENDOR
  86. OS << CLANG_VENDOR;
  87. #endif
  88. OS << ToolName << " version " CLANG_VERSION_STRING;
  89. std::string repo = getClangFullRepositoryVersion();
  90. if (!repo.empty()) {
  91. OS << " " << repo;
  92. }
  93. return buf;
  94. }
  95. std::string getClangFullCPPVersion() {
  96. // The version string we report in __VERSION__ is just a compacted version of
  97. // the one we report on the command line.
  98. std::string buf;
  99. llvm::raw_string_ostream OS(buf);
  100. #ifdef CLANG_VENDOR
  101. OS << CLANG_VENDOR;
  102. #endif
  103. OS << "Clang " CLANG_VERSION_STRING;
  104. std::string repo = getClangFullRepositoryVersion();
  105. if (!repo.empty()) {
  106. OS << " " << repo;
  107. }
  108. return buf;
  109. }
  110. } // end namespace clang