Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix use of deprecated std::char_traits<unsigned short>::length #753

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
19 changes: 15 additions & 4 deletions src/main/java/org/bytedeco/javacpp/tools/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,17 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" env->SetLongField(obj, JavaCPP_capacityFID, (jlong)size);");
out.println(" }");
out.println("}");

// This is needed because char_traits<unsigned_short>::length is deprecated:
// warning: 'char_traits' is deprecated: char_traits for T not equal to char,
// wchar_t, char8_t, char16_t or char32_t is non-standard and is provided for
// a temporary period. It will be removed in LLVM 18, so please migrate off of it.
out.println("static JavaCPP_noinline size_t JavaCPP_stringLength(const unsigned short* ptr) {");
out.println(" for(size_t i = 0; ; ++i) {");
out.println(" if (ptr[i] == 0) return i;");
out.println(" }");
out.println("}");

out.println();
if (handleExceptions || convertStrings) {
out.println("#include <string>");
Expand Down Expand Up @@ -1041,7 +1052,7 @@ boolean classes(boolean handleExceptions, boolean defineAdapters, boolean conver
out.println(" if (ptr == NULL) {");
out.println(" return NULL;");
out.println(" }");
out.println(" return JavaCPP_createStringFromUTF16(env, ptr, std::char_traits<unsigned short>::length(ptr));");
out.println(" return JavaCPP_createStringFromUTF16(env, ptr, JavaCPP_stringLength(ptr));");
out.println("}");
out.println();
}
Expand Down Expand Up @@ -2568,7 +2579,7 @@ void call(MethodInformation methodInfo, String returnPrefix, boolean secondCall)
// special considerations for char arrays as strings
if (asUtf16(methodInfo, k)) {
out.print(indent + "memcpy(");
suffix = ", (std::char_traits<unsigned short>::length(ptr" + k + ") + 1) * sizeof(unsigned short))";
suffix = ", (JavaCPP_stringLength(ptr" + k + ") + 1) * sizeof(unsigned short))";
} else {
out.print(indent + "strcpy((char*)");
}
Expand Down Expand Up @@ -3027,7 +3038,7 @@ void parametersAfter(MethodInformation methodInfo) {
if ("void*".equals(typeName[0]) && !methodInfo.parameterTypes[j].isAnnotationPresent(Opaque.class)) {
typeName[0] = "char*";
}

// If const array, then use JNI_ABORT to avoid copying unmodified data back to JVM
final String releaseArrayFlag;
if (cast.contains(" const *") || cast.startsWith("(const ")) {
Expand Down Expand Up @@ -4035,7 +4046,7 @@ AdapterInformation adapterInformation(boolean out, String valueTypeName, Annotat
cast2 = c.value()[2];
}
}
} catch (Exception ex) {
} catch (Exception ex) {
logger.warn("Could not invoke the value() method on annotation \"" + a + "\": " + ex);
}
if (valueTypeName != null && valueTypeName.length() > 0) {
Expand Down