Path: blob/main/contrib/llvm-project/clang/lib/Analysis/CocoaConventions.cpp
35233 views
//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements cocoa naming convention analysis.9//10//===----------------------------------------------------------------------===//1112#include "clang/Analysis/DomainSpecific/CocoaConventions.h"13#include "clang/AST/Decl.h"14#include "clang/AST/DeclObjC.h"15#include "clang/AST/Type.h"16#include "clang/Basic/CharInfo.h"17#include "llvm/ADT/StringExtras.h"18#include "llvm/Support/ErrorHandling.h"1920using namespace clang;21using namespace ento;2223bool cocoa::isRefType(QualType RetTy, StringRef Prefix,24StringRef Name) {25// Recursively walk the typedef stack, allowing typedefs of reference types.26while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {27StringRef TDName = TD->getDecl()->getIdentifier()->getName();28if (TDName.starts_with(Prefix) && TDName.ends_with("Ref"))29return true;30// XPC unfortunately uses CF-style function names, but aren't CF types.31if (TDName.starts_with("xpc_"))32return false;33RetTy = TD->getDecl()->getUnderlyingType();34}3536if (Name.empty())37return false;3839// Is the type void*?40const PointerType* PT = RetTy->castAs<PointerType>();41if (!PT || !PT->getPointeeType().getUnqualifiedType()->isVoidType())42return false;4344// Does the name start with the prefix?45return Name.starts_with(Prefix);46}4748/// Returns true when the passed-in type is a CF-style reference-counted49/// type from the DiskArbitration framework.50static bool isDiskArbitrationAPIRefType(QualType T) {51return cocoa::isRefType(T, "DADisk") ||52cocoa::isRefType(T, "DADissenter") ||53cocoa::isRefType(T, "DASessionRef");54}5556bool coreFoundation::isCFObjectRef(QualType T) {57return cocoa::isRefType(T, "CF") || // Core Foundation.58cocoa::isRefType(T, "CG") || // Core Graphics.59cocoa::isRefType(T, "CM") || // Core Media.60isDiskArbitrationAPIRefType(T);61}626364bool cocoa::isCocoaObjectRef(QualType Ty) {65if (!Ty->isObjCObjectPointerType())66return false;6768const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();6970// Can be true for objects with the 'NSObject' attribute.71if (!PT)72return true;7374// We assume that id<..>, id, Class, and Class<..> all represent tracked75// objects.76if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||77PT->isObjCClassType() || PT->isObjCQualifiedClassType())78return true;7980// Does the interface subclass NSObject?81// FIXME: We can memoize here if this gets too expensive.82const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();8384// Assume that anything declared with a forward declaration and no85// @interface subclasses NSObject.86if (!ID->hasDefinition())87return true;8889for ( ; ID ; ID = ID->getSuperClass())90if (ID->getIdentifier()->getName() == "NSObject")91return true;9293return false;94}9596bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {97// For now, *just* base this on the function name, not on anything else.9899const IdentifierInfo *ident = fn->getIdentifier();100if (!ident) return false;101StringRef functionName = ident->getName();102103StringRef::iterator it = functionName.begin();104StringRef::iterator start = it;105StringRef::iterator endI = functionName.end();106107while (true) {108// Scan for the start of 'create' or 'copy'.109for ( ; it != endI ; ++it) {110// Search for the first character. It can either be 'C' or 'c'.111char ch = *it;112if (ch == 'C' || ch == 'c') {113// Make sure this isn't something like 'recreate' or 'Scopy'.114if (ch == 'c' && it != start && isLetter(*(it - 1)))115continue;116117++it;118break;119}120}121122// Did we hit the end of the string? If so, we didn't find a match.123if (it == endI)124return false;125126// Scan for *lowercase* 'reate' or 'opy', followed by no lowercase127// character.128StringRef suffix = functionName.substr(it - start);129if (suffix.starts_with("reate")) {130it += 5;131} else if (suffix.starts_with("opy")) {132it += 3;133} else {134// Keep scanning.135continue;136}137138if (it == endI || !isLowercase(*it))139return true;140141// If we matched a lowercase character, it isn't the end of the142// word. Keep scanning.143}144}145146147