blob: c747980b909c43cc6f9e7c80669c4ea234cb8082 [file] [log] [blame]
Alexander Kornienko1ca3b832015-03-02 10:46:43 +00001//===--- AssertSideEffectCheck.cpp - clang-tidy ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "AssertSideEffectCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Frontend/CompilerInstance.h"
14#include "clang/Lex/Lexer.h"
15#include "llvm/ADT/SmallVector.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/Support/Casting.h"
18#include <algorithm>
19#include <string>
20
21using namespace clang::ast_matchers;
22
23namespace clang {
Etienne Bergeron456177b2016-05-02 18:00:29 +000024namespace tidy {
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +000025namespace bugprone {
Etienne Bergeron456177b2016-05-02 18:00:29 +000026
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000027namespace {
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000028
29AST_MATCHER_P(Expr, hasSideEffect, bool, CheckFunctionCalls) {
30 const Expr *E = &Node;
31
32 if (const auto *Op = dyn_cast<UnaryOperator>(E)) {
33 UnaryOperator::Opcode OC = Op->getOpcode();
34 return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
35 OC == UO_PreDec;
36 }
37
38 if (const auto *Op = dyn_cast<BinaryOperator>(E)) {
Daniel Marjamaki8ade8d22015-11-25 11:30:00 +000039 return Op->isAssignmentOp();
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000040 }
41
42 if (const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
43 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
44 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
45 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
46 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
47 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
48 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
49 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
50 OpKind == OO_PercentEqual || OpKind == OO_New ||
51 OpKind == OO_Delete || OpKind == OO_Array_New ||
52 OpKind == OO_Array_Delete;
53 }
54
55 if (const auto *CExpr = dyn_cast<CallExpr>(E)) {
56 bool Result = CheckFunctionCalls;
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000057 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
58 if (FuncDecl->getDeclName().isIdentifier() &&
59 FuncDecl->getName() == "__builtin_expect") // exceptions come here
60 Result = false;
61 else if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000062 Result &= !MethodDecl->isConst();
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000063 }
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000064 return Result;
65 }
66
67 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
68}
69
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000070} // namespace
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000071
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000072AssertSideEffectCheck::AssertSideEffectCheck(StringRef Name,
73 ClangTidyContext *Context)
74 : ClangTidyCheck(Name, Context),
75 CheckFunctionCalls(Options.get("CheckFunctionCalls", false)),
76 RawAssertList(Options.get("AssertMacros", "assert")) {
77 StringRef(RawAssertList).split(AssertMacros, ",", -1, false);
78}
79
80// The options are explained in AssertSideEffectCheck.h.
81void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
82 Options.store(Opts, "CheckFunctionCalls", CheckFunctionCalls);
83 Options.store(Opts, "AssertMacros", RawAssertList);
84}
85
86void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballmana5d55a462016-01-25 20:00:53 +000087 auto DescendantWithSideEffect =
88 hasDescendant(expr(hasSideEffect(CheckFunctionCalls)));
89 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000090 Finder->addMatcher(
Aaron Ballmana5d55a462016-01-25 20:00:53 +000091 stmt(
92 anyOf(conditionalOperator(ConditionWithSideEffect),
93 ifStmt(ConditionWithSideEffect),
94 unaryOperator(hasOperatorName("!"),
95 hasUnaryOperand(unaryOperator(
96 hasOperatorName("!"),
97 hasUnaryOperand(DescendantWithSideEffect))))))
98 .bind("condStmt"),
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000099 this);
100}
101
102void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko54f36572015-08-31 14:47:14 +0000103 const SourceManager &SM = *Result.SourceManager;
Gabor Horvathafad84c2016-09-24 02:13:45 +0000104 const LangOptions LangOpts = getLangOpts();
Stephen Kelly43465bf2018-08-09 22:42:26 +0000105 SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getBeginLoc();
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000106
Alexander Kornienko54f36572015-08-31 14:47:14 +0000107 StringRef AssertMacroName;
108 while (Loc.isValid() && Loc.isMacroID()) {
109 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
110
111 // Check if this macro is an assert.
112 if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
113 AssertMacros.end()) {
114 AssertMacroName = MacroName;
115 break;
116 }
117 Loc = SM.getImmediateMacroCallerLoc(Loc);
118 }
119 if (AssertMacroName.empty())
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000120 return;
121
Alexander Kornienkob1b2f872016-01-08 15:21:40 +0000122 diag(Loc, "found %0() with side effect") << AssertMacroName;
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000123}
124
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +0000125} // namespace bugprone
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000126} // namespace tidy
127} // namespace clang