summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp
diff options
context:
space:
mode:
authorEtienne Bergeron <etienneb@google.com>2016-04-26 16:53:21 +0000
committerEtienne Bergeron <etienneb@google.com>2016-04-26 16:53:21 +0000
commit1b94f6504b37df09244e5fe8c0507559823edc48 (patch)
tree9d34d322277f279190ac2de5e3ff7da196569ac0 /clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp
parentdbcf5b99d03a278e681e3bd5632d69751f8ce9d1 (diff)
downloadbcm5719-llvm-1b94f6504b37df09244e5fe8c0507559823edc48.tar.gz
bcm5719-llvm-1b94f6504b37df09244e5fe8c0507559823edc48.zip
[clang-tidy] Enhance misc-suspicious-string-compare to move down false-positives.
Summary: The checker was noisy when running over llvm code base. This patch is impriving the way string-compare functions are matched. 1) By default, do not report !strcmp(...) unless it's activate by the user, 2) Only match suspicious expression over a subset of expression (binary operator), 3) Added matching of macro wrapper used with clang on linux. See bug: 27465. Reviewers: alexfh Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D19497 llvm-svn: 267570
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp b/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp
index 8bdb897cbb1..4c51676ded3 100644
--- a/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp
+++ b/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp
@@ -15,6 +15,8 @@ static const unsigned char U[] = "abc";
static const unsigned char V[] = "xyz";
static const wchar_t W[] = L"abc";
+int strlen(const char *);
+
int memcmp(const void *, const void *, size);
int wmemcmp(const wchar_t *, const wchar_t *, size);
int memicmp(const void *, const void *, size);
@@ -297,3 +299,39 @@ int test_implicit_compare_with_functions() {
return 1;
}
+
+int strcmp_wrapper1(const char* a, const char* b) {
+ return strcmp(a, b);
+}
+
+int strcmp_wrapper2(const char* a, const char* b) {
+ return (a && b) ? strcmp(a, b) : 0;
+}
+
+#define macro_strncmp(s1, s2, n) \
+ (__extension__ (__builtin_constant_p (n) \
+ && ((__builtin_constant_p (s1) \
+ && strlen (s1) < ((size) (n))) \
+ || (__builtin_constant_p (s2) \
+ && strlen (s2) < ((size) (n)))) \
+ ? strcmp (s1, s2) : strncmp (s1, s2, n)))
+
+int strncmp_macro(const char* a, const char* b) {
+ if (macro_strncmp(a, b, 4))
+ return 0;
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: function 'strcmp' is called without explicitly comparing result
+
+ if (macro_strncmp(a, b, 4) == 2)
+ return 0;
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: function 'strcmp' is compared to a suspicious constant
+
+ if (macro_strncmp(a, b, 4) <= .0)
+ return 0;
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: function 'strcmp' has suspicious implicit cast
+
+ if (macro_strncmp(a, b, 4) + 0)
+ return 0;
+ // CHECK-MESSAGES: [[@LINE-2]]:7: warning: results of function 'strcmp' used by operator '+'
+
+ return 1;
+}
OpenPOWER on IntegriCloud