API¶
To parse a header file and retrieve the resulting data structure, you’ll
want to use the CppHeader
object:
import CppHeaderParser
header = CppHeaderParser.CppHeader("path/to/header.h")
Below is some documentation of the various object types that CppHeaderParser will generated after parsing a header file. The documentation is not yet currently comprehensive, so the best way to see what gets generated is by printing out the JSON representation:
python -m CppHeaderParser.tojson /path/to/header.h
Warning
CppHeaderParser is not safe to use from multiple threads
Note
CppHeaderParser only does some very minimal interpretation of preprocessor directives – and we’re looking at removing some of that from this library. If you need anything complex, you should preprocess the code yourself. You can use the excellent pure python preprocessor pcpp, or the preprocessing facilities provided by your favorite compiler.
CppHeaderParser¶
-
class
CppHeaderParser.CppHeaderParser.
CppBaseDecl
(default_access)[source]¶ Bases:
dict
Dictionary that contains the following
access
- Anything in supportedAccessSpecifierclass
- Name of the type, along with template specializationsdecl_name
- Name of the type onlydecl_params
- Only present if a template specialization (Foo<int>). Is a list ofCppTemplateParam
.decltype
- True/False indicates a decltype, the contents are indecl_name
virtual
- True/False indicates virtual inheritance...
- True/False indicates a parameter pack
-
class
CppHeaderParser.CppHeaderParser.
CppClass
(nameStack, curTemplate, doxygen, location, defaultAccess)[source]¶ Bases:
dict
Dictionary that contains at least the following keys:
name
- Name of the classdoxygen
- Doxygen comments associated with the class if they existinherits
- List of Classes that this one inherits. Values areCppBaseDecl
methods
- Dictionary where keys are from supportedAccessSpecifier and values are a lists ofCppMethod
namespace
- Namespace of classproperties
- Dictionary where keys are from supportedAccessSpecifier and values are lists ofCppVariable
enums
- Dictionary where keys are from supportedAccessSpecifier and values are lists ofCppEnum
nested_classes
- Classes and structs defined within this classfinal
- True if finalabstract
- True if abstractusing
- Using directives in this class scope: key is name for lookup, value isCppVariable
parent
- If not None, the class that this class is nested in
An example of how this could look is as follows:
{ 'name': "" 'inherits':[] 'methods': { 'public':[], 'protected':[], 'private':[] }, 'properties': { 'public':[], 'protected':[], 'private':[] }, 'enums': { 'public':[], 'protected':[], 'private':[] } }
-
class
CppHeaderParser.CppHeaderParser.
CppEnum
(name, doxygen, location)[source]¶ Bases:
CppHeaderParser.CppHeaderParser._CppEnum
Contains the following keys:
name
- Name of the enum (ex. “ItemState”)namespace
- Namespace containing the enumisclass
- True if created via ‘enum class’ or ‘enum struct’values
- List of values. The values are a dictionary with the following key/values:name
- name of the key (ex. “PARSING_HEADER”),value
- Specified value of the enum, this key will only exist if a value for a given enum value was defined
-
class
CppHeaderParser.CppHeaderParser.
CppHeader
(headerFileName, argType='file', encoding=None, **kwargs)[source]¶ Bases:
CppHeaderParser.CppHeaderParser._CppHeader
Parsed C++ class header
-
IGNORE_NAMES
= ['__extension__']¶
-
classes
= None¶ Dictionary of classes found in the header file. The key is the name of the class, value is
CppClass
-
defines
= None¶ List of #define directives found
-
defines_detail
= None¶ List of #define directives found, with location information
-
headerFileNames
= None¶ Filenames encountered in #line directives while parsing
-
includes
= None¶ List of #include directives found
-
includes_detail
= None¶ List of #include directives found with location information
-
nameSpaces
= None¶ Namespaces in this header
-
pragmas
= None¶ List of #pragma directives found as strings
-
pragmas_detail
= None¶ List of pragmas with location information
-
using
= None¶ Using directives in this header outside of class scope: key is full name for lookup, value is
CppVariable
-
variables
= None¶ List of variables in this header as
CppVariable
-
-
class
CppHeaderParser.CppHeaderParser.
CppMethod
(nameStack, curClass, methinfo, curTemplate, doxygen, location)[source]¶ Bases:
CppHeaderParser.CppHeaderParser._CppMethod
Dictionary that contains at least the following keys:
rtnType
- Return type of the method (ex. “int”)name
- Name of the methoddoxygen
- Doxygen comments associated with the method if they existparameters
- List ofCppVariable
parent
- If not None, the class this method belongs to
-
class
CppHeaderParser.CppHeaderParser.
CppTemplateParam
[source]¶ Bases:
dict
Dictionary that contains the following:
decltype
- If True, this is a decltypeparam
- Parameter value or typenameparams
- Only present if a template specialization. Is a list ofCppTemplateParam
...
- If True, indicates a parameter pack
-
class
CppHeaderParser.CppHeaderParser.
CppUnion
(nameStack, doxygen, location)[source]¶ Bases:
CppHeaderParser.CppHeaderParser.CppClass
Dictionary that contains at least the following keys:
name
- Name of the uniondoxygen
- Doxygen comments associated with the union if they existmembers
- List of members of the union
-
class
CppHeaderParser.CppHeaderParser.
CppVariable
(nameStack, doxygen, location, **kwargs)[source]¶ Bases:
CppHeaderParser.CppHeaderParser._CppVariable
Dictionary that contains at least the following keys:
type
- Type for the variable (ex. “const string &”)name
- Name of the variable (ex. “numItems”)namespace
- Namespacedesc
- If a method/function parameter, doxygen description for this parameter (optional)doxygen
- If a normal property/variable, doxygen description for thisdefault
- Default value of the variable, this key will only exist if there is a default valueextern
- True if its an extern, False if notparent
- If not None, either the class this is a property of, or the method this variable is a parameter in
-
Vars
= []¶
-
class
CppHeaderParser.CppHeaderParser.
TagStr
[source]¶ Bases:
str
Wrapper for a string that allows us to store the line number associated with it
-
CppHeaderParser.CppHeaderParser.
ignoreSymbols
= ['Q_OBJECT']¶ Symbols to ignore, usually special macros