llvm-project
151 строка · 5.7 Кб
1//===--- AssertSideEffectCheck.cpp - clang-tidy ---------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
9#include "AssertSideEffectCheck.h"
10#include "../utils/Matchers.h"
11#include "../utils/OptionsUtils.h"
12#include "clang/AST/ASTContext.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Lex/Lexer.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/Casting.h"
19#include <algorithm>
20#include <string>
21
22using namespace clang::ast_matchers;
23
24namespace clang::tidy::bugprone {
25
26namespace {
27
28AST_MATCHER_P2(Expr, hasSideEffect, bool, CheckFunctionCalls,
29clang::ast_matchers::internal::Matcher<NamedDecl>,
30IgnoredFunctionsMatcher) {
31const Expr *E = &Node;
32
33if (const auto *Op = dyn_cast<UnaryOperator>(E)) {
34UnaryOperator::Opcode OC = Op->getOpcode();
35return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
36OC == UO_PreDec;
37}
38
39if (const auto *Op = dyn_cast<BinaryOperator>(E)) {
40return Op->isAssignmentOp();
41}
42
43if (const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
44if (const auto *MethodDecl =
45dyn_cast_or_null<CXXMethodDecl>(OpCallExpr->getDirectCallee()))
46if (MethodDecl->isConst())
47return false;
48
49OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
50return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
51OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
52OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
53OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
54OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
55OpKind == OO_LessLess || OpKind == OO_GreaterGreater ||
56OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
57OpKind == OO_PercentEqual || OpKind == OO_New ||
58OpKind == OO_Delete || OpKind == OO_Array_New ||
59OpKind == OO_Array_Delete;
60}
61
62if (const auto *CExpr = dyn_cast<CallExpr>(E)) {
63if (!CheckFunctionCalls)
64return false;
65if (const auto *FuncDecl = CExpr->getDirectCallee()) {
66if (FuncDecl->getDeclName().isIdentifier() &&
67IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
68Builder)) // exceptions come here
69return false;
70for (size_t I = 0; I < FuncDecl->getNumParams(); I++) {
71const ParmVarDecl *P = FuncDecl->getParamDecl(I);
72const Expr *ArgExpr =
73I < CExpr->getNumArgs() ? CExpr->getArg(I) : nullptr;
74const QualType PT = P->getType().getCanonicalType();
75if (ArgExpr && !ArgExpr->isXValue() && PT->isReferenceType() &&
76!PT.getNonReferenceType().isConstQualified())
77return true;
78}
79if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
80return !MethodDecl->isConst();
81}
82return true;
83}
84
85return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
86}
87
88} // namespace
89
90AssertSideEffectCheck::AssertSideEffectCheck(StringRef Name,
91ClangTidyContext *Context)
92: ClangTidyCheck(Name, Context),
93CheckFunctionCalls(Options.get("CheckFunctionCalls", false)),
94RawAssertList(Options.get("AssertMacros", "assert,NSAssert,NSCAssert")),
95IgnoredFunctions(utils::options::parseListPair(
96"__builtin_expect;", Options.get("IgnoredFunctions", ""))) {
97StringRef(RawAssertList).split(AssertMacros, ",", -1, false);
98}
99
100// The options are explained in AssertSideEffectCheck.h.
101void AssertSideEffectCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
102Options.store(Opts, "CheckFunctionCalls", CheckFunctionCalls);
103Options.store(Opts, "AssertMacros", RawAssertList);
104Options.store(Opts, "IgnoredFunctions",
105utils::options::serializeStringList(IgnoredFunctions));
106}
107
108void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
109auto IgnoredFunctionsMatcher =
110matchers::matchesAnyListedName(IgnoredFunctions);
111
112auto DescendantWithSideEffect =
113traverse(TK_AsIs, hasDescendant(expr(hasSideEffect(
114CheckFunctionCalls, IgnoredFunctionsMatcher))));
115auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
116Finder->addMatcher(
117stmt(
118anyOf(conditionalOperator(ConditionWithSideEffect),
119ifStmt(ConditionWithSideEffect),
120unaryOperator(hasOperatorName("!"),
121hasUnaryOperand(unaryOperator(
122hasOperatorName("!"),
123hasUnaryOperand(DescendantWithSideEffect))))))
124.bind("condStmt"),
125this);
126}
127
128void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
129const SourceManager &SM = *Result.SourceManager;
130const LangOptions LangOpts = getLangOpts();
131SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getBeginLoc();
132
133StringRef AssertMacroName;
134while (Loc.isValid() && Loc.isMacroID()) {
135StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
136Loc = SM.getImmediateMacroCallerLoc(Loc);
137
138// Check if this macro is an assert.
139if (llvm::is_contained(AssertMacros, MacroName)) {
140AssertMacroName = MacroName;
141break;
142}
143}
144if (AssertMacroName.empty())
145return;
146
147diag(Loc, "side effect in %0() condition discarded in release builds")
148<< AssertMacroName;
149}
150
151} // namespace clang::tidy::bugprone
152