[cfe-commits] r162109 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaCast.cpp test/Sema/warn-bad-function-cast.c

Fariborz Jahanian fjahanian at apple.com
Fri Aug 17 10:22:34 PDT 2012


Author: fjahanian
Date: Fri Aug 17 12:22:34 2012
New Revision: 162109

URL: http://llvm.org/viewvc/llvm-project?rev=162109&view=rev
Log:
c: implement gcc's -Wbad-function-cast which warns
on unsafe cast of a c-function call. This is
a C-only option.

Added:
    cfe/trunk/test/Sema/warn-bad-function-cast.c
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaCast.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=162109&r1=162108&r2=162109&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Aug 17 12:22:34 2012
@@ -157,6 +157,7 @@
 def OverloadedVirtual : DiagGroup<"overloaded-virtual">;
 def PrivateExtern : DiagGroup<"private-extern">;
 def SelTypeCast : DiagGroup<"cast-of-sel-type">;
+def BadFunctionCast : DiagGroup<"bad-function-cast">;
 def ObjCPropertyImpl : DiagGroup<"objc-property-implementation">;
 def ObjCPropertyNoAttribute : DiagGroup<"objc-property-no-attribute">;
 def ObjCMissingSuperCalls : DiagGroup<"objc-missing-super-calls">;
@@ -377,7 +378,8 @@
     ObjCMissingSuperCalls,
     OverloadedVirtual,
     PrivateExtern,
-    SelTypeCast
+    SelTypeCast,
+    BadFunctionCast
  ]>;
 
 // Thread Safety warnings 

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=162109&r1=162108&r2=162109&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Aug 17 12:22:34 2012
@@ -4968,6 +4968,9 @@
 def warn_cast_pointer_from_sel : Warning<
   "cast of type %0 to %1 is deprecated; use sel_getName instead">,
   InGroup<SelTypeCast>;
+def warn_bad_function_cast : Warning<
+  "cast from function call of type %0 to non-matching type %1">,
+  InGroup<BadFunctionCast>, DefaultIgnore;
 def err_cast_pointer_to_non_pointer_int : Error<
   "pointer cannot be cast to type %0">;
 def err_typecheck_expect_scalar_operand : Error<

Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=162109&r1=162108&r2=162109&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Fri Aug 17 12:22:34 2012
@@ -1903,6 +1903,43 @@
     SrcExpr = ExprError();
 }
 
+/// DiagnoseBadFunctionCast - Warn whenever a function call is cast to a 
+///  non-matching type. Such as enum function call to int, int call to
+/// pointer; etc. Cast to 'void' is an exception.
+static void DiagnoseBadFunctionCast(Sema &Self, const ExprResult &SrcExpr,
+                                  QualType DestType) {
+  if (Self.Diags.getDiagnosticLevel(diag::warn_bad_function_cast,
+                                    SrcExpr.get()->getExprLoc()) 
+        == DiagnosticsEngine::Ignored)
+    return;
+  
+  if (!isa<CallExpr>(SrcExpr.get()))
+    return;
+  
+  QualType SrcType = SrcExpr.get()->getType();
+  if (DestType.getUnqualifiedType()->isVoidType())
+    return;
+  if ((SrcType->isAnyPointerType() || SrcType->isBlockPointerType())
+      && (DestType->isAnyPointerType() || DestType->isBlockPointerType()))
+    return;
+  if (SrcType->isIntegerType() && DestType->isIntegerType() &&
+      (SrcType->isBooleanType() == DestType->isBooleanType()) &&
+      (SrcType->isEnumeralType() == DestType->isEnumeralType()))
+    return;
+  if (SrcType->isRealFloatingType() && DestType->isRealFloatingType())
+    return;
+  if (SrcType->isEnumeralType() && DestType->isEnumeralType())
+    return;
+  if (SrcType->isComplexType() && DestType->isComplexType())
+    return;
+  if (SrcType->isComplexIntegerType() && DestType->isComplexIntegerType())
+    return;
+  
+  Self.Diag(SrcExpr.get()->getExprLoc(),
+            diag::warn_bad_function_cast)
+            << SrcType << DestType << SrcExpr.get()->getSourceRange();
+}
+
 /// Check the semantics of a C-style cast operation, in C.
 void CastOperation::CheckCStyleCast() {
   assert(!Self.getLangOpts().CPlusPlus);
@@ -2076,7 +2113,7 @@
     }
   }
   DiagnoseCastOfObjCSEL(Self, SrcExpr, DestType);
-  
+  DiagnoseBadFunctionCast(Self, SrcExpr, DestType);
   Kind = Self.PrepareScalarCast(SrcExpr, DestType);
   if (SrcExpr.isInvalid())
     return;

Added: cfe/trunk/test/Sema/warn-bad-function-cast.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-bad-function-cast.c?rev=162109&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-bad-function-cast.c (added)
+++ cfe/trunk/test/Sema/warn-bad-function-cast.c Fri Aug 17 12:22:34 2012
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 %s -fsyntax-only -Wno-unused-value -Wbad-function-cast -verify
+// rdar://9103192
+
+void vf(void);
+int if1(void);
+char if2(void);
+long if3(void);
+float rf1(void);
+double rf2(void);
+_Complex double cf(void);
+enum e { E1 } ef(void);
+_Bool bf(void);
+char *pf1(void);
+int *pf2(void);
+
+void
+foo(void)
+{
+  /* Casts to void types are always OK.  */
+  (void)vf();
+  (void)if1();
+  (void)cf();
+  (const void)bf();
+  /* Casts to the same type or similar types are OK.  */
+  (int)if1();
+  (long)if2();
+  (char)if3();
+  (float)rf1();
+  (long double)rf2();
+  (_Complex float)cf();
+  (enum f { F1 })ef();
+  (_Bool)bf();
+  (void *)pf1();
+  (char *)pf2();
+  /* All following casts issue warning */
+  (float)if1(); /* expected-warning {{cast from function call of type 'int' to non-matching type 'float'}} */
+  (double)if2(); /* expected-warning {{cast from function call of type 'char' to non-matching type 'double'}} */
+  (_Bool)if3(); /* expected-warning {{cast from function call of type 'long' to non-matching type '_Bool'}} */
+  (int)rf1(); /* expected-warning {{cast from function call of type 'float' to non-matching type 'int'}} */
+  (long)rf2(); /* expected-warning {{cast from function call of type 'double' to non-matching type 'long'}} */
+  (double)cf(); /* expected-warning {{cast from function call of type '_Complex double' to non-matching type 'double'}} */
+  (int)ef(); /* expected-warning {{cast from function call of type 'enum e' to non-matching type 'int'}} */
+  (int)bf(); /* expected-warning {{cast from function call of type '_Bool' to non-matching type 'int'}} */
+  (__SIZE_TYPE__)pf1(); /* expected-warning {{cast from function call of type 'char *' to non-matching type 'unsigned long'}} */
+  (__PTRDIFF_TYPE__)pf2(); /* expected-warning {{cast from function call of type 'int *' to non-matching type 'long'}} */
+}
+





More information about the cfe-commits mailing list