如何在clang中打印完全合格的Expr

如何在clang中打印完全合格的Expr

本文介绍了如何在clang中打印完全合格的Expr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用clang 8.0.1开发反射工具.现在,我需要使用所有完全合格的名称打印Expr.

I'm working on my reflection tool with clang 8.0.1. And right now I need to print Expr with all names fully qualified.

我已经尝试过将FullyQualifiedName位设置为true的内置prettyPrint函数.但是它仍然给出错误的结果.

I already tried builtin prettyPrint function with FullyQualifiedName bit set to true. But it still gives incorrect result.

对于这段代码:

namespace math {
   struct Transform {
     float val;
     [[custom_attr(&Transform::public_val)]]
     void foo();
   };
}

它给了我

&Transform::public_val

代替

&math::Transform::public_val

还有

static_cast<float (*)(const Transform&)>(Transform::static_overload)

作为custom_attr的值,它给了我

as value of custom_attr it gives me

static_cast<float (*)(const math::Transform &)>(Transform::static_overload)

(仅Transform::static_over)

这是我的打印代码:

std::string get_string(const Expr *expr, const ASTContext &Context) {
  PrintingPolicy print_policy(Context.getLangOpts());
  print_policy.FullyQualifiedName = 1;
  print_policy.SuppressScope = 0;
  print_policy.SuppressUnwrittenScope = 0;
  std::string expr_string;
  llvm::raw_string_ostream stream(expr_string);
  expr->printPretty(stream, nullptr, print_policy);
  stream.flush();
  return expr_string;
}

推荐答案

我所需要做的只是指定PrintCanonicalTypes标志.这是从Expr获取字符串的功能:

All I needed is to specify PrintCanonicalTypes flag. Here is function for getting string from Expr:

std::string getFullyQualified(const Expr *expr,
                                          const ASTContext &Context) {
  static PrintingPolicy print_policy(Context.getLangOpts());
  print_policy.FullyQualifiedName = 1;
  print_policy.SuppressScope = 0;
  print_policy.PrintCanonicalTypes = 1;


  std::string expr_string;
  llvm::raw_string_ostream stream(expr_string);
  expr->printPretty(stream, nullptr, print_policy);
  stream.flush();
  return expr_string;
}

这篇关于如何在clang中打印完全合格的Expr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:33