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"
28AST_MATCHER_P2(Expr, hasSideEffect,
bool, CheckFunctionCalls,
29 clang::ast_matchers::internal::Matcher<NamedDecl>,
30 IgnoredFunctionsMatcher) {
31 const Expr *
E = &
Node;
33 if (
const auto *Op = dyn_cast<UnaryOperator>(
E)) {
34 UnaryOperator::Opcode OC = Op->getOpcode();
35 return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
39 if (
const auto *Op = dyn_cast<BinaryOperator>(
E)) {
40 return Op->isAssignmentOp();
43 if (
const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(
E)) {
44 if (
const auto *MethodDecl =
45 dyn_cast_or_null<CXXMethodDecl>(OpCallExpr->getDirectCallee()))
46 if (MethodDecl->isConst())
49 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
50 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
51 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
52 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
53 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
54 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
55 OpKind == OO_LessLess || OpKind == OO_GreaterGreater ||
56 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
57 OpKind == OO_PercentEqual || OpKind == OO_New ||
58 OpKind == OO_Delete || OpKind == OO_Array_New ||
59 OpKind == OO_Array_Delete;
62 if (
const auto *CExpr = dyn_cast<CallExpr>(
E)) {
63 if (!CheckFunctionCalls)
65 if (
const auto *
FuncDecl = CExpr->getDirectCallee()) {
66 if (
FuncDecl->getDeclName().isIdentifier() &&
67 IgnoredFunctionsMatcher.matches(*
FuncDecl, Finder,
70 for (
size_t I = 0; I <
FuncDecl->getNumParams(); I++) {
71 const ParmVarDecl *P =
FuncDecl->getParamDecl(I);
73 I < CExpr->getNumArgs() ? CExpr->getArg(I) :
nullptr;
74 const QualType PT = P->getType().getCanonicalType();
75 if (ArgExpr && !ArgExpr->isXValue() && PT->isReferenceType() &&
76 !PT.getNonReferenceType().isConstQualified())
79 if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(
FuncDecl))
80 return !MethodDecl->isConst();
85 return isa<CXXNewExpr>(
E) || isa<CXXDeleteExpr>(
E) || isa<CXXThrowExpr>(
E);
93 CheckFunctionCalls(Options.get(
"CheckFunctionCalls", false)),
94 RawAssertList(Options.get(
"AssertMacros",
"assert,NSAssert,NSCAssert")),
95 IgnoredFunctions(utils::options::parseListPair(
96 "__builtin_expect;", Options.get(
"IgnoredFunctions",
""))) {
97 StringRef(RawAssertList).split(AssertMacros,
",", -1,
false);
102 Options.
store(Opts,
"CheckFunctionCalls", CheckFunctionCalls);
109 auto IgnoredFunctionsMatcher =
112 auto DescendantWithSideEffect =
113 traverse(TK_AsIs, hasDescendant(expr(hasSideEffect(
114 CheckFunctionCalls, IgnoredFunctionsMatcher))));
115 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
118 anyOf(conditionalOperator(ConditionWithSideEffect),
119 ifStmt(ConditionWithSideEffect),
120 unaryOperator(hasOperatorName(
"!"),
121 hasUnaryOperand(unaryOperator(
122 hasOperatorName(
"!"),
123 hasUnaryOperand(DescendantWithSideEffect))))))
129 const SourceManager &SM = *Result.SourceManager;
131 SourceLocation
Loc = Result.Nodes.getNodeAs<Stmt>(
"condStmt")->getBeginLoc();
133 StringRef AssertMacroName;
134 while (
Loc.isValid() &&
Loc.isMacroID()) {
135 StringRef
MacroName = Lexer::getImmediateMacroName(
Loc, SM, LangOpts);
136 Loc = SM.getImmediateMacroCallerLoc(
Loc);
139 if (llvm::is_contained(AssertMacros,
MacroName)) {
144 if (AssertMacroName.empty())
147 diag(
Loc,
"side effect in %0() condition discarded in release builds")
llvm::SmallString< 256U > Name
CodeCompletionBuilder Builder
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
AssertSideEffectCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
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.
llvm::StringMap< ClangTidyValue > OptionMap
static constexpr const char FuncDecl[]