blob: fac919a48cf8f180951c64d4847931a009a5bf5e [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 {
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000024namespace {
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000025
26AST_MATCHER_P(Expr, hasSideEffect, bool, CheckFunctionCalls) {
27 const Expr *E = &Node;
28
29 if (const auto *Op = dyn_cast<UnaryOperator>(E)) {
30 UnaryOperator::Opcode OC = Op->getOpcode();
31 return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
32 OC == UO_PreDec;
33 }
34
35 if (const auto *Op = dyn_cast<BinaryOperator>(E)) {
36 BinaryOperator::Opcode OC = Op->getOpcode();
37 return OC == BO_Assign || OC == BO_MulAssign || OC == BO_DivAssign ||
38 OC == BO_RemAssign || OC == BO_AddAssign || OC == BO_SubAssign ||
39 OC == BO_ShlAssign || OC == BO_ShrAssign || OC == BO_AndAssign ||
40 OC == BO_XorAssign || OC == BO_OrAssign;
41 }
42
43 if (const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
44 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
45 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
46 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
47 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
48 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
49 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
50 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
51 OpKind == OO_PercentEqual || OpKind == OO_New ||
52 OpKind == OO_Delete || OpKind == OO_Array_New ||
53 OpKind == OO_Array_Delete;
54 }
55
56 if (const auto *CExpr = dyn_cast<CallExpr>(E)) {
57 bool Result = CheckFunctionCalls;
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000058 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
59 if (FuncDecl->getDeclName().isIdentifier() &&
60 FuncDecl->getName() == "__builtin_expect") // exceptions come here
61 Result = false;
62 else if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000063 Result &= !MethodDecl->isConst();
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000064 }
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000065 return Result;
66 }
67
68 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
69}
70
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000071} // namespace
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000072
73namespace tidy {
74
75AssertSideEffectCheck::AssertSideEffectCheck(StringRef Name,
76 ClangTidyContext *Context)
77 : ClangTidyCheck(Name, Context),
78 CheckFunctionCalls(Options.get("CheckFunctionCalls", false)),
79 RawAssertList(Options.get("AssertMacros", "assert")) {
80 StringRef(RawAssertList).split(AssertMacros, ",", -1, false);
81}
82
83// The options are explained in AssertSideEffectCheck.h.
84void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
85 Options.store(Opts, "CheckFunctionCalls", CheckFunctionCalls);
86 Options.store(Opts, "AssertMacros", RawAssertList);
87}
88
89void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
90 auto ConditionWithSideEffect =
91 hasCondition(hasDescendant(expr(hasSideEffect(CheckFunctionCalls))));
92 Finder->addMatcher(
93 stmt(anyOf(conditionalOperator(ConditionWithSideEffect),
94 ifStmt(ConditionWithSideEffect))).bind("condStmt"),
95 this);
96}
97
98void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko54f36572015-08-31 14:47:14 +000099 const SourceManager &SM = *Result.SourceManager;
100 const LangOptions LangOpts = Result.Context->getLangOpts();
101 SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getLocStart();
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000102
Alexander Kornienko54f36572015-08-31 14:47:14 +0000103 StringRef AssertMacroName;
104 while (Loc.isValid() && Loc.isMacroID()) {
105 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
106
107 // Check if this macro is an assert.
108 if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
109 AssertMacros.end()) {
110 AssertMacroName = MacroName;
111 break;
112 }
113 Loc = SM.getImmediateMacroCallerLoc(Loc);
114 }
115 if (AssertMacroName.empty())
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000116 return;
117
Alexander Kornienko54f36572015-08-31 14:47:14 +0000118 diag(Loc, "found " + AssertMacroName.str() + "() with side effect");
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000119}
120
121} // namespace tidy
122} // namespace clang