[clang] [C++] Fix a failed assertion with nullability checking (PR #148881)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 15 09:14:08 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Aaron Ballman (AaronBallman)
<details>
<summary>Changes</summary>
This fixes a failed assertion with an operator call expression which comes from a macro expansion when performing analysis for nullability attributes.
Fixes #<!-- -->138371
---
Full diff: https://github.com/llvm/llvm-project/pull/148881.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+2)
- (modified) clang/lib/Sema/TreeTransform.h (+8-3)
- (added) clang/test/SemaTemplate/gh138371.cpp (+22)
``````````diff
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>();
+}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/148881
More information about the cfe-commits
mailing list