CocoaConventions.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements cocoa naming convention analysis.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/DeclObjC.h"
  16. #include "clang/AST/Type.h"
  17. #include "clang/Basic/CharInfo.h"
  18. #include "llvm/ADT/StringExtras.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. using namespace clang;
  21. using namespace ento;
  22. bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
  23. StringRef Name) {
  24. // Recursively walk the typedef stack, allowing typedefs of reference types.
  25. while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
  26. StringRef TDName = TD->getDecl()->getIdentifier()->getName();
  27. if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
  28. return true;
  29. // XPC unfortunately uses CF-style function names, but aren't CF types.
  30. if (TDName.startswith("xpc_"))
  31. return false;
  32. RetTy = TD->getDecl()->getUnderlyingType();
  33. }
  34. if (Name.empty())
  35. return false;
  36. // Is the type void*?
  37. const PointerType* PT = RetTy->getAs<PointerType>();
  38. if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
  39. return false;
  40. // Does the name start with the prefix?
  41. return Name.startswith(Prefix);
  42. }
  43. bool coreFoundation::isCFObjectRef(QualType T) {
  44. return cocoa::isRefType(T, "CF") || // Core Foundation.
  45. cocoa::isRefType(T, "CG") || // Core Graphics.
  46. cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
  47. cocoa::isRefType(T, "DADissenter") ||
  48. cocoa::isRefType(T, "DASessionRef");
  49. }
  50. bool cocoa::isCocoaObjectRef(QualType Ty) {
  51. if (!Ty->isObjCObjectPointerType())
  52. return false;
  53. const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
  54. // Can be true for objects with the 'NSObject' attribute.
  55. if (!PT)
  56. return true;
  57. // We assume that id<..>, id, Class, and Class<..> all represent tracked
  58. // objects.
  59. if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
  60. PT->isObjCClassType() || PT->isObjCQualifiedClassType())
  61. return true;
  62. // Does the interface subclass NSObject?
  63. // FIXME: We can memoize here if this gets too expensive.
  64. const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
  65. // Assume that anything declared with a forward declaration and no
  66. // @interface subclasses NSObject.
  67. if (!ID->hasDefinition())
  68. return true;
  69. for ( ; ID ; ID = ID->getSuperClass())
  70. if (ID->getIdentifier()->getName() == "NSObject")
  71. return true;
  72. return false;
  73. }
  74. bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
  75. // For now, *just* base this on the function name, not on anything else.
  76. const IdentifierInfo *ident = fn->getIdentifier();
  77. if (!ident) return false;
  78. StringRef functionName = ident->getName();
  79. StringRef::iterator it = functionName.begin();
  80. StringRef::iterator start = it;
  81. StringRef::iterator endI = functionName.end();
  82. while (true) {
  83. // Scan for the start of 'create' or 'copy'.
  84. for ( ; it != endI ; ++it) {
  85. // Search for the first character. It can either be 'C' or 'c'.
  86. char ch = *it;
  87. if (ch == 'C' || ch == 'c') {
  88. // Make sure this isn't something like 'recreate' or 'Scopy'.
  89. if (ch == 'c' && it != start && isLetter(*(it - 1)))
  90. continue;
  91. ++it;
  92. break;
  93. }
  94. }
  95. // Did we hit the end of the string? If so, we didn't find a match.
  96. if (it == endI)
  97. return false;
  98. // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
  99. // character.
  100. StringRef suffix = functionName.substr(it - start);
  101. if (suffix.startswith("reate")) {
  102. it += 5;
  103. }
  104. else if (suffix.startswith("opy")) {
  105. it += 3;
  106. } else {
  107. // Keep scanning.
  108. continue;
  109. }
  110. if (it == endI || !isLowercase(*it))
  111. return true;
  112. // If we matched a lowercase character, it isn't the end of the
  113. // word. Keep scanning.
  114. }
  115. }