clang-tools  14.0.0git
PreferIsaOrDynCastInConditionalsCheck.cpp
Go to the documentation of this file.
1 //===--- PreferIsaOrDynCastInConditionalsCheck.cpp - clang-tidy
2 //---------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace ast_matchers {
19 AST_MATCHER(Expr, isMacroID) { return Node.getExprLoc().isMacroID(); }
20 } // namespace ast_matchers
21 
22 namespace tidy {
23 namespace llvm_check {
24 
25 void PreferIsaOrDynCastInConditionalsCheck::registerMatchers(
26  MatchFinder *Finder) {
27  auto Condition = hasCondition(implicitCastExpr(has(
28  callExpr(
29  allOf(unless(isMacroID()), unless(cxxMemberCallExpr()),
30  anyOf(callee(namedDecl(hasName("cast"))),
31  callee(namedDecl(hasName("dyn_cast")).bind("dyn_cast")))))
32  .bind("call"))));
33 
34  auto Any = anyOf(
35  has(declStmt(containsDeclaration(
36  0,
37  varDecl(hasInitializer(
38  callExpr(allOf(unless(isMacroID()), unless(cxxMemberCallExpr()),
39  callee(namedDecl(hasName("cast")))))
40  .bind("assign")))))),
41  Condition);
42 
43  auto CallExpression =
44  callExpr(
45  allOf(
46  unless(isMacroID()), unless(cxxMemberCallExpr()),
47  allOf(callee(namedDecl(hasAnyName("isa", "cast", "cast_or_null",
48  "dyn_cast", "dyn_cast_or_null"))
49  .bind("func")),
50  hasArgument(
51  0,
52  mapAnyOf(declRefExpr, cxxMemberCallExpr).bind("arg")))))
53  .bind("rhs");
54 
55  Finder->addMatcher(
56  traverse(TK_AsIs,
57  stmt(anyOf(
58  ifStmt(Any), whileStmt(Any), doStmt(Condition),
59  binaryOperator(
60  allOf(unless(isExpansionInFileMatching(
61  "llvm/include/llvm/Support/Casting.h")),
62  hasOperatorName("&&"),
63  hasLHS(implicitCastExpr().bind("lhs")),
64  hasRHS(anyOf(implicitCastExpr(has(CallExpression)),
65  CallExpression))))
66  .bind("and")))),
67  this);
68 }
69 
71  const MatchFinder::MatchResult &Result) {
72  if (const auto *MatchedDecl = Result.Nodes.getNodeAs<CallExpr>("assign")) {
73  SourceLocation StartLoc = MatchedDecl->getCallee()->getExprLoc();
74  SourceLocation EndLoc =
75  StartLoc.getLocWithOffset(StringRef("cast").size() - 1);
76 
77  diag(MatchedDecl->getBeginLoc(),
78  "cast<> in conditional will assert rather than return a null pointer")
79  << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc),
80  "dyn_cast");
81  } else if (const auto *MatchedDecl =
82  Result.Nodes.getNodeAs<CallExpr>("call")) {
83  SourceLocation StartLoc = MatchedDecl->getCallee()->getExprLoc();
84  SourceLocation EndLoc =
85  StartLoc.getLocWithOffset(StringRef("cast").size() - 1);
86 
87  StringRef Message =
88  "cast<> in conditional will assert rather than return a null pointer";
89  if (Result.Nodes.getNodeAs<NamedDecl>("dyn_cast"))
90  Message = "return value from dyn_cast<> not used";
91 
92  diag(MatchedDecl->getBeginLoc(), Message)
93  << FixItHint::CreateReplacement(SourceRange(StartLoc, EndLoc), "isa");
94  } else if (const auto *MatchedDecl =
95  Result.Nodes.getNodeAs<BinaryOperator>("and")) {
96  const auto *LHS = Result.Nodes.getNodeAs<ImplicitCastExpr>("lhs");
97  const auto *RHS = Result.Nodes.getNodeAs<CallExpr>("rhs");
98  const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
99  const auto *Func = Result.Nodes.getNodeAs<NamedDecl>("func");
100 
101  assert(LHS && "LHS is null");
102  assert(RHS && "RHS is null");
103  assert(Arg && "Arg is null");
104  assert(Func && "Func is null");
105 
106  StringRef LHSString(Lexer::getSourceText(
107  CharSourceRange::getTokenRange(LHS->getSourceRange()),
108  *Result.SourceManager, getLangOpts()));
109 
110  StringRef ArgString(Lexer::getSourceText(
111  CharSourceRange::getTokenRange(Arg->getSourceRange()),
112  *Result.SourceManager, getLangOpts()));
113 
114  if (ArgString != LHSString)
115  return;
116 
117  StringRef RHSString(Lexer::getSourceText(
118  CharSourceRange::getTokenRange(RHS->getSourceRange()),
119  *Result.SourceManager, getLangOpts()));
120 
121  std::string Replacement("isa_and_nonnull");
122  Replacement += RHSString.substr(Func->getName().size());
123 
124  diag(MatchedDecl->getBeginLoc(),
125  "isa_and_nonnull<> is preferred over an explicit test for null "
126  "followed by calling isa<>")
127  << FixItHint::CreateReplacement(SourceRange(MatchedDecl->getBeginLoc(),
128  MatchedDecl->getEndLoc()),
129  Replacement);
130  }
131 }
132 
133 } // namespace llvm_check
134 } // namespace tidy
135 } // namespace clang
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
clang::tidy::cppcoreguidelines::getSourceText
static std::string getSourceText(const CXXDestructorDecl &Destructor)
Definition: VirtualClassDestructorCheck.cpp:109
clang::ast_matchers
Definition: AbseilMatcher.h:14
Condition
std::string Condition
Condition used after the preprocessor directive.
Definition: RedundantPreprocessorCheck.cpp:24
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::clangd::check
bool check(llvm::StringRef File, llvm::function_ref< bool(const Position &)> ShouldCheckLine, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:253
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::readability::isMacroID
static bool isMacroID(SourceRange R)
Definition: IsolateDeclarationCheck.cpp:52
PreferIsaOrDynCastInConditionalsCheck.h