C++ libclang返回了太多关于函数声明的信息

C++ libclang返回了太多关于函数声明的信息,c++,clang,libclang,C++,Clang,Libclang,我有以下使用clang-CAPI的代码 #include <iostream> #include <string> #include <clang-c/Index.h> CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) { CXCursor cursor1 = clang_getCursorRefere

我有以下使用clang-CAPI的代码

#include <iostream>
#include <string>
#include <clang-c/Index.h>

CXChildVisitResult printVisitor(CXCursor cursor, CXCursor parent, CXClientData client_data) 
{       
    CXCursor cursor1 = clang_getCursorReferenced(cursor);

    CXType type = clang_getCursorType(cursor1);
    CXCursorKind kind = clang_getCursorKind(cursor1);
    CXString str = clang_getTypeSpelling(type);
    CXString str1 = clang_getCursorSpelling(cursor1);
    std::string cstr = clang_getCString(str);
    std::string cstr1 = clang_getCString(str1);

    if(type.kind != 0 && kind == CXCursorKind::CXCursor_FunctionDecl)
    {
        std::cout << "Declaration!\n" << "type is: " << cstr << std::endl;
        std::cout << "name is: " << cstr1 << std::endl;
    }

    return CXChildVisit_Recurse;
}

int main (int argc, char** argv)
{
 CXIndex index = clang_createIndex (
         false, // excludeDeclarationFromPCH
         true   // displayDiagnostics
 );
 CXTranslationUnit unit = clang_parseTranslationUnit (
         index,                           // CIdx
         "main1.cpp",                      // source_filename
         argv + 1 ,                        // command_line_args
         argc - 1 ,                        // num_command_line_args
         0,                                // unsave_files
         0,                                // num_unsaved_files
         CXTranslationUnit_None           // options
 );
 if (unit != 0 )
         std::cout << "Translation unit successfully created" << std::endl;
 else
         std::cout << "Translation unit was not created" << std::endl;

 CXCursor rootCursor = clang_getTranslationUnitCursor(unit);

    clang_visitChildren(rootCursor, printVisitor, NULL);


 clang_disposeTranslationUnit(unit);
 clang_disposeIndex(index);
}
当程序运行时,我看到以下内容

double getSum(double a, float b)
{
    return a + b;
}

int main(void)
{
    int a = 5;
    float b = 6;
    double c = a + b;
    return getSum(c, b);
}
    Translation unit successfully created
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: int ()
    name is: main
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: double (double, float)
    name is: getSum
    Declaration!
    type is: double (double, float)
    name is: getSum

当我在代码中只有一个声明时,为什么我会得到这么多的
getSum()

我从来没有使用过clang,但基于这一点,
CXCursorKind
似乎总是
declaration
,只要光标在函数中的某个地方(
main
我想可能会得到特殊处理)。这可以解释为什么您有多个报告声明
Sum

我的推测基于
CXCursorKind
的定义,发现:


当您使用
clang\u getCursorReferenced
时,您将获得在当前位置引用的
CXCursor
。例如,函数声明引用自身,并由相应的函数调用引用。在您的示例中,您将因此获得对函数声明(函数声明本身或函数调用)的每个引用的正匹配

现在,另一件事是,每个
CXCursor
表示AST的一部分,或者是一个叶子,或者是一个更复杂的带有子部分的部分。例如,在遍历AST时,您将依次找到以下游标:

  • 返回getSum(c,b)
  • getSum(c,b)
  • getSum
所有这些游标都引用
getSum
函数声明,我猜这些游标触发了对
getSum
的多个引用


您可以通过调用

您的答案来检查源代码的哪一部分对应于当前光标,以了解原因是使用clang_GetCursorReference;我不知道为什么,但当使用简单的CXCursor时,它是正确的:)谢谢!
enum CXCursorKind {
  /* Declarations */
  /**
   * \brief A declaration whose specific kind is not exposed via this
   * interface.
   *
   * Unexposed declarations have the same operations as any other kind
   * of declaration; one can extract their location information,
   * spelling, find their definitions, etc. However, the specific kind
   * of the declaration is not reported.
   */
...
}