John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame^] | 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 | using namespace clang; |
| 18 | |
| 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 | |
| 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::GNU: out << "gnu"; break; |
| 34 | case ObjCRuntime::FragileGNU: out << "gnu-fragile"; break; |
| 35 | } |
| 36 | if (value.getVersion() > VersionTuple(0)) { |
| 37 | out << '-' << value.getVersion(); |
| 38 | } |
| 39 | return out; |
| 40 | } |
| 41 | |
| 42 | bool ObjCRuntime::tryParse(StringRef input) { |
| 43 | // Look for the last dash. |
| 44 | std::size_t dash = input.rfind('-'); |
| 45 | |
| 46 | // We permit (1) dashes in the runtime name and (2) the version to |
| 47 | // be omitted, so ignore dashes that aren't followed by a digit. |
| 48 | if (dash != StringRef::npos && dash + 1 != input.size() && |
| 49 | (input[dash+1] < '0' || input[dash+1] > '9')) { |
| 50 | dash = StringRef::npos; |
| 51 | } |
| 52 | |
| 53 | // Everything prior to that must be a valid string name. |
| 54 | Kind kind; |
| 55 | StringRef runtimeName = input.substr(0, dash); |
| 56 | if (runtimeName == "macosx") { |
| 57 | kind = ObjCRuntime::MacOSX; |
| 58 | } else if (runtimeName == "macosx-fragile") { |
| 59 | kind = ObjCRuntime::FragileMacOSX; |
| 60 | } else if (runtimeName == "ios") { |
| 61 | kind = ObjCRuntime::iOS; |
| 62 | } else if (runtimeName == "gnu") { |
| 63 | kind = ObjCRuntime::GNU; |
| 64 | } else if (runtimeName == "gnu-fragile") { |
| 65 | kind = ObjCRuntime::FragileGNU; |
| 66 | } else { |
| 67 | return true; |
| 68 | } |
| 69 | TheKind = kind; |
| 70 | |
| 71 | Version = VersionTuple(0); |
| 72 | if (dash != StringRef::npos) { |
| 73 | StringRef verString = input.substr(dash + 1); |
| 74 | if (Version.tryParse(verString)) |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |