Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

Example 4: Accessing inheritance information

In 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