0

I am unable to parse the body of a namespace function in c++ with libclang.

I have a class in a namespace like so:

namespace outer {
    namespace inner {
        class MyClass {
            public:
                short myMethod(){
                    return NTOHS(10);
                }

                short anotherMethod();
        };

        short MyClass::anotherMethod(){
            return NTOHS(11);
        }

        short myFunction(){
            return NTOHS(12);
        }
    }
}

Using a python wrapper for libclang, I can find each node through recursion:

def find_node(node):
    print node  # Just print stuff about the node (spelling, location, etc.)
    for child in node.get_children():
        find_node(child)

I am able to detect the usage of NTOHS in myMethod and myFunction and print information about those nodes, but am unable to detect it in MyClass::anotherMethod.

Someone else ran into a similar problem, but it doesn't seem to have been answered.

NTOHS here is just the linux/unix command for converting network to host order.

How can I use libclang to detect NTOHS in the namespace function?

BrockLee
  • 931
  • 2
  • 9
  • 24

1 Answers1

0

I suspect that this issue may relate to information not present in your question and that it may be dependent on which platform and version of clang you're using, and to which version of NTOHS you're referring.

On OSX Yostemite, NTOHS is defined as:

#define NTOHS(x)    (x) = ntohs((__uint16_t)x) 

To even get your sample to compile I had to introduce a temporary.

On Ubuntu 14.04, NTOHS isn't in /usr/include, but ntohs is, and is defined as (something like):

#define ntohs(x)        __bswap_16 (x)

To get your sample to compile I had to switch to ntohs.

For completeness, on Ubuntu 14.04, I used this sample:

#include <netinet/in.h>

namespace outer {
    namespace inner {
        class MyClass {
            public:
                short myMethod(){
                    return ntohs(10);
                }

                short anotherMethod();
        };

        short MyClass::anotherMethod(){
            return ntohs(11);
        }

        short myFunction(){
            return ntohs(12);
        }
    }
}

And got this tree with clang 3.7 from http://llvm.org/apt/trusty/ (after I'd set my include path for libclang)

TRANSLATION_UNIT sample.cpp
  +--NAMESPACE outer
     +--NAMESPACE inner
        +--CLASS_DECL MyClass
        |  +--CXX_ACCESS_SPEC_DECL
        |  +--CXX_METHOD myMethod
        |  |  +--COMPOUND_STMT
        |  |     +--RETURN_STMT
        |  |        +--UNEXPOSED_EXPR ntohs
        |  |           +--CALL_EXPR ntohs
        |  |              +--UNEXPOSED_EXPR ntohs
        |  |              |  +--DECL_REF_EXPR ntohs
        |  |              +--UNEXPOSED_EXPR
        |  |                 +--INTEGER_LITERAL
        |  +--CXX_METHOD anotherMethod
        +--CXX_METHOD anotherMethod
        |  +--TYPE_REF class outer::inner::MyClass
        |  +--COMPOUND_STMT
        |     +--RETURN_STMT
        |        +--UNEXPOSED_EXPR ntohs
        |           +--CALL_EXPR ntohs
        |              +--UNEXPOSED_EXPR ntohs
        |              |  +--DECL_REF_EXPR ntohs
        |              +--UNEXPOSED_EXPR
        |                 +--INTEGER_LITERAL
        +--FUNCTION_DECL myFunction
           +--COMPOUND_STMT
              +--RETURN_STMT
                 +--UNEXPOSED_EXPR ntohs
                    +--CALL_EXPR ntohs
                       +--UNEXPOSED_EXPR ntohs
                       |  +--DECL_REF_EXPR ntohs
                       +--UNEXPOSED_EXPR
                          +--INTEGER_LITERAL
Andrew Walker
  • 40,984
  • 8
  • 62
  • 84