File: cxx-member-initializer-const-field.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (120 lines) | stat: -rw-r--r-- 3,048 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s

// This tests false-positive issues related to PR48534.
//
// Essentially, having a default member initializer for a constant member does
// not necessarily imply the member will have the given default value.

struct WithConstructor {
  int *const ptr = nullptr;
  WithConstructor(int *x) : ptr(x) {}

  static auto compliant() {
    WithConstructor c(new int);
    return *(c.ptr); // no warning
  }

  static auto compliantWithParam(WithConstructor c) {
    return *(c.ptr); // no warning
  }

  static auto issue() {
    WithConstructor c(nullptr);
    return *(c.ptr); // expected-warning{{Dereference of null pointer (loaded from field 'ptr')}}
  }
};

struct RegularAggregate {
  int *const ptr = nullptr;

  static int compliant() {
    RegularAggregate c{new int};
    return *(c.ptr); // no warning
  }

  static int issue() {
    RegularAggregate c;
    return *(c.ptr); // expected-warning{{Dereference of null pointer (loaded from field 'ptr')}}
  }
};

struct WithConstructorAndArithmetic {
  int const i = 0;
  WithConstructorAndArithmetic(int x) : i(x + 1) {}

  static int compliant(int y) {
    WithConstructorAndArithmetic c(0);
    return y / c.i; // no warning
  }

  static int issue(int y) {
    WithConstructorAndArithmetic c(-1);
    return y / c.i; // expected-warning{{Division by zero}}
  }
};

struct WithConstructorDeclarationOnly {
  int const i = 0;
  WithConstructorDeclarationOnly(int x); // definition not visible.

  static int compliant1(int y) {
    WithConstructorDeclarationOnly c(0);
    return y / c.i; // no warning
  }

  static int compliant2(int y) {
    WithConstructorDeclarationOnly c(-1);
    return y / c.i; // no warning
  }
};

// NonAggregateFP is not an aggregate (j is a private non-static field) and has no custom constructor.
// So we know i and j will always be 0 and 42, respectively.
// That being said, this is not implemented because it is deemed too rare to be worth the complexity.
struct NonAggregateFP {
public:
  int const i = 0;

private:
  int const j = 42;

public:
  static int falsePositive1(NonAggregateFP c) {
    return 10 / c.i; // FIXME: Currently, no warning.
  }

  static int falsePositive2(NonAggregateFP c) {
    return 10 / (c.j - 42); // FIXME: Currently, no warning.
  }
};

struct NonAggregate {
public:
  int const i = 0;

private:
  int const j = 42;

  NonAggregate(NonAggregate const &); // not provided, could set i and j to arbitrary values.

public:
  static int compliant1(NonAggregate c) {
    return 10 / c.i; // no warning
  }

  static int compliant2(NonAggregate c) {
    return 10 / (c.j - 42); // no warning
  }
};

struct WithStaticMember {
  static int const i = 0;

  static int issue1(WithStaticMember c) {
    return 10 / c.i; // expected-warning{{division by zero is undefined}} expected-warning{{Division by zero}}
  }

  static int issue2() {
    return 10 / WithStaticMember::i; // expected-warning{{division by zero is undefined}} expected-warning{{Division by zero}}
  }
};