【问题标题】:Clang AST: get CXXCtorInitializer list for for constructors that their declaration is not also a definitionClang AST:为构造函数获取 CXXCtorInitializer 列表,它们的声明也不是定义
【发布时间】:2016-05-24 09:03:57
【问题描述】:

我遇到以下问题。 CXXConstructorDecl 的 inits 在以下场景中返回一个空列表:

class Test3 {
  int a = 2;
  int b;
  Test3();
};

Test3::Test3() : b(0) {
}

现在,如果我更改 Test3 构造函数的定义并将其内联为:

class Test3 {
  int a = 2;
  int b;
  Test3()
  : a(2)
  , b(2){

  }
};

一切正常,函数 inits 返回 a 和 b 的 2 项列表。

【问题讨论】:

  • 你能给我们看一个最小的完整例子吗?
  • 试试打电话给getBody()吧?

标签: c++ clang abstract-syntax-tree matcher


【解决方案1】:

正确的做法是:

    if (!ctor->isThisDeclarationADefinition()) {
  for (auto ctorFromList : ctor->redecls()) {
    CXXConstructorDecl *ctr = dyn_cast_or_null<CXXConstructorDecl>(
        ctorFromList);

    if (ctr != ctor) {
      ctor = ctr;
      break;
    }
  }

现在 ctor 是一个指向定义的指针,该定义也具有 inits 列表。

【讨论】:

    猜你喜欢
    • 2020-03-21
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2017-03-17
    • 2019-07-28
    • 2018-06-11
    相关资源
    最近更新 更多