CocoaConventions.cpp [plain text]
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/Type.h"
#include "clang/Basic/CharInfo.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
using namespace ento;
bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
StringRef Name) {
while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
StringRef TDName = TD->getDecl()->getIdentifier()->getName();
if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
return true;
if (TDName.startswith("xpc_"))
return false;
RetTy = TD->getDecl()->getUnderlyingType();
}
if (Name.empty())
return false;
const PointerType* PT = RetTy->getAs<PointerType>();
if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
return false;
return Name.startswith(Prefix);
}
bool coreFoundation::isCFObjectRef(QualType T) {
return cocoa::isRefType(T, "CF") || cocoa::isRefType(T, "CG") || cocoa::isRefType(T, "DADisk") || cocoa::isRefType(T, "DADissenter") ||
cocoa::isRefType(T, "DASessionRef");
}
bool cocoa::isCocoaObjectRef(QualType Ty) {
if (!Ty->isObjCObjectPointerType())
return false;
const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
if (!PT)
return true;
if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
PT->isObjCClassType() || PT->isObjCQualifiedClassType())
return true;
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
if (!ID->hasDefinition())
return true;
for ( ; ID ; ID = ID->getSuperClass())
if (ID->getIdentifier()->getName() == "NSObject")
return true;
return false;
}
bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
const IdentifierInfo *ident = fn->getIdentifier();
if (!ident) return false;
StringRef functionName = ident->getName();
StringRef::iterator it = functionName.begin();
StringRef::iterator start = it;
StringRef::iterator endI = functionName.end();
while (true) {
for ( ; it != endI ; ++it) {
char ch = *it;
if (ch == 'C' || ch == 'c') {
if (ch == 'c' && it != start && isLetter(*(it - 1)))
continue;
++it;
break;
}
}
if (it == endI)
return false;
StringRef suffix = functionName.substr(it - start);
if (suffix.startswith("reate")) {
it += 5;
}
else if (suffix.startswith("opy")) {
it += 3;
} else {
continue;
}
if (it == endI || !isLowercase(*it))
return true;
}
}