Skip to content

Commit 5f136d8

Browse files
committed
Build json::Object on-the-fly
1 parent 7cd7ae3 commit 5f136d8

3 files changed

Lines changed: 17 additions & 36 deletions

File tree

cpp2rust/converter/rule_src_parser.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct LookupInfo {
6969

7070
class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
7171
public:
72-
explicit Callback(SrcStrings &strings) : strings_(strings) {}
72+
explicit Callback(llvm::json::Object &out) : out_(out) {}
7373

7474
void init(clang::Sema &sema) {
7575
sema_ = &sema;
@@ -87,13 +87,15 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
8787
} else {
8888
type = var->getUnderlyingType();
8989
}
90-
strings_.types[var->getQualifiedNameAsString()] = Mapper::ToString(type);
90+
out_[var->getQualifiedNameAsString()] =
91+
llvm::json::Object{{"to_string", Mapper::ToString(type)}};
9192
return;
9293
}
9394

9495
if (auto func = R.Nodes.getNodeAs<clang::FunctionDecl>("func")) {
9596
auto add = [&](std::string &&src) {
96-
strings_.functions[func->getQualifiedNameAsString()] = std::move(src);
97+
out_[func->getQualifiedNameAsString()] =
98+
llvm::json::Object{{"to_string", std::move(src)}};
9799
};
98100

99101
if (const auto *fcall = R.Nodes.getNodeAs<clang::CallExpr>("fcall")) {
@@ -168,7 +170,7 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
168170
}
169171

170172
private:
171-
SrcStrings &strings_;
173+
llvm::json::Object &out_;
172174
clang::Sema *sema_ = nullptr;
173175
clang::SourceLocation loc_;
174176

@@ -644,7 +646,7 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
644646

645647
class ActionFactory : public clang::tooling::FrontendActionFactory {
646648
public:
647-
explicit ActionFactory(SrcStrings &strings) : cb_(strings) {
649+
explicit ActionFactory(llvm::json::Object &out) : cb_(out) {
648650
using namespace clang::ast_matchers;
649651
finder_.addMatcher(
650652
returnStmt(
@@ -719,16 +721,14 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
719721

720722
} // namespace
721723

722-
SrcStrings Extract(const std::filesystem::path &src_path) {
723-
SrcStrings strings;
724+
void Extract(const std::filesystem::path &src_path, llvm::json::Object &out) {
724725
auto flags = getPlatformClangBeginFlags();
725726
auto end_flags = getPlatformClangEndFlags();
726727
flags.insert(flags.end(), end_flags.begin(), end_flags.end());
727728
clang::tooling::FixedCompilationDatabase compilations(".", flags);
728-
ActionFactory factory(strings);
729+
ActionFactory factory(out);
729730
clang::tooling::ClangTool tool(compilations, {src_path.string()});
730731
tool.run(&factory);
731-
return strings;
732732
}
733733

734734
} // namespace cpp2rust::RuleSrcParser
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
#pragma once
2-
31
// Copyright (c) 2022-present INESC-ID.
42
// Distributed under the MIT license that can be found in the LICENSE file.
53

4+
#pragma once
5+
6+
#include <llvm/Support/JSON.h>
7+
68
#include <filesystem>
7-
#include <string>
8-
#include <unordered_map>
99

1010
namespace cpp2rust::RuleSrcParser {
1111

12-
struct SrcStrings {
13-
std::unordered_map<std::string, std::string> functions; // f<n> -> ToString
14-
std::unordered_map<std::string, std::string> types; // t<n> -> ToString
15-
};
16-
17-
SrcStrings Extract(const std::filesystem::path &src_path);
12+
// Visit every f<n> / t<n> declaration in `src_path`:
13+
// f<n> / t<n>: { "to_string": <Mapper::ToString> }
14+
void Extract(const std::filesystem::path &src_path, llvm::json::Object &out);
1815

1916
} // namespace cpp2rust::RuleSrcParser

cpp2rust/cpp_rule_preprocessor.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include <cstdlib>
1010
#include <filesystem>
11-
#include <map>
1211
#include <string>
1312

1413
#include "converter/rule_src_parser.h"
@@ -32,23 +31,8 @@ int main(int argc, char *argv[]) {
3231

3332
fs::path src = SrcFile.getValue();
3433
llvm::errs() << "Preprocessing " << src.string() << '\n';
35-
auto strings = cpp2rust::RuleSrcParser::Extract(src);
36-
37-
// Sort by name for deterministic output.
38-
std::map<std::string, std::string> sorted;
39-
for (auto &[k, v] : strings.functions) {
40-
sorted.emplace(k, v);
41-
}
42-
for (auto &[k, v] : strings.types) {
43-
sorted.emplace(k, v);
44-
}
45-
4634
llvm::json::Object root;
47-
for (auto &[k, v] : sorted) {
48-
llvm::json::Object entry;
49-
entry["to_string"] = v;
50-
root.try_emplace(k, std::move(entry));
51-
}
35+
cpp2rust::RuleSrcParser::Extract(src, root);
5236

5337
auto out_path = src.parent_path() / "ir_src.json";
5438
std::error_code ec;

0 commit comments

Comments
 (0)