summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp
diff options
context:
space:
mode:
authorEtienne Bergeron <etienneb@google.com>2016-04-15 16:31:15 +0000
committerEtienne Bergeron <etienneb@google.com>2016-04-15 16:31:15 +0000
commit3c5be6c9a72bad003348459a578b6b37678b075e (patch)
tree45050b741332121ab3581141e4fafd2e72ff0a68 /clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp
parent19124d3d7e01fd0f88e4884e92f757f3801a1e86 (diff)
downloadbcm5719-llvm-3c5be6c9a72bad003348459a578b6b37678b075e.tar.gz
bcm5719-llvm-3c5be6c9a72bad003348459a578b6b37678b075e.zip
[clang-tidy] Add checker for operations between integrals and pointers
Summary: This check is finding suspicious operations involving pointers and integral types; which are most likely bugs. Examples: subversion/v1_6/subversion/libsvn_subr/utf.c ``` static const char * fuzzy_escape(const char *src, apr_size_t len, apr_pool_t *pool) { [...] while (src_orig < src_end) { if (! svn_ctype_isascii(*src_orig) || src_orig == '\0') // Should be *src_orig { ``` apache2/v2_2_23/modules/metadata/mod_headers.c ``` static char *parse_format_tag(apr_pool_t *p, format_tag *tag, const char **sa) { [...] tag->arg = '\0'; // ERROR: tag->arg has type char* /* grab the argument if there is one */ if (*s == '{') { ++s; tag->arg = ap_getword(p,&s,'}'); } ``` Reviewers: alexfh Subscribers: Eugene.Zelenko, cfe-commits Differential Revision: http://reviews.llvm.org/D19118 llvm-svn: 266450
Diffstat (limited to 'clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp')
-rw-r--r--clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp b/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp
new file mode 100644
index 00000000000..4834a469e76
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/misc-pointer-and-integral-operation-cxx98.cpp
@@ -0,0 +1,45 @@
+// RUN: %check_clang_tidy %s misc-pointer-and-integral-operation %t -- -- -std=c++98
+
+bool* pb;
+char* pc;
+int* pi;
+
+int Test() {
+ pb = false;
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: suspicious assignment from bool to pointer [misc-pointer-and-integral-operation]
+ pc = '\0';
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: suspicious assignment from char to pointer
+
+ pb = (false?false:false);
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: suspicious assignment from bool to pointer
+ pb = (4 != 5?false:false);
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: suspicious assignment from bool to pointer
+
+ if (pb < false) return 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious operation between pointer and bool literal
+ if (pb != false) return 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious operation between pointer and bool literal
+ if (pc < '\0') return 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious operation between pointer and character literal
+ if (pc != '\0') return 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious operation between pointer and character literal
+ if (pi < '\0') return 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious operation between pointer and character literal
+ if (pi != '\0') return 0;
+ // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: suspicious operation between pointer and character literal
+
+ return 1;
+}
+
+int Valid() {
+ *pb = false;
+ *pc = '\0';
+
+ pb += 0;
+ pc += 0;
+ pi += 0;
+
+ pb += (pb != 0);
+ pc += (pc != 0);
+ pi += (pi != 0);
+}
OpenPOWER on IntegriCloud