]> granicus.if.org Git - clang/commitdiff
Implement -Wold-style-cast
authorAlp Toker <alp@nuanti.com>
Wed, 27 Nov 2013 03:18:17 +0000 (03:18 +0000)
committerAlp Toker <alp@nuanti.com>
Wed, 27 Nov 2013 03:18:17 +0000 (03:18 +0000)
Based on a patch by Ondřej Hošek!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@195808 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
test/SemaCXX/old-style-cast.cpp [new file with mode: 0644]

index 38a939faae3ad46bd7d4a8d0475f72e020e88581..6de1a48d9f9dbd9f62e0acf8ee2b4ef8a214f5c6 100644 (file)
@@ -216,7 +216,7 @@ def NonPODVarargs : DiagGroup<"non-pod-varargs">;
 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">;
index b8d99ba52cf6bd7d8d8b026da7768720b34af457..a314bf493a884901bccead3a2906e5de0eb72514 100644 (file)
@@ -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,
index 55f9d6f8a08b8259cdf12ba0149b048f676761bc..b2bff51cd955af9a59234cde04abd0dec3f213d0 100644 (file)
@@ -5138,6 +5138,10 @@ Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc,
     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);
 }
 
diff --git a/test/SemaCXX/old-style-cast.cpp b/test/SemaCXX/old-style-cast.cpp
new file mode 100644 (file)
index 0000000..d8198fc
--- /dev/null
@@ -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;
+}