summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/test/clang-tidy')
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.c13
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.cpp38
2 files changed, 51 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.c b/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.c
index b291854509c..d1dbf9eff9f 100644
--- a/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.c
+++ b/clang-tools-extra/test/clang-tidy/misc-suspicious-string-compare.c
@@ -64,3 +64,16 @@ int test_valid_patterns() {
if (strcmp(A, "a") == strcmp(A, "b")) return 0;
return 1;
}
+
+int wrapper(const char* a, const char* b) {
+ return strcmp(a, b);
+}
+
+int assignment_wrapper(const char* a, const char* b) {
+ int cmp = strcmp(a, b);
+ return cmp;
+}
+
+int condexpr_wrapper(const char* a, const char* b) {
+ return (a < b) ? strcmp(a, b) : strcmp(b, a);
+}
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