【问题标题】:Parsing namespace functions with libclang使用 libclang 解析命名空间函数
【发布时间】:2016-02-23 13:39:59
【问题描述】:

我无法使用 libclang 解析 C++ 中命名空间函数的主体。

我在这样的命名空间中有一个类:

namespace outer {
    namespace inner {
        class MyClass {
            public:
                short myMethod(){
                    return NTOHS(10);
                }

                short anotherMethod();
        };

        short MyClass::anotherMethod(){
            return NTOHS(11);
        }

        short myFunction(){
            return NTOHS(12);
        }
    }
}

对libclang使用python包装器,我可以通过递归找到每个节点:

def find_node(node):
    print node  # Just print stuff about the node (spelling, location, etc.)
    for child in node.get_children():
        find_node(child)

我能够在 myMethodmyFunction 中检测到 NTOHS 的使用情况并打印有关这些节点的信息,但无法在 MyClass::anotherMethod 中检测到它。

Someone else ran into a similar problem, but it doesn't seem to have been answered.

这里的 NTOHS 只是用于将网络转换为主机顺序的 linux/unix 命令。

如何使用 libclang 检测命名空间函数中的 NTOHS?

【问题讨论】:

  • 你确定你是在anotherMethod定义节点而不是之前出现的声明
  • @KurtStutsman 我很确定我可以找到声明,但找不到定义。

标签: python c++ libclang


【解决方案1】:

我怀疑此问题可能与您的问题中不存在的信息有关,并且可能取决于您使用的 clang 的哪个平台和版本,以及您指的是哪个版本的 NTOHS

在 OSX Yostemite 上,NTOHS 定义为:

#define NTOHS(x)    (x) = ntohs((__uint16_t)x) 

为了编译你的示例,我不得不引入一个临时的。

在 Ubuntu 14.04 上,NTOHS 不在 /usr/include 中,但 ntohs 是,并且被定义为(类似于):

#define ntohs(x)        __bswap_16 (x)

要编译您的示例,我必须切换到 ntohs

为了完整起见,在 Ubuntu 14.04 上,我使用了这个示例:

#include <netinet/in.h>

namespace outer {
    namespace inner {
        class MyClass {
            public:
                short myMethod(){
                    return ntohs(10);
                }

                short anotherMethod();
        };

        short MyClass::anotherMethod(){
            return ntohs(11);
        }

        short myFunction(){
            return ntohs(12);
        }
    }
}

并从http://llvm.org/apt/trusty/ 获得了这棵带有 clang 3.7 的树(在我为 libclang 设置了包含路径之后)

TRANSLATION_UNIT sample.cpp
  +--NAMESPACE outer
     +--NAMESPACE inner
        +--CLASS_DECL MyClass
        |  +--CXX_ACCESS_SPEC_DECL
        |  +--CXX_METHOD myMethod
        |  |  +--COMPOUND_STMT
        |  |     +--RETURN_STMT
        |  |        +--UNEXPOSED_EXPR ntohs
        |  |           +--CALL_EXPR ntohs
        |  |              +--UNEXPOSED_EXPR ntohs
        |  |              |  +--DECL_REF_EXPR ntohs
        |  |              +--UNEXPOSED_EXPR
        |  |                 +--INTEGER_LITERAL
        |  +--CXX_METHOD anotherMethod
        +--CXX_METHOD anotherMethod
        |  +--TYPE_REF class outer::inner::MyClass
        |  +--COMPOUND_STMT
        |     +--RETURN_STMT
        |        +--UNEXPOSED_EXPR ntohs
        |           +--CALL_EXPR ntohs
        |              +--UNEXPOSED_EXPR ntohs
        |              |  +--DECL_REF_EXPR ntohs
        |              +--UNEXPOSED_EXPR
        |                 +--INTEGER_LITERAL
        +--FUNCTION_DECL myFunction
           +--COMPOUND_STMT
              +--RETURN_STMT
                 +--UNEXPOSED_EXPR ntohs
                    +--CALL_EXPR ntohs
                       +--UNEXPOSED_EXPR ntohs
                       |  +--DECL_REF_EXPR ntohs
                       +--UNEXPOSED_EXPR
                          +--INTEGER_LITERAL

【讨论】:

    猜你喜欢
    • 2021-05-03
    • 2014-01-10
    • 2018-07-15
    • 2012-06-11
    • 2015-09-08
    • 2011-09-20
    • 2012-10-14
    • 2010-11-04
    相关资源
    最近更新 更多