1 //===--- 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 
21 using namespace clang::ast_matchers;
22 
23 namespace clang {
24 namespace tidy {
25 namespace bugprone {
26 
27 namespace {
28 
29 AST_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)) {
39     return Op->isAssignmentOp();
40   }
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;
57     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))
62         Result &= !MethodDecl->isConst();
63     }
64     return Result;
65   }
66 
67   return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
68 }
69 
70 } // namespace
71 
72 AssertSideEffectCheck::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.
81 void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
82   Options.store(Opts, "CheckFunctionCalls", CheckFunctionCalls);
83   Options.store(Opts, "AssertMacros", RawAssertList);
84 }
85 
86 void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
87   auto DescendantWithSideEffect =
88       hasDescendant(expr(hasSideEffect(CheckFunctionCalls)));
89   auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
90   Finder->addMatcher(
91       stmt(
92           anyOf(conditionalOperator(ConditionWithSideEffect),
93                 ifStmt(ConditionWithSideEffect),
94                 unaryOperator(hasOperatorName("!"),
95                               hasUnaryOperand(unaryOperator(
96                                   hasOperatorName("!"),
97                                   hasUnaryOperand(DescendantWithSideEffect))))))
98           .bind("condStmt"),
99       this);
100 }
101 
102 void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
103   const SourceManager &SM = *Result.SourceManager;
104   const LangOptions LangOpts = getLangOpts();
105   SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getLocStart();
106 
107   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())
120     return;
121 
122   diag(Loc, "found %0() with side effect") << AssertMacroName;
123 }
124 
125 } // namespace bugprone
126 } // namespace tidy
127 } // namespace clang
128