File: nonnull.c

package info (click to toggle)
chibicc 1.0.23.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,832 kB
  • sloc: ansic: 62,911; sh: 275; makefile: 92
file content (39 lines) | stat: -rw-r--r-- 819 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stddef.h>

#include "test.h"

// Declarations + definitions so linker can find them
void foo(char *p, char *q) __attribute__((__nonnull__));
void foo(char *p, char *q) { }

void bar(char *p, char *q, char *r) __attribute__((__nonnull__(1, 3)));
void bar(char *p, char *q, char *r) { }

void baz(char *p, char *q) __attribute__((__nonnull__));
void baz(char *p, char *q) {
  if (p)
    *p = 'A';
  if (q)
    *q = 'B';
}

typedef void (*nonnull_fn_t)(char *p, char *q) __attribute__((__nonnull__(2)));

void use_typedef(nonnull_fn_t fn, char *a, char *b) {
  fn(a, b);
}

int main() {
  char x = 'x', y = 'y', z = 'z';

  foo(&x, &y);
  bar(&x, NULL, &z);
  baz(&x, &y);

  use_typedef(baz, &x, &y);
  printf("x: %c, y: %c, z: %c\n", x, y, z);
  ASSERT(65, x);
  ASSERT(66, y);
  ASSERT(122, z);
  return 0;
}