Get operator type for CXCursor_BinaryOperator in libclang

  abstract-syntax-tree, c++, clang, libclang

i have the same problem as Get operator type for CXCursor_BinaryOperator.

with the solution in this post as mentioned: only works for trivial expressions. For 2 + 2 * 2 this will confuse * by finding + first, since the extent covers the whole expression, including subexpressions, and not just the operator

so i changed the code as below:

CXToken *tokens;
  unsigned numTokens;
  CXSourceRange range = clang_getCursorExtent(cursor);
  clang_tokenize(tu, range, &tokens, &numTokens);
  for(unsigned i=0; i<numTokens; i++) {
    CXString s = clang_getTokenSpelling(tu, tokens[i]);
    const char* str = clang_getCString(s);
    CXString s = clang_getTokenSpelling(tu, tokens[i]);
     const char* str = clang_getCString(s);
    CXSourceLocation token_location = clang_getTokenLocation(tu, tokens[i]);
     CXFile file_token;
    unsigned line_token, column_token, offset_token;
     clang_getFileLocation(token_location, &file_token, &line_token, &column_token, &offset_token);

     CXSourceLocation cursor_location = clang_getCursorLocation(cursor);
    CXFile file_cursor;
     unsigned line_cursor, column_cursor, offset_cursor;
  clang_getSpellingLocation(cursor_location, &file_cursor, &line_cursor, &column_cursor, &offset_cursor);
     if( (strcmp(str, "+") == 0) && (line_cursor==line_token) && (column_cursor==column_token) && (offset_cursor==offset_token)) {
      /* found */
    }
    clang_disposeString(s);
  }
  clang_disposeTokens(tu, tokens, numTokens);

to check the locations to resolve the problem. but this time no operator is found. what’s the problem? what should i do?

Source: Windows Questions C++

LEAVE A COMMENT