[clang] d8a5c79 - [clang][Sema] Correct end for the `CastOperation.OpRange` (#69480)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 07:44:21 PDT 2023


Author: Botond István Horváth
Date: 2023-10-24T16:44:12+02:00
New Revision: d8a5c79e8e006cf878e05aa316367c9684193117

URL: https://github.com/llvm/llvm-project/commit/d8a5c79e8e006cf878e05aa316367c9684193117
DIFF: https://github.com/llvm/llvm-project/commit/d8a5c79e8e006cf878e05aa316367c9684193117.diff

LOG: [clang][Sema] Correct end for the `CastOperation.OpRange` (#69480)

Set the correct end for the CastOperation.OpRange in CXXFunctionalCastExpr.
Now it is the closing bracket's location instead of the parameter's location.

This can lead to better highlight in the diagnostics.
Similar to https://github.com/llvm/llvm-project/pull/66853

Before:

warning: cast from 'long (*)(const int &)' to 'decltype(fun_ptr)' (aka 'long (*)(int &)') converts to incompatible function type [-Wcast-function-type-strict]
   24 | return decltype(fun_ptr)( f_ptr /*comment*/);
      |        ^~~~~~~~~~~~~~~~~~~~~~~~

After:

warning: cast from 'long (*)(const int &)' to 'decltype(fun_ptr)' (aka 'long (*)(int &)') converts to incompatible function type [-Wcast-function-type-strict]
   24 | return decltype(fun_ptr)( f_ptr /*comment*/);
      |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reviewed By: AaronBallman, tbaederr

GitHub PR: https://github.com/llvm/llvm-project/pull/69480

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaCast.cpp
    clang/test/Misc/misc-source-ranges.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 43fb725c78ea441..06e083391da41d6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -379,6 +379,25 @@ Improvements to Clang's diagnostics
   can sometimes lead to worse ordering.
 
 
+- When describing a warning/error in a function-style type conversion Clang underlines only until
+  the end of the expression we convert from. Now Clang underlines until the closing parenthesis.
+
+  Before:
+
+  .. code-block:: text
+
+    warning: cast from 'long (*)(const int &)' to 'decltype(fun_ptr)' (aka 'long (*)(int &)') converts to incompatible function type [-Wcast-function-type-strict]
+    24 | return decltype(fun_ptr)( f_ptr /*comment*/);
+       |        ^~~~~~~~~~~~~~~~~~~~~~~~
+
+  After:
+
+  .. code-block:: text
+
+    warning: cast from 'long (*)(const int &)' to 'decltype(fun_ptr)' (aka 'long (*)(int &)') converts to incompatible function type [-Wcast-function-type-strict]
+    24 | return decltype(fun_ptr)( f_ptr /*comment*/);
+       |        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
 Bug Fixes in This Version
 -------------------------
 - Fixed an issue where a class template specialization whose declaration is

diff  --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index 98b5879456e2175..87e6d1a2198fcea 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -3362,7 +3362,7 @@ ExprResult Sema::BuildCXXFunctionalCastExpr(TypeSourceInfo *CastTypeInfo,
   assert(LPLoc.isValid() && "List-initialization shouldn't get here.");
   CastOperation Op(*this, Type, CastExpr);
   Op.DestRange = CastTypeInfo->getTypeLoc().getSourceRange();
-  Op.OpRange = SourceRange(Op.DestRange.getBegin(), CastExpr->getEndLoc());
+  Op.OpRange = SourceRange(Op.DestRange.getBegin(), RPLoc);
 
   Op.CheckCXXCStyleCast(/*FunctionalCast=*/true, /*ListInit=*/false);
   if (Op.SrcExpr.isInvalid())

diff  --git a/clang/test/Misc/misc-source-ranges.cpp b/clang/test/Misc/misc-source-ranges.cpp
index 7a9d9d057dac407..1835952fd755902 100644
--- a/clang/test/Misc/misc-source-ranges.cpp
+++ b/clang/test/Misc/misc-source-ranges.cpp
@@ -1,7 +1,13 @@
-// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info %s 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-print-source-range-info -Wcast-function-type-strict %s 2>&1 | FileCheck %s
 
 struct S {
   char a : 12 - 12;
 };
 // CHECK: misc-source-ranges.cpp:[[@LINE-2]]:8:{[[@LINE-2]]:12-[[@LINE-2]]:19}
 
+using fun = long(*)(int &);
+fun foo(){
+  long (*f_ptr)(const int &);
+  return fun(f_ptr);
+}
+// CHECK: misc-source-ranges.cpp:[[@LINE-2]]:10:{[[@LINE-2]]:10-[[@LINE-2]]:20}


        


More information about the cfe-commits mailing list