[clang] 7358973 - [CLANG-CL] Remove the 'static' specifier for _FUNCTION_ in MSVC mode. (#128184)

via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 6 04:51:48 PST 2025


Author: Zahira Ammarguellat
Date: 2025-03-06T07:51:45-05:00
New Revision: 7358973df1b53785cace2e3431d141078dc53e7c

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

LOG: [CLANG-CL] Remove the 'static' specifier for _FUNCTION_ in MSVC mode. (#128184)

The macro `FUNCTION` is returning the `static` specifier for static
templated functions. It's not the case for MSVC.
See https://godbolt.org/z/KnhWhqs47
Clang-cl is returning:
`__FUNCTION__ static inner::C<class A>::f`
`__FUNCTION__ static inner::C<class A>::f`
for the reproducer.

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/AST/Expr.cpp
    clang/test/SemaCXX/source_location.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 629149bf459b1..2576801aff708 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -251,6 +251,8 @@ Bug Fixes in This Version
 - Non-local variable and non-variable declarations in the first clause of a ``for`` loop in C are no longer incorrectly
   considered an error in C23 mode and are allowed as an extension in earlier language modes.
 
+- Remove the ``static`` specifier for the value of ``_FUNCTION_`` for static functions, in MSVC compatibility mode.
+
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

diff  --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index b747aa8df807d..ccfec7fda0cbc 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -747,7 +747,7 @@ std::string PredefinedExpr::ComputeName(PredefinedIdentKind IK,
     if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
       if (MD->isVirtual() && IK != PredefinedIdentKind::PrettyFunctionNoVirtual)
         Out << "virtual ";
-      if (MD->isStatic())
+      if (MD->isStatic() && !ForceElaboratedPrinting)
         Out << "static ";
     }
 

diff  --git a/clang/test/SemaCXX/source_location.cpp b/clang/test/SemaCXX/source_location.cpp
index 069a9004927a9..c8b65d6eab50d 100644
--- a/clang/test/SemaCXX/source_location.cpp
+++ b/clang/test/SemaCXX/source_location.cpp
@@ -524,6 +524,57 @@ TestClass<test_func::C> t2;
 TestStruct<test_func::S> t3;
 TestEnum<test_func::E> t4;
 
+class A { int b;};
+namespace inner {
+  template <class Ty>
+  class C {
+  public:
+    template <class T>
+    static void f(int i) {
+      (void)i;
+#ifdef MS
+     static_assert(is_equal(__FUNCTION__, "test_func::inner::C<class test_func::A>::f"));
+#else
+     static_assert(is_equal(__FUNCTION__, "f"));
+#endif
+    }
+    template <class T>
+    static constexpr void cf(int i) {
+      (void)i;
+#ifdef MS
+     static_assert(is_equal(__FUNCTION__, "test_func::inner::C<class test_func::A>::cf"));
+#else
+     static_assert(is_equal(__FUNCTION__, "cf"));
+#endif
+    }
+    template <class T>
+    static void df(double f) {
+      (void)f;
+#ifdef MS
+      static_assert(is_equal(__FUNCTION__, "test_func::inner::C<class test_func::A>::df"));
+#else
+      static_assert(is_equal(__FUNCTION__, "df"));
+#endif
+    }
+    template <class T>
+    static constexpr void cdf(double f) {
+      (void)f;
+#ifdef MS
+      static_assert(is_equal(__FUNCTION__, "test_func::inner::C<class test_func::A>::cdf"));
+#else
+      static_assert(is_equal(__FUNCTION__, "cdf"));
+#endif
+    }
+  };
+}
+
+  void foo() {
+  test_func::inner::C<test_func::A>::f<char>(1);
+  test_func::inner::C<test_func::A>::cf<char>(1);
+  test_func::inner::C<test_func::A>::df<void>(1.0);
+  test_func::inner::C<test_func::A>::cdf<void>(1.0);
+}
+
 } // namespace test_func
 
 


        


More information about the cfe-commits mailing list