Skip to content

Don't apply lvalue-to-rvalue conversion in DefaultLValueConversion to the expression that is passed to it if it has a function type or array type #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10412,9 +10412,8 @@ class Sema final {
bool Diagnose = true);

// DefaultLvalueConversion - performs lvalue-to-rvalue conversion on
// the operand. This is DefaultFunctionArrayLvalueConversion,
// except that it assumes the operand isn't of function or array
// type.
// the operand. This function is a no-op if the operand has a function type
// or an array type.
ExprResult DefaultLvalueConversion(Expr *E);

// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ ExprResult Sema::DefaultLvalueConversion(Expr *E) {
QualType T = E->getType();
assert(!T.isNull() && "r-value conversion on typeless expression?");

// lvalue-to-rvalue conversion cannot be applied to function or array types.
if (T->isFunctionType() || T->isArrayType())
return E;

// We don't want to throw lvalue-to-rvalue casts on top of
// expressions of certain types in C++.
if (getLangOpts().CPlusPlus &&
Expand Down
5 changes: 4 additions & 1 deletion clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,10 @@ void Sema::DiagnoseUnusedExprResult(const Stmt *S) {
}
}

if (E->isGLValue() && E->getType().isVolatileQualified()) {
// Tell the user to assign it into a variable to force a volatile load if this
// isn't an array.
if (E->isGLValue() && E->getType().isVolatileQualified() &&
!E->getType()->isArrayType()) {
Diag(Loc, diag::warn_unused_volatile) << R1 << R2;
return;
}
Expand Down
9 changes: 9 additions & 0 deletions clang/test/CXX/expr/p10-0x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ void f2(volatile int *x) {
refcall();
1 ? refcall() : *x;
}

// CHECK: define void @_Z2f3v()
// CHECK-NOT: load
// CHECK-NOT: memcpy

void f3(void) {
volatile char a[10];
a;
}
11 changes: 10 additions & 1 deletion clang/test/SemaCXX/warn-unused-value-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,13 @@ void j() {
(void)noexcept(s.g() = 5); // Ok
}

}
}

namespace volatile_array {
void test() {
char a[10];
volatile char b[10];
a; // expected-warning-re {{expression result unused{{$}}}}
b; // expected-warning-re {{expression result unused{{$}}}}
}
}