From 4d260bf0c7d3875e8588ee22731f8e354ac3b450 Mon Sep 17 00:00:00 2001 From: Clement Courbet Date: Thu, 22 Dec 2016 14:12:31 +0000 Subject: [PATCH] [clang-tidy] cppcoreguidelines-slicing: display discarded state size in bytes https://reviews.llvm.org/D27212 llvm-svn: 290340 --- .../clang-tidy/cppcoreguidelines/SlicingCheck.cpp | 5 +++-- .../test/clang-tidy/cppcoreguidelines-slicing.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/SlicingCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/SlicingCheck.cpp index 2a6cf098e697..53b2f728fe4a 100644 --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/SlicingCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/SlicingCheck.cpp @@ -122,10 +122,11 @@ void SlicingCheck::check(const MatchFinder::MatchResult &Result) { BaseDecl->getASTContext().getASTRecordLayout(BaseDecl); const auto &DerivedLayout = DerivedDecl->getASTContext().getASTRecordLayout(DerivedDecl); - const auto StateSize = DerivedLayout.getDataSize() - BaseLayout.getDataSize(); + const CharUnits StateSize = + DerivedLayout.getDataSize() - BaseLayout.getDataSize(); if (StateSize.isPositive()) { diag(Call->getExprLoc(), "slicing object from type %0 to %1 discards " - "%2*sizeof(char) bytes of state") + "%2 bytes of state") << DerivedDecl << BaseDecl << static_cast(StateSize.getQuantity()); } } diff --git a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-slicing.cpp b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-slicing.cpp index ca2291c85c78..6856f52f7061 100644 --- a/clang-tools-extra/test/clang-tidy/cppcoreguidelines-slicing.cpp +++ b/clang-tools-extra/test/clang-tidy/cppcoreguidelines-slicing.cpp @@ -31,18 +31,18 @@ DerivedWithMemberVariables ReturnsDerived(); void positivesWithMemberVariables() { DerivedWithMemberVariables b; Base a{b}; - // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}}*sizeof(char) bytes of state [cppcoreguidelines-slicing] + // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state [cppcoreguidelines-slicing] a = b; - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}}*sizeof(char) bytes of state + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state TakesBaseByValue(b); - // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}}*sizeof(char) bytes of state + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state TwiceDerivedWithNoMemberVariables c; a = c; - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'TwiceDerivedWithNoMemberVariables' to 'Base' discards {{[0-9]*}}*sizeof(char) bytes of state + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'TwiceDerivedWithNoMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state a = ReturnsDerived(); - // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}}*sizeof(char) bytes of state + // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: slicing object from type 'DerivedWithMemberVariables' to 'Base' discards {{[0-9]*}} bytes of state } void positivesWithOverride() {