计算 clang::attr 的长度

Compute the length of a clang::attr

我正在开发一个项目,该项目使用 Clang AST 匹配器在变量声明中定位有趣的属性(例如“__boo__”),然后使用 clang::Replacement 更改其拼写(例如, "__foo__").为了构造一个有效的 clang::Replacement 对象,我成功地获取了属性位置,如下所示。

auto &SM = getCompilerInstance().getSourceManager();
clang::SourceLocation sl = SM.getExpansionLoc(Attr->getLocation());

//Construct the replacement object
clang::tooling::Replacement Rep(SM, sl, length, "__foo__"); //?? how to compute the length of Attr

我的问题是如何获得我想要更改的属性的长度(因为程序员可以自由定义他们想要的任何属性)。

在此先感谢您的帮助。

得到以下有效解决方案:

      auto &SM = getCompilerInstance().getSourceManager();
      clang::SourceLocation sl = SM.getExpansionLoc(Attr->getLocation());
      clang::LangOptions langOps;
      SmallString<256> buffer;
      StringRef sp = clang::Lexer::getSpelling(sl, buffer, SM, langOps);
      unsigned length = sp.size();
      ct::Replacement Rep(SM, sl, length, rep);