ObjCRuntime.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- ObjCRuntime.cpp - Objective-C Runtime Handling ---------------------===//
  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 implements the ObjCRuntime class, which represents the
  10. // target Objective-C runtime.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/ObjCRuntime.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/Support/VersionTuple.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <cstddef>
  18. #include <string>
  19. using namespace clang;
  20. std::string ObjCRuntime::getAsString() const {
  21. std::string Result;
  22. {
  23. llvm::raw_string_ostream Out(Result);
  24. Out << *this;
  25. }
  26. return Result;
  27. }
  28. raw_ostream &clang::operator<<(raw_ostream &out, const ObjCRuntime &value) {
  29. switch (value.getKind()) {
  30. case ObjCRuntime::MacOSX: out << "macosx"; break;
  31. case ObjCRuntime::FragileMacOSX: out << "macosx-fragile"; break;
  32. case ObjCRuntime::iOS: out << "ios"; break;
  33. case ObjCRuntime::WatchOS: out << "watchos"; break;
  34. case ObjCRuntime::GNUstep: out << "gnustep"; break;
  35. case ObjCRuntime::GCC: out << "gcc"; break;
  36. case ObjCRuntime::ObjFW: out << "objfw"; break;
  37. }
  38. if (value.getVersion() > VersionTuple(0)) {
  39. out << '-' << value.getVersion();
  40. }
  41. return out;
  42. }
  43. bool ObjCRuntime::tryParse(StringRef input) {
  44. // Look for the last dash.
  45. std::size_t dash = input.rfind('-');
  46. // We permit dashes in the runtime name, and we also permit the
  47. // version to be omitted, so if we see a dash not followed by a
  48. // digit then we need to ignore it.
  49. if (dash != StringRef::npos && dash + 1 != input.size() &&
  50. (input[dash+1] < '0' || input[dash+1] > '9')) {
  51. dash = StringRef::npos;
  52. }
  53. // Everything prior to that must be a valid string name.
  54. Kind kind;
  55. StringRef runtimeName = input.substr(0, dash);
  56. Version = VersionTuple(0);
  57. if (runtimeName == "macosx") {
  58. kind = ObjCRuntime::MacOSX;
  59. } else if (runtimeName == "macosx-fragile") {
  60. kind = ObjCRuntime::FragileMacOSX;
  61. } else if (runtimeName == "ios") {
  62. kind = ObjCRuntime::iOS;
  63. } else if (runtimeName == "watchos") {
  64. kind = ObjCRuntime::WatchOS;
  65. } else if (runtimeName == "gnustep") {
  66. // If no version is specified then default to the most recent one that we
  67. // know about.
  68. Version = VersionTuple(1, 6);
  69. kind = ObjCRuntime::GNUstep;
  70. } else if (runtimeName == "gcc") {
  71. kind = ObjCRuntime::GCC;
  72. } else if (runtimeName == "objfw") {
  73. kind = ObjCRuntime::ObjFW;
  74. Version = VersionTuple(0, 8);
  75. } else {
  76. return true;
  77. }
  78. TheKind = kind;
  79. if (dash != StringRef::npos) {
  80. StringRef verString = input.substr(dash + 1);
  81. if (Version.tryParse(verString))
  82. return true;
  83. }
  84. if (kind == ObjCRuntime::ObjFW && Version > VersionTuple(0, 8))
  85. Version = VersionTuple(0, 8);
  86. return false;
  87. }