C++ libclang: Retrieving cursor from CXSourceLocation returning wrong cursor?

时光毁灭记忆、已成空白 提交于 2019-12-04 09:51:15

Either I'm missing your point entirely, or you are using clang_equalCursors the wrong way: when both cursors are equals, clang_equalCursors returns a non-zero value. Which means I think you're testing cursors inequalities instead of equalities.

Now, let me try to explain why certain cursors apparently behave differently than others. Each cursor has only one source location. However, there might be several cursors at the same source location. Think for example about the following line:

CXIndex index = clang_createIndex(0, 0);
//      ^

There should be at least two cursors at the marked position above:

  1. VarDecl: index
  2. DeclRefExpr: index = clang_createIndex(0,0)

When you convert the source location back to a cursor, clang_getCursor gives you the most specific one (the variable declaration in this case). I suspect this is what happens to you in this case: getCursor(loc) only gives you back the cursor that you are visiting if it is the most specific at its location.

Try printing the physical source location of each cursor (using for example clang_getCursorExtent and clang_getExpansionLocation) to understand what happens.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!