-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Generate AST when importing a cpp file #4790
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
844ad0f
Generate AST when importing cpp files
bricknerb ff87420
Address review comments:
bricknerb 941ce84
Explicitly unsupport multiple Cpp imports
bricknerb e7bf180
When using Carbon Driver for Fuzzing, diagnose an error when trying t…
bricknerb 7ff444c
Use backticks when quoting filenames.
bricknerb be23014
Make TestKindCoverage for DiagnosticKind more restrictive.
bricknerb 82f5736
Use SouceBuffer for reading cpp files
bricknerb dfc0b4a
Clarify multiple Cpp imports will be supported and use context.TODO t…
bricknerb 9fe5276
Rephrase Cpp import with fuzzing error
bricknerb a9a055b
Improve diagnostics for bad Cpp imports
bricknerb 8bd3bde
Removed redundant `import Cpp library` test
bricknerb f111712
Remove bad copy paste
bricknerb 0c17d3c
Avoid StringLiteral in CARBON_DIAGNOSTIC
bricknerb 9f75de5
Rephrase referring to Cpp import when it triggers diagnostics
bricknerb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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( | ||
| CppInteropCodeErrors, Error, | ||
bricknerb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "{0} error{0:s} and {1} warning{1:s} in C++ file '{2}':\n{3}", | ||
bricknerb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| IntAsSelect, IntAsSelect, std::string, std::string); | ||
| context.emitter().Emit(loc, CppInteropCodeErrors, num_errors, num_warnings, | ||
| file_path.str(), diagnostics_str); | ||
| } else if (num_warnings > 0) { | ||
| CARBON_DIAGNOSTIC(CppInteropCodeWarnings, Warning, | ||
bricknerb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "{0} warning{0:s} in C++ file '{1}':\n{2}", IntAsSelect, | ||
| std::string, std::string); | ||
| context.emitter().Emit(loc, CppInteropCodeWarnings, num_warnings, | ||
| file_path.str(), diagnostics_str); | ||
| } | ||
| } | ||
|
|
||
| } // namespace Carbon::Check | ||
This file contains hidden or 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_ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.