]> granicus.if.org Git - clang/commitdiff
CastToStructChecker: use 'isStructureType()' instead of 'isRecordType()' to determine...
authorTed Kremenek <kremenek@apple.com>
Wed, 11 Nov 2009 06:43:42 +0000 (06:43 +0000)
committerTed Kremenek <kremenek@apple.com>
Wed, 11 Nov 2009 06:43:42 +0000 (06:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86813 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/CastToStructChecker.cpp
test/Analysis/misc-ps.m

index 5d0110e4eb23020d7619c40f841fe59f7dabe4be..bda8ff47bf85a98b31297e63814623f6ed0b75e8 100644 (file)
@@ -50,7 +50,7 @@ void CastToStructChecker::PreVisitCastExpr(CheckerContext &C,
   QualType OrigPointeeTy = OrigPTy->getPointeeType();
   QualType ToPointeeTy = ToPTy->getPointeeType();
 
-  if (!ToPointeeTy->isRecordType())
+  if (!ToPointeeTy->isStructureType())
     return;
 
   // We allow cast from void*.
index d9bba7d10207b6bd2fa499e30dfa056ea18310e1..b3b4e9ab7aba3763d7f2ef2d9850070d2bba24b1 100644 (file)
@@ -692,7 +692,6 @@ void *rdar7152418_bar();
 // conversions of the symbol as necessary.
 //===----------------------------------------------------------------------===//
 
-
 // Previously this would crash once we started eagerly evaluating symbols whose 
 // values were constrained to a single value.
 void test_symbol_fold_1(signed char x) {
@@ -722,5 +721,27 @@ unsigned test_symbol_fold_3(void) {
   if (x == 54)
     return (x << 8) | 0x5;
   return 0;
-}  
+} 
+
+//===----------------------------------------------------------------------===//
+// Tests for the warning of casting a non-struct type to a struct type
+//===----------------------------------------------------------------------===//
+
+typedef struct {unsigned int v;} NSSwappedFloat;
+
+NSSwappedFloat test_cast_nonstruct_to_struct(float x) {
+  struct hodor {
+    float number;
+    NSSwappedFloat sf;
+  };
+  return ((struct hodor *)&x)->sf; // expected-warning{{Casting a non-structure type to a structure type and accessing a field can lead to memory access errors or data corruption}}
+}
+
+NSSwappedFloat test_cast_nonstruct_to_union(float x) {
+  union bran {
+    float number;
+    NSSwappedFloat sf;
+  };
+  return ((union bran *)&x)->sf; // no-warning
+}