11 #include "../utils/Matchers.h"
12 #include "../utils/OptionsUtils.h"
13 #include "clang/AST/ASTContext.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "clang/Lex/Lexer.h"
17 using namespace clang::ast_matchers;
27 "__builtin_strcasecmp;"
29 "__builtin_strncasecmp;"
71 SuspiciousStringCompareCheck::SuspiciousStringCompareCheck(
74 WarnOnImplicitComparison(Options.get(
"WarnOnImplicitComparison", 1)),
75 WarnOnLogicalNotComparison(Options.get(
"WarnOnLogicalNotComparison", 0)),
76 StringCompareLikeFunctions(
77 Options.get(
"StringCompareLikeFunctions",
"")) {}
81 Options.
store(Opts,
"WarnOnImplicitComparison", WarnOnImplicitComparison);
82 Options.
store(Opts,
"WarnOnLogicalNotComparison", WarnOnLogicalNotComparison);
83 Options.
store(Opts,
"StringCompareLikeFunctions", StringCompareLikeFunctions);
88 const auto ComparisonUnaryOperator = unaryOperator(hasOperatorName(
"!"));
89 const auto ComparisonBinaryOperator =
90 binaryOperator(matchers::isComparisonOperator());
91 const auto ComparisonOperator =
92 expr(anyOf(ComparisonUnaryOperator, ComparisonBinaryOperator));
101 const auto FunctionCompareDecl =
102 functionDecl(hasAnyName(std::vector<StringRef>(FunctionNames.begin(),
103 FunctionNames.end())))
105 const auto DirectStringCompareCallExpr =
106 callExpr(hasDeclaration(FunctionCompareDecl)).bind(
"call");
107 const auto MacroStringCompareCallExpr =
109 anyOf(hasTrueExpression(ignoringParenImpCasts(DirectStringCompareCallExpr)),
110 hasFalseExpression(ignoringParenImpCasts(DirectStringCompareCallExpr))));
112 const auto StringCompareCallExpr = ignoringParenImpCasts(
113 anyOf(DirectStringCompareCallExpr, MacroStringCompareCallExpr));
115 if (WarnOnImplicitComparison) {
119 stmt(anyOf(ifStmt(hasCondition(StringCompareCallExpr)),
120 whileStmt(hasCondition(StringCompareCallExpr)),
121 doStmt(hasCondition(StringCompareCallExpr)),
122 forStmt(hasCondition(StringCompareCallExpr)),
124 anyOf(hasOperatorName(
"&&"), hasOperatorName(
"||")),
125 hasEitherOperand(StringCompareCallExpr))))
126 .bind(
"missing-comparison"),
130 if (WarnOnLogicalNotComparison) {
133 Finder->addMatcher(unaryOperator(hasOperatorName(
"!"),
134 hasUnaryOperand(ignoringParenImpCasts(
135 StringCompareCallExpr)))
136 .bind(
"logical-not-comparison"),
142 implicitCastExpr(unless(hasType(isInteger())),
143 hasSourceExpression(StringCompareCallExpr))
144 .bind(
"invalid-conversion"),
150 unless(anyOf(matchers::isComparisonOperator(), hasOperatorName(
"&&"),
151 hasOperatorName(
"||"), hasOperatorName(
"="))),
152 hasEitherOperand(StringCompareCallExpr))
153 .bind(
"suspicious-operator"),
157 const auto InvalidLiteral = ignoringParenImpCasts(
158 anyOf(integerLiteral(unless(equals(0))),
160 hasOperatorName(
"-"),
161 has(ignoringParenImpCasts(integerLiteral(unless(equals(0)))))),
162 characterLiteral(), cxxBoolLiteral()));
164 Finder->addMatcher(binaryOperator(matchers::isComparisonOperator(),
165 hasEitherOperand(StringCompareCallExpr),
166 hasEitherOperand(InvalidLiteral))
167 .bind(
"invalid-comparison"),
172 const MatchFinder::MatchResult &
Result) {
173 const auto *Decl = Result.Nodes.getNodeAs<FunctionDecl>(
"decl");
174 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
175 assert(Decl !=
nullptr && Call !=
nullptr);
177 if (Result.Nodes.getNodeAs<Stmt>(
"missing-comparison")) {
178 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
179 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
180 Result.Context->getLangOpts());
182 diag(Call->getLocStart(),
183 "function %0 is called without explicitly comparing result")
184 << Decl << FixItHint::CreateInsertion(EndLoc,
" != 0");
187 if (
const auto *E = Result.Nodes.getNodeAs<Expr>(
"logical-not-comparison")) {
188 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
189 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
190 Result.Context->getLangOpts());
191 SourceLocation NotLoc = E->getLocStart();
193 diag(Call->getLocStart(),
194 "function %0 is compared using logical not operator")
195 << Decl << FixItHint::CreateRemoval(
196 CharSourceRange::getTokenRange(NotLoc, NotLoc))
197 << FixItHint::CreateInsertion(EndLoc,
" == 0");
200 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-comparison")) {
201 diag(Call->getLocStart(),
202 "function %0 is compared to a suspicious constant")
206 if (
const auto* BinOp = Result.Nodes.getNodeAs<BinaryOperator>(
"suspicious-operator")) {
207 diag(Call->getLocStart(),
"results of function %0 used by operator '%1'")
208 << Decl << BinOp->getOpcodeStr();
211 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-conversion")) {
212 diag(Call->getLocStart(),
"function %0 has suspicious implicit cast")
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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
static const char KnownStringCompareFunctions[]
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.