ObjCRuntime.cpp 2.9 KB

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