warn-cast-align.cpp revision 2340ba00f3e312e9df014e474531270d3263550d
12340ba00f3e312e9df014e474531270d3263550dJohn McCall// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -Wcast-align -verify %s 22340ba00f3e312e9df014e474531270d3263550dJohn McCall 32340ba00f3e312e9df014e474531270d3263550dJohn McCall// Simple casts. 42340ba00f3e312e9df014e474531270d3263550dJohn McCallvoid test0(char *P) { 52340ba00f3e312e9df014e474531270d3263550dJohn McCall char *a; short *b; int *c; 62340ba00f3e312e9df014e474531270d3263550dJohn McCall 72340ba00f3e312e9df014e474531270d3263550dJohn McCall a = (char*) P; 82340ba00f3e312e9df014e474531270d3263550dJohn McCall a = static_cast<char*>(P); 92340ba00f3e312e9df014e474531270d3263550dJohn McCall a = reinterpret_cast<char*>(P); 102340ba00f3e312e9df014e474531270d3263550dJohn McCall typedef char *CharPtr; 112340ba00f3e312e9df014e474531270d3263550dJohn McCall a = CharPtr(P); 122340ba00f3e312e9df014e474531270d3263550dJohn McCall 132340ba00f3e312e9df014e474531270d3263550dJohn McCall b = (short*) P; // expected-warning {{cast from 'char *' to 'short *' increases required alignment from 1 to 2}} 142340ba00f3e312e9df014e474531270d3263550dJohn McCall b = reinterpret_cast<short*>(P); // expected-warning {{cast from 'char *' to 'short *' increases required alignment from 1 to 2}} 152340ba00f3e312e9df014e474531270d3263550dJohn McCall typedef short *ShortPtr; 162340ba00f3e312e9df014e474531270d3263550dJohn McCall b = ShortPtr(P); // expected-warning {{cast from 'char *' to 'ShortPtr' (aka 'short *') increases required alignment from 1 to 2}} 172340ba00f3e312e9df014e474531270d3263550dJohn McCall 182340ba00f3e312e9df014e474531270d3263550dJohn McCall c = (int*) P; // expected-warning {{cast from 'char *' to 'int *' increases required alignment from 1 to 4}} 192340ba00f3e312e9df014e474531270d3263550dJohn McCall c = reinterpret_cast<int*>(P); // expected-warning {{cast from 'char *' to 'int *' increases required alignment from 1 to 4}} 202340ba00f3e312e9df014e474531270d3263550dJohn McCall typedef int *IntPtr; 212340ba00f3e312e9df014e474531270d3263550dJohn McCall c = IntPtr(P); // expected-warning {{cast from 'char *' to 'IntPtr' (aka 'int *') increases required alignment from 1 to 4}} 222340ba00f3e312e9df014e474531270d3263550dJohn McCall} 232340ba00f3e312e9df014e474531270d3263550dJohn McCall 242340ba00f3e312e9df014e474531270d3263550dJohn McCall// Casts from void* are a special case. 252340ba00f3e312e9df014e474531270d3263550dJohn McCallvoid test1(void *P) { 262340ba00f3e312e9df014e474531270d3263550dJohn McCall char *a; short *b; int *c; 272340ba00f3e312e9df014e474531270d3263550dJohn McCall 282340ba00f3e312e9df014e474531270d3263550dJohn McCall a = (char*) P; 292340ba00f3e312e9df014e474531270d3263550dJohn McCall a = static_cast<char*>(P); 302340ba00f3e312e9df014e474531270d3263550dJohn McCall a = reinterpret_cast<char*>(P); 312340ba00f3e312e9df014e474531270d3263550dJohn McCall typedef char *CharPtr; 322340ba00f3e312e9df014e474531270d3263550dJohn McCall a = CharPtr(P); 332340ba00f3e312e9df014e474531270d3263550dJohn McCall 342340ba00f3e312e9df014e474531270d3263550dJohn McCall b = (short*) P; 352340ba00f3e312e9df014e474531270d3263550dJohn McCall b = static_cast<short*>(P); 362340ba00f3e312e9df014e474531270d3263550dJohn McCall b = reinterpret_cast<short*>(P); 372340ba00f3e312e9df014e474531270d3263550dJohn McCall typedef short *ShortPtr; 382340ba00f3e312e9df014e474531270d3263550dJohn McCall b = ShortPtr(P); 392340ba00f3e312e9df014e474531270d3263550dJohn McCall 402340ba00f3e312e9df014e474531270d3263550dJohn McCall c = (int*) P; 412340ba00f3e312e9df014e474531270d3263550dJohn McCall c = static_cast<int*>(P); 422340ba00f3e312e9df014e474531270d3263550dJohn McCall c = reinterpret_cast<int*>(P); 432340ba00f3e312e9df014e474531270d3263550dJohn McCall typedef int *IntPtr; 442340ba00f3e312e9df014e474531270d3263550dJohn McCall c = IntPtr(P); 452340ba00f3e312e9df014e474531270d3263550dJohn McCall} 46