[clang] Fix CXXNewExpr end source location for 'new struct S' (#92266)

Currently, `new struct S` fails to set any valid end source location
because the token corresponding to `S` is consumed in
`ParseClassSpecifier` and is not accessible in the
`ParseDeclarationSpecifiers` that normally sets the end source location.

Fixes #35300
This commit is contained in:
Arseniy Zaostrovnykh 2024-05-16 12:14:53 +02:00 committed by GitHub
parent 83974a4b92
commit ba2e4fe4e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 5 deletions

View File

@ -606,11 +606,8 @@ void invoke_template() {
template_fun(foo);
}
void no_fix_for_invalid_new_loc() {
// FIXME: Although the code is valid, the end location of `new struct Base` is
// invalid. Correct it once https://bugs.llvm.org/show_bug.cgi?id=35952 is
// fixed.
void fix_for_c_style_struct() {
auto T = std::unique_ptr<Base>(new struct Base);
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use std::make_unique instead
// CHECK-FIXES: auto T = std::unique_ptr<Base>(new struct Base);
// CHECK-FIXES: auto T = std::make_unique<Base>();
}

View File

@ -1883,6 +1883,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
if (Tok.is(tok::identifier)) {
Name = Tok.getIdentifierInfo();
NameLoc = ConsumeToken();
DS.SetRangeEnd(NameLoc);
if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
// The name was supposed to refer to a template, but didn't.

View File

@ -583,3 +583,10 @@ void NonADLCall3() {
f(x);
}
} // namespace test_adl_call_three
namespace GH35300 {
struct Sock {};
void leakNewFn() { new struct Sock; }
// CHECK: CXXNewExpr {{.*}} <col:20, col:31> 'struct Sock *'
}