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

Code before:
          clang::SourceLocation()),
      clang::Sema::LookupNameKind::LookupOrdinaryName);

  bool found = sema.LookupQualifiedName(
      lookup, ast->getASTContext().getTranslationUnitDecl());

  if (lookup.isClassLookup()) {
    // TODO: To support class lookup, also return the AccessKind for storage.
    context.TODO(loc_id, "Unsupported: Lookup in Class");
    return std::nullopt;
  }

  if (!found) {
    return std::nullopt;
  }

  return lookup;
}

// Returns the return type of the given function declaration.
// Currently only void and 32-bit int are supported.
// TODO: Support more return types.
static auto GetReturnType(Context& context,
                          const clang::FunctionDecl* clang_decl)
    -> SemIR::InstId {
  auto ret_type = clang_decl->getReturnType().getCanonicalType();
  if (ret_type->isVoidType()) {
    return SemIR::InstId::None;
  }
  const int supported_int_width = 32;
  if (ret_type->isSignedIntegerType() &&
      clang_decl->getASTContext().getTypeSize(ret_type) ==
          supported_int_width &&
      clang_decl->getASTContext().getTargetInfo().getIntWidth() ==
          supported_int_width) {
    auto size_id = context.ints().Add(supported_int_width);
    auto type_id = MakeIntType(context, Parse::NodeId::None,
                               SemIR::IntKind::Signed, size_id);
    auto type_inst_id = MakeIntTypeLiteral(context, Parse::NodeId::None,
                                           SemIR::IntKind::Signed, size_id);

    auto return_slot_pattern_id = AddInstInNoBlock(
        // TODO: have a location for the return type once available.
        context, SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
                     {.type_id = type_id, .type_inst_id = type_inst_id})));
    auto param_pattern_id = AddInstInNoBlock(
        // TODO: same here, have a location for the return type once available.
        context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
                     {.type_id = type_id,
                      .subpattern_id = return_slot_pattern_id,
                      .index = SemIR::CallParamIndex::None})));

Review comment (suggestion):
Can you replace auto with the actual type?
I think it would be easier to follow.

See also below and consider every type that is not a complex template and non trivial.
See https://google.github.io/styleguide/cppguide.html#Type_deduction
"use type deduction only to make the code clearer or safer, and do not use it merely to avoid the inconvenience of writing an explicit type"

Code after:

  clang::LookupResult lookup(
      sema,
      clang::DeclarationNameInfo(
          clang::DeclarationName(
              sema.getPreprocessor().getIdentifierInfo(*name)),
          clang::SourceLocation()),
      clang::Sema::LookupNameKind::LookupOrdinaryName);

  bool found = sema.LookupQualifiedName(
      lookup, context.name_scopes().Get(scope_id).cpp_decl_context());

  if (lookup.isClassLookup()) {
    // TODO: To support class lookup, also return the AccessKind for storage.
    context.TODO(loc_id, "Unsupported: Lookup in Class");
    return std::nullopt;
  }

  if (!found) {
    return std::nullopt;
  }

  return lookup;
}

// Returns the return type of the given function declaration.
// Currently only void and 32-bit int are supported.
// TODO: Support more return types.
static auto GetReturnType(Context& context, SemIRLoc loc_id,
                          const clang::FunctionDecl* clang_decl)
    -> SemIR::InstId {
  clang::QualType ret_type = clang_decl->getReturnType().getCanonicalType();
  if (ret_type->isVoidType()) {
    return SemIR::InstId::None;
  }
  if (const auto* builtin_type = dyn_cast<clang::BuiltinType>(ret_type);
      builtin_type && builtin_type->getKind() == clang::BuiltinType::Int) {
    constexpr int SupportedIntWidth = 32;
    uint64_t int_size = context.ast_context().getTypeSize(ret_type);
    if (int_size != SupportedIntWidth) {
      // TODO: Add tests for this case.
      context.TODO(loc_id,
                   llvm::formatv("Unsupported: return type: {0}, size: {1}",
                                 ret_type.getAsString(), int_size));
      return SemIR::ErrorInst::SingletonInstId;
    }
    IntId size_id = context.ints().Add(int_size);
    // TODO: Fill in a location for the type once available.
    SemIR::TypeId type_id = MakeIntType(context, Parse::NodeId::None,
                                        SemIR::IntKind::Signed, size_id);
    // TODO: Fill in a location for the type once available.