comparison clang-tools-extra/clang-tidy/bugprone/AssertSideEffectCheck.cpp @ 150:1d019706d866

LLVM10
author anatofuz
date Thu, 13 Feb 2020 15:10:13 +0900
parents
children 0572611fdcc8
comparison
equal deleted inserted replaced
147:c2174574ed3a 150:1d019706d866
1 //===--- AssertSideEffectCheck.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 "AssertSideEffectCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Lex/Lexer.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Casting.h"
17 #include <algorithm>
18 #include <string>
19
20 using namespace clang::ast_matchers;
21
22 namespace clang {
23 namespace tidy {
24 namespace bugprone {
25
26 namespace {
27
28 AST_MATCHER_P(Expr, hasSideEffect, bool, CheckFunctionCalls) {
29 const Expr *E = &Node;
30
31 if (const auto *Op = dyn_cast<UnaryOperator>(E)) {
32 UnaryOperator::Opcode OC = Op->getOpcode();
33 return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
34 OC == UO_PreDec;
35 }
36
37 if (const auto *Op = dyn_cast<BinaryOperator>(E)) {
38 return Op->isAssignmentOp();
39 }
40
41 if (const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
42 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
43 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
44 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
45 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
46 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
47 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
48 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
49 OpKind == OO_PercentEqual || OpKind == OO_New ||
50 OpKind == OO_Delete || OpKind == OO_Array_New ||
51 OpKind == OO_Array_Delete;
52 }
53
54 if (const auto *CExpr = dyn_cast<CallExpr>(E)) {
55 bool Result = CheckFunctionCalls;
56 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
57 if (FuncDecl->getDeclName().isIdentifier() &&
58 FuncDecl->getName() == "__builtin_expect") // exceptions come here
59 Result = false;
60 else if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
61 Result &= !MethodDecl->isConst();
62 }
63 return Result;
64 }
65
66 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
67 }
68
69 } // namespace
70
71 AssertSideEffectCheck::AssertSideEffectCheck(StringRef Name,
72 ClangTidyContext *Context)
73 : ClangTidyCheck(Name, Context),
74 CheckFunctionCalls(Options.get("CheckFunctionCalls", false)),
75 RawAssertList(Options.get("AssertMacros", "assert")) {
76 StringRef(RawAssertList).split(AssertMacros, ",", -1, false);
77 }
78
79 // The options are explained in AssertSideEffectCheck.h.
80 void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
81 Options.store(Opts, "CheckFunctionCalls", CheckFunctionCalls);
82 Options.store(Opts, "AssertMacros", RawAssertList);
83 }
84
85 void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
86 auto DescendantWithSideEffect =
87 hasDescendant(expr(hasSideEffect(CheckFunctionCalls)));
88 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
89 Finder->addMatcher(
90 stmt(
91 anyOf(conditionalOperator(ConditionWithSideEffect),
92 ifStmt(ConditionWithSideEffect),
93 unaryOperator(hasOperatorName("!"),
94 hasUnaryOperand(unaryOperator(
95 hasOperatorName("!"),
96 hasUnaryOperand(DescendantWithSideEffect))))))
97 .bind("condStmt"),
98 this);
99 }
100
101 void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
102 const SourceManager &SM = *Result.SourceManager;
103 const LangOptions LangOpts = getLangOpts();
104 SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getBeginLoc();
105
106 StringRef AssertMacroName;
107 while (Loc.isValid() && Loc.isMacroID()) {
108 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
109
110 // Check if this macro is an assert.
111 if (llvm::is_contained(AssertMacros, MacroName)) {
112 AssertMacroName = MacroName;
113 break;
114 }
115 Loc = SM.getImmediateMacroCallerLoc(Loc);
116 }
117 if (AssertMacroName.empty())
118 return;
119
120 diag(Loc, "found %0() with side effect") << AssertMacroName;
121 }
122
123 } // namespace bugprone
124 } // namespace tidy
125 } // namespace clang