54
54
#include "clang/Basic/DiagnosticSema.h"
55
55
#include "clang/Basic/TargetInfo.h"
56
56
#include "llvm/ADT/APFixedPoint.h"
57
+ #include "llvm/ADT/Sequence.h"
57
58
#include "llvm/ADT/SmallBitVector.h"
58
59
#include "llvm/ADT/StringExtras.h"
60
+ #include "llvm/Support/Casting.h"
59
61
#include "llvm/Support/Debug.h"
60
62
#include "llvm/Support/SaveAndRestore.h"
61
63
#include "llvm/Support/SipHash.h"
@@ -2061,15 +2063,21 @@ static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
2061
2063
return true;
2062
2064
}
2063
2065
2064
- /// Should this call expression be treated as a no-op ?
2065
- static bool IsNoOpCall (const CallExpr *E) {
2066
+ /// Should this call expression be treated as forming an opaque constant ?
2067
+ static bool IsOpaqueConstantCall (const CallExpr *E) {
2066
2068
unsigned Builtin = E->getBuiltinCallee();
2067
2069
return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
2068
2070
Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
2069
2071
Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
2070
2072
Builtin == Builtin::BI__builtin_function_start);
2071
2073
}
2072
2074
2075
+ static bool IsOpaqueConstantCall(const LValue &LVal) {
2076
+ const auto *BaseExpr =
2077
+ llvm::dyn_cast_if_present<CallExpr>(LVal.Base.dyn_cast<const Expr *>());
2078
+ return BaseExpr && IsOpaqueConstantCall(BaseExpr);
2079
+ }
2080
+
2073
2081
static bool IsGlobalLValue(APValue::LValueBase B) {
2074
2082
// C++11 [expr.const]p3 An address constant expression is a prvalue core
2075
2083
// constant expression of pointer type that evaluates to...
@@ -2115,7 +2123,7 @@ static bool IsGlobalLValue(APValue::LValueBase B) {
2115
2123
case Expr::ObjCBoxedExprClass:
2116
2124
return cast<ObjCBoxedExpr>(E)->isExpressibleAsConstantInitializer();
2117
2125
case Expr::CallExprClass:
2118
- return IsNoOpCall (cast<CallExpr>(E));
2126
+ return IsOpaqueConstantCall (cast<CallExpr>(E));
2119
2127
// For GCC compatibility, &&label has static storage duration.
2120
2128
case Expr::AddrLabelExprClass:
2121
2129
return true;
@@ -2142,11 +2150,91 @@ static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2142
2150
return LVal.Base.dyn_cast<const ValueDecl*>();
2143
2151
}
2144
2152
2145
- static bool IsLiteralLValue(const LValue &Value) {
2146
- if (Value.getLValueCallIndex())
2153
+ // Information about an LValueBase that is some kind of string.
2154
+ struct LValueBaseString {
2155
+ std::string ObjCEncodeStorage;
2156
+ StringRef Bytes;
2157
+ int CharWidth;
2158
+ };
2159
+
2160
+ // Gets the lvalue base of LVal as a string.
2161
+ static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2162
+ LValueBaseString &AsString) {
2163
+ const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2164
+ if (!BaseExpr)
2165
+ return false;
2166
+
2167
+ // For ObjCEncodeExpr, we need to compute and store the string.
2168
+ if (const auto *EE = dyn_cast<ObjCEncodeExpr>(BaseExpr)) {
2169
+ Info.Ctx.getObjCEncodingForType(EE->getEncodedType(),
2170
+ AsString.ObjCEncodeStorage);
2171
+ AsString.Bytes = AsString.ObjCEncodeStorage;
2172
+ AsString.CharWidth = 1;
2173
+ return true;
2174
+ }
2175
+
2176
+ // Otherwise, we have a StringLiteral.
2177
+ const auto *Lit = dyn_cast<StringLiteral>(BaseExpr);
2178
+ if (const auto *PE = dyn_cast<PredefinedExpr>(BaseExpr))
2179
+ Lit = PE->getFunctionName();
2180
+
2181
+ if (!Lit)
2147
2182
return false;
2148
- const Expr *E = Value.Base.dyn_cast<const Expr*>();
2149
- return E && !isa<MaterializeTemporaryExpr>(E);
2183
+
2184
+ AsString.Bytes = Lit->getBytes();
2185
+ AsString.CharWidth = Lit->getCharByteWidth();
2186
+ return true;
2187
+ }
2188
+
2189
+ // Determine whether two string literals potentially overlap. This will be the
2190
+ // case if they agree on the values of all the bytes on the overlapping region
2191
+ // between them.
2192
+ //
2193
+ // The overlapping region is the portion of the two string literals that must
2194
+ // overlap in memory if the pointers actually point to the same address at
2195
+ // runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2196
+ // the overlapping region is "cdef\0", which in this case does agree, so the
2197
+ // strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2198
+ // "bazbar" + 3, the overlapping region contains all of both strings, so they
2199
+ // are not potentially overlapping, even though they agree from the given
2200
+ // addresses onwards.
2201
+ //
2202
+ // See open core issue CWG2765 which is discussing the desired rule here.
2203
+ static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2204
+ const LValue &LHS,
2205
+ const LValue &RHS) {
2206
+ LValueBaseString LHSString, RHSString;
2207
+ if (!GetLValueBaseAsString(Info, LHS, LHSString) ||
2208
+ !GetLValueBaseAsString(Info, RHS, RHSString))
2209
+ return false;
2210
+
2211
+ // This is the byte offset to the location of the first character of LHS
2212
+ // within RHS. We don't need to look at the characters of one string that
2213
+ // would appear before the start of the other string if they were merged.
2214
+ CharUnits Offset = RHS.Offset - LHS.Offset;
2215
+ if (Offset.isNegative())
2216
+ LHSString.Bytes = LHSString.Bytes.drop_front(-Offset.getQuantity());
2217
+ else
2218
+ RHSString.Bytes = RHSString.Bytes.drop_front(Offset.getQuantity());
2219
+
2220
+ bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2221
+ StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2222
+ StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2223
+ int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2224
+
2225
+ // The null terminator isn't included in the string data, so check for it
2226
+ // manually. If the longer string doesn't have a null terminator where the
2227
+ // shorter string ends, they aren't potentially overlapping.
2228
+ for (int NullByte : llvm::seq(ShorterCharWidth)) {
2229
+ if (Shorter.size() + NullByte >= Longer.size())
2230
+ break;
2231
+ if (Longer[Shorter.size() + NullByte])
2232
+ return false;
2233
+ }
2234
+
2235
+ // Otherwise, they're potentially overlapping if and only if the overlapping
2236
+ // region is the same.
2237
+ return Shorter == Longer.take_front(Shorter.size());
2150
2238
}
2151
2239
2152
2240
static bool IsWeakLValue(const LValue &Value) {
@@ -8573,7 +8661,10 @@ class LValueExprEvaluator
8573
8661
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
8574
8662
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
8575
8663
bool VisitMemberExpr(const MemberExpr *E);
8576
- bool VisitStringLiteral(const StringLiteral *E) { return Success(E); }
8664
+ bool VisitStringLiteral(const StringLiteral *E) {
8665
+ return Success(APValue::LValueBase(
8666
+ E, 0, Info.getASTContext().getNextStringLiteralVersion()));
8667
+ }
8577
8668
bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); }
8578
8669
bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
8579
8670
bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
@@ -9639,7 +9730,7 @@ static bool isOneByteCharacterType(QualType T) {
9639
9730
9640
9731
bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
9641
9732
unsigned BuiltinOp) {
9642
- if (IsNoOpCall (E))
9733
+ if (IsOpaqueConstantCall (E))
9643
9734
return Success(E);
9644
9735
9645
9736
switch (BuiltinOp) {
@@ -13889,13 +13980,22 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
13889
13980
(!RHSValue.Base && !RHSValue.Offset.isZero()))
13890
13981
return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
13891
13982
!RHSValue.Base);
13892
- // It's implementation-defined whether distinct literals will have
13893
- // distinct addresses. In clang, the result of such a comparison is
13894
- // unspecified, so it is not a constant expression. However, we do know
13895
- // that the address of a literal will be non-null.
13896
- if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) &&
13897
- LHSValue.Base && RHSValue.Base)
13983
+ // C++2c [intro.object]/10:
13984
+ // Two objects [...] may have the same address if [...] they are both
13985
+ // potentially non-unique objects.
13986
+ // C++2c [intro.object]/9:
13987
+ // An object is potentially non-unique if it is a string literal object,
13988
+ // the backing array of an initializer list, or a subobject thereof.
13989
+ //
13990
+ // This makes the comparison result unspecified, so it's not a constant
13991
+ // expression.
13992
+ //
13993
+ // TODO: Do we need to handle the initializer list case here?
13994
+ if (ArePotentiallyOverlappingStringLiterals(Info, LHSValue, RHSValue))
13898
13995
return DiagComparison(diag::note_constexpr_literal_comparison);
13996
+ if (IsOpaqueConstantCall(LHSValue) || IsOpaqueConstantCall(RHSValue))
13997
+ return DiagComparison(diag::note_constexpr_opaque_call_comparison,
13998
+ !IsOpaqueConstantCall(LHSValue));
13899
13999
// We can't tell whether weak symbols will end up pointing to the same
13900
14000
// object.
13901
14001
if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue))
0 commit comments