Skip to content

Commit b77e5f9

Browse files
committed
[clang][ExprConst] Allow comparisons with string literals
1 parent b294951 commit b77e5f9

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ static bool IsLiteralLValue(const LValue &Value) {
21422142
if (Value.getLValueCallIndex())
21432143
return false;
21442144
const Expr *E = Value.Base.dyn_cast<const Expr*>();
2145-
return E && !isa<MaterializeTemporaryExpr>(E);
2145+
return E && !isa<MaterializeTemporaryExpr, StringLiteral>(E);
21462146
}
21472147

21482148
static bool IsWeakLValue(const LValue &Value) {

clang/test/AST/ByteCode/cxx20.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,16 @@ constexpr auto p2 = "test2";
108108
constexpr bool b1 = foo(p1) == foo(p1);
109109
static_assert(b1);
110110

111-
constexpr bool b2 = foo(p1) == foo(p2); // ref-error {{must be initialized by a constant expression}} \
112-
// ref-note {{comparison of addresses of literals}} \
113-
// ref-note {{declared here}}
114-
static_assert(!b2); // ref-error {{not an integral constant expression}} \
115-
// ref-note {{not a constant expression}}
111+
constexpr bool b2 = foo(p1) == foo(p2);
112+
static_assert(!b2);
116113

117114
constexpr auto name1() { return "name1"; }
118115
constexpr auto name2() { return "name2"; }
119116

120117
constexpr auto b3 = name1() == name1();
121118
static_assert(b3);
122-
constexpr auto b4 = name1() == name2(); // ref-error {{must be initialized by a constant expression}} \
123-
// ref-note {{has unspecified value}} \
124-
// ref-note {{declared here}}
125-
static_assert(!b4); // ref-error {{not an integral constant expression}} \
126-
// ref-note {{not a constant expression}}
119+
constexpr auto b4 = name1() == name2();
120+
static_assert(!b4);
127121

128122
namespace UninitializedFields {
129123
class A {

clang/test/SemaCXX/constant-expression-cxx11.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,9 @@ struct Str {
358358

359359
extern char externalvar[];
360360
constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}}
361-
constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}}
362-
// expected-note@-1 {{comparison of addresses of literals has unspecified value}}
363-
// cxx20_23-warning@-2 {{comparison between two arrays is deprecated}}
361+
constexpr bool litaddress = "foo" == "foo"; // cxx20_23-warning {{comparison between two arrays is deprecated}}
364362
static_assert(0 != "foo", "");
363+
static_assert("foo" != "foo", "");
365364

366365
}
367366

0 commit comments

Comments
 (0)