clang  10.0.0svn
ObjCRuntime.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/VersionTuple.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cstddef>
19 #include <string>
20 
21 using namespace clang;
22 
23 std::string ObjCRuntime::getAsString() const {
24  std::string Result;
25  {
26  llvm::raw_string_ostream Out(Result);
27  Out << *this;
28  }
29  return Result;
30 }
31 
32 raw_ostream &clang::operator<<(raw_ostream &out, const ObjCRuntime &value) {
33  switch (value.getKind()) {
34  case ObjCRuntime::MacOSX: out << "macosx"; break;
35  case ObjCRuntime::FragileMacOSX: out << "macosx-fragile"; break;
36  case ObjCRuntime::iOS: out << "ios"; break;
37  case ObjCRuntime::WatchOS: out << "watchos"; break;
38  case ObjCRuntime::GNUstep: out << "gnustep"; break;
39  case ObjCRuntime::GCC: out << "gcc"; break;
40  case ObjCRuntime::ObjFW: out << "objfw"; break;
41  }
42  if (value.getVersion() > VersionTuple(0)) {
43  out << '-' << value.getVersion();
44  }
45  return out;
46 }
47 
48 bool ObjCRuntime::tryParse(StringRef input) {
49  // Look for the last dash.
50  std::size_t dash = input.rfind('-');
51 
52  // We permit dashes in the runtime name, and we also permit the
53  // version to be omitted, so if we see a dash not followed by a
54  // digit then we need to ignore it.
55  if (dash != StringRef::npos && dash + 1 != input.size() &&
56  (input[dash+1] < '0' || input[dash+1] > '9')) {
57  dash = StringRef::npos;
58  }
59 
60  // Everything prior to that must be a valid string name.
61  Kind kind;
62  StringRef runtimeName = input.substr(0, dash);
63  Version = VersionTuple(0);
64  if (runtimeName == "macosx") {
65  kind = ObjCRuntime::MacOSX;
66  } else if (runtimeName == "macosx-fragile") {
68  } else if (runtimeName == "ios") {
69  kind = ObjCRuntime::iOS;
70  } else if (runtimeName == "watchos") {
71  kind = ObjCRuntime::WatchOS;
72  } else if (runtimeName == "gnustep") {
73  // If no version is specified then default to the most recent one that we
74  // know about.
75  Version = VersionTuple(1, 6);
76  kind = ObjCRuntime::GNUstep;
77  } else if (runtimeName == "gcc") {
78  kind = ObjCRuntime::GCC;
79  } else if (runtimeName == "objfw") {
80  kind = ObjCRuntime::ObjFW;
81  Version = VersionTuple(0, 8);
82  } else {
83  return true;
84  }
85  TheKind = kind;
86 
87  if (dash != StringRef::npos) {
88  StringRef verString = input.substr(dash + 1);
89  if (Version.tryParse(verString))
90  return true;
91  }
92 
93  if (kind == ObjCRuntime::ObjFW && Version > VersionTuple(0, 8))
94  Version = VersionTuple(0, 8);
95 
96  return false;
97 }
Defines types useful for describing an Objective-C runtime.
const DiagnosticBuilder & operator<<(const DiagnosticBuilder &DB, const Attr *At)
Definition: Attr.h:334
&#39;gcc&#39; is the Objective-C runtime shipped with GCC, implementing a fragile Objective-C ABI ...
Definition: ObjCRuntime.h:52
&#39;macosx-fragile&#39; is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
Definition: ObjCRuntime.h:39
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c-base.h:40
&#39;watchos&#39; is a variant of iOS for Apple&#39;s watchOS.
Definition: ObjCRuntime.h:48
&#39;macosx&#39; is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
Definition: ObjCRuntime.h:34
std::string getAsString() const
Definition: ObjCRuntime.cpp:23
bool tryParse(StringRef input)
Try to parse an Objective-C runtime specification from the given string.
Definition: ObjCRuntime.cpp:48
The result type of a method or function.
&#39;gnustep&#39; is the modern non-fragile GNUstep runtime.
Definition: ObjCRuntime.h:55
&#39;objfw&#39; is the Objective-C runtime included in ObjFW
Definition: ObjCRuntime.h:58
Kind getKind() const
Definition: ObjCRuntime.h:76
const VersionTuple & getVersion() const
Definition: ObjCRuntime.h:77
Dataflow Directional Tag Classes.
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:27
&#39;ios&#39; is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
Definition: ObjCRuntime.h:44
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:60
Kind
The basic Objective-C runtimes that we know about.
Definition: ObjCRuntime.h:30