11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Frontend/CompilerInstance.h"
14 #include "clang/Lex/Lexer.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Casting.h"
21 using namespace clang::ast_matchers;
29 AST_MATCHER_P(Expr, hasSideEffect,
bool, CheckFunctionCalls) {
30 const Expr *E = &Node;
32 if (
const auto *Op = dyn_cast<UnaryOperator>(E)) {
33 UnaryOperator::Opcode OC = Op->getOpcode();
34 return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
38 if (
const auto *Op = dyn_cast<BinaryOperator>(E)) {
39 return Op->isAssignmentOp();
42 if (
const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
43 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
44 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
45 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
46 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
47 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
48 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
49 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
50 OpKind == OO_PercentEqual || OpKind == OO_New ||
51 OpKind == OO_Delete || OpKind == OO_Array_New ||
52 OpKind == OO_Array_Delete;
55 if (
const auto *CExpr = dyn_cast<CallExpr>(E)) {
56 bool Result = CheckFunctionCalls;
57 if (
const auto *FuncDecl = CExpr->getDirectCallee()) {
58 if (FuncDecl->getDeclName().isIdentifier() &&
59 FuncDecl->getName() ==
"__builtin_expect")
61 else if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
62 Result &= !MethodDecl->isConst();
67 return isa<CXXNewExpr>(E) || isa<CXXDeleteExpr>(E) || isa<CXXThrowExpr>(E);
72 AssertSideEffectCheck::AssertSideEffectCheck(StringRef
Name,
75 CheckFunctionCalls(Options.get(
"CheckFunctionCalls", false)),
76 RawAssertList(Options.get(
"AssertMacros",
"assert")) {
77 StringRef(RawAssertList).split(AssertMacros,
",", -1,
false);
82 Options.
store(Opts,
"CheckFunctionCalls", CheckFunctionCalls);
87 auto DescendantWithSideEffect =
88 hasDescendant(expr(hasSideEffect(CheckFunctionCalls)));
89 auto ConditionWithSideEffect = hasCondition(DescendantWithSideEffect);
92 anyOf(conditionalOperator(ConditionWithSideEffect),
93 ifStmt(ConditionWithSideEffect),
94 unaryOperator(hasOperatorName(
"!"),
95 hasUnaryOperand(unaryOperator(
97 hasUnaryOperand(DescendantWithSideEffect))))))
103 const SourceManager &
SM = *Result.SourceManager;
104 const LangOptions
LangOpts = Result.Context->getLangOpts();
105 SourceLocation
Loc = Result.Nodes.getNodeAs<Stmt>(
"condStmt")->getLocStart();
107 StringRef AssertMacroName;
108 while (Loc.isValid() && Loc.isMacroID()) {
109 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
112 if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
113 AssertMacros.end()) {
114 AssertMacroName = MacroName;
117 Loc = SM.getImmediateMacroCallerLoc(Loc);
119 if (AssertMacroName.empty())
122 diag(Loc,
"found %0() with side effect") << AssertMacroName;
SourceLocation Loc
'#' location in the include directive
std::unique_ptr< ast_matchers::MatchFinder > Finder
AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt, ast_matchers::internal::Matcher< DeclStmt >, InnerMatcher)
Base class for all clang-tidy checks.
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.
std::map< std::string, std::string > OptionMap
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
ClangTidyContext & Context
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.