| // Part of the Crubit 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 "migrator/rs_from_cc/converter.h" |
| |
| #include <string> |
| |
| #include "absl/strings/str_split.h" |
| #include "clang/AST/Decl.h" |
| #include "clang/AST/DeclBase.h" |
| #include "llvm/Support/raw_ostream.h" |
| #include "third_party/re2/re2.h" |
| |
| namespace crubit_rs_from_cc { |
| |
| void Converter::Convert(const clang::TranslationUnitDecl* translation_unit) { |
| for (clang::Decl* decl : translation_unit->decls()) { |
| if (decl->getBeginLoc().isInvalid()) { |
| // Skip declarations with invalid locations, e.g. builtins that Clang |
| // generates. |
| continue; |
| } |
| Convert(decl); |
| } |
| } |
| |
| void Converter::Convert(const clang::Decl* decl) { |
| switch (decl->getKind()) { |
| case clang::Decl::TranslationUnit: |
| Convert(dynamic_cast<const clang::TranslationUnitDecl*>(decl)); |
| break; |
| |
| default: |
| ConvertUnhandled(decl); |
| } |
| } |
| |
| void Converter::ConvertUnhandled(const clang::Decl* decl) { |
| std::string ast; |
| llvm::raw_string_ostream os(ast); |
| decl->dump(os); |
| os.flush(); |
| result_ += "\n"; |
| result_ += "// Unsupported decl:\n//\n"; |
| // Remove addresses since they're not useful and add non-determinism that |
| // would break golden testing. |
| // Also remove spaces at the end of each line, those are a pain in golden |
| // tests since IDEs often strip spaces at end of line. |
| RE2::GlobalReplace(&ast, "(?m) 0x[a-z0-9]+| +$", ""); |
| for (auto line : absl::StrSplit(ast, '\n')) { |
| if (line.empty()) { |
| continue; |
| } |
| result_ += "// "; |
| result_ += line; |
| result_ += '\n'; |
| } |
| } |
| |
| } // namespace crubit_rs_from_cc |