================
@@ -188,3 +188,12 @@ 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" void* malloc(unsigned size);
+
+void check_prints(){
+ char* a = (char*) malloc(20);
+ const char* b = "Hello World";
+ snprintf(a, sizeof(a), "%s", b); // expected-warning{{'snprintf' call
operates on objects of type 'char' while the size is based on a different type
'char *'}} expected-note {{did you mean to provide an explicit length?}}
----------------
ojhunt wrote:
This can be done more nicely with
```cpp
snprintf(a, sizeof(a), "%s", b); // #some_label
// expected-warning@#some_label {{'snprintf' call operates on objects of type
'char' while the size is based on a different type 'char *'}}
// expected-note@#some_label {{did you mean to provide an explicit length?}}
```
I think the multiple checks trailing the line predates relative locations and
labels we have these days.
In principle you can do
```cpp
snprintf(a, sizeof(a), "%s", b); // #some_label
// expected-warning@-1 {{'snprintf' call operates on objects of type 'char'
while the size is based on a different type 'char *'}}
// expected-note@-2 {{did you mean to provide an explicit length?}}
```
But since learning of them I've found explicit labels to be much clearer, and
more robust as additional tests and expectations are added, so they're what I'd
recommend be your default choice.
They're especially helpful with template diagnostics, as they make it easier to
actually show which code is responsible for specific diagnostics, e.g given
something like
```cpp
template <class T> void foo() {
some_expression_that_triggers_a_diagnostic();
}
...
foo<int>();
foo<float>();
```
This will produce the diagnostic indicating the line in the template definition
with a note pointing to the line that caused the instantiation. Diagnostics on
the same line or relative diagnostics are not useful to anyone looking at the
tests, using labels we can do
```cpp
template <class T> void foo() {
some_expression_that_triggers_a_diagnostic(); //#foo_diagnostic
}
...
foo<int>(); // #foo_int_call
// expected-whatever@#foo_diagnostic {{foo<int> specific diagnostic}}
// expected-note@#foo_int_call {{instantiated from here}}
foo<float>(); // #foo_float_call
// expected-whatever@#foo_diagnostic {{foo<float> specific diagnostic}}
// expected-note@#foo_float_call {{instantiated from here}}
```
https://github.com/llvm/llvm-project/pull/170637
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits