llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Wrong end location for `VarDecl` that is move constructed from a prvalue

Open tJener opened this issue 1 year ago • 3 comments

See https://godbolt.org/z/jPcdfeW6G.

// Minimal repro.
struct S {};
S MakeS();
void f() {
  S s(MakeS());
}

The source range for the VarDecl s ends on the first ), which is part of the call to MakeS().

This occurs starting in C++17. In C++14, there is a ExprWithCleanups, CXXConstructExpr, and MaterializeTemporaryExpr between the VarDecl and the CallExpr to MakeS(). This seems similar to #143711, except this time we lose a paren, instead of gaining one.

tJener avatar Nov 29 '25 08:11 tJener

@llvm/issue-subscribers-clang-frontend

Author: Eric Li (tJener)

See https://godbolt.org/z/jPcdfeW6G.
// Minimal repro.
struct S {};
S MakeS();
void f() {
  S s(MakeS());
}

The source range for the VarDecl s ends on the first ), which is part of the call to MakeS().

This occurs starting in C++17. In C++14, there is a ExprWithCleanups, CXXConstructExpr, and MaterializeTemporaryExpr between the VarDecl and the CallExpr to MakeS(). This seems similar to #143711, except this time we lose a paren, instead of gaining one.

llvmbot avatar Nov 29 '25 08:11 llvmbot

@llvm/issue-subscribers-bug

Author: Eric Li (tJener)

See https://godbolt.org/z/jPcdfeW6G.
// Minimal repro.
struct S {};
S MakeS();
void f() {
  S s(MakeS());
}

The source range for the VarDecl s ends on the first ), which is part of the call to MakeS().

This occurs starting in C++17. In C++14, there is a ExprWithCleanups, CXXConstructExpr, and MaterializeTemporaryExpr between the VarDecl and the CallExpr to MakeS(). This seems similar to #143711, except this time we lose a paren, instead of gaining one.

llvmbot avatar Nov 29 '25 08:11 llvmbot

Looking at the AST, I don't think we have anywhere to actually store the location of the closing parenthesis. VarDecl itself doesn't have an end location: it just uses the initializer's end location. But the initializer doesn't include the parentheses.

Probably if there isn't a constructor call, we should construct a ParenExpr to represent the parentheses.

Related testcase:

void f() {
    int i(0         );
}

efriedma-quic avatar Dec 04 '25 23:12 efriedma-quic