blob: 6b93d9c9253ddd7f4e5202cfbbd4141169d2e9e7 [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)) {
Daniel Marjamaki8ade8d22015-11-25 11:30:00 +000036 return Op->isAssignmentOp();
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000037 }
38
39 if (const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
40 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
41 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
42 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
43 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
44 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
45 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
46 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
47 OpKind == OO_PercentEqual || OpKind == OO_New ||
48 OpKind == OO_Delete || OpKind == OO_Array_New ||
49 OpKind == OO_Array_Delete;
50 }
51
52 if (const auto *CExpr = dyn_cast<CallExpr>(E)) {
53 bool Result = CheckFunctionCalls;
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000054 if (const auto *FuncDecl = CExpr->getDirectCallee()) {
55 if (FuncDecl->getDeclName().isIdentifier() &&
56 FuncDecl->getName() == "__builtin_expect") // exceptions come here
57 Result = false;
58 else if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000059 Result &= !MethodDecl->isConst();
Szabolcs Sipos43a298c2015-05-29 09:49:59 +000060 }
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000061 return Result;
62 }
63
64 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
65}
66
Alexander Kornienko50d7f4612015-06-17 13:11:37 +000067} // namespace
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000068
69namespace tidy {
70
71AssertSideEffectCheck::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) {
86 auto ConditionWithSideEffect =
87 hasCondition(hasDescendant(expr(hasSideEffect(CheckFunctionCalls))));
88 Finder->addMatcher(
89 stmt(anyOf(conditionalOperator(ConditionWithSideEffect),
90 ifStmt(ConditionWithSideEffect))).bind("condStmt"),
91 this);
92}
93
94void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko54f36572015-08-31 14:47:14 +000095 const SourceManager &SM = *Result.SourceManager;
96 const LangOptions LangOpts = Result.Context->getLangOpts();
97 SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getLocStart();
Alexander Kornienko1ca3b832015-03-02 10:46:43 +000098
Alexander Kornienko54f36572015-08-31 14:47:14 +000099 StringRef AssertMacroName;
100 while (Loc.isValid() && Loc.isMacroID()) {
101 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
102
103 // Check if this macro is an assert.
104 if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
105 AssertMacros.end()) {
106 AssertMacroName = MacroName;
107 break;
108 }
109 Loc = SM.getImmediateMacroCallerLoc(Loc);
110 }
111 if (AssertMacroName.empty())
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000112 return;
113
Alexander Kornienkob1b2f872016-01-08 15:21:40 +0000114 diag(Loc, "found %0() with side effect") << AssertMacroName;
Alexander Kornienko1ca3b832015-03-02 10:46:43 +0000115}
116
117} // namespace tidy
118} // namespace clang