[clang] 6260d8f - [C++] Fix a failed assertion with nullability checking (#148881)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 15 10:06:48 PDT 2025


Author: Aaron Ballman
Date: 2025-07-15T13:06:45-04:00
New Revision: 6260d8ff8277d35ff783ba7c6febb1489eb94b24

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

LOG: [C++] Fix a failed assertion with nullability checking (#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

Added: 
    clang/test/SemaTemplate/gh138371.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/TreeTransform.h

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2add72a1654b1..487bc6bc10f1f 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>();
+}
+


        


More information about the cfe-commits mailing list