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"
26AST_MATCHER_P2(Expr, hasSideEffect,
bool, CheckFunctionCalls,
27 clang::ast_matchers::internal::Matcher<NamedDecl>,
28 IgnoredFunctionsMatcher) {
29 const Expr *E = &Node;
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 ||
37 if (
const auto *Op = dyn_cast<BinaryOperator>(E)) {
38 return Op->isAssignmentOp();
41 if (
const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
42 if (
const auto *MethodDecl =
43 dyn_cast_or_null<CXXMethodDecl>(OpCallExpr->getDirectCallee()))
44 if (MethodDecl->isConst())
47 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
48 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
49 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
50 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
51 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
52 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
53 OpKind == OO_LessLess || OpKind == OO_GreaterGreater ||
54 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
55 OpKind == OO_PercentEqual || OpKind == OO_New ||
56 OpKind == OO_Delete || OpKind == OO_Array_New ||
57 OpKind == OO_Array_Delete;
60 if (
const auto *CExpr = dyn_cast<CallExpr>(E)) {
61 if (!CheckFunctionCalls)
63 if (
const auto *
FuncDecl = CExpr->getDirectCallee()) {
64 if (
FuncDecl->getDeclName().isIdentifier() &&
65 IgnoredFunctionsMatcher.matches(*
FuncDecl, Finder,
68 for (
size_t I = 0; I <
FuncDecl->getNumParams(); I++) {
69 const ParmVarDecl *
P =
FuncDecl->getParamDecl(I);
71 I < CExpr->getNumArgs() ? CExpr->getArg(I) :
nullptr;
72 const QualType PT =
P->getType().getCanonicalType();
73 if (ArgExpr && !ArgExpr->isXValue() && PT->isReferenceType() &&
74 !PT.getNonReferenceType().isConstQualified())
77 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(
FuncDecl))
78 return !MethodDecl->isConst();
83 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
91 CheckFunctionCalls(Options.get(
"CheckFunctionCalls", false)),
92 RawAssertList(Options.get(
"AssertMacros",
"assert,NSAssert,NSCAssert")),
93 IgnoredFunctions(
utils::options::parseListPair(
94 "__builtin_expect;", Options.get(
"IgnoredFunctions",
""))) {
95 StringRef(RawAssertList).split(AssertMacros,
",", -1,
false);
100 Options.store(Opts,
"CheckFunctionCalls", CheckFunctionCalls);
101 Options.store(Opts,
"AssertMacros", RawAssertList);
102 Options.store(Opts,
"IgnoredFunctions",
107 auto IgnoredFunctionsMatcher =
110 auto DescendantWithSideEffect =
111 traverse(TK_AsIs, hasDescendant(expr(hasSideEffect(
112 CheckFunctionCalls, IgnoredFunctionsMatcher))));
113 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
116 anyOf(conditionalOperator(ConditionWithSideEffect),
117 ifStmt(ConditionWithSideEffect),
118 unaryOperator(hasOperatorName(
"!"),
119 hasUnaryOperand(unaryOperator(
120 hasOperatorName(
"!"),
121 hasUnaryOperand(DescendantWithSideEffect))))))
127 const SourceManager &SM = *Result.SourceManager;
128 const LangOptions LangOpts = getLangOpts();
129 SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>(
"condStmt")->getBeginLoc();
131 StringRef AssertMacroName;
132 while (Loc.isValid() && Loc.isMacroID()) {
133 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
134 Loc = SM.getImmediateMacroCallerLoc(Loc);
137 if (llvm::is_contained(AssertMacros, MacroName)) {
138 AssertMacroName = MacroName;
142 if (AssertMacroName.empty())
145 diag(Loc,
"side effect in %0() condition discarded in release builds")
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
AssertSideEffectCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
cppcoreguidelines::ProBoundsAvoidUncheckedContainerAccess P
llvm::StringMap< ClangTidyValue > OptionMap
static constexpr const char FuncDecl[]