[cfe-dev] clang-c: unexpected invalid cursor location for MemberRefExpr in cast operations

Milian Wolff mail at milianw.de
Tue May 19 15:41:40 PDT 2015


Hello again,

for the following code, I get unexpected behavior of the clang-c API:

~~~~~~~~~~~~~~~~
struct Foo
{
    operator int()
    {
        return 1;
    }
};

struct Bar { Foo member; };

void castUse(Bar bar)
{
    static_cast<int>(bar.member);
}
~~~~~~~~~~~~~~~~~

Our debug visitor yields something like

~~~~~~~~~~~~~~~~~~
StructDecl (2) | type: "Foo" (105) | display: "Foo" | loc: test.cpp@[(1,1),
(7,2)] | isDecl | file: 0x7f056c00ab10 = test.cpp
  CXXConversion (26) | type: "int ()" (111) | display: "operator int()" | loc: 
test.cpp@[(3,5),(6,6)] | isDecl | file: 0x7f056c00ab10 = test.cpp
    CompoundStmt (202) | loc: test.cpp@[(4,5),(6,6)]  | file: 0x7f056c00ab10 = 
test.cpp
      ReturnStmt (214) | loc: test.cpp@[(5,9),(5,17)]  | file: 0x7f056c00ab10 
= test.cpp
        IntegerLiteral (106) | type: "int" (17) | loc: test.cpp@[(5,16),
(5,17)]  | file: 0x7f056c00ab10 = test.cpp
StructDecl (2) | type: "Bar" (105) | display: "Bar" | loc: test.cpp@[(9,1),
(9,27)] | isDecl | file: 0x7f056c00ab10 = test.cpp
  FieldDecl (6) | type: "Foo" (105) | display: "member" | loc: 
test.cpp@[(9,14),(9,24)] | isDecl | file: 0x7f056c00ab10 = test.cpp
    TypeRef (43) | type: "Foo" (105) | display: "struct Foo" | loc: 
test.cpp@[(9,14),(9,17)] | isUse | file: 0x7f056c00ab10 = test.cpp
FunctionDecl (8) | type: "void (Bar)" (111) | display: "castUse(Bar)" | loc: 
test.cpp@[(11,1),(14,2)] | isDecl | file: 0x7f056c00ab10 = test.cpp
  ParmDecl (10) | type: "Bar" (105) | display: "bar" | loc: test.cpp@[(11,14),
(11,21)] | isDecl | file: 0x7f056c00ab10 = test.cpp
    TypeRef (43) | type: "Bar" (105) | display: "struct Bar" | loc: 
test.cpp@[(11,14),(11,17)] | isUse | file: 0x7f056c00ab10 = test.cpp
  CompoundStmt (202) | loc: test.cpp@[(12,1),(14,2)]  | file: 0x7f056c00ab10 = 
test.cpp
    CXXStaticCastExpr (124) | type: "int" (17) | loc: test.cpp@[(13,5),
(13,33)]  | file: 0x7f056c00ab10 = test.cpp
      UnexposedExpr (100) | type: "int" (17) | display: "operator int" | loc: 
test.cpp@[(13,22),(13,32)]  | file: 0x7f056c00ab10 = test.cpp
        CallExpr (103) | type: "int" (17) | display: "operator int" | loc: 
test.cpp@[(13,22),(13,32)] | isUse | file: 0x7f056c00ab10 = test.cpp
          MemberRefExpr (102) | type: "<bound member function type>" (1) | 
display: "operator int" | loc: @[(13,22),(13,32)] | isUse | file: 0x0 = 
            MemberRefExpr (102) | type: "Foo" (105) | display: "member" | loc: 
test.cpp@[(13,22),(13,32)] | isUse | file: 0x7f056c00ab10 = test.cpp
              DeclRefExpr (101) | type: "Bar" (105) | display: "bar" | loc: 
test.cpp@[(13,22),(13,25)] | isUse | file: 0x7f056c00ab10 = test.cpp
~~~~~~~~~~~~~~~~~~

This is all good and fine, except for this line:

          MemberRefExpr (102) | type: "<bound member function type>" (1) | 
display: "operator int" | loc: @[(13,22),(13,32)] | isUse | file: 0x0 = 

here, we show the output of the cursor file obtained via
~~~~~~~~~~~~~~~~~~
    auto location = clang_getCursorLocation(cursor);
    CXFile file;
    uint line;
    uint column;
    clang_getFileLocation(location, &file, &line, &column, 0);
~~~~~~~~~~~~~~~~~~

For some reason, this returns a nullptr on the CXFile. Is this a simple bug, 
or is there a reason for this and does it need to be fixed? It is an issue for 
us, as we usually visit the AST and filter out any cursor not belonging to the 
file we are currently interested in. As such, we currently filter out 
everything below and including this MemberRefExpr node.

Thanks, any input appreciated
-- 
Milian Wolff
mail at milianw.de
http://milianw.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150520/c78ffdf3/attachment.sig>


More information about the cfe-dev mailing list