我无法使用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)我能够在myMethod和myFunction中检测到NTOHS的使用情况,并打印出有关这些节点的信息,但在MyClass::anotherMethod中检测不到。
Someone else ran into a similar problem, but it doesn't seem to have been answered.
这里的NTOHS只是linux/unix命令,用于将网络转换为主机命令。
如何使用libclang检测命名空间函数中的NTOHS?
发布于 2016-02-25 20:52:56
我怀疑这个问题可能与您的问题中没有出现的信息有关,并且可能取决于您使用的是哪个平台和版本的clang,以及您指的是哪个版本的NTOHS。
在OSX Yostemite上,NTOHS被定义为:
#define NTOHS(x) (x) = ntohs((__uint16_t)x) 为了编译你的样例,我不得不引入一个临时的。
在Ubuntu14.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/获得了这棵带有clang3.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_LITERALhttps://stackoverflow.com/questions/35579118
复制相似问题