C/C++ Analysis
This page documents the C and C++ related APIs in the CLDK library.
Use the table of contents to navigate classes, functions, and submodules exposed under the C/C++ analysis and schema packages.
Analysis#
C/C++ analysis utilities and workflows.
C analysis utilities.
Provides a high-level API to analyze C projects using a Clang-based analyzer and to query functions, macros, typedefs, structs/unions, enums, and globals.
CAnalysis
#
Source code in cldk/analysis/c/c_analysis.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
get_c_application()
#
Return the C application object.
Returns:
Name | Type | Description |
---|---|---|
CApplication |
CApplication
|
Application model. |
Examples:
>>> # Assuming CAnalysis was constructed
>>> isinstance(CAnalysis(project_dir=Path('.')).get_c_application(), CApplication)
True
Source code in cldk/analysis/c/c_analysis.py
get_imports()
#
Return all include/import statements in the project.
Raises:
Type | Description |
---|---|
NotImplementedError
|
This functionality is not implemented yet. |
Examples:
>>> from pathlib import Path
>>> CAnalysis(project_dir=Path('.')).get_imports()
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_variables(**kwargs)
#
Return all variables discovered across the project.
Raises:
Type | Description |
---|---|
NotImplementedError
|
This functionality is not implemented yet. |
Examples:
>>> from pathlib import Path
>>> CAnalysis(project_dir=Path('.')).get_variables()
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_application_view()
#
Return the application view of the C project.
Returns:
Name | Type | Description |
---|---|---|
CApplication |
CApplication
|
Application model summarizing translation units. |
Examples:
>>> from pathlib import Path
>>> ca = CAnalysis(project_dir=Path('.'))
>>> isinstance(ca.get_application_view(), CApplication)
True
Source code in cldk/analysis/c/c_analysis.py
get_symbol_table()
#
Return a symbol table view keyed by file path.
Raises:
Type | Description |
---|---|
NotImplementedError
|
This functionality is not implemented yet. |
Examples:
>>> from pathlib import Path
>>> CAnalysis(project_dir=Path('.')).get_symbol_table()
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_compilation_units()
#
Return all compilation units parsed from C sources.
Raises:
Type | Description |
---|---|
NotImplementedError
|
This functionality is not implemented yet. |
Examples:
>>> from pathlib import Path
>>> CAnalysis(project_dir=Path('.')).get_compilation_units()
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
is_parsable(source_code)
#
Check if the source code is parsable using Clang.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source_code
|
str
|
Source code to parse. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if parsable, False otherwise. |
Examples:
Source code in cldk/analysis/c/c_analysis.py
get_call_graph()
#
Return the call graph of the C code.
Returns:
Type | Description |
---|---|
DiGraph
|
networkx.DiGraph: Call graph. |
Examples:
Source code in cldk/analysis/c/c_analysis.py
get_call_graph_json()
#
Return the call graph serialized as JSON.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Call graph encoded as JSON. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Single-file mode unsupported. |
Examples:
Source code in cldk/analysis/c/c_analysis.py
get_callers(function)
#
Return callers of a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
CFunction
|
Target function. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Dict
|
Mapping of callers to call sites/details. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Not implemented yet. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_callers(CFunction(name='f', return_type='int', parameters=[], storage_class=None, is_inline=False, is_variadic=False, body='', comment='', call_sites=[], local_variables=[], start_line=1, end_line=1))
Traceback (most recent call last):
NotImplementedError: Generating all callers over a single file is not implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_callees(function)
#
Return callees of a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function
|
CFunction
|
Source function. |
required |
Returns:
Name | Type | Description |
---|---|---|
dict |
Dict
|
Callee details. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Not implemented yet. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_callees(CFunction(name='f', return_type='int', parameters=[], storage_class=None, is_inline=False, is_variadic=False, body='', comment='', call_sites=[], local_variables=[], start_line=1, end_line=1))
Traceback (most recent call last):
NotImplementedError: Generating all callees over a single file is not implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_functions()
#
Return all functions in the project.
Returns:
Type | Description |
---|---|
Dict[str, CFunction]
|
dict[str, CFunction]: Functions keyed by signature/name for a translation unit. |
Examples:
Source code in cldk/analysis/c/c_analysis.py
get_function(function_name, file_name)
#
Return a function object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
function_name
|
str
|
Function name. |
required |
file_name
|
str | None
|
Optional file name. |
required |
Returns:
Type | Description |
---|---|
CFunction | List[CFunction]
|
CFunction | list[CFunction]: Function object(s) matching the query. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_function('main', None)
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_C_file(file_name)
#
Return a C file path by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
C source file path. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Not implemented yet. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_C_file('hello.c')
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_C_compilation_unit(file_path)
#
Return the compilation unit for a C source file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path
|
str
|
Absolute path to a C source file. |
required |
Returns:
Name | Type | Description |
---|---|---|
CTranslationUnit |
CTranslationUnit
|
Compilation unit object. |
Examples:
>>> # Retrieve a compilation unit by path
>>> cu = CAnalysis(project_dir=Path('.')).get_C_compilation_unit('file.c')
>>> (cu is None) or hasattr(cu, 'functions')
True
Source code in cldk/analysis/c/c_analysis.py
get_functions_in_file(file_name)
#
Return all functions in a given file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CFunction]
|
list[CFunction]: Functions in the file. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Not implemented yet. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_functions_in_file('file.c')
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_macros()
#
Return all macros in the project.
Returns:
Type | Description |
---|---|
List[CMacro]
|
list[CMacro]: All macros. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Not implemented yet. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_macros()
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_macros_in_file(file_name)
#
Return all macros in the given file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CMacro] | None
|
list[CMacro] | None: Macros in the file, or None if not found. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
Not implemented yet. |
Examples:
>>> CAnalysis(project_dir=Path('.')).get_macros_in_file('file.c')
Traceback (most recent call last):
NotImplementedError: Support for this functionality has not been implemented yet.
Source code in cldk/analysis/c/c_analysis.py
get_includes(self)
#
Return all include statements across the project.
Returns:
Type | Description |
---|---|
List[str]
|
list[str]: All include statements. |
Source code in cldk/analysis/c/c_analysis.py
get_includes_in_file(self, file_name)
#
Return include statements in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[str] | None
|
list[str] | None: Includes in the file, or None if not found. |
Source code in cldk/analysis/c/c_analysis.py
get_macros(self)
#
Return all macro definitions across the project.
Returns:
Type | Description |
---|---|
List[CMacro]
|
list[CMacro]: Macro definitions. |
Source code in cldk/analysis/c/c_analysis.py
get_macros_in_file(self, file_name)
#
Return macro definitions in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CMacro] | None
|
list[CMacro] | None: Macros in the file, or None if not found. |
Source code in cldk/analysis/c/c_analysis.py
get_typedefs(self)
#
Return typedef declarations across the project.
Returns:
Type | Description |
---|---|
List[CTypedef]
|
list[CTypedef]: Typedef declarations. |
Source code in cldk/analysis/c/c_analysis.py
get_typedefs_in_file(self, file_name)
#
Return typedef declarations in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CTypedef] | None
|
list[CTypedef] | None: Typedefs in the file, or None if not found. |
Source code in cldk/analysis/c/c_analysis.py
get_structs(self)
#
Return struct/union declarations across the project.
Returns:
Type | Description |
---|---|
List[CStruct]
|
list[CStruct]: Struct/union declarations. |
Source code in cldk/analysis/c/c_analysis.py
get_structs_in_file(self, file_name)
#
Return struct/union declarations in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CStruct] | None
|
list[CStruct] | None: Structs in the file, or None if not found. |
Source code in cldk/analysis/c/c_analysis.py
get_enums(self)
#
Return enum declarations across the project.
Returns:
Type | Description |
---|---|
List[CEnum]
|
list[CEnum]: Enum declarations. |
Source code in cldk/analysis/c/c_analysis.py
get_enums_in_file(self, file_name)
#
Return enum declarations in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CEnum] | None
|
list[CEnum] | None: Enums in the file, or None if not found. |
Source code in cldk/analysis/c/c_analysis.py
get_globals(self, file_name)
#
Return global variable declarations in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
File name. |
required |
Returns:
Type | Description |
---|---|
List[CVariable] | None
|
list[CVariable] | None: Globals in the file, or None if not found. |
Source code in cldk/analysis/c/c_analysis.py
Schema#
Data models used by the C/C++ analyzers.
StorageClass
#
CVariable
#
Bases: BaseModel
Represents a variable declaration in C.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
The name of the variable |
type |
str
|
The type of the variable (including any type qualifiers) |
storage_class |
Optional[StorageClass]
|
The storage class specifier (if any) |
is_const |
bool
|
Whether the variable is const-qualified |
is_volatile |
bool
|
Whether the variable is volatile-qualified |
initializer |
str
|
Initial value expression, if any |
array_dimensions |
List[str]
|
Dimensions if this is an array variable |
is_pointer |
bool
|
Whether this is a pointer variable |
pointer_level |
int
|
Level of pointer indirection (e.g., 2 for char**) |
Source code in cldk/models/c/models.py
CFunctionPointer
#
Bases: BaseModel
Represents a function pointer type.
Attributes:
Name | Type | Description |
---|---|---|
return_type |
str
|
Return type of the function being pointed to |
parameter_types |
List[str]
|
Types of the parameters |
calling_convention |
Optional[str]
|
Calling convention if specified |
Source code in cldk/models/c/models.py
CMacro
#
Bases: BaseModel
Represents a C preprocessor macro.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the macro |
parameters |
List[str]
|
Parameters for function-like macros |
replacement |
str
|
Replacement text |
is_function_like |
bool
|
Whether this is a function-like macro |
start_line |
int
|
Starting line in source |
end_line |
int
|
Ending line in source |
Source code in cldk/models/c/models.py
CParameter
#
Bases: BaseModel
Represents a parameter in a function declaration.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Parameter name (may be empty in declarations) |
type |
str
|
Parameter type |
is_const |
bool
|
Whether parameter is const-qualified |
is_volatile |
bool
|
Whether parameter is volatile-qualified |
is_pointer |
bool
|
Whether parameter is a pointer |
pointer_level |
int
|
Level of pointer indirection |
array_dimensions |
List[str]
|
Array dimensions if parameter is array |
Source code in cldk/models/c/models.py
CCallSite
#
Bases: BaseModel
Represents a function call in C code.
Attributes:
Name | Type | Description |
---|---|---|
function_name |
str
|
Name of the called function |
argument_types |
List[str]
|
Types of the arguments |
is_indirect_call |
bool
|
Whether this is a call through function pointer |
is_macro_expansion |
bool
|
Whether this call is from macro expansion |
return_type |
str
|
Return type of the called function |
start_line |
int
|
Starting line of the call |
start_column |
int
|
Starting column of the call |
end_line |
int
|
Ending line of the call |
end_column |
int
|
Ending column of the call |
Source code in cldk/models/c/models.py
CFunction
#
Bases: BaseModel
Represents a C function.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Function name |
return_type |
str
|
Return type |
parameters |
List[CParameter]
|
Function parameters |
storage_class |
Optional[StorageClass]
|
Storage class if specified |
is_inline |
bool
|
Whether function is inline |
is_const |
bool
|
Whether function is const-qualified (C++) |
is_variadic |
bool
|
Whether function takes variable arguments |
body |
str
|
Function body code |
comment |
str
|
Associated comments/documentation |
referenced_types |
List[str]
|
Types referenced in function |
accessed_globals |
List[str]
|
Global variables accessed |
call_sites |
List[CCallSite]
|
Function calls made |
local_variables |
List[CVariable]
|
Local variable declarations |
macros_used |
List[str]
|
Macros used in function |
start_line |
int
|
Starting line in source |
end_line |
int
|
Ending line in source |
cyclomatic_complexity |
Optional[int]
|
Cyclomatic complexity if calculated |
Source code in cldk/models/c/models.py
CStruct
#
Bases: BaseModel
Represents a C struct or union.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the struct |
is_union |
bool
|
Whether this is a union |
members |
List[CVariable]
|
Member variables |
is_packed |
bool
|
Whether struct is packed |
alignment |
Optional[int]
|
Specified alignment if any |
comment |
str
|
Associated comments |
referenced_types |
List[str]
|
Types referenced in struct |
Source code in cldk/models/c/models.py
CEnum
#
Bases: BaseModel
Represents a C enum declaration.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the enum |
constants |
Dict[str, int]
|
Enum constants and their values |
comment |
str
|
Associated comments |
Source code in cldk/models/c/models.py
CTypedef
#
Bases: BaseModel
Represents a typedef declaration.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
New type name being defined |
underlying_type |
str
|
The actual type being aliased |
is_function_pointer |
bool
|
Whether this is a function pointer typedef |
function_pointer |
Optional[CFunctionPointer]
|
Details if this is a function pointer typedef |
Source code in cldk/models/c/models.py
CInclude
#
Bases: BaseModel
Represents a C include directive.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Name of the included file |
is_system |
bool
|
Whether this is a system include |
line_number |
int
|
Line number in source |
full_text |
str
|
Full text of the include directive |
Source code in cldk/models/c/models.py
CTranslationUnit
#
Bases: BaseModel
Represents a C source file.
Attributes:
Name | Type | Description |
---|---|---|
file_path |
str
|
Path to the source file |
includes |
List[str]
|
Header files included |
macros |
List[CMacro]
|
Macro definitions |
typedefs |
List[CTypedef]
|
Typedef declarations |
structs |
List[CStruct]
|
Struct/union declarations |
enums |
List[CEnum]
|
Enum declarations |
globals |
List[CVariable]
|
Global variable declarations |
functions |
Dict[str, CFunction]
|
Function declarations/definitions |
is_header |
bool
|
Whether this is a header file |
Source code in cldk/models/c/models.py
CFunctionDetail
#
Bases: BaseModel
Represents detailed information about a function.
Attributes:
Name | Type | Description |
---|---|---|
function_declaration |
str
|
Full function declaration |
file_path |
str
|
Path to the file containing the function |
function |
CFunction
|
Detailed function information |
Source code in cldk/models/c/models.py
CCallGraphEdge
#
Bases: BaseModel
Represents an edge in the call graph.
Attributes:
Name | Type | Description |
---|---|---|
source |
CFunctionDetail
|
Calling function |
target |
CFunctionDetail
|
Called function |
type |
str
|
Type of call relationship |
weight |
str
|
Edge weight/importance |
is_indirect |
bool
|
Whether this is through function pointer |
Source code in cldk/models/c/models.py
CApplication
#
Bases: BaseModel
Represents a complete C application.
Attributes:
Name | Type | Description |
---|---|---|
translation_units |
Dict[str, CTranslationUnit]
|
All source files |
call_graph |
List[CCallGraphEdge]
|
Function call relationships |