Marco Poletti | b5239c9 | 2022-05-11 09:46:05 -0700 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
| 5 | #include "migrator/rs_from_cc/rs_from_cc_lib.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "absl/container/flat_hash_map.h" |
| 13 | #include "absl/status/status.h" |
| 14 | #include "absl/status/statusor.h" |
| 15 | #include "absl/strings/string_view.h" |
| 16 | #include "absl/strings/substitute.h" |
| 17 | #include "absl/types/span.h" |
| 18 | #include "migrator/rs_from_cc/converter.h" |
| 19 | #include "migrator/rs_from_cc/frontend_action.h" |
| 20 | #include "clang/Basic/FileManager.h" |
| 21 | #include "clang/Basic/FileSystemOptions.h" |
| 22 | #include "clang/Frontend/FrontendAction.h" |
| 23 | #include "clang/Tooling/Tooling.h" |
| 24 | |
| 25 | namespace crubit_rs_from_cc { |
| 26 | |
| 27 | absl::StatusOr<std::string> RsFromCc(const absl::string_view cc_file_content, |
| 28 | const absl::string_view cc_file_name, |
| 29 | absl::Span<const absl::string_view> args) { |
| 30 | std::vector<std::string> args_as_strings{ |
| 31 | // Parse non-doc comments that are used as documention |
| 32 | "-fparse-all-comments"}; |
| 33 | args_as_strings.insert(args_as_strings.end(), args.begin(), args.end()); |
| 34 | |
| 35 | Converter::Invocation invocation; |
| 36 | if (clang::tooling::runToolOnCodeWithArgs( |
| 37 | std::make_unique<FrontendAction>(invocation), cc_file_content, |
| 38 | args_as_strings, cc_file_name, "rs_from_cc", |
| 39 | std::make_shared<clang::PCHContainerOperations>(), |
| 40 | clang::tooling::FileContentMappings())) { |
| 41 | return invocation.rs_code_; |
| 42 | } else { |
| 43 | return absl::Status(absl::StatusCode::kInvalidArgument, |
| 44 | "Could not compile source file contents"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | } // namespace crubit_rs_from_cc |