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

Code before:
#include <memory>
#include <optional>

#include "common/vlog.h"
#include "llvm/IR/Verifier.h"
#include "toolchain/lower/context.h"
#include "toolchain/lower/file_context.h"

namespace Carbon::Lower {

auto LowerToLLVM(
    llvm::LLVMContext& llvm_context,
    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
    llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
    const SemIR::File& sem_ir, const LowerToLLVMOptions& options)
    -> std::unique_ptr<llvm::Module> {
  Context context(llvm_context, std::move(fs), options.want_debug_info,
                  tree_and_subtrees_getters, sem_ir.filename(),
                  options.vlog_stream);

  // TODO: Consider disabling instruction naming by default if we're not
  // producing textual LLVM IR.
  SemIR::InstNamer inst_namer(&sem_ir);
  context.GetFileContext(&sem_ir, &inst_namer).LowerDefinitions();

  auto module = std::move(context).Finalize();

  if (options.vlog_stream) {
    CARBON_VLOG_TO(options.vlog_stream, "*** llvm::Module ***\n");
    module->print(*options.vlog_stream, /*AAW=*/nullptr,
                  /*ShouldPreserveUseListOrder=*/false,
                  /*IsForDebug=*/true);
  }
  if (options.dump_stream) {
    module->print(*options.dump_stream, /*AAW=*/nullptr,
                  /*ShouldPreserveUseListOrder=*/true);
  }

  if (options.llvm_verifier_stream) {
    CARBON_CHECK(!llvm::verifyModule(*module, options.llvm_verifier_stream));
  }

  return module;
}

}  // namespace Carbon::Lower


Review comment (suggestion):
Type doesn't seem clear to me here, which suggests not using `auto`.

Code after:
#include <memory>
#include <optional>

#include "common/vlog.h"
#include "llvm/IR/Verifier.h"
#include "toolchain/lower/context.h"
#include "toolchain/lower/file_context.h"

namespace Carbon::Lower {

auto LowerToLLVM(
    llvm::LLVMContext& llvm_context,
    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
    llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
    const SemIR::File& sem_ir, const LowerToLLVMOptions& options)
    -> std::unique_ptr<llvm::Module> {
  Context context(llvm_context, std::move(fs), options.want_debug_info,
                  tree_and_subtrees_getters, sem_ir.filename(),
                  options.vlog_stream);

  // TODO: Consider disabling instruction naming by default if we're not
  // producing textual LLVM IR.
  SemIR::InstNamer inst_namer(&sem_ir);
  context.GetFileContext(&sem_ir, &inst_namer).LowerDefinitions();

  std::unique_ptr<llvm::Module> module = std::move(context).Finalize();

  if (options.vlog_stream) {
    CARBON_VLOG_TO(options.vlog_stream, "*** llvm::Module ***\n");
    module->print(*options.vlog_stream, /*AAW=*/nullptr,
                  /*ShouldPreserveUseListOrder=*/false,
                  /*IsForDebug=*/true);
  }
  if (options.dump_stream) {
    module->print(*options.dump_stream, /*AAW=*/nullptr,
                  /*ShouldPreserveUseListOrder=*/true);
  }

  if (options.llvm_verifier_stream) {
    CARBON_CHECK(!llvm::verifyModule(*module, options.llvm_verifier_stream));
  }

  return module;
}

}  // namespace Carbon::Lower