https://github.com/AaronBallman created 
https://github.com/llvm/llvm-project/pull/148881

This fixes a failed assertion with an operator call expression which comes from 
a macro expansion when performing analysis for nullability attributes.

Fixes #138371

>From f1afcc9aeefc352de338fa62eb0fbf1226e7c379 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aa...@aaronballman.com>
Date: Tue, 15 Jul 2025 12:10:32 -0400
Subject: [PATCH] [C++] Fix a failed assertion with nullability checking

This fixes a failed assertion with an operator call expression which
comes from a macro expansion when performing analysis for nullability
attributes.

Fixes #138371
---
 clang/docs/ReleaseNotes.rst          |  2 ++
 clang/lib/Sema/TreeTransform.h       | 11 ++++++++---
 clang/test/SemaTemplate/gh138371.cpp | 22 ++++++++++++++++++++++
 3 files changed, 32 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaTemplate/gh138371.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 40213cd103c43..26c0fa48193ce 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -800,6 +800,8 @@ Bug Fixes in This Version
   declaration statements. Clang now emits a warning for these patterns. 
(#GH141659)
 - Fixed false positives for redeclaration errors of using enum in
   nested scopes. (#GH147495)
+- Fixed a failed assertion with an operator call expression which comes from a
+  macro expansion when performing analysis for nullability attributes. 
(#GH138371)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 758012f894a41..3e38f8b183dfd 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -14034,9 +14034,14 @@ 
TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
     if (Object.isInvalid())
       return ExprError();
 
-    // FIXME: Poor location information
-    SourceLocation FakeLParenLoc = SemaRef.getLocForEndOfToken(
-        static_cast<Expr *>(Object.get())->getEndLoc());
+    // FIXME: Poor location information. Also, if the location for the end of
+    // the token is within a macro expansion, getLocForEndOfToken() will return
+    // an invalid source location. If that happens and we have an otherwise
+    // valid end location, use the valid one instead of the invalid one.
+    SourceLocation EndLoc = static_cast<Expr *>(Object.get())->getEndLoc();
+    SourceLocation FakeLParenLoc = SemaRef.getLocForEndOfToken(EndLoc);
+    if (FakeLParenLoc.isInvalid() && EndLoc.isValid())
+      FakeLParenLoc = EndLoc;
 
     // Transform the call arguments.
     SmallVector<Expr*, 8> Args;
diff --git a/clang/test/SemaTemplate/gh138371.cpp 
b/clang/test/SemaTemplate/gh138371.cpp
new file mode 100644
index 0000000000000..1b8fe09eb7e97
--- /dev/null
+++ b/clang/test/SemaTemplate/gh138371.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+// This would previously trigger a failed assertion when instantiating the
+// template which uses an overloaded call operator because the end location
+// for the expression came from a macro expansion.
+
+#define ASSIGN_OR_RETURN(...)  (__VA_ARGS__)
+
+struct Loc {
+  int operator()(const char* _Nonnull f = __builtin_FILE()) const;
+};
+
+template <typename Ty>
+void f() {
+  ASSIGN_OR_RETURN(Loc()());
+}
+
+void test() {
+  f<int>();
+}
+

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to