10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Frontend/CompilerInstance.h" 13 #include "clang/Lex/Lexer.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Support/Casting.h" 28 AST_MATCHER_P(Expr, hasSideEffect,
bool, CheckFunctionCalls) {
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 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
43 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
44 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
45 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
46 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
47 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
48 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
49 OpKind == OO_PercentEqual || OpKind == OO_New ||
50 OpKind == OO_Delete || OpKind == OO_Array_New ||
51 OpKind == OO_Array_Delete;
54 if (
const auto *CExpr = dyn_cast<CallExpr>(E)) {
55 bool Result = CheckFunctionCalls;
56 if (
const auto *FuncDecl = CExpr->getDirectCallee()) {
57 if (FuncDecl->getDeclName().isIdentifier() &&
58 FuncDecl->getName() ==
"__builtin_expect")
60 else if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
61 Result &= !MethodDecl->isConst();
66 return isa<CXXNewExpr>(
E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(
E);
71 AssertSideEffectCheck::AssertSideEffectCheck(StringRef
Name,
74 CheckFunctionCalls(Options.get(
"CheckFunctionCalls", false)),
75 RawAssertList(Options.get(
"AssertMacros",
"assert")) {
76 StringRef(RawAssertList).split(AssertMacros,
",", -1,
false);
81 Options.
store(Opts,
"CheckFunctionCalls", CheckFunctionCalls);
86 auto DescendantWithSideEffect =
87 hasDescendant(expr(hasSideEffect(CheckFunctionCalls)));
88 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
91 anyOf(conditionalOperator(ConditionWithSideEffect),
92 ifStmt(ConditionWithSideEffect),
93 unaryOperator(hasOperatorName(
"!"),
94 hasUnaryOperand(unaryOperator(
96 hasUnaryOperand(DescendantWithSideEffect))))))
102 const SourceManager &SM = *Result.SourceManager;
104 SourceLocation
Loc = Result.Nodes.getNodeAs<Stmt>(
"condStmt")->getBeginLoc();
106 StringRef AssertMacroName;
107 while (Loc.isValid() && Loc.isMacroID()) {
108 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
111 if (llvm::is_contained(AssertMacros, MacroName)) {
112 AssertMacroName = MacroName;
115 Loc = SM.getImmediateMacroCallerLoc(Loc);
117 if (AssertMacroName.empty())
120 diag(Loc,
"found %0() with side effect") << AssertMacroName;
SourceLocation Loc
'#' location in the include directive
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
AST_MATCHER_P(CXXMethodDecl, hasCanonicalDecl, ast_matchers::internal::Matcher< CXXMethodDecl >, InnerMatcher)