mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
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:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user