Skip to content

Commit

Permalink
Fix bug in 'value_of'.
Browse files Browse the repository at this point in the history
Should work for getting the value of reflection of a local reference to
an object having static storage duration.
  • Loading branch information
katzdm committed Jun 24, 2024
1 parent 5157802 commit ebb430e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
14 changes: 11 additions & 3 deletions clang/lib/Sema/Metafunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,9 +1704,17 @@ bool value_of(APValue &Result, Sema &S, EvalFn Evaluator, QualType ResultTy,
if (!VD->isUsableInConstantExpressions(S.Context))
return true;

if (APValue *VarValue = VD->evaluateValue())
Value = *VarValue;
else
QualType QT = VD->getType();
if (auto *LVRT = dyn_cast<LValueReferenceType>(QT)) {
QT = LVRT->getPointeeType();
}

Expr *Synthesized = DeclRefExpr::Create(S.Context,
NestedNameSpecifierLoc(),
SourceLocation(), VD, false,
Range.getBegin(), QT,
VK_LValue, Decl, nullptr);
if (!Evaluator(Value, Synthesized, true))
return true;
} else if (isa<EnumConstantDecl>(Decl)) {
Expr *Synthesized = DeclRefExpr::Create(S.Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ static_assert(type_of(value_of(^ref)) == ^int);
constexpr std::pair<std::pair<int, bool>, int> p = {{1, true}, 2};
static_assert(type_of(value_of(std::meta::reflect_object(p.first))) ==
^const std::pair<int, bool>);

constexpr int g = 3;
consteval std::meta::info fn() {
const int &r = g;
static_assert([:value_of(^r):] == 3);
return value_of(^r);
}
static_assert([:fn():] == 3);
} // namespace value_of_types

// ===================
Expand Down

0 comments on commit ebb430e

Please sign in to comment.