Home | History | Annotate | Download | only in Inputs
      1 // This is a fake system header with divide-by-zero bugs introduced in
      2 // c++ std library functions. We use these bugs to test hard-coded
      3 // suppression of diagnostics within standard library functions that are known
      4 // to produce false positives.
      5 
      6 #pragma clang system_header
      7 
      8 typedef unsigned char uint8_t;
      9 
     10 typedef __typeof__(sizeof(int)) size_t;
     11 void *memmove(void *s1, const void *s2, size_t n);
     12 
     13 namespace std {
     14 
     15   template <class _Tp>
     16   class allocator {
     17   public:
     18     void deallocate(void *p) {
     19       ::delete p;
     20     }
     21   };
     22 
     23   template <class _Alloc>
     24   class allocator_traits {
     25   public:
     26     static void deallocate(void *p) {
     27       _Alloc().deallocate(p);
     28     }
     29   };
     30 
     31   template <class _Tp, class _Alloc>
     32   class __list_imp
     33   {};
     34 
     35   template <class _Tp, class _Alloc = allocator<_Tp> >
     36   class list
     37   : private __list_imp<_Tp, _Alloc>
     38   {
     39   public:
     40     void pop_front() {
     41       // Fake use-after-free.
     42       // No warning is expected as we are suppressing warning coming
     43       // out of std::list.
     44       int z = 0;
     45       z = 5/z;
     46     }
     47     bool empty() const;
     48   };
     49 
     50   // basic_string
     51   template<class _CharT, class _Alloc = allocator<_CharT> >
     52   class __attribute__ ((__type_visibility__("default"))) basic_string {
     53     bool isLong;
     54     union {
     55       _CharT localStorage[4];
     56       _CharT *externalStorage;
     57 
     58       void assignExternal(_CharT *newExternal) {
     59         externalStorage = newExternal;
     60       }
     61     } storage;
     62 
     63     typedef allocator_traits<_Alloc> __alloc_traits;
     64 
     65   public:
     66     basic_string();
     67 
     68     void push_back(int c) {
     69       // Fake error trigger.
     70       // No warning is expected as we are suppressing warning coming
     71       // out of std::basic_string.
     72       int z = 0;
     73       z = 5/z;
     74     }
     75 
     76     _CharT *getBuffer() {
     77       return isLong ? storage.externalStorage : storage.localStorage;
     78     }
     79 
     80     basic_string &operator +=(int c) {
     81       // Fake deallocate stack-based storage.
     82       // No warning is expected as we are suppressing warnings within
     83       // std::basic_string.
     84       __alloc_traits::deallocate(getBuffer());
     85     }
     86 
     87     basic_string &operator =(const basic_string &other) {
     88       // Fake deallocate stack-based storage, then use the variable in the
     89       // same union.
     90       // No warning is expected as we are suppressing warnings within
     91       // std::basic_string.
     92       __alloc_traits::deallocate(getBuffer());
     93       storage.assignExternal(new _CharT[4]);
     94     }
     95   };
     96 
     97 template<class _Engine, class _UIntType>
     98 class __independent_bits_engine {
     99 public:
    100   // constructors and seeding functions
    101   __independent_bits_engine(_Engine& __e, size_t __w);
    102 };
    103 
    104 template<class _Engine, class _UIntType>
    105 __independent_bits_engine<_Engine, _UIntType>
    106     ::__independent_bits_engine(_Engine& __e, size_t __w)
    107 {
    108   // Fake error trigger.
    109   // No warning is expected as we are suppressing warning coming
    110   // out of std::__independent_bits_engine.
    111   int z = 0;
    112   z = 5/z;
    113 }
    114 
    115 #if __has_feature(cxx_decltype)
    116 typedef decltype(nullptr) nullptr_t;
    117 
    118 template<class _Tp>
    119 class shared_ptr
    120 {
    121 public:
    122   constexpr shared_ptr(nullptr_t);
    123   explicit shared_ptr(_Tp* __p);
    124 
    125   shared_ptr(shared_ptr&& __r) { }
    126 
    127   ~shared_ptr();
    128 
    129   shared_ptr& operator=(shared_ptr&& __r) {
    130     // Fake error trigger.
    131     // No warning is expected as we are suppressing warning coming
    132     // out of std::shared_ptr.
    133     int z = 0;
    134     z = 5/z;
    135   }
    136 };
    137 
    138 template<class _Tp>
    139 inline
    140 constexpr
    141 shared_ptr<_Tp>::shared_ptr(nullptr_t) {
    142 }
    143 
    144 #endif // __has_feature(cxx_decltype)
    145 }
    146 
    147