mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Generate AST when importing a cpp file (#4790)
Ignore the AST and support a single Cpp import, for now. Report cpp compilation errors and warnings. Part of #4666
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
// 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 {
|
||||
|
||||
auto ImportCppFile(Context& context, SemIRLoc loc, llvm::StringRef file_path,
|
||||
llvm::StringRef code) -> void {
|
||||
std::string diagnostics_str;
|
||||
llvm::raw_string_ostream diagnostics_stream(diagnostics_str);
|
||||
|
||||
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnostic_options(
|
||||
new clang::DiagnosticOptions());
|
||||
clang::TextDiagnosticPrinter diagnostics_consumer(diagnostics_stream,
|
||||
diagnostic_options.get());
|
||||
// TODO: Share compilation flags with ClangRunner.
|
||||
auto ast = clang::tooling::buildASTFromCodeWithArgs(
|
||||
code, {}, file_path, "clang-tool",
|
||||
std::make_shared<clang::PCHContainerOperations>(),
|
||||
clang::tooling::getClangStripDependencyFileAdjuster(),
|
||||
clang::tooling::FileContentMappings(), &diagnostics_consumer);
|
||||
// TODO: Implement and use a DynamicRecursiveASTVisitor to traverse the AST.
|
||||
int num_errors = diagnostics_consumer.getNumErrors();
|
||||
int num_warnings = diagnostics_consumer.getNumWarnings();
|
||||
if (num_errors > 0) {
|
||||
// TODO: Remove the warnings part when there are no warnings.
|
||||
CARBON_DIAGNOSTIC(
|
||||
CppInteropParseError, Error,
|
||||
"{0} error{0:s} and {1} warning{1:s} in `Cpp` import `{2}`:\n{3}",
|
||||
IntAsSelect, IntAsSelect, std::string, std::string);
|
||||
context.emitter().Emit(loc, CppInteropParseError, num_errors, num_warnings,
|
||||
file_path.str(), diagnostics_str);
|
||||
} else if (num_warnings > 0) {
|
||||
CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning,
|
||||
"{0} warning{0:s} in `Cpp` import `{1}`:\n{2}",
|
||||
IntAsSelect, std::string, std::string);
|
||||
context.emitter().Emit(loc, CppInteropParseWarning, num_warnings,
|
||||
file_path.str(), diagnostics_str);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
Reference in New Issue
Block a user