brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 92d590c Raw
56 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "AvoidThrowingObjCExceptionCheck.h"10#include "clang/ASTMatchers/ASTMatchFinder.h"11 12using namespace clang::ast_matchers;13 14namespace clang::tidy::google::objc {15 16void AvoidThrowingObjCExceptionCheck::registerMatchers(MatchFinder *Finder) {17  Finder->addMatcher(objcThrowStmt().bind("throwStmt"), this);18  Finder->addMatcher(19      objcMessageExpr(anyOf(hasSelector("raise:format:"),20                            hasSelector("raise:format:arguments:")),21                      hasReceiverType(asString("NSException")))22          .bind("raiseException"),23      this);24}25 26void AvoidThrowingObjCExceptionCheck::check(27    const MatchFinder::MatchResult &Result) {28  const auto *MatchedStmt =29      Result.Nodes.getNodeAs<ObjCAtThrowStmt>("throwStmt");30  const auto *MatchedExpr =31      Result.Nodes.getNodeAs<ObjCMessageExpr>("raiseException");32  auto SourceLoc = MatchedStmt == nullptr ? MatchedExpr->getSelectorStartLoc()33                                          : MatchedStmt->getThrowLoc();34 35  // Early return on invalid locations.36  if (SourceLoc.isInvalid())37    return;38 39  // If the match location was in a macro, check if the macro was in a system40  // header.41  if (SourceLoc.isMacroID()) {42    const SourceManager &SM = *Result.SourceManager;43    auto MacroLoc = SM.getImmediateMacroCallerLoc(SourceLoc);44 45    // Matches in system header macros should be ignored.46    if (SM.isInSystemHeader(MacroLoc))47      return;48  }49 50  diag(SourceLoc,51       "pass in NSError ** instead of throwing exception to indicate "52       "Objective-C errors");53}54 55} // namespace clang::tidy::google::objc56