Skip to content

Commit c66d25d

Browse files
authored
[APINotes] Do not crash for C++ operators
This fixes a crash during `CXXMethod->getName()` in `Sema::ProcessAPINotes`: we were trying to get the name of a C++ method as a string, which fails with an assertion if the name is not a simple identifier.
1 parent 2f3ae2f commit c66d25d

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,11 +1044,16 @@ void Sema::ProcessAPINotes(Decl *D) {
10441044

10451045
if (auto TagContext = dyn_cast<TagDecl>(D->getDeclContext())) {
10461046
if (auto CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
1047-
for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1048-
if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1049-
auto Info =
1050-
Reader->lookupCXXMethod(Context->id, CXXMethod->getName());
1051-
ProcessVersionedAPINotes(*this, CXXMethod, Info);
1047+
if (!isa<CXXConstructorDecl>(CXXMethod) &&
1048+
!isa<CXXDestructorDecl>(CXXMethod) &&
1049+
!isa<CXXConversionDecl>(CXXMethod) &&
1050+
!CXXMethod->isOverloadedOperator()) {
1051+
for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
1052+
if (auto Context = UnwindTagContext(TagContext, APINotes)) {
1053+
auto Info =
1054+
Reader->lookupCXXMethod(Context->id, CXXMethod->getName());
1055+
ProcessVersionedAPINotes(*this, CXXMethod, Info);
1056+
}
10521057
}
10531058
}
10541059
}

clang/test/APINotes/Inputs/Headers/Methods.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@ struct IntWrapper {
22
int value;
33

44
IntWrapper getIncremented() const { return {value + 1}; }
5+
6+
IntWrapper operator+(const IntWrapper& RHS) const { return {value + RHS.value}; }
57
};
68

79
struct Outer {
810
struct Inner {
911
int value;
1012

1113
Inner getDecremented() const { return {value - 1}; }
14+
15+
bool operator==(const Inner& RHS) const {
16+
return value == RHS.value;
17+
}
1218
};
1319
};

0 commit comments

Comments
 (0)