From: Alp Toker Date: Wed, 27 Nov 2013 03:18:17 +0000 (+0000) Subject: Implement -Wold-style-cast X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d07e35a5bc72fac07e015808c4dda20245a18ed;p=clang Implement -Wold-style-cast 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 --- diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index 38a939faae..6de1a48d9f 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -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">; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index b8d99ba52c..a314bf493a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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, DefaultIgnore; +def warn_old_style_cast : Warning< + "use of old-style cast">, InGroup, DefaultIgnore; // Separate between casts to void* and non-void* pointers. // Some APIs use (abuse) void* for something like a user context, diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 55f9d6f8a0..b2bff51cd9 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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 index 0000000000..d8198fcc56 --- /dev/null +++ b/test/SemaCXX/old-style-cast.cpp @@ -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(12); + (void)y; + typedef void VOID; + (VOID)y; +}