Repository: carbon-language/carbon-lang
File: toolchain/check/cpp/import.cpp (C++)

Code before:
  CARBON_CHECK(expr, "empty expression");

  SemIR::TypeId type_id = MapType(context, loc_id, expr->getType()).type_id;
  if (!type_id.has_value()) {
    context.TODO(
        loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
    return SemIR::ErrorInst::InstId;
  }
  SemIR::InstId inst_id = SemIR::InstId::None;
  if (auto* integer_literal = dyn_cast<clang::IntegerLiteral>(expr)) {
    auto int_id =
        context.ints().Add(integer_literal->getValue().getSExtValue());
    inst_id = AddInstInNoBlock<SemIR::IntValue>(
        context, loc_id, {.type_id = type_id, .int_id = int_id});
  } else if (auto* float_literal = dyn_cast<clang::FloatingLiteral>(expr)) {
    auto float_id = context.floats().Add(float_literal->getValue());
    inst_id = AddInstInNoBlock<SemIR::FloatValue>(
        context, loc_id, {.type_id = type_id, .float_id = float_id});
  } else {
    context.TODO(
        loc_id, "Unsupported: constant type: " + expr->getType().getAsString());
    inst_id = SemIR::ErrorInst::InstId;
  }
  if (inst_id != SemIR::ErrorInst::InstId) {
    context.imports().push_back(inst_id);
  }
  return inst_id;
}

// Imports a macro definition into the scope. Currently supports only simple
// object-like macros that expand to a constant integer value.
// TODO: Add support for other macro types and non-integer literal values.
static auto ImportMacro(Context& context, SemIR::LocId loc_id,
                        SemIR::NameScopeId scope_id, SemIR::NameId name_id,
                        clang::MacroInfo* macro_info)
    -> SemIR::ScopeLookupResult {
  clang::Expr* macro_expr =
      TryEvaluateMacroToConstant(context, loc_id, name_id, macro_info);

  if (!macro_expr) {
    return SemIR::ScopeLookupResult::MakeNotFound();
  }

  auto inst_id = MapConstant(context, loc_id, macro_expr);
  if (inst_id == SemIR::ErrorInst::InstId) {
    return SemIR::ScopeLookupResult::MakeNotFound();
  }

  AddNameToScope(context, scope_id, name_id, SemIR::AccessKind::Public,
                 inst_id);
  return SemIR::ScopeLookupResult::MakeWrappedLookupResult(

Review comment (suggestion):
```suggestion
    return SemIR::ErrorInst::InstId;
  }
  context.imports().push_back(inst_id);
```

Code after:
    FloatId float_id = context.floats().Add(float_literal->getValue());
    inst_id = AddInstInNoBlock(context,
                               MakeImportedLocIdAndInst<SemIR::FloatValue>(
                                   context, imported_loc_id,
                                   {.type_id = type_id, .float_id = float_id}));
  } else {
    context.TODO(loc_id, llvm::formatv(
                             "Unsupported: C++ constant expression type: '{0}'",
                             expr->getType().getAsString()));
    return SemIR::ErrorInst::InstId;
  }

  context.imports().push_back(inst_id);
  return inst_id;
}

// Imports a macro definition into the scope. Currently supports only simple
// object-like macros that expand to a constant integer value.
// TODO: Add support for other macro types and non-integer literal values.
static auto ImportMacro(Context& context, SemIR::LocId loc_id,
                        SemIR::NameScopeId scope_id, SemIR::NameId name_id,
                        clang::MacroInfo* macro_info)
    -> SemIR::ScopeLookupResult {
  clang::Expr* macro_expr =
      TryEvaluateMacroToConstant(context, loc_id, name_id, macro_info);

  if (!macro_expr) {
    return SemIR::ScopeLookupResult::MakeNotFound();
  }

  auto inst_id = MapConstant(context, loc_id, macro_expr);
  if (inst_id == SemIR::ErrorInst::InstId) {
    return SemIR::ScopeLookupResult::MakeNotFound();
  }

  AddNameToScope(context, scope_id, name_id, SemIR::AccessKind::Public,
                 inst_id);
  return SemIR::ScopeLookupResult::MakeWrappedLookupResult(
      inst_id, SemIR::AccessKind::Public);
}

// Looks up a macro definition in the top-level `Cpp` scope. Returns nullptr if
// the macro is not found or if it is a builtin macro, function-like macro or a
// macro used for header guards.
// TODO: Function-like and builtin macros are currently not supported and their
// support still needs to be clarified.
static auto LookupMacro(Context& context, SemIR::NameScopeId scope_id,
                        clang::IdentifierInfo* identifier_info)
    -> clang::MacroInfo* {
  if (!IsTopCppScope(context, scope_id)) {
    return nullptr;