blob: 5cb55639db7852c4ce9487788559455a86326111 [file] [log] [blame]
Alexander Kornienko1ca3b832015-03-02 10:46:43 +00001//===--- AssertSideEffectCheck.cpp - clang-tidy ---------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Kornienko1ca3b832015-03-02 10:46:43 +00006//
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
20using namespace clang::ast_matchers;
21
22namespace clang {
Etienne Bergeron456177b2016-05-02 18:00:29 +000023namespace tidy {
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +000024namespace bugprone {
Etienne Bergeron456177b2016-05-02 18:00:29 +000025
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000026namespace {
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000027
28AST_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)) {
Daniel Marjamaki8ade8d22015-11-25 11:30:00 +000038 return Op->isAssignmentOp();
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000039 }
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;
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000056 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))
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000061 Result &= !MethodDecl->isConst();
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000062 }
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000063 return Result;
64 }
65
66 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
67}
68
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000069} // namespace
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000070
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000071AssertSideEffectCheck::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.
80void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
81 Options.store(Opts, "CheckFunctionCalls", CheckFunctionCalls);
82 Options.store(Opts, "AssertMacros", RawAssertList);
83}
84
85void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
Aaron Ballmana5d55a462016-01-25 20:00:53 +000086 auto DescendantWithSideEffect =
Stephen Kellya72307c2019-11-12 15:15:56 +000087 traverse(ast_type_traits::TK_AsIs,
88 hasDescendant(expr(hasSideEffect(CheckFunctionCalls))));
Aaron Ballmana5d55a462016-01-25 20:00:53 +000089 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.
Fangrui Song36fbd0d2019-07-13 07:23:12 +0000112 if (llvm::is_contained(AssertMacros, MacroName)) {
Alexander Kornienko54f36572015-08-31 14:47:14 +0000113 AssertMacroName = MacroName;
114 break;
115 }
116 Loc = SM.getImmediateMacroCallerLoc(Loc);
117 }
118 if (AssertMacroName.empty())
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000119 return;
120
Alexander Kornienkob1b2f872016-01-08 15:21:40 +0000121 diag(Loc, "found %0() with side effect") << AssertMacroName;
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000122}
123
Alexander Kornienkod4ac4af2017-11-24 14:16:29 +0000124} // namespace bugprone
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000125} // namespace tidy
126} // namespace clang