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;
26 AST_MATCHER_P(Expr, hasSideEffect,
bool, CheckFunctionCalls) {
27 const Expr *E = &Node;
29 if (
const auto *Op = dyn_cast<UnaryOperator>(E)) {
30 UnaryOperator::Opcode OC = Op->getOpcode();
31 return OC == UO_PostInc || OC == UO_PostDec || OC == UO_PreInc ||
35 if (
const auto *Op = dyn_cast<BinaryOperator>(E)) {
36 return Op->isAssignmentOp();
39 if (
const auto *OpCallExpr = dyn_cast<CXXOperatorCallExpr>(E)) {
40 OverloadedOperatorKind OpKind = OpCallExpr->getOperator();
41 return OpKind == OO_Equal || OpKind == OO_PlusEqual ||
42 OpKind == OO_MinusEqual || OpKind == OO_StarEqual ||
43 OpKind == OO_SlashEqual || OpKind == OO_AmpEqual ||
44 OpKind == OO_PipeEqual || OpKind == OO_CaretEqual ||
45 OpKind == OO_LessLessEqual || OpKind == OO_GreaterGreaterEqual ||
46 OpKind == OO_PlusPlus || OpKind == OO_MinusMinus ||
47 OpKind == OO_PercentEqual || OpKind == OO_New ||
48 OpKind == OO_Delete || OpKind == OO_Array_New ||
49 OpKind == OO_Array_Delete;
52 if (
const auto *CExpr = dyn_cast<CallExpr>(E)) {
53 bool Result = CheckFunctionCalls;
54 if (
const auto *FuncDecl = CExpr->getDirectCallee()) {
55 if (FuncDecl->getDeclName().isIdentifier() &&
56 FuncDecl->getName() ==
"__builtin_expect")
58 else if (
const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
59 Result &= !MethodDecl->isConst();
64 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 ConditionWithSideEffect =
87 hasCondition(hasDescendant(expr(hasSideEffect(CheckFunctionCalls))));
89 stmt(anyOf(conditionalOperator(ConditionWithSideEffect),
90 ifStmt(ConditionWithSideEffect))).bind(
"condStmt"),
95 const SourceManager &
SM = *Result.SourceManager;
96 const LangOptions
LangOpts = Result.Context->getLangOpts();
97 SourceLocation
Loc = Result.Nodes.getNodeAs<Stmt>(
"condStmt")->getLocStart();
99 StringRef AssertMacroName;
100 while (Loc.isValid() && Loc.isMacroID()) {
101 StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
104 if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
105 AssertMacros.end()) {
106 AssertMacroName = MacroName;
109 Loc = SM.getImmediateMacroCallerLoc(Loc);
111 if (AssertMacroName.empty())
114 diag(Loc,
"found %0() with side effect") << AssertMacroName;
SourceLocation Loc
'#' location in the include directive
std::unique_ptr< ast_matchers::MatchFinder > Finder
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register ASTMatchers with Finder.
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
AST_MATCHER_P(CXXForRangeStmt, hasRangeBeginEndStmt, ast_matchers::internal::Matcher< DeclStmt >, InnerMatcher)
ClangTidyContext & Context
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.