1896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -std=c++11 -verify %s
2896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -std=c++11 %s 2>&1 | FileCheck %s
3ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
41838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu#include <stddef.h>
51838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu
6ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef   signed char  int8_t;
7ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef   signed short int16_t;
8ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef   signed int   int32_t;
9ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef   signed long  int64_t;
10ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
11ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef unsigned char  uint8_t;
12ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef unsigned short uint16_t;
13ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef unsigned int   uint32_t;
14ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCalltypedef unsigned long  uint64_t;
15ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
16ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall// <rdar://problem/7909130>
17ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCallnamespace test0 {
18ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  int32_t test1_positive(char *I, char *E) {
19ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return (E - I); // expected-warning {{implicit conversion loses integer precision}}
20ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
21ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
22ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  int32_t test1_negative(char *I, char *E) {
23ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return static_cast<int32_t>(E - I);
24ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
25ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
26ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  uint32_t test2_positive(uint64_t x) {
27ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return x; // expected-warning {{implicit conversion loses integer precision}}
28ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
29ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
30ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  uint32_t test2_negative(uint64_t x) {
31ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return (uint32_t) x;
32ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
33ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall}
34ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
35ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCallnamespace test1 {
36ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  uint64_t test1(int x, unsigned y) {
37ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return sizeof(x == y);
38ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
39ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
40ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  uint64_t test2(int x, unsigned y) {
41ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return __alignof(x == y);
42ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
43ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall
44ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  void * const foo();
45ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  bool test2(void *p) {
46ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall    return p == foo();
47ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall  }
48ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall}
4915d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall
5015d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCallnamespace test2 {
5115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall  struct A {
5215d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall    unsigned int x : 2;
5315d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall    A() : x(10) {} // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
5415d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall  };
5515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall}
561838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu
5795187bdd5c955c8edf3527eae41f1e4f80377f27David Blaikie// This file tests -Wnull-conversion, a subcategory of -Wconversion
5895187bdd5c955c8edf3527eae41f1e4f80377f27David Blaikie// which is on by default.
5995187bdd5c955c8edf3527eae41f1e4f80377f27David Blaikie
601838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieuvoid test3() {
612b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  int a = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
621838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu  int b;
632b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  b = NULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
6495187bdd5c955c8edf3527eae41f1e4f80377f27David Blaikie  long l = NULL; // FIXME: this should also warn, but currently does not if sizeof(NULL)==sizeof(inttype)
652b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  int c = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
661838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu  int d;
672b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  d = ((((NULL)))); // expected-warning {{implicit conversion of NULL constant to 'int'}}
68be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie  bool bl = NULL; // expected-warning {{implicit conversion of NULL constant to 'bool'}}
692b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  char ch = NULL; // expected-warning {{implicit conversion of NULL constant to 'char'}}
702b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  unsigned char uch = NULL; // expected-warning {{implicit conversion of NULL constant to 'unsigned char'}}
712b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  short sh = NULL; // expected-warning {{implicit conversion of NULL constant to 'short'}}
72b26331b4ff419b22861b0823daf83bc8f22a6803David Blaikie  double dbl = NULL; // expected-warning {{implicit conversion of NULL constant to 'double'}}
73b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie
74b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // Use FileCheck to ensure we don't get any unnecessary macro-expansion notes
75b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // (that don't appear as 'real' notes & can't be seen/tested by -verify)
76b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // CHECK-NOT: note:
77b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // CHECK: note: expanded from macro 'FINIT'
78b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie#define FINIT int a3 = NULL;
79b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  FINIT // expected-warning {{implicit conversion of NULL constant to 'int'}}
809fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie
819fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie  // we don't catch the case of #define FOO NULL ... int i = FOO; but that seems a bit narrow anyway
829fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie  // and avoiding that helps us skip these cases:
839fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie#define NULL_COND(cond) ((cond) ? &a : NULL)
849fb1ac520d1806ba2b069f6087f40fc9c704b067David Blaikie  bool bl2 = NULL_COND(true); // don't warn on NULL conversion through the conditional operator across a macro boundary
85def07624ecc535431e0c814b4b5b842de8a06997David Blaikie  if (NULL_COND(true))
86def07624ecc535431e0c814b4b5b842de8a06997David Blaikie    ;
87def07624ecc535431e0c814b4b5b842de8a06997David Blaikie  while (NULL_COND(true))
88def07624ecc535431e0c814b4b5b842de8a06997David Blaikie    ;
89def07624ecc535431e0c814b4b5b842de8a06997David Blaikie  for (; NULL_COND(true); )
90def07624ecc535431e0c814b4b5b842de8a06997David Blaikie    ;
91def07624ecc535431e0c814b4b5b842de8a06997David Blaikie  do ;
92def07624ecc535431e0c814b4b5b842de8a06997David Blaikie  while(NULL_COND(true));
9328a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie  int *ip = NULL;
9428a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie  int (*fp)() = NULL;
9528a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie  struct foo {
9628a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie    int n;
9728a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie    void func();
9828a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie  };
9928a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie  int foo::*datamem = NULL;
10028a5f0cdfcc927a94c1bda6a225200762df22411David Blaikie  int (foo::*funmem)() = NULL;
1011838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu}
102c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie
103c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikienamespace test4 {
1045729672524865f212cbadb51170dca0694e79a71David Blaikie  // FIXME: We should warn for non-dependent args (only when the param type is also non-dependent) only once
1055729672524865f212cbadb51170dca0694e79a71David Blaikie  // not once for the template + once for every instantiation
106c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  template<typename T>
107ca2e1b7990230c5088eb2f280cea9db5f516ad69David Blaikie  void tmpl(char c = NULL, // expected-warning 4 {{implicit conversion of NULL constant to 'char'}}
108c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie            T a = NULL, // expected-warning {{implicit conversion of NULL constant to 'char'}} \
109ca2e1b7990230c5088eb2f280cea9db5f516ad69David Blaikie                           expected-warning 2 {{implicit conversion of NULL constant to 'int'}}
110c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie            T b = 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}}
111c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  }
112c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie
113c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  template<typename T>
114c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  void tmpl2(T t = NULL) {
115c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  }
116c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie
117c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  void func() {
1185729672524865f212cbadb51170dca0694e79a71David Blaikie    tmpl<char>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<char>' required here}}
1195729672524865f212cbadb51170dca0694e79a71David Blaikie    tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
120ca2e1b7990230c5088eb2f280cea9db5f516ad69David Blaikie    // FIXME: We should warn only once for each template instantiation - not once for each call
121ca2e1b7990230c5088eb2f280cea9db5f516ad69David Blaikie    tmpl<int>(); // expected-note 2 {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
122c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie    tmpl2<int*>();
123c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  }
124c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie}
125be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie
126be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikienamespace test5 {
127be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie  template<int I>
128be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie  void func() {
129be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie    bool b = I;
130be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie  }
131be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie
132be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie  template void func<3>();
133be0ee875d8a91c031a085cbbd73ad9e8dc1aa8ffDavid Blaikie}
134896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie
135896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikienamespace test6 {
136896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie  decltype(nullptr) func() {
137896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie    return NULL;
138896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie  }
139896c7dd62801c0aacf14b05183c3c471c1fe9f7aDavid Blaikie}
140