Skip to content

Commit ba2e4fe

Browse files
authored
[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
1 parent 83974a4 commit ba2e4fe

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

clang-tools-extra/test/clang-tidy/checkers/modernize/make-unique.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,8 @@ void invoke_template() {
606606
template_fun(foo);
607607
}
608608

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

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
18831883
if (Tok.is(tok::identifier)) {
18841884
Name = Tok.getIdentifierInfo();
18851885
NameLoc = ConsumeToken();
1886+
DS.SetRangeEnd(NameLoc);
18861887

18871888
if (Tok.is(tok::less) && getLangOpts().CPlusPlus) {
18881889
// The name was supposed to refer to a template, but didn't.

clang/test/AST/ast-dump-expr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,10 @@ void NonADLCall3() {
583583
f(x);
584584
}
585585
} // namespace test_adl_call_three
586+
587+
namespace GH35300 {
588+
struct Sock {};
589+
void leakNewFn() { new struct Sock; }
590+
// CHECK: CXXNewExpr {{.*}} <col:20, col:31> 'struct Sock *'
591+
}
592+

0 commit comments

Comments
 (0)