From: Jordan Rose Date: Thu, 28 Mar 2013 19:09:40 +0000 (+0000) Subject: Provide a fixit to static_cast for reinterpret_casts within a class hierarchy. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5fd1fac0104e22533bc0284fdd0f42f843c975f5;p=clang Provide a fixit to static_cast for reinterpret_casts within a class hierarchy. The suggestion was already in the text of the note; this just adds the actual fixit and the appropriate test cases. Patch by Alexander Zinenko! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@178274 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaCast.cpp b/lib/Sema/SemaCast.cpp index 0e19adfd28..e2a4084766 100644 --- a/lib/Sema/SemaCast.cpp +++ b/lib/Sema/SemaCast.cpp @@ -766,12 +766,13 @@ static void DiagnoseReinterpretUpDownCast(Sema &Self, const Expr *SrcExpr, QualType DerivedType = ReinterpretKind == ReinterpretUpcast? SrcType : DestType; - Self.Diag(OpRange.getBegin(), diag::warn_reinterpret_different_from_static) - << DerivedType << BaseType << !VirtualBase << ReinterpretKind; - Self.Diag(OpRange.getBegin(), diag::note_reinterpret_updowncast_use_static) - << ReinterpretKind; - - // TODO: emit fixits. This requires passing operator SourceRange from Parser. + SourceLocation BeginLoc = OpRange.getBegin(); + Self.Diag(BeginLoc, diag::warn_reinterpret_different_from_static) + << DerivedType << BaseType << !VirtualBase << ReinterpretKind + << OpRange; + Self.Diag(BeginLoc, diag::note_reinterpret_updowncast_use_static) + << ReinterpretKind + << FixItHint::CreateReplacement(BeginLoc, "static_cast"); } /// CheckReinterpretCast - Check that a reinterpret_cast\(SrcExpr) is diff --git a/test/SemaCXX/warn-reinterpret-base-class.cpp b/test/SemaCXX/warn-reinterpret-base-class.cpp index a7deafeadf..67902f7a90 100644 --- a/test/SemaCXX/warn-reinterpret-base-class.cpp +++ b/test/SemaCXX/warn-reinterpret-base-class.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s +// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fdiagnostics-parseable-fixits -Wreinterpret-base-class -Wno-unused-volatile-lvalue %s 2>&1 | FileCheck %s // PR 13824 class A { @@ -117,12 +118,17 @@ void reinterpret_pointer_downcast(A *a, const A *ca) { // expected-warning@+2 {{'reinterpret_cast' to class 'DVA *' from its virtual base 'A *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)*reinterpret_cast(a); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' to class 'DDVA *' from its virtual base 'A *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)*reinterpret_cast(a); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' to class 'DMA *' from its virtual base 'A *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)*reinterpret_cast(a); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" } void reinterpret_reference_downcast(A a, A &ra, const A &cra) { @@ -145,12 +151,17 @@ void reinterpret_reference_downcast(A a, A &ra, const A &cra) { // expected-warning@+2 {{'reinterpret_cast' to class 'DVA &' from its virtual base 'A' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(a); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' to class 'DDVA &' from its virtual base 'A' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(a); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' to class 'DMA &' from its virtual base 'A' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(a); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" } void reinterpret_pointer_upcast(DA *da, const DA *cda, DDA *dda, DAo *dao, @@ -170,18 +181,27 @@ void reinterpret_pointer_upcast(DA *da, const DA *cda, DDA *dda, DAo *dao, // expected-warning@+2 {{'reinterpret_cast' from class 'DVA *' to its virtual base 'A *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)*reinterpret_cast(dva); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA *' to its virtual base 'A *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)*reinterpret_cast(ddva); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA *' to its virtual base 'DA *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)*reinterpret_cast(ddva); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DMA *' to its virtual base 'A *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)*reinterpret_cast(dma); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DMA *' to its virtual base 'DA *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)*reinterpret_cast(dma); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:26}:"static_cast" } void reinterpret_reference_upcast(DA &da, const DA &cda, DDA &dda, DAo &dao, @@ -201,18 +221,27 @@ void reinterpret_reference_upcast(DA &da, const DA &cda, DDA &dda, DAo &dao, // expected-warning@+2 {{'reinterpret_cast' from class 'DVA' to its virtual base 'A &' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(dva); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA' to its virtual base 'A &' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(ddva); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DDVA' to its virtual base 'DA &' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(ddva); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DMA' to its virtual base 'A &' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(dma); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'DMA' to its virtual base 'DA &' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(dma); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" } struct E { @@ -239,35 +268,45 @@ void different_subobject_downcast(E *e, F *f, A *a) { // expected-warning@+2 {{'reinterpret_cast' to class 'F *' from its base at non-zero offset 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(e); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' to class 'G *' from its base at non-zero offset 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(e); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + (void)reinterpret_cast(e); // expected-warning@+2 {{'reinterpret_cast' to class 'I *' from its virtual base 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(e); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + (void)reinterpret_cast(f); // expected-warning@+2 {{'reinterpret_cast' to class 'I *' from its virtual base 'F *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(f); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" (void)reinterpret_cast(a); // expected-warning@+2 {{'reinterpret_cast' to class 'L' (aka 'const F *volatile') from its base at non-zero offset 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while downcasting}} (void)reinterpret_cast(e); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" } void different_subobject_upcast(F *f, G *g, H *h, I *i) { // expected-warning@+2 {{'reinterpret_cast' from class 'F *' to its base at non-zero offset 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(f); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" (void)reinterpret_cast(g); // expected-warning@+2 {{'reinterpret_cast' from class 'G *' to its base at non-zero offset 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(g); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" (void)reinterpret_cast(h); (void)reinterpret_cast(h); @@ -275,7 +314,10 @@ void different_subobject_upcast(F *f, G *g, H *h, I *i) { // expected-warning@+2 {{'reinterpret_cast' from class 'I *' to its virtual base 'F *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(i); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" + // expected-warning@+2 {{'reinterpret_cast' from class 'I *' to its virtual base 'E *' behaves differently from 'static_cast'}} // expected-note@+1 {{use 'static_cast' to adjust the pointer correctly while upcasting}} (void)reinterpret_cast(i); + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:9-[[@LINE-1]]:25}:"static_cast" }