Skip to content

Commit

Permalink
fix: Improve reference handling a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Berstanio committed Jan 15, 2025
1 parent b76f1f9 commit 98df216
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public boolean hasCTypeMapping(String name) {
return getCTypeMapping(name) != null;
}

public boolean hasFunctionWithName(String name) {
return globalType.getFunctions().stream().anyMatch(functionType -> functionType.getSignature().getName().equals(name));
}

public void addClosure(ClosureType closureType) {
globalType.addClosure(closureType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.badlogic.gdx.jnigen.generator.parser;

import com.badlogic.gdx.jnigen.generator.Manager;
import org.bytedeco.llvm.clang.CXCursor;
import org.bytedeco.llvm.clang.CXString;

Expand All @@ -9,6 +10,7 @@

public class CommentParser {

private static final String SEE_TAG = "@see";
private final CXCursor cursor;

public CommentParser (CXCursor cursor) {
Expand All @@ -19,6 +21,29 @@ public boolean isPresent() {
return clang_Cursor_getRawCommentText(cursor).data() != null;
}

public String normalizeSee(String comment) {
// This method assumes, that the targeted struct/function is already seen
int see = comment.indexOf(SEE_TAG);
if (see < 0)
return comment;

String ref = comment.substring(see + SEE_TAG.length()).trim();
String base = comment.substring(0, see + SEE_TAG.length());

if (ref.contains("."))
ref = ref.replace(".", "#");

if (Manager.getInstance().hasCTypeMapping(ref))
return base + " " + ref;

if (!Manager.getInstance().hasFunctionWithName(ref))
return base + " " + ref;

ref = Manager.getInstance().getGlobalType().abstractType() + "#" + ref;

return base + " " + ref;
}

public String parse () {
CXString string = clang_Cursor_getRawCommentText(cursor);
if (string.data() == null)
Expand All @@ -35,11 +60,15 @@ public String parse () {
comment = comment.replace("*/", "");
comment = comment.lines()
.map(String::trim)
.filter(s -> !s.isBlank())
.filter(s -> !s.isEmpty())
.map(s -> s.startsWith("*") ? s.substring(1) : s)
.collect(Collectors.joining("\n"));
}

comment = comment.lines()
.map(this::normalizeSee)
.collect(Collectors.joining("\n"));

return comment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public FunctionType(FunctionSignature signature, String comment) {
this.comment = comment;
}

public FunctionSignature getSignature() {
return signature;
}

public void write(CompilationUnit cu, ClassOrInterfaceDeclaration wrappingClass, HashMap<MethodDeclaration, String> patchNativeMethod) {
String name = signature.getName();
TypeDefinition returnType = signature.getReturnType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public void addFunction(FunctionType functionType) {
functions.add(functionType);
}

public List<FunctionType> getFunctions() {
return functions;
}

public void write(CompilationUnit cuPublic, ClassOrInterfaceDeclaration global, CompilationUnit cuInternal, ClassOrInterfaceDeclaration globalInternal, HashMap<MethodDeclaration, String> patchNativeMethods) {
cuPublic.addImport(ClassNameConstants.CXXEXCEPTION_CLASS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ public static TestEnum returnTestEnum() {
return 0;
*/

/**
* Combined attempt
* @see TestData#returnTestEnum
*/
public static TestEnum passAndReturnTestEnum(TestEnum enumValue) {
return TestEnum.getByIndex((int) passAndReturnTestEnum_internal(enumValue.getIndex()));
}
Expand Down
2 changes: 2 additions & 0 deletions gdx-jnigen-generator-test/src/test/resources/test_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ void call_methodWithCallbackTestStructPointerArg(methodWithCallbackTestStructPoi
// TestEnum stuff
int passTestEnum(TestEnum enumValue);
TestEnum returnTestEnum(void);
/// Combined attempt
/// @see returnTestEnum
TestEnum passAndReturnTestEnum(TestEnum enumValue);

int passTestEnumPointer(TestEnum* enumValue);
Expand Down

0 comments on commit 98df216

Please sign in to comment.