r195808 - Implement -Wold-style-cast

Alp Toker alp at nuanti.com
Tue Nov 26 19:18:17 PST 2013


Author: alp
Date: Tue Nov 26 21:18:17 2013
New Revision: 195808

URL: http://llvm.org/viewvc/llvm-project?rev=195808&view=rev
Log:
Implement -Wold-style-cast

Based on a patch by Ondřej Hošek!

Added:
    cfe/trunk/test/SemaCXX/old-style-cast.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=195808&r1=195807&r2=195808&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Nov 26 21:18:17 2013
@@ -216,7 +216,7 @@ def NonPODVarargs : DiagGroup<"non-pod-v
 def : DiagGroup<"nonportable-cfstrings">;
 def NonVirtualDtor : DiagGroup<"non-virtual-dtor">;
 def OveralignedType : DiagGroup<"over-aligned">;
-def : DiagGroup<"old-style-cast">;
+def OldStyleCast : DiagGroup<"old-style-cast">;
 def : DiagGroup<"old-style-definition">;
 def OutOfLineDeclaration : DiagGroup<"out-of-line-declaration">;
 def : DiagGroup<"overflow">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=195808&r1=195807&r2=195808&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Nov 26 21:18:17 2013
@@ -2299,6 +2299,8 @@ def note_function_to_bool_call : Note<
 def warn_cast_align : Warning<
   "cast from %0 to %1 increases required alignment from %2 to %3">,
   InGroup<CastAlign>, DefaultIgnore;
+def warn_old_style_cast : Warning<
+  "use of old-style cast">, InGroup<OldStyleCast>, DefaultIgnore;
 
 // Separate between casts to void* and non-void* pointers.
 // Some APIs use (abuse) void* for something like a user context,

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=195808&r1=195807&r2=195808&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Nov 26 21:18:17 2013
@@ -5138,6 +5138,10 @@ Sema::ActOnCastExpr(Scope *S, SourceLoca
     CastExpr = Result.take();
   }
 
+  if (getLangOpts().CPlusPlus && !castType->isVoidType())
+    Diag(CastExpr->getLocStart(), diag::warn_old_style_cast)
+        << SourceRange(LParenLoc, RParenLoc);
+
   return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr);
 }
 

Added: cfe/trunk/test/SemaCXX/old-style-cast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/old-style-cast.cpp?rev=195808&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/old-style-cast.cpp (added)
+++ cfe/trunk/test/SemaCXX/old-style-cast.cpp Tue Nov 26 21:18:17 2013
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wold-style-cast %s
+
+void test1() {
+  long x = (long)12; // expected-warning {{use of old-style cast}}
+  (long)x; // expected-warning {{use of old-style cast}} expected-warning {{expression result unused}}
+  (void**)x; // expected-warning {{use of old-style cast}} expected-warning {{expression result unused}}
+  long y = static_cast<long>(12);
+  (void)y;
+  typedef void VOID;
+  (VOID)y;
+}





More information about the cfe-commits mailing list