Example 4: Accessing inheritance informationIn this example, we'll extend the custom predicate from Example 3 so that we can dump data members from the inheritance chain into the log. First, let's expand our test case to include inheritance: class CBar { private: int m_z; }; class CFoo : public CBar { ... }; Second, modify the checker definition to use the following pattern: <pattern> // FuncDef [ getName() = ‘init’] [ listAllClassInfo() ] </pattern> To see members from the inheritance chain, we must introduce the ability to traverse base classes: #include <stdio.h> #include <XPath_plugins.h> #include <ktcAPI.h> // Forward declarations void logClassInfo(ktc_semanticInfo_t); void logBaseClasses(ktc_semanticInfo_t); // Called by ktc_sema_forAllClassDeclarations, filter to variables void logger(ktc_semanticInfo_t mi) { if( ktc_sema_isVariable(mi) ) fprintf(stderr, "\tMember: %s\n", ktc_sema_getQualifiedName(mi)); } // Log information about the members of a specific class void logClassInfo(ktc_semanticInfo_t ci) { fprintf(stderr, "Class name: %s\n", ktc_sema_getIdentifier(ci)); ktc_sema_forAllClassDeclarations(ci, logger); logBaseClasses(ci); } // Traverse the inheritance hierarchy void logBaseClasses(ktc_semanticInfo_t ci) { int nr = ktc_sema_getNumberOfBaseInfo(ci); for( int i = 0; i < nr; i++ ) logClassInfo(ktc_sema_getBaseInfo(ci, i)); } // Custom predicate int listAllClassInfo(ktc_tree_t node) { ktc_semanticInfo_t si = ktc_getSemanticInfo(node); if( ktc_sema_isFunction(si) ) { ktc_semanticInfo_t ci = ktc_sema_getScope(si); if( ci && ktc_sema_isClass(ci) ) logClassInfo(ci); } return 1; } HOOKS_SET_START XPath_register_int_hook("listAllClassInfo", listAllClassInfo); HOOKS_SET_END Executing our test again will show the following information in the build log: Class name: CFoo Member: CFoo::m_x Member: CFoo::m_y Class name: CBar Member: CBar::m_z |