ObjCNoReturn.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //= ObjCNoReturn.cpp - Handling of Cocoa APIs known not to return --*- 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 special handling of recognizing ObjC API hooks that
  11. // do not return but aren't marked as such in API headers.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/ExprObjC.h"
  16. #include "clang/Analysis/DomainSpecific/ObjCNoReturn.h"
  17. using namespace clang;
  18. static bool isSubclass(const ObjCInterfaceDecl *Class, IdentifierInfo *II) {
  19. if (!Class)
  20. return false;
  21. if (Class->getIdentifier() == II)
  22. return true;
  23. return isSubclass(Class->getSuperClass(), II);
  24. }
  25. ObjCNoReturn::ObjCNoReturn(ASTContext &C)
  26. : RaiseSel(GetNullarySelector("raise", C)),
  27. NSExceptionII(&C.Idents.get("NSException"))
  28. {
  29. // Generate selectors.
  30. SmallVector<IdentifierInfo*, 3> II;
  31. // raise:format:
  32. II.push_back(&C.Idents.get("raise"));
  33. II.push_back(&C.Idents.get("format"));
  34. NSExceptionInstanceRaiseSelectors[0] =
  35. C.Selectors.getSelector(II.size(), &II[0]);
  36. // raise:format:arguments:
  37. II.push_back(&C.Idents.get("arguments"));
  38. NSExceptionInstanceRaiseSelectors[1] =
  39. C.Selectors.getSelector(II.size(), &II[0]);
  40. }
  41. bool ObjCNoReturn::isImplicitNoReturn(const ObjCMessageExpr *ME) {
  42. Selector S = ME->getSelector();
  43. if (ME->isInstanceMessage()) {
  44. // Check for the "raise" message.
  45. return S == RaiseSel;
  46. }
  47. if (const ObjCInterfaceDecl *ID = ME->getReceiverInterface()) {
  48. if (isSubclass(ID, NSExceptionII)) {
  49. for (unsigned i = 0; i < NUM_RAISE_SELECTORS; ++i) {
  50. if (S == NSExceptionInstanceRaiseSelectors[i])
  51. return true;
  52. }
  53. }
  54. }
  55. return false;
  56. }