================
@@ -188,3 +188,111 @@ void strcpy_and_friends() {
   strndup(FOO, sizeof(FOO)); // \
       // expected-warning {{'strndup' call operates on objects of type 'const 
char' while the size is based on a different type 'const char *'}} 
expected-note{{did you mean to provide an explicit length?}}
 }
+
+extern "C" int snprintf(char* buffer, __SIZE_TYPE__ buf_size, const char* 
format, ...);
+extern "C" int vsnprintf(char* buffer, __SIZE_TYPE__ buf_size, const char* 
format, __builtin_va_list arg);
+extern "C" void* malloc(unsigned size);
+
+// This is a dependent context by none of the actual operations are dependent.
+template <class T> void fooNone() {
+   char* a = (char*) malloc(20);
+   const char* b = "Hello World";
+   snprintf(a, sizeof(a), "%s", b); // #fooNone_diagnostic
+    // expected-warning@#fooNone_diagnostic {{'snprintf' call operates on 
objects of type 'char' while the size is based on a different type 'char *'}}
+    // expected-note@#fooNone_diagnostic {{did you mean to provide an explicit 
length?}}
+}
+
+template <class T> void fooType() {
+   T a = (T) malloc(20); // #fooType_error
+   const char* b = "Hello World";
+   snprintf((char *)a, sizeof(a), "%s", b);// #fooType_diagnostic
+}
+
+template <class T> void fooTypePtr() {
+   T *a = (T *) malloc(20);
+   const char* b = "Hello World";
+   snprintf((char *)a, sizeof(a), "%s", b);// #fooTypePtr_diagnostic
+}
+
+void check_prints(){
+    char* a = (char*) malloc(20);
+    const char* b = "Hello World";
+    snprintf(a, sizeof(a), "%s", b); // #CheckBasePrint
+    // expected-warning@#CheckBasePrint {{'snprintf' call operates on objects 
of type 'char' while the size is based on a different type 'char *'}}
+    // expected-note@#CheckBasePrint {{did you mean to provide an explicit 
length?}}
+
+    snprintf((char*)a, sizeof(a), "%s", b); // #CheckCastPrint
+    // expected-warning@#CheckCastPrint {{'snprintf' call operates on objects 
of type 'char' while the size is based on a different type 'char *'}}
+    // expected-note@#CheckCastPrint {{did you mean to provide an explicit 
length?}}
+
+    __builtin_va_list list;
+    vsnprintf(a, sizeof(a), "%s", list);  // #VSNprintCheck
+    // expected-warning@#VSNprintCheck {{'vsnprintf' call operates on objects 
of type 'char' while the size is based on a different type 'char *'}}
+    // expected-note@#VSNprintCheck {{did you mean to provide an explicit 
length?}}
+
+    vsnprintf((char*)a, sizeof(a), "%s", list);  // #VSNprintCastCheck
+    // expected-warning@#VSNprintCastCheck {{'vsnprintf' call operates on 
objects of type 'char' while the size is based on a different type 'char *'}}
+    // expected-note@#VSNprintCastCheck {{did you mean to provide an explicit 
length?}}
+
+    //No diagnostic output when dest is an array
+    char c[20];
+    const char* d = "Hello World";
+    snprintf(c, sizeof(c), "%s", d); // expected-none
----------------
ojhunt wrote:

and today I learned about expected-none :D

https://github.com/llvm/llvm-project/pull/170637
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to