AvoidThrowingObjCExceptionCheck.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //===--- AvoidThrowingObjCExceptionCheck.cpp - clang-tidy------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "AvoidThrowingObjCExceptionCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. using namespace clang::ast_matchers;
  12. namespace clang::tidy::google::objc {
  13. void AvoidThrowingObjCExceptionCheck::registerMatchers(MatchFinder *Finder) {
  14. Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this);
  15. Finder->addMatcher(
  16. objcMessageExpr(anyOf(hasSelector("raise:format:"),
  17. hasSelector("raise:format:arguments:")),
  18. hasReceiverType(asString("NSException")))
  19. .bind("raiseException"),
  20. this);
  21. }
  22. void AvoidThrowingObjCExceptionCheck::check(
  23. const MatchFinder::MatchResult &Result) {
  24. const auto *MatchedStmt =
  25. Result.Nodes.getNodeAs<ObjCAtThrowStmt>("throwStmt");
  26. const auto *MatchedExpr =
  27. Result.Nodes.getNodeAs<ObjCMessageExpr>("raiseException");
  28. auto SourceLoc = MatchedStmt == nullptr ? MatchedExpr->getSelectorStartLoc()
  29. : MatchedStmt->getThrowLoc();
  30. // Early return on invalid locations.
  31. if (SourceLoc.isInvalid())
  32. return;
  33. // If the match location was in a macro, check if the macro was in a system
  34. // header.
  35. if (SourceLoc.isMacroID()) {
  36. SourceManager &SM = *Result.SourceManager;
  37. auto MacroLoc = SM.getImmediateMacroCallerLoc(SourceLoc);
  38. // Matches in system header macros should be ignored.
  39. if (SM.isInSystemHeader(MacroLoc))
  40. return;
  41. }
  42. diag(SourceLoc,
  43. "pass in NSError ** instead of throwing exception to indicate "
  44. "Objective-C errors");
  45. }
  46. } // namespace clang::tidy::google::objc