Add support for macros evaluating to an enum constant (#6432)

Enum constants in a macro replacement list are recognized only when
prefixed with “::”.
There is a `todo` test to make explicit that this still needs to be
fixed.
When prefixed with a global scope “::”, they are correctly found and
evaluated to a const.


Part of #6303
This commit is contained in:
Ivana Ivanovska
2025-11-25 17:44:07 +00:00
committed by GitHub
parent 44d86e11bb
commit cf2c66c1b2
2 changed files with 43 additions and 0 deletions
+7
View File
@@ -25,6 +25,7 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
clang::Sema& sema = context.clang_sema();
clang::Preprocessor& preprocessor = sema.getPreprocessor();
clang::Parser parser(preprocessor, sema, false);
llvm::SmallVector<clang::Token> tokens(macro_info->tokens().begin(),
@@ -45,7 +46,12 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
/*IsReinject=*/false);
parser.ConsumeAnyToken(true);
// TODO: Identifiers are still only available if prefixed with "::" (e.g.
// "#define M_Var ::myVar").
parser.EnterScope(clang::Scope::DeclScope);
clang::ExprResult result = parser.ParseConstantExpression();
parser.ExitScope();
clang::Expr* result_expr = result.get();
bool success =
@@ -70,6 +76,7 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
clang::Expr::EvalResult evaluated_result;
CARBON_CHECK(result_expr->EvaluateAsConstantExpr(evaluated_result,
sema.getASTContext()));
clang::APValue ap_value = evaluated_result.Val;
switch (ap_value.getKind()) {
case clang::APValue::Int: