Documentation
¶
Overview ¶
Package libclang is a minimal binding of libclang, clang's C interface, loaded at run time with purego. It exposes what code generators need to read declarations: cursors, types, record layouts, comments and diagnostics.
A parse sees what the clang driver would with the same arguments, but the headers it finds are those of the libclang it loaded: clang's resource directory beside the library, for stddef.h and, in CUDA mode, the CUDA wrapper headers. Function bodies are skipped.
Index ¶
- type Cursor
- func (c Cursor) Arguments() []Cursor
- func (c Cursor) Children() []Cursor
- func (c Cursor) Definition() Cursor
- func (c Cursor) EnumIntegerType() Type
- func (c Cursor) EnumUnsignedValue() uint64
- func (c Cursor) EnumValue() int64
- func (c Cursor) Evaluate() (any, bool)
- func (c Cursor) IsBitField() bool
- func (c Cursor) IsMacroFunctionLike() bool
- func (c Cursor) IsNull() bool
- func (c Cursor) IsScoped() bool
- func (c Cursor) Kind() CursorKind
- func (c Cursor) Location() (file string, line, col int)
- func (c Cursor) Mangling() string
- func (c Cursor) OffsetOfField() int64
- func (c Cursor) RawComment() string
- func (c Cursor) ResultType() Type
- func (c Cursor) Spelling() string
- func (c Cursor) Type() Type
- func (c Cursor) TypedefUnderlyingType() Type
- type CursorKind
- type Diagnostic
- type Library
- type Severity
- type TranslationUnit
- type Type
- type TypeKind
- type UnsavedFile
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
A Cursor is a node of the syntax tree.
func (Cursor) Definition ¶
Definition returns the definition of the declared entity, or a null cursor if the translation unit has none.
func (Cursor) EnumIntegerType ¶
EnumIntegerType returns the integer type underlying an enum declaration.
func (Cursor) EnumUnsignedValue ¶
EnumUnsignedValue returns an unsigned enum constant's value.
func (Cursor) Evaluate ¶
Evaluate returns the value of c, an expression or a variable with an initializer, if it is a constant: an int64, a uint64 if it has an unsigned type, or a float64.
func (Cursor) IsBitField ¶
IsBitField reports whether c is a bit-field.
func (Cursor) IsMacroFunctionLike ¶
IsMacroFunctionLike reports whether a macro definition takes arguments.
func (Cursor) OffsetOfField ¶
OffsetOfField returns the offset in bits of a field in its record, or a negative value if it has none.
func (Cursor) RawComment ¶
RawComment returns the documentation comment attached to the declaration, with its comment markers.
func (Cursor) ResultType ¶
ResultType returns the result type of a function declaration.
func (Cursor) TypedefUnderlyingType ¶
TypedefUnderlyingType returns the type a typedef names.
type CursorKind ¶
type CursorKind int32
A CursorKind is the kind of a Cursor, a value of CXCursorKind.
const ( StructDecl CursorKind = 2 UnionDecl CursorKind = 3 ClassDecl CursorKind = 4 EnumDecl CursorKind = 5 FieldDecl CursorKind = 6 EnumConstantDecl CursorKind = 7 FunctionDecl CursorKind = 8 VarDecl CursorKind = 9 ParmDecl CursorKind = 10 TypedefDecl CursorKind = 20 FunctionTemplate CursorKind = 30 Namespace CursorKind = 22 LinkageSpec CursorKind = 23 TypeAliasDecl CursorKind = 36 MacroDefinition CursorKind = 501 CUDAConstantAttr CursorKind = 412 CUDADeviceAttr CursorKind = 413 CUDAGlobalAttr CursorKind = 414 )
type Diagnostic ¶
type Diagnostic struct {
Severity Severity
Text string // formatted as clang prints it, with its location
}
A Diagnostic is a message clang produced while parsing.
type Library ¶
type Library struct {
// contains filtered or unexported fields
}
A Library is a loaded libclang.
func Open ¶
Open loads the libclang at path. An empty path searches the libraries of llvm-config and of clang on $PATH, then the LLVM installations in /usr/lib, newest first, and prefers one with a resource directory, since without one clang cannot find even stddef.h.
func (*Library) DeviceParseArgs ¶
DeviceParseArgs returns the arguments a device-only CUDA parse needs with this library, beyond the caller's own: on libclang 18 and earlier, names for the AArch64 SVE types as incomplete structs.
A device-only parse reads the host's system headers with the host's macros. On aarch64, glibc's bits/math-vector.h (2.39 and later), which math.h includes, checks only for clang 11 or later before declaring SVE vector math functions on __SVFloat32_t, __SVFloat64_t and __SVBool_t. Clang 19 declares those types when either the target or the host (aux) target has them; clang 18 and earlier only when the target does, and for a device parse the target is NVPTX. Device code cannot call the host functions, so an incomplete type is enough. These are macros, not an -include, because clang includes the CUDA runtime wrapper, and with it math.h, before any -include on the command line.
func (*Library) Parse ¶
func (l *Library) Parse(file string, args []string, unsaved ...UnsavedFile) (*TranslationUnit, error)
Parse parses file with the clang command-line arguments args. The unsaved files shadow or add to the files on disk. Parse fails only if clang produces no translation unit; a parse with errors succeeds and reports them in TranslationUnit.Diagnostics.
func (*Library) ResourceDir ¶
ResourceDir returns clang's resource directory beside the library, which holds the headers clang supplies, or "" if there is none. Parsing needs it passed as -resource-dir.
type TranslationUnit ¶
type TranslationUnit struct {
// contains filtered or unexported fields
}
A TranslationUnit is a parsed source file.
func (*TranslationUnit) Close ¶
func (t *TranslationUnit) Close()
Close frees the translation unit. Its cursors and types are invalid afterward.
func (*TranslationUnit) Cursor ¶
func (t *TranslationUnit) Cursor() Cursor
Cursor returns the cursor of the translation unit itself.
func (*TranslationUnit) Diagnostics ¶
func (t *TranslationUnit) Diagnostics() []Diagnostic
Diagnostics returns the diagnostics of the parse.
func (*TranslationUnit) Files ¶
func (t *TranslationUnit) Files() []string
Files returns the names of the files the translation unit read: the main file and every file it included, directly or not.
type Type ¶
type Type struct {
// contains filtered or unexported fields
}
A Type is a C or C++ type.
func (Type) Align ¶
Align returns the alignment of the type in bytes, or a negative libclang error code.
func (Type) Declaration ¶
Declaration returns the declaration of a record, enum or typedef type.
type TypeKind ¶
type TypeKind int32
A TypeKind is the kind of a Type, a value of CXTypeKind.
const ( Invalid TypeKind = 0 Void TypeKind = 2 Bool TypeKind = 3 CharU TypeKind = 4 UChar TypeKind = 5 UShort TypeKind = 8 UInt TypeKind = 9 ULong TypeKind = 10 ULongLong TypeKind = 11 CharS TypeKind = 13 SChar TypeKind = 14 Short TypeKind = 16 Int TypeKind = 17 Long TypeKind = 18 LongLong TypeKind = 19 Float TypeKind = 21 Double TypeKind = 22 Half TypeKind = 31 Float16 TypeKind = 32 BFloat16 TypeKind = 39 Pointer TypeKind = 101 LValueReference TypeKind = 103 Record TypeKind = 105 Enum TypeKind = 106 Typedef TypeKind = 107 FunctionProto TypeKind = 111 ConstantArray TypeKind = 112 IncompleteArray TypeKind = 114 Elaborated TypeKind = 119 )
type UnsavedFile ¶
An UnsavedFile is a file that parsing reads from memory rather than disk.