2019-03-01 17:52:53 +08:00
|
|
|
//===-- StringCompareCheck.cpp - clang-tidy--------------------------------===//
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "StringCompareCheck.h"
|
|
|
|
#include "../utils/FixItHintUtils.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/Tooling/FixIt.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2018-01-30 22:55:50 +08:00
|
|
|
namespace readability {
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
|
|
|
|
static const StringRef CompareMessage = "do not use 'compare' to test equality "
|
|
|
|
"of strings; use the string equality "
|
|
|
|
"operator instead";
|
|
|
|
|
|
|
|
void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
|
|
|
|
if (!getLangOpts().CPlusPlus)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto StrCompare = cxxMemberCallExpr(
|
|
|
|
callee(cxxMethodDecl(hasName("compare"),
|
|
|
|
ofClass(classTemplateSpecializationDecl(
|
|
|
|
hasName("::std::basic_string"))))),
|
|
|
|
hasArgument(0, expr().bind("str2")), argumentCountIs(1),
|
|
|
|
callee(memberExpr().bind("str1")));
|
|
|
|
|
|
|
|
// First and second case: cast str.compare(str) to boolean.
|
|
|
|
Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()),
|
|
|
|
has(StrCompare))
|
|
|
|
.bind("match1"),
|
|
|
|
this);
|
|
|
|
|
|
|
|
// Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0.
|
|
|
|
Finder->addMatcher(
|
|
|
|
binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
|
|
|
|
hasEitherOperand(StrCompare.bind("compare")),
|
|
|
|
hasEitherOperand(integerLiteral(equals(0)).bind("zero")))
|
|
|
|
.bind("match2"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringCompareCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match1")) {
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(Matched->getBeginLoc(), CompareMessage);
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match2")) {
|
|
|
|
const ASTContext &Ctx = *Result.Context;
|
|
|
|
|
|
|
|
if (const auto *Zero = Result.Nodes.getNodeAs<Stmt>("zero")) {
|
|
|
|
const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>("str1");
|
|
|
|
const auto *Str2 = Result.Nodes.getNodeAs<Stmt>("str2");
|
|
|
|
const auto *Compare = Result.Nodes.getNodeAs<Stmt>("compare");
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
auto Diag = diag(Matched->getBeginLoc(), CompareMessage);
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
|
|
|
|
if (Str1->isArrow())
|
2018-08-10 06:42:26 +08:00
|
|
|
Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(), "*");
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
|
|
|
|
Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx)
|
|
|
|
<< tooling::fixit::createReplacement(*Compare, *Str1->getBase(),
|
|
|
|
Ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Add fixit to fix the code for case one and two (match1).
|
|
|
|
}
|
|
|
|
|
2018-01-30 22:55:50 +08:00
|
|
|
} // namespace readability
|
[clang-tidy] Add check 'misc-string-compare'.
I have a created a new check for clang tidy: misc-string-compare. This will check for incorrect usage of std::string::compare when used to check equality or inequality of string instead of the string equality or inequality operators.
Example:
```
std::string str1, str2;
if (str1.compare(str2)) {
}
```
Reviewers: hokein, aaron.ballman, alexfh, malcolm.parsons
Subscribers: xazax.hun, Eugene.Zelenko, cfe-commits, malcolm.parsons, Prazek, mgorny, JDevlieghere
Differential Revision: https://reviews.llvm.org/D27210
llvm-svn: 290747
2016-12-30 18:09:46 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|