1 //===- ObjCRuntime.cpp - Objective-C Runtime Handling ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ObjCRuntime class, which represents the
11 // target Objective-C runtime.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/ObjCRuntime.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/VersionTuple.h"
18 #include "llvm/Support/raw_ostream.h"
22 using namespace clang;
24 std::string ObjCRuntime::getAsString() const {
27 llvm::raw_string_ostream Out(Result);
33 raw_ostream &clang::operator<<(raw_ostream &out, const ObjCRuntime &value) {
34 switch (value.getKind()) {
35 case ObjCRuntime::MacOSX: out << "macosx"; break;
36 case ObjCRuntime::FragileMacOSX: out << "macosx-fragile"; break;
37 case ObjCRuntime::iOS: out << "ios"; break;
38 case ObjCRuntime::WatchOS: out << "watchos"; break;
39 case ObjCRuntime::GNUstep: out << "gnustep"; break;
40 case ObjCRuntime::GCC: out << "gcc"; break;
41 case ObjCRuntime::ObjFW: out << "objfw"; break;
43 if (value.getVersion() > VersionTuple(0)) {
44 out << '-' << value.getVersion();
49 bool ObjCRuntime::tryParse(StringRef input) {
50 // Look for the last dash.
51 std::size_t dash = input.rfind('-');
53 // We permit dashes in the runtime name, and we also permit the
54 // version to be omitted, so if we see a dash not followed by a
55 // digit then we need to ignore it.
56 if (dash != StringRef::npos && dash + 1 != input.size() &&
57 (input[dash+1] < '0' || input[dash+1] > '9')) {
58 dash = StringRef::npos;
61 // Everything prior to that must be a valid string name.
63 StringRef runtimeName = input.substr(0, dash);
64 Version = VersionTuple(0);
65 if (runtimeName == "macosx") {
66 kind = ObjCRuntime::MacOSX;
67 } else if (runtimeName == "macosx-fragile") {
68 kind = ObjCRuntime::FragileMacOSX;
69 } else if (runtimeName == "ios") {
70 kind = ObjCRuntime::iOS;
71 } else if (runtimeName == "watchos") {
72 kind = ObjCRuntime::WatchOS;
73 } else if (runtimeName == "gnustep") {
74 // If no version is specified then default to the most recent one that we
76 Version = VersionTuple(1, 6);
77 kind = ObjCRuntime::GNUstep;
78 } else if (runtimeName == "gcc") {
79 kind = ObjCRuntime::GCC;
80 } else if (runtimeName == "objfw") {
81 kind = ObjCRuntime::ObjFW;
82 Version = VersionTuple(0, 8);
88 if (dash != StringRef::npos) {
89 StringRef verString = input.substr(dash + 1);
90 if (Version.tryParse(verString))
94 if (kind == ObjCRuntime::ObjFW && Version > VersionTuple(0, 8))
95 Version = VersionTuple(0, 8);