clang API Documentation

ObjCRuntime.cpp
Go to the documentation of this file.
00001 //===- ObjCRuntime.cpp - Objective-C Runtime Handling -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the ObjCRuntime class, which represents the
00011 // target Objective-C runtime.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #include "clang/Basic/ObjCRuntime.h"
00015 #include "llvm/Support/raw_ostream.h"
00016 
00017 using namespace clang;
00018 
00019 std::string ObjCRuntime::getAsString() const {
00020   std::string Result;
00021   {
00022     llvm::raw_string_ostream Out(Result);
00023     Out << *this;
00024   }
00025   return Result;  
00026 }
00027 
00028 raw_ostream &clang::operator<<(raw_ostream &out, const ObjCRuntime &value) {
00029   switch (value.getKind()) {
00030   case ObjCRuntime::MacOSX: out << "macosx"; break;
00031   case ObjCRuntime::FragileMacOSX: out << "macosx-fragile"; break;
00032   case ObjCRuntime::iOS: out << "ios"; break;
00033   case ObjCRuntime::GNUstep: out << "gnustep"; break;
00034   case ObjCRuntime::GCC: out << "gcc"; break;
00035   case ObjCRuntime::ObjFW: out << "objfw"; break;
00036   }
00037   if (value.getVersion() > VersionTuple(0)) {
00038     out << '-' << value.getVersion();
00039   }
00040   return out;
00041 }
00042 
00043 bool ObjCRuntime::tryParse(StringRef input) {
00044   // Look for the last dash.
00045   std::size_t dash = input.rfind('-');
00046 
00047   // We permit dashes in the runtime name, and we also permit the
00048   // version to be omitted, so if we see a dash not followed by a
00049   // digit then we need to ignore it.
00050   if (dash != StringRef::npos && dash + 1 != input.size() &&
00051       (input[dash+1] < '0' || input[dash+1] > '9')) {
00052     dash = StringRef::npos;
00053   }
00054 
00055   // Everything prior to that must be a valid string name.
00056   Kind kind;
00057   StringRef runtimeName = input.substr(0, dash);
00058   Version = VersionTuple(0);
00059   if (runtimeName == "macosx") {
00060     kind = ObjCRuntime::MacOSX;
00061   } else if (runtimeName == "macosx-fragile") {
00062     kind = ObjCRuntime::FragileMacOSX;
00063   } else if (runtimeName == "ios") {
00064     kind = ObjCRuntime::iOS;
00065   } else if (runtimeName == "gnustep") {
00066     // If no version is specified then default to the most recent one that we
00067     // know about.
00068     Version = VersionTuple(1, 6);
00069     kind = ObjCRuntime::GNUstep;
00070   } else if (runtimeName == "gcc") {
00071     kind = ObjCRuntime::GCC;
00072   } else if (runtimeName == "objfw") {
00073     kind = ObjCRuntime::ObjFW;
00074     Version = VersionTuple(0, 8);
00075   } else {
00076     return true;
00077   }
00078   TheKind = kind;
00079 
00080   if (dash != StringRef::npos) {
00081     StringRef verString = input.substr(dash + 1);
00082     if (Version.tryParse(verString))
00083       return true;
00084   }
00085 
00086   if (kind == ObjCRuntime::ObjFW && Version > VersionTuple(0, 8))
00087     Version = VersionTuple(0, 8);
00088 
00089   return false;
00090 }