StringCompareCheck.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===-- StringCompareCheck.cpp - clang-tidy--------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "StringCompareCheck.h"
  9. #include "../utils/FixItHintUtils.h"
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/ASTMatchers/ASTMatchFinder.h"
  12. #include "clang/Tooling/FixIt.h"
  13. using namespace clang::ast_matchers;
  14. namespace clang::tidy::readability {
  15. static const StringRef CompareMessage = "do not use 'compare' to test equality "
  16. "of strings; use the string equality "
  17. "operator instead";
  18. void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
  19. const auto StrCompare = cxxMemberCallExpr(
  20. callee(cxxMethodDecl(hasName("compare"),
  21. ofClass(classTemplateSpecializationDecl(
  22. hasName("::std::basic_string"))))),
  23. hasArgument(0, expr().bind("str2")), argumentCountIs(1),
  24. callee(memberExpr().bind("str1")));
  25. // First and second case: cast str.compare(str) to boolean.
  26. Finder->addMatcher(
  27. traverse(TK_AsIs,
  28. implicitCastExpr(hasImplicitDestinationType(booleanType()),
  29. has(StrCompare))
  30. .bind("match1")),
  31. this);
  32. // Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0.
  33. Finder->addMatcher(
  34. binaryOperator(hasAnyOperatorName("==", "!="),
  35. hasOperands(StrCompare.bind("compare"),
  36. integerLiteral(equals(0)).bind("zero")))
  37. .bind("match2"),
  38. this);
  39. }
  40. void StringCompareCheck::check(const MatchFinder::MatchResult &Result) {
  41. if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match1")) {
  42. diag(Matched->getBeginLoc(), CompareMessage);
  43. return;
  44. }
  45. if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match2")) {
  46. const ASTContext &Ctx = *Result.Context;
  47. if (const auto *Zero = Result.Nodes.getNodeAs<Stmt>("zero")) {
  48. const auto *Str1 = Result.Nodes.getNodeAs<MemberExpr>("str1");
  49. const auto *Str2 = Result.Nodes.getNodeAs<Stmt>("str2");
  50. const auto *Compare = Result.Nodes.getNodeAs<Stmt>("compare");
  51. auto Diag = diag(Matched->getBeginLoc(), CompareMessage);
  52. if (Str1->isArrow())
  53. Diag << FixItHint::CreateInsertion(Str1->getBeginLoc(), "*");
  54. Diag << tooling::fixit::createReplacement(*Zero, *Str2, Ctx)
  55. << tooling::fixit::createReplacement(*Compare, *Str1->getBase(),
  56. Ctx);
  57. }
  58. }
  59. // FIXME: Add fixit to fix the code for case one and two (match1).
  60. }
  61. } // namespace clang::tidy::readability