0

I am using libclang to parse .cpp and get all function calls in the file. However , the only returned function calls are the one defined at the parsed .h / .cpp file not all the ones from the include files.

for example I am parsing example.cpp which include

#include "a.h" 
#include "example.h"

All calls for functions defined/declared in example.h are detected but it skips all function calls which are defined in a.h. How can I detect both of them ?

In a.h

foo ();

In example.hpp

boo();

In example.cpp

foo();
boo();

Only boo() is detected as function call. It does not check for function call recursivly in all headers. How can I force this check ?

I am using this function to return function calls

CXChildVisitResult functionVisitor(CXCursor cursor){              
    CXCursorKind cursorKind     = clang_getCursorKind(cursor);
    CXString     cursorSpelling = clang_getCursorSpelling(cursor);

    if (cursorKind == CXCursor_CallExpr) {
        CXString cursorUsr = clang_getCursorUSR(cursor);
        print_function_prototype(cursor);
        clang_disposeString(cursorUsr);
    }

    return CXChildVisit_Continue;
}

Hazem Abaza
  • 331
  • 4
  • 21
  • 3
    Your very first check says "if this is not the main file, skip this whole subtree of the AST." – Botje Oct 19 '20 at 11:49
  • I want the function calls that only happen in the main file not the other header :( How can I do both ? – Hazem Abaza Oct 19 '20 at 11:59
  • 1
    Your question is complaining that "function calls which are defined in example.h are detected but it skips all function calls which are defined in a.h." if you remove that check you get ALL function calls. But that is also not what you want, apparently. What DO you want then? – Botje Oct 19 '20 at 12:16
  • I edited the question and made it more clear . – Hazem Abaza Oct 20 '20 at 07:19
  • A header file typically does not contain function calls. Did you mean function _definitions_ or _declarations_? Please provide the output of clang's AST dump for your example.cpp file. – Botje Oct 20 '20 at 08:16
  • My question is clear. "All calls for functions defined/declared in example.h are detected but it skips all function calls which are defined in a.h" I did not say there is function call in the header. I said function calls for functions defined in certain headers are not mentioned. – Hazem Abaza Oct 20 '20 at 08:22
  • Okay, then show an AST dump and proof that your visitor actually gets to that ast node. Do you understand the difference between returning CXChildVisit_Recurse and returning CXChildVisit_Continue? – Botje Oct 20 '20 at 08:25
  • if ((kind == CXCursorKind::CXCursor_FunctionDecl || kind == CXCursorKind::CXCursor_CXXMethod || kind == CXCursorKind::CXCursor_FunctionTemplate ||kind == CXCursorKind::CXCursor_Constructor)) { print_function_prototype(cursor); } Returns foo() and boo() Which means that the header file is already checked – Hazem Abaza Oct 20 '20 at 09:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223343/discussion-between-hazem-abaza-and-botje). – Hazem Abaza Oct 20 '20 at 09:16

0 Answers0