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 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
45 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
46 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
47 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
48 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
49 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
50 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
51 OpKind == OO_PercentEqual || OpKind == OO_New ||
52 OpKind == OO_Delete || OpKind == OO_Array_New ||
53 OpKind == OO_Array_Delete;
56 if (
const auto *CExpr = dyn_cast<CallExpr>(
E)) {
57 bool Result = CheckFunctionCalls;
58 if (
const auto *FuncDecl = CExpr->getDirectCallee()) {
59 if (FuncDecl->getDeclName().isIdentifier() &&
60 IgnoredFunctionsMatcher.matches(*FuncDecl, Finder,
63 else if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
64 Result &= !MethodDecl->isConst();
69 return isa<CXXNewExpr>(
E) || isa<CXXDeleteExpr>(
E) || isa<CXXThrowExpr>(
E);
77 CheckFunctionCalls(Options.get(
"CheckFunctionCalls", false)),
78 RawAssertList(Options.get(
"AssertMacros",
"assert,NSAssert,NSCAssert")),
79 IgnoredFunctions(utils::options::parseListPair(
80 "__builtin_expect;", Options.get(
"IgnoredFunctions",
""))) {
81 StringRef(RawAssertList).split(AssertMacros,
",", -1,
false);
86 Options.
store(Opts,
"CheckFunctionCalls", CheckFunctionCalls);
93 auto IgnoredFunctionsMatcher =
96 auto DescendantWithSideEffect =
97 traverse(TK_AsIs, hasDescendant(expr(hasSideEffect(
98 CheckFunctionCalls, IgnoredFunctionsMatcher))));
99 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
102 anyOf(conditionalOperator(ConditionWithSideEffect),
103 ifStmt(ConditionWithSideEffect),
104 unaryOperator(hasOperatorName(
"!"),
105 hasUnaryOperand(unaryOperator(
106 hasOperatorName(
"!"),
107 hasUnaryOperand(DescendantWithSideEffect))))))
113 const SourceManager &SM = *Result.SourceManager;
115 SourceLocation
Loc = Result.Nodes.getNodeAs<Stmt>(
"condStmt")->getBeginLoc();
117 StringRef AssertMacroName;
118 while (
Loc.isValid() &&
Loc.isMacroID()) {
119 StringRef
MacroName = Lexer::getImmediateMacroName(
Loc, SM, LangOpts);
120 Loc = SM.getImmediateMacroCallerLoc(
Loc);
123 if (llvm::is_contained(AssertMacros,
MacroName)) {
128 if (AssertMacroName.empty())
131 diag(
Loc,
"side effect in %0() condition discarded in release builds")
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