clang-tools 10.0.0

MustCheckErrsCheck.cpp
Go to the documentation of this file.
1 //===--- MustCheckErrsCheck.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
9 #include "MustCheckErrsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang {
16 namespace tidy {
17 namespace linuxkernel {
18
19 void MustCheckErrsCheck::registerMatchers(MatchFinder *Finder) {
20 auto ErrFn =
21 functionDecl(hasAnyName("ERR_PTR", "PTR_ERR", "IS_ERR", "IS_ERR_OR_NULL",
22 "ERR_CAST", "PTR_ERR_OR_ZERO"));
23 auto NonCheckingStmts = stmt(anyOf(compoundStmt(), labelStmt()));
24 Finder->addMatcher(
25 callExpr(callee(ErrFn), hasParent(NonCheckingStmts)).bind("call"),
26 this);
27
28 auto ReturnToCheck = returnStmt(hasReturnValue(callExpr(callee(ErrFn))));
29 auto ReturnsErrFn = functionDecl(hasDescendant(ReturnToCheck));
30 Finder->addMatcher(callExpr(callee(ReturnsErrFn), hasParent(NonCheckingStmts))
31 .bind("transitive_call"),
32 this);
33 }
34
35 void MustCheckErrsCheck::check(const MatchFinder::MatchResult &Result) {
36 const CallExpr *MatchedCallExpr = Result.Nodes.getNodeAs<CallExpr>("call");
37 if (MatchedCallExpr) {
38 diag(MatchedCallExpr->getExprLoc(), "result from function %0 is unused")
39 << MatchedCallExpr->getDirectCallee();
40 }
41
42 const CallExpr *MatchedTransitiveCallExpr =
43 Result.Nodes.getNodeAs<CallExpr>("transitive_call");
44 if (MatchedTransitiveCallExpr) {
45 diag(MatchedTransitiveCallExpr->getExprLoc(),
46 "result from function %0 is unused but represents an error value")
47 << MatchedTransitiveCallExpr->getDirectCallee();
48 }
49 }
50
51 } // namespace linuxkernel
52 } // namespace tidy
53 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
MustCheckErrsCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27