r336746 - PR38095: Allow constant-folding of loads through bitcasted pointers if
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 10 17:29:06 PDT 2018
Author: rsmith
Date: Tue Jul 10 17:29:05 2018
New Revision: 336746
URL: http://llvm.org/viewvc/llvm-project?rev=336746&view=rev
Log:
PR38095: Allow constant-folding of loads through bitcasted pointers if
the bitcast only changed cvr-qualifications within the pointer type.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/test/Sema/diagnose_if.c
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=336746&r1=336745&r2=336746&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Jul 10 17:29:05 2018
@@ -5787,8 +5787,8 @@ bool PointerExprEvaluator::VisitUnaryAdd
return evaluateLValue(E->getSubExpr(), Result);
}
-bool PointerExprEvaluator::VisitCastExpr(const CastExpr* E) {
- const Expr* SubExpr = E->getSubExpr();
+bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
+ const Expr *SubExpr = E->getSubExpr();
switch (E->getCastKind()) {
default:
@@ -5805,7 +5805,11 @@ bool PointerExprEvaluator::VisitCastExpr
// permitted in constant expressions in C++11. Bitcasts from cv void* are
// also static_casts, but we disallow them as a resolution to DR1312.
if (!E->getType()->isVoidPointerType()) {
- Result.Designator.setInvalid();
+ // If we changed anything other than cvr-qualifiers, we can't use this
+ // value for constant folding. FIXME: Qualification conversions should
+ // always be CK_NoOp, but we get this wrong in C.
+ if (!Info.Ctx.hasCvrSimilarType(E->getType(), E->getSubExpr()->getType()))
+ Result.Designator.setInvalid();
if (SubExpr->getType()->isVoidPointerType())
CCEDiag(E, diag::note_constexpr_invalid_cast)
<< 3 << SubExpr->getType();
Modified: cfe/trunk/test/Sema/diagnose_if.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/diagnose_if.c?rev=336746&r1=336745&r2=336746&view=diff
==============================================================================
--- cfe/trunk/test/Sema/diagnose_if.c (original)
+++ cfe/trunk/test/Sema/diagnose_if.c Tue Jul 10 17:29:05 2018
@@ -157,3 +157,13 @@ void runAlwaysWarnWithArg(int a) {
// Bug: we would complain about `a` being undeclared if this was spelled
// __diagnose_if__.
void underbarName(int a) __attribute__((__diagnose_if__(a, "", "warning")));
+
+// PR38095
+void constCharStar(const char *str) __attribute__((__diagnose_if__(!str[0], "empty string not allowed", "error"))); // expected-note {{from}}
+void charStar(char *str) __attribute__((__diagnose_if__(!str[0], "empty string not allowed", "error"))); // expected-note {{from}}
+void runConstCharStar() {
+ constCharStar("foo");
+ charStar("bar");
+ constCharStar(""); // expected-error {{empty string not allowed}}
+ charStar(""); // expected-error {{empty string not allowed}}
+}
More information about the cfe-commits
mailing list