Skip to content

Commit

Permalink
[clang] Fix CXXNewExpr end source location for 'new struct S' (llvm#9…
Browse files Browse the repository at this point in the history
…2266)

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 llvm#35300
  • Loading branch information
necto committed May 16, 2024
1 parent 83974a4 commit ba2e4fe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
}
1 change: 1 addition & 0 deletions clang/lib/Parse/ParseDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions clang/test/AST/ast-dump-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 *'
}

0 comments on commit ba2e4fe

Please sign in to comment.