brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.7 KiB · 57ce9ac Raw
66 lines · c
1// RUN: %clang_analyze_cc1 -fblocks -analyzer-checker=core,nullability,debug.ExprInspection -verify %s2 3void clang_analyzer_warnIfReached(void);4 5void it_takes_two(int a, int b);6void function_pointer_arity_mismatch(void) {7  void(*fptr)() = it_takes_two;8  fptr(1); // no-crash expected-warning {{Function taking 2 arguments is called with fewer (1)}}9  // expected-warning@-1 {{passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C23}}10}11 12void block_arity_mismatch(void) {13  void(^b)() = ^(int a, int b) { };14  b(1);  // no-crash expected-warning {{Block taking 2 arguments is called with fewer (1)}}15  // expected-warning@-1 {{passing arguments to a function without a prototype is deprecated in all versions of C and is not supported in C23}}16}17 18int *nonnull_return_annotation_indirect(void) __attribute__((returns_nonnull));19int *nonnull_return_annotation_indirect(void) {20  int *x = 0;21  return x; // expected-warning {{Null returned from a function that is expected to return a non-null value}}22}23 24int *nonnull_return_annotation_direct(void) __attribute__((returns_nonnull));25int *nonnull_return_annotation_direct(void) {26  return 0; // expected-warning {{Null returned from a function that is expected to return a non-null value}}27} // expected-warning@-1 {{null returned from function that requires a non-null return value}}28 29int *nonnull_return_annotation_assumed(int* ptr) __attribute__((returns_nonnull));30int *nonnull_return_annotation_assumed(int* ptr) {31  if (ptr) {32    return ptr;33  }34  return ptr; // expected-warning {{Null returned from a function that is expected to return a non-null value}}35}36 37int *produce_nonnull_ptr(void) __attribute__((returns_nonnull));38 39__attribute__((returns_nonnull))40int *cannot_return_null(void) {41  int *x = produce_nonnull_ptr();42  if (!x) {43    clang_analyzer_warnIfReached();44    // expected-warning@-1 {{REACHABLE}}45    // TODO: This warning is a false positive, according to the contract of46    // produce_nonnull_ptr, x cannot be null.47  }48  // Regardless of the potential state split above, x cannot be nullptr49  // according to the produce_nonnull_ptr annotation.50  return x;51  // False positive: expected-warning@-1 {{Null returned from a function that is expected to return a non-null value}}52}53 54__attribute__((returns_nonnull)) int *passthrough(int *p) {55  return p; // no-warning: we have no evidence that `p` is null, i.e., violating the contract56}57 58__attribute__((returns_nonnull)) int *passthrough2(int *p);59int *passthrough2(int *p) {60  return p; // expected-warning{{Null returned from a function that is expected to return a non-null value}}61}62 63void call_with_null(void) {64  passthrough2(0);65}66