Skip to content

Commit 6260d8f

Browse files
authored
[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
1 parent 20c8e3c commit 6260d8f

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,8 @@ Bug Fixes in This Version
800800
declaration statements. Clang now emits a warning for these patterns. (#GH141659)
801801
- Fixed false positives for redeclaration errors of using enum in
802802
nested scopes. (#GH147495)
803+
- Fixed a failed assertion with an operator call expression which comes from a
804+
macro expansion when performing analysis for nullability attributes. (#GH138371)
803805

804806
Bug Fixes to Compiler Builtins
805807
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14034,9 +14034,14 @@ TreeTransform<Derived>::TransformCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
1403414034
if (Object.isInvalid())
1403514035
return ExprError();
1403614036

14037-
// FIXME: Poor location information
14038-
SourceLocation FakeLParenLoc = SemaRef.getLocForEndOfToken(
14039-
static_cast<Expr *>(Object.get())->getEndLoc());
14037+
// FIXME: Poor location information. Also, if the location for the end of
14038+
// the token is within a macro expansion, getLocForEndOfToken() will return
14039+
// an invalid source location. If that happens and we have an otherwise
14040+
// valid end location, use the valid one instead of the invalid one.
14041+
SourceLocation EndLoc = static_cast<Expr *>(Object.get())->getEndLoc();
14042+
SourceLocation FakeLParenLoc = SemaRef.getLocForEndOfToken(EndLoc);
14043+
if (FakeLParenLoc.isInvalid() && EndLoc.isValid())
14044+
FakeLParenLoc = EndLoc;
1404014045

1404114046
// Transform the call arguments.
1404214047
SmallVector<Expr*, 8> Args;

clang/test/SemaTemplate/gh138371.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
// expected-no-diagnostics
3+
4+
// This would previously trigger a failed assertion when instantiating the
5+
// template which uses an overloaded call operator because the end location
6+
// for the expression came from a macro expansion.
7+
8+
#define ASSIGN_OR_RETURN(...) (__VA_ARGS__)
9+
10+
struct Loc {
11+
int operator()(const char* _Nonnull f = __builtin_FILE()) const;
12+
};
13+
14+
template <typename Ty>
15+
void f() {
16+
ASSIGN_OR_RETURN(Loc()());
17+
}
18+
19+
void test() {
20+
f<int>();
21+
}
22+

0 commit comments

Comments
 (0)