Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

fix(struct-alias): Support struct typedef aliasing #58

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "lerna workspace for clangffi, libclang-bindings tools",
"scripts": {
"clangffi": "node packages/clangffi/dist/bin/clangffi.js",
"generate-dogfood": "npm run clangffi -- -i vendor/llvm-project/clang/include/clang-c/Index.h -I vendor/llvm-project/clang/include/ -o packages/libclang-bindings/src/libclang.ts --include *time*_t --include-file vendor/llvm-project/clang/include/clang-c/",
bengreenier marked this conversation as resolved.
Show resolved Hide resolved
"generate-dogfood": "npm run clangffi -- --clean-enum-constants=false -i vendor/llvm-project/clang/include/clang-c/Index.h -I vendor/llvm-project/clang/include/ -o packages/libclang-bindings/src/libclang.ts --include *time*_t --include-file vendor/llvm-project/clang/include/clang-c/",
"bootstrap": "lerna bootstrap",
"build": "lerna run build --stream",
"test": "lerna run test --stream"
Expand All @@ -14,5 +14,8 @@
"license": "MIT",
"devDependencies": {
"lerna": "^4.0.0"
},
"volta": {
"node": "17.9.1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adds support for volta correctly

}
}
1 change: 1 addition & 0 deletions packages/clangffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Options:
--include-file File paths to explicitly include symbols from, in addition to the default. If a directory path is given symbols from all files in that directory will be included. [array] [default: []]
--exclude Symbols to explicitly exclude from the bindings. Overrides `include` if there is a conflict. [array] [default: []]
--hard-remap, --remap Custom native to node symbol mappings that override the default. [array] [default: []]
--clean-enum-constants Convert constant names like `ENUM_NAME_FOO_BAR` to `FooBar` in enums. [boolean] [default: true]
bengreenier marked this conversation as resolved.
Show resolved Hide resolved
--help Show help [boolean]

Examples:
Expand Down
15 changes: 13 additions & 2 deletions packages/clangffi/src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
CXTypeKind,
} from "libclang-bindings";
import { ISourceGenerator } from "./types.js";
import { formatPath, resolveName } from "./util.js";
import { formatPath, resolveName, simpleDesugar } from "./util.js";
import { matches, SelectorData } from "./selector.js";

const log = debug("clangffi:parser");
Expand Down Expand Up @@ -192,9 +192,20 @@ export class Parser {
if (decl instanceof TypedefDecl) {
const td = decl as TypedefDecl;

const canonicalType = td.underlyingTypeClass.isCanonical
? td.underlyingTypeClass
: td.underlyingTypeClass.canonicalType;
const canonicalName = canonicalType
? simpleDesugar(canonicalType)
: td.name;

// elaborated types will be auto walked already
// for non elaborate types we need to manually walk em
if (td.underlyingTypeClass.kind != CXTypeKind.CXType_Elaborated) {
// we also manually walk if the typedef name and the underlying class name don't align
if (
td.underlyingTypeClass.kind != CXTypeKind.CXType_Elaborated ||
symbolName !== canonicalName
) {
this.opts.generator.openTypedef(td);
visit(decl, this.visit.bind(this));
this.opts.generator.closeTypedef(td);
Expand Down
10 changes: 10 additions & 0 deletions packages/clangffi/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export function simpleDesugar(type: Type) {
desugarLog(`removed const: '${desugared}'`);
}

if (desugared.startsWith("struct ")) {
desugared = desugared.replace("struct ", "");
desugarLog(`removed struct prefix: '${desugared}'`);
}

if (desugared.startsWith("enum ")) {
desugared = desugared.replace("enum ", "");
desugarLog(`removed enum prefix: '${desugared}'`);
}

return desugared;
}

Expand Down
58 changes: 56 additions & 2 deletions packages/libclang-bindings/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/libclang-bindings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/ref-array-di": "^1.2.3",
"@types/ref-napi": "^3.0.4",
"@types/ref-struct-di": "^1.1.5",
"@types/ref-union-di": "^1.0.3",
bengreenier marked this conversation as resolved.
Show resolved Hide resolved
"babel-jest": "^27.4.5",
"jest": "^27.4.5",
"typescript": "^4.5.4"
Expand All @@ -42,7 +43,8 @@
"ffi-napi": "^4.0.3",
"ref-array-di": "^1.2.2",
"ref-napi": "^3.0.3",
"ref-struct-di": "^1.1.1"
"ref-struct-di": "^1.1.1",
"ref-union-di": "^1.0.1"
bengreenier marked this conversation as resolved.
Show resolved Hide resolved
},
"engines": {
"node": ">=17.x"
Expand Down
3 changes: 2 additions & 1 deletion packages/libclang-bindings/src/libclang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import ffi from "ffi-napi";
import ref, { Pointer as TypedPointer, UnderlyingType } from "ref-napi";
import refStructDi, { StructObject } from "ref-struct-di";
import refArrayDi, { TypedArray } from "ref-array-di";

import refUnionDi from "ref-union-di";
bengreenier marked this conversation as resolved.
Show resolved Hide resolved
const Struct = refStructDi(ref);
const Union = refUnionDi(ref);
const ArrayType = refArrayDi(ref);
const Pointer = ref.refType;
export type __time32_t = number;
Expand Down