conversion.cpp revision c1c0725978ed31253af5ef4849a124e8fc13ebbb
1ecda6fb71288b1dc3b70ade0c8c75db26ceb1426John McCall// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion -verify %s
2b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -Wconversion %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'}}
682b2bbee3071cee1b03520d9bf0c80c4f0bbe60ebDavid Blaikie  bool bl = NULL; // FIXME: this should warn but we currently suppress a bunch of conversion-to-bool warnings including this one
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'}}
72b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie
73b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // Use FileCheck to ensure we don't get any unnecessary macro-expansion notes
74b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // (that don't appear as 'real' notes & can't be seen/tested by -verify)
75b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // CHECK-NOT: note:
76b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // CHECK: note: expanded from macro 'FNULL'
77b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie#define FNULL NULL
78b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  int a2 = FNULL; // expected-warning {{implicit conversion of NULL constant to 'int'}}
79b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // CHECK-NOT: note:
80b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  // CHECK: note: expanded from macro 'FINIT'
81b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie#define FINIT int a3 = NULL;
82b1360498b0c0794fd7925f4bdac68ae91ff7493bDavid Blaikie  FINIT // expected-warning {{implicit conversion of NULL constant to 'int'}}
831838ca5618bc6c84ca3d13d84717bf32d9862bb3Richard Trieu}
84c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie
85c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikienamespace test4 {
86c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  template<typename T>
87c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  void tmpl(char c = NULL, // expected-warning {{implicit conversion of NULL constant to 'char'}}
88c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie            T a = NULL, // expected-warning {{implicit conversion of NULL constant to 'char'}} \
89c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie                           expected-warning {{implicit conversion of NULL constant to 'int'}}
90c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie            T b = 1024) { // expected-warning {{implicit conversion from 'int' to 'char' changes value from 1024 to 0}}
91c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  }
92c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie
93c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  template<typename T>
94c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  void tmpl2(T t = NULL) {
95c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  }
96c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie
97c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  void func() {
98c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie    tmpl<char>(); // expected-note {{in instantiation of default function argument expression for 'tmpl<char>' required here}}
99c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie    tmpl<int>(); // expected-note {{in instantiation of default function argument expression for 'tmpl<int>' required here}}
100c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie    tmpl2<int*>();
101c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie  }
102c1c0725978ed31253af5ef4849a124e8fc13ebbbDavid Blaikie}
103