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/Lex/Lexer.h"
23 "__builtin_strcasecmp;"
25 "__builtin_strncasecmp;"
70 WarnOnImplicitComparison(Options.get(
"WarnOnImplicitComparison", true)),
71 WarnOnLogicalNotComparison(
72 Options.get(
"WarnOnLogicalNotComparison", false)),
73 StringCompareLikeFunctions(
74 Options.get(
"StringCompareLikeFunctions",
"")) {}
78 Options.
store(Opts,
"WarnOnImplicitComparison", WarnOnImplicitComparison);
79 Options.
store(Opts,
"WarnOnLogicalNotComparison", WarnOnLogicalNotComparison);
80 Options.
store(Opts,
"StringCompareLikeFunctions", StringCompareLikeFunctions);
85 const auto ComparisonUnaryOperator = unaryOperator(hasOperatorName(
"!"));
86 const auto ComparisonBinaryOperator = binaryOperator(isComparisonOperator());
87 const auto ComparisonOperator =
88 expr(anyOf(ComparisonUnaryOperator, ComparisonBinaryOperator));
96 const auto FunctionCompareDecl =
97 functionDecl(hasAnyName(FunctionNames)).bind(
"decl");
98 const auto DirectStringCompareCallExpr =
99 callExpr(hasDeclaration(FunctionCompareDecl)).bind(
"call");
100 const auto MacroStringCompareCallExpr = conditionalOperator(anyOf(
101 hasTrueExpression(ignoringParenImpCasts(DirectStringCompareCallExpr)),
102 hasFalseExpression(ignoringParenImpCasts(DirectStringCompareCallExpr))));
104 const auto StringCompareCallExpr = ignoringParenImpCasts(
105 anyOf(DirectStringCompareCallExpr, MacroStringCompareCallExpr));
107 if (WarnOnImplicitComparison) {
111 stmt(anyOf(mapAnyOf(ifStmt, whileStmt, doStmt, forStmt)
112 .with(hasCondition(StringCompareCallExpr)),
113 binaryOperator(hasAnyOperatorName(
"&&",
"||"),
114 hasEitherOperand(StringCompareCallExpr))))
115 .bind(
"missing-comparison"),
119 if (WarnOnLogicalNotComparison) {
122 Finder->addMatcher(unaryOperator(hasOperatorName(
"!"),
123 hasUnaryOperand(ignoringParenImpCasts(
124 StringCompareCallExpr)))
125 .bind(
"logical-not-comparison"),
132 implicitCastExpr(unless(hasType(isInteger())),
133 hasSourceExpression(StringCompareCallExpr))
134 .bind(
"invalid-conversion")),
139 binaryOperator(unless(anyOf(isComparisonOperator(), hasOperatorName(
"&&"),
140 hasOperatorName(
"||"), hasOperatorName(
"="))),
141 hasEitherOperand(StringCompareCallExpr))
142 .bind(
"suspicious-operator"),
146 const auto InvalidLiteral = ignoringParenImpCasts(
147 anyOf(integerLiteral(unless(equals(0))),
149 hasOperatorName(
"-"),
150 has(ignoringParenImpCasts(integerLiteral(unless(equals(0)))))),
151 characterLiteral(), cxxBoolLiteral()));
154 binaryOperator(isComparisonOperator(),
155 hasOperands(StringCompareCallExpr, InvalidLiteral))
156 .bind(
"invalid-comparison"),
161 const MatchFinder::MatchResult &Result) {
162 const auto *
Decl = Result.Nodes.getNodeAs<FunctionDecl>(
"decl");
163 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
164 assert(
Decl !=
nullptr && Call !=
nullptr);
166 if (Result.Nodes.getNodeAs<Stmt>(
"missing-comparison")) {
167 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
168 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
171 diag(Call->getBeginLoc(),
172 "function %0 is called without explicitly comparing result")
173 <<
Decl << FixItHint::CreateInsertion(EndLoc,
" != 0");
176 if (
const auto *
E = Result.Nodes.getNodeAs<Expr>(
"logical-not-comparison")) {
177 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
178 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
180 SourceLocation NotLoc =
E->getBeginLoc();
182 diag(Call->getBeginLoc(),
183 "function %0 is compared using logical not operator")
185 << FixItHint::CreateRemoval(
186 CharSourceRange::getTokenRange(NotLoc, NotLoc))
187 << FixItHint::CreateInsertion(EndLoc,
" == 0");
190 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-comparison")) {
191 diag(Call->getBeginLoc(),
192 "function %0 is compared to a suspicious constant")
196 if (
const auto *BinOp =
197 Result.Nodes.getNodeAs<BinaryOperator>(
"suspicious-operator")) {
198 diag(Call->getBeginLoc(),
"results of function %0 used by operator '%1'")
199 <<
Decl << BinOp->getOpcodeStr();
202 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-conversion")) {
203 diag(Call->getBeginLoc(),
"function %0 has suspicious implicit cast")
const FunctionDecl * Decl
llvm::SmallString< 256U > Name
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 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.
SuspiciousStringCompareCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static const char KnownStringCompareFunctions[]
std::vector< StringRef > parseListPair(StringRef L, StringRef R)
llvm::StringMap< ClangTidyValue > OptionMap