madsravn updated this revision to Diff 80177.
madsravn added a comment.
Did as comments suggested: Fixed the description about compare returning -1, 0
or 1. Fixed the ast matcher to only find compare with one argument.
Clang-formatted everything. Added a new test (str.compare("foo")) and wrote a
FIXME for the fixit.
https://reviews.llvm.org/D27210
Files:
clang-tidy/misc/CMakeLists.txt
clang-tidy/misc/MiscTidyModule.cpp
clang-tidy/misc/StringCompareCheck.cpp
clang-tidy/misc/StringCompareCheck.h
docs/ReleaseNotes.rst
docs/clang-tidy/checks/list.rst
docs/clang-tidy/checks/misc-string-compare.rst
test/clang-tidy/misc-string-compare.cpp
Index: test/clang-tidy/misc-string-compare.cpp
===================================================================
--- test/clang-tidy/misc-string-compare.cpp
+++ test/clang-tidy/misc-string-compare.cpp
@@ -0,0 +1,72 @@
+// RUN: %check_clang_tidy %s misc-string-compare %t -- -- -std=c++11
+
+namespace std {
+template <typename T>
+class allocator {};
+template <typename T>
+class char_traits {};
+template <typename C, typename T = std::char_traits<C>, typename A = std::allocator<C>>
+struct basic_string {
+ basic_string();
+ basic_string(const C *, unsigned int size);
+ int compare(const basic_string<char> &str);
+ int compare(const C *);
+ bool empty();
+};
+bool operator==(const basic_string<char> &lhs, const basic_string<char> &rhs);
+bool operator!=(const basic_string<char> &lhs, const basic_string<char> &rhs);
+typedef basic_string<char> string;
+}
+
+void func(bool b);
+
+void Test() {
+ std::string str1("a", 1);
+ std::string str2("b", 1);
+
+ if (str1.compare(str2)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use compare to test equality of strings; use the string equality operator instead [misc-string-compare]
+ if (!str1.compare(str2)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:8: warning: do not use compare to test equality of strings; use the string equality operator instead [misc-string-compare]
+ if (str1.compare("foo")) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use compare to test equality of strings;
+ if (str1.compare(str2) == 0) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use compare to test equality of strings;
+ if (str1.compare(str2) != 0) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use compare to test equality of strings;
+ if (0 == str1.compare(str2)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use compare to test equality of strings;
+ if (0 != str1.compare(str2)) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: do not use compare to test equality of strings;
+ func(str1.compare(str2));
+ // CHECK-MESSAGES: [[@LINE-1]]:8: warning: do not use compare to test equality of strings;
+ if (str2.empty() || str1.compare(str2) != 0) {
+ }
+ // CHECK-MESSAGES: [[@LINE-2]]:23: warning: do not use compare to test equality of strings;
+}
+
+void Valid() {
+ std::string str1("a", 1);
+ std::string str2("b", 1);
+ if (str1 == str2) {
+ }
+ if (str1 != str2) {
+ }
+ if (str1.compare(str2) == 1) {
+ }
+ if (str1.compare(str2) == str1.compare(str2)) {
+ }
+ if (0 == 0) {
+ }
+ if (1 == str1.compare(str2)) {
+ }
+ if (str1.compare(str2) > 0) {
+ }
+}
Index: docs/clang-tidy/checks/misc-string-compare.rst
===================================================================
--- docs/clang-tidy/checks/misc-string-compare.rst
+++ docs/clang-tidy/checks/misc-string-compare.rst
@@ -0,0 +1,47 @@
+.. title:: clang-tidy - misc-string-compare
+
+misc-string-compare
+===================
+
+Finds string comparisons using the compare method.
+
+A common mistake is to use the string's ``compare`` method instead of using the
+equality or inequality operators. The compare method is intended for sorting
+functions and thus returns a negative number, a positive number or
+zero depending on the lexicographical relationship between the strings compared.
+If an equality or inequality check can suffice, that is recommended.
+
+Examples:
+
+.. code-block:: c++
+
+ std::string str1{"a"};
+ std::string str2{"b"};
+
+ // use str1 != str2 instead.
+ if (str1.compare(str2)) {
+ }
+
+ // use str1 == str2 instead.
+ if (!str1.compare(str2)) {
+ }
+
+ // use str1 == str2 instead.
+ if (str1.compare(str2) == 0) {
+ }
+
+ // use str1 != str2 instead.
+ if (str1.compare(str2) != 0) {
+ }
+
+ // use str1 == str2 instead.
+ if (0 == str1.compare(str2)) {
+ }
+
+ // use str1 != str2 instead.
+ if (0 != str1.compare(str2)) {
+ }
+
+The above code examples shows the list of if-statements that this check will
+give a warning for. All of them uses ``compare`` to check if equality or
+inequality of two strings instead of using the correct operators.
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -80,6 +80,7 @@
misc-sizeof-container
misc-sizeof-expression
misc-static-assert
+ misc-string-compare
misc-string-constructor
misc-string-integer-assignment
misc-string-literal-with-embedded-nul
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -75,6 +75,11 @@
- `misc-pointer-and-integral-operation` check was removed.
+- New `misc-string-compare
+ <http://clang.llvm.org/extra/clang-tidy/checks/misc-string-compare.html>`_ check
+
+ Warns about using ``compare`` to test for string equality or ineqaulity.
+
- New `misc-use-after-move
<http://clang.llvm.org/extra/clang-tidy/checks/misc-use-after-move.html>`_ check
Index: clang-tidy/misc/StringCompareCheck.h
===================================================================
--- clang-tidy/misc/StringCompareCheck.h
+++ clang-tidy/misc/StringCompareCheck.h
@@ -0,0 +1,36 @@
+//===--- StringCompareCheck.h - clang-tidy-----------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef STRING_COMPARE_CHECK_H
+#define STRING_COMPARE_CHECK_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace misc {
+
+/// This check flags all calls compare when used to check for string
+/// equality or inequality.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/misc-string-compare-check.html
+class StringCompareCheck : public ClangTidyCheck {
+public:
+ StringCompareCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
+
+#endif // STRING_COMPARE_CHECK_H
Index: clang-tidy/misc/StringCompareCheck.cpp
===================================================================
--- clang-tidy/misc/StringCompareCheck.cpp
+++ clang-tidy/misc/StringCompareCheck.cpp
@@ -0,0 +1,57 @@
+//===--- MiscStringCompare.cpp - clang-tidy--------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "StringCompareCheck.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 {
+namespace misc {
+
+void StringCompareCheck::registerMatchers(MatchFinder *Finder) {
+ if (!getLangOpts().CPlusPlus)
+ return;
+ const auto strCompare = cxxMemberCallExpr(
+ callee(cxxMethodDecl(hasName("compare"),
+ ofClass(classTemplateSpecializationDecl(
+ hasName("::std::basic_string"))))),
+ argumentCountIs(1), callee(memberExpr()));
+
+ // First and second case: cast str.compare(str) to boolean.
+ Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()),
+ has(strCompare))
+ .bind("match"),
+ this);
+
+ // Third and fourth case: str.compare(str) == 0 and str.compare(str) != 0.
+ Finder->addMatcher(
+ binaryOperator(anyOf(hasOperatorName("=="), hasOperatorName("!=")),
+ hasEitherOperand(strCompare),
+ hasEitherOperand(integerLiteral(equals(0))))
+ .bind("match"),
+ this);
+}
+
+void StringCompareCheck::check(const MatchFinder::MatchResult &Result) {
+ if (const auto *Matched = Result.Nodes.getNodeAs<Stmt>("match")) {
+ diag(Matched->getLocStart(),
+ "do not use compare to test equality of strings; "
+ "use the string equality operator instead");
+ }
+ // FIXME: Add fixit to fix the code. This seems a little tricky for
+ // cases one and two. Cases three and four seems easy to fix.
+}
+
+} // namespace misc
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/misc/MiscTidyModule.cpp
===================================================================
--- clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tidy/misc/MiscTidyModule.cpp
@@ -35,6 +35,7 @@
#include "SizeofContainerCheck.h"
#include "SizeofExpressionCheck.h"
#include "StaticAssertCheck.h"
+#include "StringCompareCheck.h"
#include "StringConstructorCheck.h"
#include "StringIntegerAssignmentCheck.h"
#include "StringLiteralWithEmbeddedNulCheck.h"
@@ -105,6 +106,7 @@
CheckFactories.registerCheck<SizeofExpressionCheck>(
"misc-sizeof-expression");
CheckFactories.registerCheck<StaticAssertCheck>("misc-static-assert");
+ CheckFactories.registerCheck<StringCompareCheck>("misc-string-compare");
CheckFactories.registerCheck<StringConstructorCheck>(
"misc-string-constructor");
CheckFactories.registerCheck<StringIntegerAssignmentCheck>(
Index: clang-tidy/misc/CMakeLists.txt
===================================================================
--- clang-tidy/misc/CMakeLists.txt
+++ clang-tidy/misc/CMakeLists.txt
@@ -28,6 +28,7 @@
SizeofContainerCheck.cpp
SizeofExpressionCheck.cpp
StaticAssertCheck.cpp
+ StringCompareCheck.cpp
StringConstructorCheck.cpp
StringIntegerAssignmentCheck.cpp
StringLiteralWithEmbeddedNulCheck.cpp
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits