libclang

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

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) Arguments

func (c Cursor) Arguments() []Cursor

Arguments returns the parameters of a function declaration.

func (Cursor) Children

func (c Cursor) Children() []Cursor

Children returns the children of c.

func (Cursor) Definition

func (c Cursor) Definition() Cursor

Definition returns the definition of the declared entity, or a null cursor if the translation unit has none.

func (Cursor) EnumIntegerType

func (c Cursor) EnumIntegerType() Type

EnumIntegerType returns the integer type underlying an enum declaration.

func (Cursor) EnumUnsignedValue

func (c Cursor) EnumUnsignedValue() uint64

EnumUnsignedValue returns an unsigned enum constant's value.

func (Cursor) EnumValue

func (c Cursor) EnumValue() int64

EnumValue returns the value of an enum constant.

func (Cursor) Evaluate

func (c Cursor) Evaluate() (any, bool)

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

func (c Cursor) IsBitField() bool

IsBitField reports whether c is a bit-field.

func (Cursor) IsMacroFunctionLike

func (c Cursor) IsMacroFunctionLike() bool

IsMacroFunctionLike reports whether a macro definition takes arguments.

func (Cursor) IsNull

func (c Cursor) IsNull() bool

IsNull reports whether c is the null cursor.

func (Cursor) IsScoped

func (c Cursor) IsScoped() bool

IsScoped reports whether an enum is a scoped enum, an enum class.

func (Cursor) Kind

func (c Cursor) Kind() CursorKind

Kind returns the kind of the cursor.

func (Cursor) Location

func (c Cursor) Location() (file string, line, col int)

Location returns the file, line and column where c is spelled.

func (Cursor) Mangling

func (c Cursor) Mangling() string

Mangling returns the symbol name of a declaration with linkage.

func (Cursor) OffsetOfField

func (c Cursor) OffsetOfField() int64

OffsetOfField returns the offset in bits of a field in its record, or a negative value if it has none.

func (Cursor) RawComment

func (c Cursor) RawComment() string

RawComment returns the documentation comment attached to the declaration, with its comment markers.

func (Cursor) ResultType

func (c Cursor) ResultType() Type

ResultType returns the result type of a function declaration.

func (Cursor) Spelling

func (c Cursor) Spelling() string

Spelling returns the name of the declaration or reference.

func (Cursor) Type

func (c Cursor) Type() Type

Type returns the type of the declaration.

func (Cursor) TypedefUnderlyingType

func (c Cursor) TypedefUnderlyingType() Type

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
	CUDASharedAttr   CursorKind = 416
)

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

func Open(path string) (*Library, error)

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

func (l *Library) DeviceParseArgs() []string

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) Path

func (l *Library) Path() string

Path returns the path the library was loaded from.

func (*Library) ResourceDir

func (l *Library) ResourceDir() string

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.

func (*Library) Version

func (l *Library) Version() string

Version returns clang's version string, such as "clang version 21.0.0".

type Severity

type Severity int32

Severity is the severity of a diagnostic.

const (
	Ignored Severity = iota
	Note
	Warning
	Error
	Fatal
)

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

func (t Type) Align() int64

Align returns the alignment of the type in bytes, or a negative libclang error code.

func (Type) Canonical

func (t Type) Canonical() Type

Canonical returns the type with typedefs and elaboration removed.

func (Type) Declaration

func (t Type) Declaration() Cursor

Declaration returns the declaration of a record, enum or typedef type.

func (Type) Elem

func (t Type) Elem() Type

Elem returns the element type of an array.

func (Type) Kind

func (t Type) Kind() TypeKind

Kind returns the kind of the type.

func (Type) Len

func (t Type) Len() int64

Len returns the length of a constant array, or -1.

func (Type) Pointee

func (t Type) Pointee() Type

Pointee returns the type a pointer or reference points to.

func (Type) Size

func (t Type) Size() int64

Size returns the size of the type in bytes, or a negative libclang error code for a type without one.

func (Type) Spelling

func (t Type) Spelling() string

Spelling returns the type as clang prints it.

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

type UnsavedFile struct {
	Name     string
	Contents string
}

An UnsavedFile is a file that parsing reads from memory rather than disk.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL