[cfe-dev] How to get symbol's info. when the symbol is in macro instantiation

jiyang zhao zjyzhaojiyang at yahoo.com.cn
Tue Jan 25 19:08:41 PST 2011


Thanks for your explanation.

But I can't get the result you said, when i run my test programe on the input, i get totally different result list below,  could you kindly check my code please? 
//run( cursor is on previous)
./clang_test /home/lucency/Study/source/lua/src/lcode.c 45 21 -I/home/lucency/Study/source/lua/src

//Result begin
Display: []
Kind [200] - [UnexposedStmt].
LK [0].
USR: [].
Can't find Cursor's reference.
//Result end

//code begin
#include "clang-c/Index.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int displayCursorInfo(CXCursor Cursor)
{    
    CXString String = clang_getCursorDisplayName(Cursor);
    printf("Display: [%s]\n", clang_getCString(String));
    clang_disposeString(String);

    CXCursorKind Kind = clang_getCursorKind(Cursor);
    String = clang_getCursorKindSpelling(Kind);
    printf("Kind [%d] - [%s].\n", Kind, clang_getCString(String));
    clang_disposeString(String);

    CXLinkageKind LK = clang_getCursorLinkage(Cursor);
    printf("LK [%d].\n", LK);

    CXString str2 = clang_getCursorUSR(Cursor);
    printf("USR: [%s].\n", clang_getCString(str2));
    clang_disposeString(str2);

    return 0;
}
int main(int argc, char** argv)
{
    CXTranslationUnit TU;
    CXIndex Index = clang_createIndex(0,0);

    //clang_test file line column -I..
    int idx = 1;
    char* szFileName = argv[idx++];
    int nLine = atoi(argv[idx++]);
    int nColumn = atoi(argv[idx++]);
    int nArgs = argc - idx;
    char** szArgs = &argv[idx];

    TU = clang_parseTranslationUnit(Index, szFileName, szArgs, nArgs, 0, 0, CXTranslationUnit_DetailedPreprocessingRecord);

    if( !TU )
    {
        printf("File has some errors.\n");

        clang_disposeIndex(Index);

        return -1;
    }

    CXFile File = clang_getFile(TU, szFileName );
    if( File == NULL )
    {
        printf("Can't get CXFile.\n");

        clang_disposeTranslationUnit(TU);
        clang_disposeIndex(Index);

        return -1;
    }
    CXSourceLocation Location = clang_getLocation(TU, File, nLine, nColumn);

    CXCursor Cursor = clang_getCursor(TU, Location);

    displayCursorInfo(Cursor);

    CXCursor iDefCursor;
    if( clang_getCursorKind(Cursor) == CXCursor_MacroDefinition )
        iDefCursor = Cursor;
    else
        iDefCursor = clang_getCursorReferenced(Cursor);
    if( clang_equalCursors(iDefCursor, clang_getNullCursor() ) )
    {
        printf("Can't find Cursor's reference.\n");

        clang_disposeTranslationUnit(TU);
        clang_disposeIndex(Index);

        return -1;
    }

    displayCursorInfo(iDefCursor);

    clang_disposeTranslationUnit(TU);
    clang_disposeIndex(Index);

    return 0;
}
//code ends

// /Input file(only important piece, the most left column in lcode.c is line number)
//lopcodes.h
... ...
#define SETARG_B(i,b)    ((i) = (((i)&MASK0(SIZE_B,POS_B)) | \
        ((cast(Instruction, b)<<POS_B)&MASK1(SIZE_B,POS_B))))
... ...

//lcode.c 
... ...
35 void luaK_nil (FuncState *fs, int from, int n) {
36   Instruction *previous;
37  if (fs->pc > fs->lasttarget) {  /* no jumps to current position? */
38    if (fs->pc == 0)  /* function start? */
39      return;  /* positions are already clean */
40   if (GET_OPCODE(*(previous = &fs->f->code[fs->pc-1])) == OP_LOADNIL) {
41      int pfrom = GETARG_A(*previous);
42      int pto = GETARG_B(*previous);
43      if (pfrom <= from && from <= pto+1) {  /* can connect both? */
44        if (from+n-1 > pto)
45          SETARG_B(*previous, from+n-1);
46        return;
47      }
48    }
49  }
50  luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0);  /* else no optimization */
51}
... ...

--- 11年1月25日,周二, Douglas Gregor <dgregor at apple.com> 写道:

发件人: Douglas Gregor <dgregor at apple.com>
主题: Re: [cfe-dev] How to get symbol's info. when the symbol is in macro instantiation
收件人: "jiyang zhao" <zjyzhaojiyang at yahoo.com.cn>
抄送: cfe-dev at cs.uiuc.edu
日期: 2011年1月25日,周二,下午11:53


On Jan 25, 2011, at 12:30 AM, jiyang zhao wrote:

Hi list
I meet a problem about macro when using libclang, when i want to get a viriable's infomation which is in a macro instantiation, for example, its linkage kind, i get 200(UnexposedStmt), is this the expected result? If so, how can i get the exact information of the symbol(e.g. previous in following code, SETARG_B is macro)?

//example
SETARG_B(*previous, from+n-1);

Thanks

This reference to previous will show up as a CXCursor_DeclRefExpr cursor. You can then use clang_getCursorReferenced() to find the declaration that the cursor points to.
	- Doug




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110126/50e2bdd9/attachment.html>


More information about the cfe-dev mailing list