[clang] [Clang][WIP] Constant Expressions inside of gcc'asm strings (PR #131003)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 13 10:08:40 PDT 2025
================
@@ -17261,33 +17261,50 @@ void Sema::DiagnoseStaticAssertDetails(const Expr *E) {
}
}
-bool Sema::EvaluateStaticAssertMessageAsString(Expr *Message,
- std::string &Result,
- ASTContext &Ctx,
- bool ErrorOnInvalidMessage) {
+template <typename ResultType>
+static bool EvaluateAsStringImpl(Sema &SemaRef, Expr *Message,
+ ResultType &Result, ASTContext &Ctx,
+ Sema::StringEvaluationContext EvalContext,
+ bool ErrorOnInvalidMessage) {
+
assert(Message);
assert(!Message->isTypeDependent() && !Message->isValueDependent() &&
"can't evaluate a dependant static assert message");
if (const auto *SL = dyn_cast<StringLiteral>(Message)) {
assert(SL->isUnevaluated() && "expected an unevaluated string");
- Result.assign(SL->getString().begin(), SL->getString().end());
+ if constexpr (std::is_same_v<APValue, ResultType>) {
+ Result =
+ APValue(APValue::UninitArray{}, SL->getLength(), SL->getLength());
+ const ConstantArrayType *CAT =
+ SemaRef.getASTContext().getAsConstantArrayType(SL->getType());
+ assert(CAT && "string literal isn't an array");
+ QualType CharType = CAT->getElementType();
+ llvm::APSInt Value(SemaRef.getASTContext().getTypeSize(CharType),
+ CharType->isUnsignedIntegerType());
+ for (unsigned I = 0; I < SL->getLength(); I++) {
+ Value = SL->getCodeUnit(I);
+ Result.getArrayInitializedElt(I) = APValue(Value);
+ }
+ } else {
+ Result.assign(SL->getString().begin(), SL->getString().end());
+ }
return true;
}
SourceLocation Loc = Message->getBeginLoc();
QualType T = Message->getType().getNonReferenceType();
auto *RD = T->getAsCXXRecordDecl();
if (!RD) {
- Diag(Loc, diag::err_static_assert_invalid_message);
+ SemaRef.Diag(Loc, diag::err_user_defined_msg_invalid) << EvalContext;
return false;
}
auto FindMember = [&](StringRef Member, bool &Empty,
bool Diag = false) -> std::optional<LookupResult> {
- DeclarationName DN = PP.getIdentifierInfo(Member);
- LookupResult MemberLookup(*this, DN, Loc, Sema::LookupMemberName);
- LookupQualifiedName(MemberLookup, RD);
+ DeclarationName DN = SemaRef.PP.getIdentifierInfo(Member);
+ LookupResult MemberLookup(SemaRef, DN, Loc, Sema::LookupMemberName);
+ SemaRef.LookupQualifiedName(MemberLookup, RD);
Empty = MemberLookup.empty();
----------------
erichkeane wrote:
Whats hte point of `Empty` if we are returning an optional?
https://github.com/llvm/llvm-project/pull/131003
More information about the cfe-commits
mailing list