2010-01-27 14:13:48 +08:00
|
|
|
//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-06-17 07:19:36 +08:00
|
|
|
// This file implements cocoa naming convention analysis.
|
2010-01-27 14:13:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-17 13:21:58 +08:00
|
|
|
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
|
2010-01-28 02:00:17 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2013-02-09 06:30:41 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2010-01-27 14:13:48 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2011-03-02 09:50:55 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-08-10 18:58:18 +08:00
|
|
|
|
2010-01-27 14:13:48 +08:00
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-01-27 14:13:48 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
|
|
|
|
StringRef Name) {
|
2010-01-28 02:00:17 +08:00
|
|
|
// Recursively walk the typedef stack, allowing typedefs of reference types.
|
2015-06-20 07:17:46 +08:00
|
|
|
while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef TDName = TD->getDecl()->getIdentifier()->getName();
|
2010-02-09 02:38:55 +08:00
|
|
|
if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
|
2010-01-28 02:00:17 +08:00
|
|
|
return true;
|
2012-01-04 08:35:48 +08:00
|
|
|
// XPC unfortunately uses CF-style function names, but aren't CF types.
|
|
|
|
if (TDName.startswith("xpc_"))
|
|
|
|
return false;
|
2010-01-28 02:00:17 +08:00
|
|
|
RetTy = TD->getDecl()->getUnderlyingType();
|
|
|
|
}
|
|
|
|
|
2010-02-09 02:38:55 +08:00
|
|
|
if (Name.empty())
|
2010-01-28 02:00:17 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Is the type void*?
|
|
|
|
const PointerType* PT = RetTy->getAs<PointerType>();
|
|
|
|
if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Does the name start with the prefix?
|
2010-02-09 02:38:55 +08:00
|
|
|
return Name.startswith(Prefix);
|
2010-01-28 02:00:17 +08:00
|
|
|
}
|
|
|
|
|
2011-07-17 03:50:32 +08:00
|
|
|
bool coreFoundation::isCFObjectRef(QualType T) {
|
|
|
|
return cocoa::isRefType(T, "CF") || // Core Foundation.
|
|
|
|
cocoa::isRefType(T, "CG") || // Core Graphics.
|
|
|
|
cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
|
|
|
|
cocoa::isRefType(T, "DADissenter") ||
|
|
|
|
cocoa::isRefType(T, "DASessionRef");
|
2010-01-28 02:00:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool cocoa::isCocoaObjectRef(QualType Ty) {
|
|
|
|
if (!Ty->isObjCObjectPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
|
|
|
|
|
|
|
|
// Can be true for objects with the 'NSObject' attribute.
|
|
|
|
if (!PT)
|
|
|
|
return true;
|
|
|
|
|
2010-08-05 08:19:24 +08:00
|
|
|
// We assume that id<..>, id, Class, and Class<..> all represent tracked
|
|
|
|
// objects.
|
2010-01-28 02:00:17 +08:00
|
|
|
if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
|
2010-08-05 08:19:24 +08:00
|
|
|
PT->isObjCClassType() || PT->isObjCQualifiedClassType())
|
2010-01-28 02:00:17 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Does the interface subclass NSObject?
|
|
|
|
// FIXME: We can memoize here if this gets too expensive.
|
|
|
|
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
|
|
|
|
|
|
|
|
// Assume that anything declared with a forward declaration and no
|
|
|
|
// @interface subclasses NSObject.
|
2011-12-16 04:29:51 +08:00
|
|
|
if (!ID->hasDefinition())
|
2010-01-28 02:00:17 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
for ( ; ID ; ID = ID->getSuperClass())
|
|
|
|
if (ID->getIdentifier()->getName() == "NSObject")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-17 03:50:32 +08:00
|
|
|
|
2011-10-01 08:48:56 +08:00
|
|
|
bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
|
|
|
|
// For now, *just* base this on the function name, not on anything else.
|
|
|
|
|
|
|
|
const IdentifierInfo *ident = fn->getIdentifier();
|
|
|
|
if (!ident) return false;
|
|
|
|
StringRef functionName = ident->getName();
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef::iterator it = functionName.begin();
|
|
|
|
StringRef::iterator start = it;
|
|
|
|
StringRef::iterator endI = functionName.end();
|
2011-07-17 03:50:36 +08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
// Scan for the start of 'create' or 'copy'.
|
|
|
|
for ( ; it != endI ; ++it) {
|
|
|
|
// Search for the first character. It can either be 'C' or 'c'.
|
|
|
|
char ch = *it;
|
|
|
|
if (ch == 'C' || ch == 'c') {
|
2011-10-01 08:48:56 +08:00
|
|
|
// Make sure this isn't something like 'recreate' or 'Scopy'.
|
2013-02-09 06:30:41 +08:00
|
|
|
if (ch == 'c' && it != start && isLetter(*(it - 1)))
|
2011-10-01 08:48:56 +08:00
|
|
|
continue;
|
|
|
|
|
2011-07-17 03:50:36 +08:00
|
|
|
++it;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did we hit the end of the string? If so, we didn't find a match.
|
|
|
|
if (it == endI)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
|
|
|
|
// character.
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef suffix = functionName.substr(it - start);
|
2011-07-17 03:50:36 +08:00
|
|
|
if (suffix.startswith("reate")) {
|
|
|
|
it += 5;
|
|
|
|
}
|
|
|
|
else if (suffix.startswith("opy")) {
|
|
|
|
it += 3;
|
2011-08-18 07:08:45 +08:00
|
|
|
} else {
|
2011-07-17 03:50:36 +08:00
|
|
|
// Keep scanning.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-09 06:30:41 +08:00
|
|
|
if (it == endI || !isLowercase(*it))
|
2011-07-17 03:50:36 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// If we matched a lowercase character, it isn't the end of the
|
|
|
|
// word. Keep scanning.
|
|
|
|
}
|
2011-07-17 03:50:32 +08:00
|
|
|
}
|