-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
16 changed files
with
481 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 | ||
|
||
#ifndef CARBON_TOOLCHAIN_CHECK_IMPORT_CPP_H_ | ||
#define CARBON_TOOLCHAIN_CHECK_IMPORT_CPP_H_ | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "toolchain/check/context.h" | ||
#include "toolchain/check/diagnostic_helpers.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
// Parses the C++ code and report errors and warnings. | ||
auto ImportCppFile(Context& context, SemIRLoc loc, llvm::StringRef file_path, | ||
llvm::StringRef code) -> void; | ||
|
||
} // namespace Carbon::Check | ||
|
||
#endif // CARBON_TOOLCHAIN_CHECK_IMPORT_CPP_H_ |
55 changes: 55 additions & 0 deletions
55
toolchain/check/testdata/interop/cpp/no_prelude/bad_import.carbon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 | ||
// | ||
// AUTOUPDATE | ||
// TIP: To test this file alone, run: | ||
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/no_prelude/bad_import.carbon | ||
// TIP: To dump output, run: | ||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/no_prelude/bad_import.carbon | ||
|
||
// --- fail_import_cpp.carbon | ||
|
||
library "[[@TEST_NAME]]"; | ||
|
||
// CHECK:STDERR: fail_import_cpp.carbon:[[@LINE+4]]:1: error: `Cpp` import missing library [CppInteropMissingLibrary] | ||
// CHECK:STDERR: import Cpp; | ||
// CHECK:STDERR: ^~~~~~ | ||
// CHECK:STDERR: | ||
import Cpp; | ||
|
||
// --- fail_import_cpp_library_empty.carbon | ||
|
||
library "[[@TEST_NAME]]"; | ||
|
||
// CHECK:STDERR: fail_import_cpp_library_empty.carbon:[[@LINE+5]]:1: error: `Cpp` import missing library [CppInteropMissingLibrary] | ||
// CHECK:STDERR: import Cpp library ""; | ||
// CHECK:STDERR: ^~~~~~ | ||
// CHECK:STDERR: | ||
// CHECK:STDERR: "foo.h": error: error opening file for read: No such file or directory [ErrorOpeningFile] | ||
import Cpp library ""; | ||
|
||
// --- fail_import_cpp_library_file_with_quotes.carbon | ||
|
||
library "[[@TEST_NAME]]"; | ||
|
||
import Cpp library "\"foo.h\""; | ||
|
||
// CHECK:STDOUT: --- fail_import_cpp.carbon | ||
// CHECK:STDOUT: | ||
// CHECK:STDOUT: file { | ||
// CHECK:STDOUT: package: <namespace> = namespace [template] {} | ||
// CHECK:STDOUT: } | ||
// CHECK:STDOUT: | ||
// CHECK:STDOUT: --- fail_import_cpp_library_empty.carbon | ||
// CHECK:STDOUT: | ||
// CHECK:STDOUT: file { | ||
// CHECK:STDOUT: package: <namespace> = namespace [template] {} | ||
// CHECK:STDOUT: } | ||
// CHECK:STDOUT: | ||
// CHECK:STDOUT: --- fail_import_cpp_library_file_with_quotes.carbon | ||
// CHECK:STDOUT: | ||
// CHECK:STDOUT: file { | ||
// CHECK:STDOUT: package: <namespace> = namespace [template] {} | ||
// CHECK:STDOUT: } | ||
// CHECK:STDOUT: |
Oops, something went wrong.