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

Code before:
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "toolchain/check/import_cpp.h"

#include <memory>
#include <string>

#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "toolchain/check/context.h"
#include "toolchain/check/diagnostic_helpers.h"
#include "toolchain/diagnostics/diagnostic.h"
#include "toolchain/diagnostics/format_providers.h"

namespace Carbon::Check {
namespace {

auto GenerateCppIncludesHeaderFilename(llvm::StringRef importing_file_path)
    -> std::string {
  return llvm::Twine(importing_file_path)
      .concat(".generated.cpp_imports.h")
      .str();
}

auto GenerateCppIncludesHeaderCode(
    llvm::ArrayRef<std::pair<llvm::StringRef, SemIRLoc>> imports)
    -> std::string {
  constexpr llvm::StringLiteral IncludePrefix(R"(#include ")");
  constexpr llvm::StringLiteral IncludeSuffix(R"("
)");

  std::string code;
  for (const auto& [path, _] : imports) {
    code.append(
        llvm::Twine(IncludePrefix).concat(path).concat(IncludeSuffix).str());
  }
  return code;
}

}  // namespace


Review comment (suggestion):
[Style](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide):

> For [internal linkage](https://google.github.io/styleguide/cppguide.html#Internal_Linkage) of definitions of functions and variables, prefer static over anonymous namespaces. static minimizes the context necessary to notice the internal linkage of a definition.


Code after:
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "toolchain/check/import_cpp.h"

#include <memory>
#include <string>

#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Tooling/Tooling.h"
#include "common/raw_string_ostream.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "toolchain/check/context.h"
#include "toolchain/check/diagnostic_helpers.h"
#include "toolchain/diagnostics/diagnostic.h"
#include "toolchain/diagnostics/format_providers.h"

namespace Carbon::Check {

// Generates C++ file contents to #include all requested imports.
static auto GenerateCppIncludesHeaderCode(
    llvm::ArrayRef<std::pair<llvm::StringRef, SemIRLoc>> imports)
    -> std::string {
  std::string code;
  llvm::raw_string_ostream code_stream(code);
  for (const auto& [path, _] : imports) {
    code_stream << "#include \"";
    code_stream.write_escaped(path);
    code_stream << "\"\n";
  }
  return code;
}

auto ImportCppFiles(
    Context& context, llvm::StringRef importing_file_path,
    llvm::ArrayRef<std::pair<llvm::StringRef, SemIRLoc>> imports,
    llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs) -> void {
  size_t num_imports = imports.size();
  if (num_imports == 0) {
    return;
  }

  // TODO: Use all import locations by referring each Clang diagnostic to the