aboutsummaryrefslogtreecommitdiff
path: root/test/Analysis/inlining/false-positive-suppression.cpp
blob: f27c7cf364a9f4e572ea7ebfd4bf4795242b2b10 (plain)
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
121
122
123
124
125
126
127
// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-config suppress-null-return-paths=false -verify %s
// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify -DSUPPRESSED=1 %s

namespace rdar12676053 {
  // Delta-reduced from a preprocessed file.
  template<class T>
  class RefCount {
    T *ref;
  public:
    T *operator->() const {
      return ref ? ref : 0;
    }
  };

  class string {};

  class ParserInputState {
  public:
    string filename;
  };

  class Parser {
    void setFilename(const string& f)  {
      inputState->filename = f;
#ifndef SUPPRESSED
// expected-warning@-2 {{Called C++ object pointer is null}}
#endif
    }
  protected:
    RefCount<ParserInputState> inputState;
  };
}


// This is the standard placement new.
inline void* operator new(__typeof__(sizeof(int)), void* __p) throw()
{
  return __p;
}

extern bool coin();

namespace References {
  class Map {
    int *&getNewBox();
    int *firstBox;

  public:
    int *&getValue(int key) {
      if (coin()) {
        return firstBox;
      } else {
        int *&newBox = getNewBox();
        newBox = 0;
        return newBox;
      }
    }

    int *&getValueIndirectly(int key) {
      int *&valueBox = getValue(key);
      return valueBox;
    }
  };

  void testMap(Map &m, int i) {
    *m.getValue(i) = 1;
#ifndef SUPPRESSED
    // expected-warning@-2 {{Dereference of null pointer}}
#endif

    *m.getValueIndirectly(i) = 1;
#ifndef SUPPRESSED
    // expected-warning@-2 {{Dereference of null pointer}}
#endif

    int *&box = m.getValue(i);
    extern int *getPointer();
    box = getPointer();
    *box = 1; // no-warning

    int *&box2 = m.getValue(i);
    box = 0;
    *box = 1; // expected-warning {{Dereference of null pointer}}
  }

  class SomeClass {
  public:
    void doSomething();
  };

  SomeClass *&getSomeClass() {
    if (coin()) {
      extern SomeClass *&opaqueClass();
      return opaqueClass();
    } else {
      static SomeClass *sharedClass;
      sharedClass = 0;
      return sharedClass;
    }
  }

  void testClass() {
    getSomeClass()->doSomething();
#ifndef SUPPRESSED
    // expected-warning@-2 {{Called C++ object pointer is null}}
#endif

    // Separate the lvalue-to-rvalue conversion from the subsequent dereference.
    SomeClass *object = getSomeClass();
    object->doSomething();
#ifndef SUPPRESSED
    // expected-warning@-2 {{Called C++ object pointer is null}}
#endif
  }

  SomeClass *getNull() {
    return 0;
  }

  SomeClass &returnNullReference() {
    SomeClass *x = getNull();
    return *x;
#ifndef SUPPRESSED
    // expected-warning@-2 {{Returning null reference}}
#endif
  }
}