微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Clang 索引未检测到 C++20 ConceptDecls

如何解决Clang 索引未检测到 C++20 ConceptDecls

我无法检测 Clang 的 IndexingAction clang::ConceptDecls。 我有一个初始化的最小工作示例 一个 IndexingAction 并在一些包含两个的测试代码上运行它 概念声明。我已经验证前端“看到”了这些声明 转储 AST。

然而,IndexingAction 被设置为打印一条简单的消息 当它遇到任何类型的decl,并且没有这样的消息时 在运行上述代码时打印。因此,它 似乎 ConceptDecls 对 IndexingAction 不可见, 而其他声明被正确检测(clang::CXXRecordDecl, clang::FunctionDecl 等)。

我在 LLVM/Clang 11 和 12 中重现了这个问题。

示例代码如下:

#include "clang/Tooling/Tooling.h"
#include "clang/Index/IndexDataConsumer.h"
#include "clang/Index/IndexingAction.h"
#include "clang/Index/IndexingOptions.h"
#include <cstdio>

class IndexDataConsumer : public clang::index::IndexDataConsumer {
public:
  // Print a message to stdout if any kind of declaration is found
  bool handleDeclOccurrence(const clang::Decl*,clang::index::SymbolRoleSet,llvm::ArrayRef<clang::index::SymbolRelation>,clang::SourceLocation,clang::index::IndexDataConsumer::ASTNodeInfo) override {
    printf("Found a decl occurrence\n");
    return true;
  }
};

class IndexActionFactory : public clang::tooling::FrontendActionFactory {
public:
  std::unique_ptr<clang::FrontendAction> create() override {
    // The most permissive set of indexing options possible
    clang::index::IndexingOptions opts;
    opts.IndexFunctionLocals           = true;
    opts.IndexImplicitInstantiation    = true;
    opts.IndexMacrosInPreprocessor     = true;
    opts.IndexParametersInDeclarations = true;
    opts.IndexTemplateParameters       = true;
    opts.SystemSymbolFilter            = clang::index::IndexingOptions::SystemSymbolFilterKind::None;
    IndexDataConsumer idx;
    return createIndexingAction(std::make_shared<IndexDataConsumer>(idx),opts);
  }
};

int main() {
  const std::string code = R"(
    // Basic example of a concept
    template <typename T,typename U = T> concept Sumable = requires(T a,U b) {
      {a + b};
      {b + a};
    };

    // Another basic example of a concept
    template<typename T>
    concept has_type_member = requires { typename T::type; };
  )";

  // Run the indexing action over the code above.
  // If any decl is found in the AST,the printf should fire.
  IndexActionFactory IndexFactory;
  clang::tooling::runToolOnCodeWithArgs(IndexFactory.create(),code,{"-std=c++20"});
  return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。