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" 25 "__builtin_strcasecmp;" 27 "__builtin_strncasecmp;" 69 SuspiciousStringCompareCheck::SuspiciousStringCompareCheck(
72 WarnOnImplicitComparison(Options.get(
"WarnOnImplicitComparison", 1)),
73 WarnOnLogicalNotComparison(Options.get(
"WarnOnLogicalNotComparison", 0)),
74 StringCompareLikeFunctions(
75 Options.get(
"StringCompareLikeFunctions",
"")) {}
79 Options.
store(Opts,
"WarnOnImplicitComparison", WarnOnImplicitComparison);
80 Options.
store(Opts,
"WarnOnLogicalNotComparison", WarnOnLogicalNotComparison);
81 Options.
store(Opts,
"StringCompareLikeFunctions", StringCompareLikeFunctions);
86 const auto ComparisonUnaryOperator = unaryOperator(hasOperatorName(
"!"));
87 const auto ComparisonBinaryOperator =
88 binaryOperator(matchers::isComparisonOperator());
89 const auto ComparisonOperator =
90 expr(anyOf(ComparisonUnaryOperator, ComparisonBinaryOperator));
95 (llvm::Twine(KnownStringCompareFunctions) + StringCompareLikeFunctions)
99 const auto FunctionCompareDecl =
100 functionDecl(hasAnyName(std::vector<StringRef>(FunctionNames.begin(),
101 FunctionNames.end())))
103 const auto DirectStringCompareCallExpr =
104 callExpr(hasDeclaration(FunctionCompareDecl)).bind(
"call");
105 const auto MacroStringCompareCallExpr = conditionalOperator(anyOf(
106 hasTrueExpression(ignoringParenImpCasts(DirectStringCompareCallExpr)),
107 hasFalseExpression(ignoringParenImpCasts(DirectStringCompareCallExpr))));
109 const auto StringCompareCallExpr = ignoringParenImpCasts(
110 anyOf(DirectStringCompareCallExpr, MacroStringCompareCallExpr));
112 if (WarnOnImplicitComparison) {
116 stmt(anyOf(ifStmt(hasCondition(StringCompareCallExpr)),
117 whileStmt(hasCondition(StringCompareCallExpr)),
118 doStmt(hasCondition(StringCompareCallExpr)),
119 forStmt(hasCondition(StringCompareCallExpr)),
121 anyOf(hasOperatorName(
"&&"), hasOperatorName(
"||")),
122 hasEitherOperand(StringCompareCallExpr))))
123 .bind(
"missing-comparison"),
127 if (WarnOnLogicalNotComparison) {
130 Finder->addMatcher(unaryOperator(hasOperatorName(
"!"),
131 hasUnaryOperand(ignoringParenImpCasts(
132 StringCompareCallExpr)))
133 .bind(
"logical-not-comparison"),
139 implicitCastExpr(unless(hasType(isInteger())),
140 hasSourceExpression(StringCompareCallExpr))
141 .bind(
"invalid-conversion"),
147 unless(anyOf(matchers::isComparisonOperator(), hasOperatorName(
"&&"),
148 hasOperatorName(
"||"), hasOperatorName(
"="))),
149 hasEitherOperand(StringCompareCallExpr))
150 .bind(
"suspicious-operator"),
154 const auto InvalidLiteral = ignoringParenImpCasts(
155 anyOf(integerLiteral(unless(equals(0))),
157 hasOperatorName(
"-"),
158 has(ignoringParenImpCasts(integerLiteral(unless(equals(0)))))),
159 characterLiteral(), cxxBoolLiteral()));
161 Finder->addMatcher(binaryOperator(matchers::isComparisonOperator(),
162 hasEitherOperand(StringCompareCallExpr),
163 hasEitherOperand(InvalidLiteral))
164 .bind(
"invalid-comparison"),
169 const MatchFinder::MatchResult &Result) {
170 const auto *
Decl = Result.Nodes.getNodeAs<FunctionDecl>(
"decl");
171 const auto *Call = Result.Nodes.getNodeAs<CallExpr>(
"call");
172 assert(
Decl !=
nullptr && Call !=
nullptr);
174 if (Result.Nodes.getNodeAs<Stmt>(
"missing-comparison")) {
175 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
176 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
179 diag(Call->getBeginLoc(),
180 "function %0 is called without explicitly comparing result")
181 <<
Decl << FixItHint::CreateInsertion(EndLoc,
" != 0");
184 if (
const auto *
E = Result.Nodes.getNodeAs<Expr>(
"logical-not-comparison")) {
185 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
186 Call->getRParenLoc(), 0, Result.Context->getSourceManager(),
188 SourceLocation NotLoc =
E->getBeginLoc();
190 diag(Call->getBeginLoc(),
191 "function %0 is compared using logical not operator")
193 << FixItHint::CreateRemoval(
195 << FixItHint::CreateInsertion(EndLoc,
" == 0");
198 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-comparison")) {
199 diag(Call->getBeginLoc(),
200 "function %0 is compared to a suspicious constant")
204 if (
const auto *BinOp =
205 Result.Nodes.getNodeAs<BinaryOperator>(
"suspicious-operator")) {
206 diag(Call->getBeginLoc(),
"results of function %0 used by operator '%1'")
207 <<
Decl << BinOp->getOpcodeStr();
210 if (Result.Nodes.getNodeAs<Stmt>(
"invalid-conversion")) {
211 diag(Call->getBeginLoc(),
"function %0 has suspicious implicit cast")
const FunctionDecl * Decl
static const char KnownStringCompareFunctions[]
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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.
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...
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.