2010-01-27 14:13:48 +08:00
|
|
|
//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-01-27 14:13:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-07-31 03:24:48 +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();
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-02-09 02:38:55 +08:00
|
|
|
if (Name.empty())
|
2010-01-28 02:00:17 +08:00
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
// Is the type void*?
|
2019-08-29 05:19:58 +08:00
|
|
|
const PointerType* PT = RetTy->castAs<PointerType>();
|
|
|
|
if (!PT || !PT->getPointeeType().getUnqualifiedType()->isVoidType())
|
2010-01-28 02:00:17 +08:00
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-11-25 22:57:42 +08:00
|
|
|
/// Returns true when the passed-in type is a CF-style reference-counted
|
|
|
|
/// type from the DiskArbitration framework.
|
|
|
|
static bool isDiskArbitrationAPIRefType(QualType T) {
|
|
|
|
return cocoa::isRefType(T, "DADisk") ||
|
|
|
|
cocoa::isRefType(T, "DADissenter") ||
|
|
|
|
cocoa::isRefType(T, "DASessionRef");
|
|
|
|
}
|
|
|
|
|
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.
|
2017-11-25 22:57:42 +08:00
|
|
|
cocoa::isRefType(T, "CM") || // Core Media.
|
|
|
|
isDiskArbitrationAPIRefType(T);
|
2010-01-28 02:00:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool cocoa::isCocoaObjectRef(QualType Ty) {
|
|
|
|
if (!Ty->isObjCObjectPointerType())
|
|
|
|
return false;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
// Can be true for objects with the 'NSObject' attribute.
|
|
|
|
if (!PT)
|
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
// Does the interface subclass NSObject?
|
|
|
|
// FIXME: We can memoize here if this gets too expensive.
|
|
|
|
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
// 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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
for ( ; ID ; ID = ID->getSuperClass())
|
|
|
|
if (ID->getIdentifier()->getName() == "NSObject")
|
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-01-28 02:00:17 +08:00
|
|
|
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();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef::iterator it = functionName.begin();
|
|
|
|
StringRef::iterator start = it;
|
|
|
|
StringRef::iterator endI = functionName.end();
|
2018-07-31 03:24:48 +08:00
|
|
|
|
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;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-07-17 03:50:36 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2013-02-09 06:30:41 +08:00
|
|
|
if (it == endI || !isLowercase(*it))
|
2011-07-17 03:50:36 +08:00
|
|
|
return true;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-07-17 03:50:36 +08:00
|
|
|
// If we matched a lowercase character, it isn't the end of the
|
|
|
|
// word. Keep scanning.
|
|
|
|
}
|
2011-07-17 03:50:32 +08:00
|
|
|
}
|