Skip to content

Commit 1c5e252

Browse files
committed
Use the context of the matcher to print types
1 parent 7b5d4c5 commit 1c5e252

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

cpp2rust/converter/mapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ std::unordered_map<std::string, TranslationRule::TypeTgt>
3232
clang::PrintingPolicy getPrintPolicy() {
3333
assert(ctx_);
3434
clang::PrintingPolicy policy(ctx_->getLangOpts());
35+
policy.Bool = true;
3536
policy.SuppressTagKeyword = true;
3637
policy.SuppressScope = false;
3738
policy.FullyQualifiedName = true;
@@ -562,6 +563,11 @@ std::string normalizeTranslationRule(std::string rule) {
562563

563564
} // namespace
564565

566+
PushASTContext::PushASTContext(clang::ASTContext &ctx) : prev_(ctx_) {
567+
ctx_ = &ctx;
568+
}
569+
PushASTContext::~PushASTContext() { ctx_ = prev_; }
570+
565571
bool Contains(clang::QualType qual_type) {
566572
return search(qual_type) != types_.end();
567573
}

cpp2rust/converter/mapper.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2022-present INESC-ID.
44
// Distributed under the MIT license that can be found in the LICENSE file.
55

6+
#include <clang/AST/ASTContext.h>
67
#include <clang/AST/Expr.h>
78
#include <clang/AST/Type.h>
89

@@ -14,6 +15,17 @@
1415
#include "converter/translation_rule.h"
1516

1617
namespace cpp2rust::Mapper {
18+
class PushASTContext {
19+
public:
20+
explicit PushASTContext(clang::ASTContext &ctx);
21+
~PushASTContext();
22+
PushASTContext(const PushASTContext &) = delete;
23+
PushASTContext &operator=(const PushASTContext &) = delete;
24+
25+
private:
26+
clang::ASTContext *prev_;
27+
};
28+
1729
bool Contains(clang::QualType qual_type);
1830
bool Contains(const clang::Expr *expr);
1931

cpp2rust/converter/translation_rule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
8686

8787
void run(const clang::ast_matchers::MatchFinder::MatchResult &R) override {
8888
assert(sema_);
89+
Mapper::PushASTContext scoped(*R.Context);
8990
if (auto var = R.Nodes.getNodeAs<clang::TypeAliasDecl>("tvar")) {
9091
clang::QualType type;
9192
if (auto *tdecl = var->getDescribedAliasTemplate()) {

0 commit comments

Comments
 (0)