Skip to content

Commit

Permalink
String fixes (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Nov 29, 2023
1 parent 4abe5c8 commit 9dbc3ee
Show file tree
Hide file tree
Showing 24 changed files with 71 additions and 66 deletions.
7 changes: 2 additions & 5 deletions example/c/include/diplomat_runtime.h

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

7 changes: 2 additions & 5 deletions example/c2/include/diplomat_runtime.h

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

7 changes: 2 additions & 5 deletions example/cpp/include/diplomat_runtime.h

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

7 changes: 2 additions & 5 deletions example/cpp2/include/diplomat_runtime.h

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

7 changes: 2 additions & 5 deletions feature_tests/c/include/diplomat_runtime.h

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

2 changes: 1 addition & 1 deletion feature_tests/c2/include/BorrowedFields.d.h

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

7 changes: 2 additions & 5 deletions feature_tests/c2/include/diplomat_runtime.h

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

2 changes: 1 addition & 1 deletion feature_tests/cpp/docs/source/lifetimes_ffi.rst

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

2 changes: 1 addition & 1 deletion feature_tests/cpp/include/BorrowedFields.hpp

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

4 changes: 4 additions & 0 deletions feature_tests/cpp/include/MyString.hpp

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

7 changes: 2 additions & 5 deletions feature_tests/cpp/include/diplomat_runtime.h

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

2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/BorrowedFields.d.h

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

2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/BorrowedFields.d.hpp

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

2 changes: 1 addition & 1 deletion feature_tests/cpp2/include/BorrowedFields.hpp

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

7 changes: 2 additions & 5 deletions feature_tests/cpp2/include/diplomat_runtime.h

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

7 changes: 2 additions & 5 deletions tool/src/c/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ typedef struct DiplomatWriteable {

DiplomatWriteable diplomat_simple_writeable(char* buf, size_t buf_size);

typedef struct DiplomatStringView {
const char* data;
size_t len;
} DiplomatStringView;

#define MAKE_SLICE_VIEW(name, c_ty) \
typedef struct Diplomat##name##View { \
const c_ty* data; \
Expand All @@ -60,6 +55,8 @@ MAKE_SLICE_VIEW(F32, float)
MAKE_SLICE_VIEW(F64, double)
MAKE_SLICE_VIEW(Bool, bool)
MAKE_SLICE_VIEW(Char, char32_t)
MAKE_SLICE_VIEW(String, char)
MAKE_SLICE_VIEW(U16String, char16_t)


#ifdef __cplusplus
Expand Down
2 changes: 1 addition & 1 deletion tool/src/c/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn gen_method<W: fmt::Write>(
} else if let ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) =
&param.ty
{
write!(out, "const uint16_t* {0}_data, size_t {0}_len", param.name)?;
write!(out, "const char16_t* {0}_data, size_t {0}_len", param.name)?;
} else if let ast::TypeName::PrimitiveSlice(_, mutability, prim) = &param.ty {
write!(
out,
Expand Down
7 changes: 5 additions & 2 deletions tool/src/c2/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
if !is_struct =>
{
vec![
("const wchar_t*".into(), format!("{param_name}_data").into()),
(
"const char16_t*".into(),
format!("{param_name}_data").into(),
),
("size_t".into(), format!("{param_name}_len").into()),
]
}
Expand Down Expand Up @@ -329,7 +332,7 @@ impl<'ccx, 'tcx: 'ccx, 'header> TyGenContext<'ccx, 'tcx, 'header> {
_,
hir::StringEncoding::UnvalidatedUtf8 | hir::StringEncoding::Utf8,
) => "char".into(),
hir::Slice::Str(_, hir::StringEncoding::UnvalidatedUtf16) => "wchar_t".into(),
hir::Slice::Str(_, hir::StringEncoding::UnvalidatedUtf16) => "char16_t".into(),
hir::Slice::Primitive(_, prim) => self.cx.formatter.fmt_primitive_as_c(*prim),
&_ => unreachable!("unknown AST/HIR variant"),
};
Expand Down
5 changes: 5 additions & 0 deletions tool/src/cpp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct LibraryConfig {
pub someopt: CallableLibraryType,
pub span: LibraryType,
pub string_view: LibraryType,
pub string_view16: LibraryType,
pub unique_ptr: LibraryType,
}

Expand Down Expand Up @@ -53,6 +54,10 @@ impl LibraryConfig {
name: "std::string_view".into(),
expr: "std::string_view".into(),
},
string_view16: LibraryType {
name: "std::u16string_view".into(),
expr: "std::u16string_view".into(),
},
unique_ptr: LibraryType {
name: "std::unique_ptr".into(),
expr: "std::unique_ptr".into(),
Expand Down
4 changes: 4 additions & 0 deletions tool/src/cpp/configs/mfbt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ expr = "mozilla::Span"
name = "mozilla::Span"
expr = "mozilla::Span<const char>"

[string_view16]
name = "mozilla::Span"
expr = "mozilla::Span<const char16_t>"

[unique_ptr]
name = "mozilla::UniquePtr"
expr = "mozilla::UniquePtr"
4 changes: 2 additions & 2 deletions tool/src/cpp/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,12 @@ pub fn gen_rust_to_cpp<W: Write>(
}
ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) => {
let raw_value_id = format!("diplomat_slice_raw_{path}");
writeln!(out, "capi::DiplomatU16View {raw_value_id} = {cpp};").unwrap();
writeln!(out, "capi::DiplomatU16StringView {raw_value_id} = {cpp};").unwrap();

let span = &library_config.span.expr;
writeln!(
out,
"{span}<const uint16_t> slice({raw_value_id}.data, {raw_value_id}.len);"
"{span}<const char16_t> slice({raw_value_id}.data, {raw_value_id}.len);"
)
.unwrap();
"slice".into()
Expand Down
31 changes: 23 additions & 8 deletions tool/src/cpp/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,29 @@ fn gen_method<W: fmt::Write>(
}

if is_header {
gen_comment_block(
out,
&super::docs::gen_docs_and_lifetime_notes_markdown(
method,
docs_url_gen,
ast::MarkdownStyle::Normal,
),
)?;
let mut comment = super::docs::gen_docs_and_lifetime_notes_markdown(
method,
docs_url_gen,
ast::MarkdownStyle::Normal,
);

if method.params.iter().any(|p| {
matches!(
p,
ast::Param {
ty: ast::TypeName::StrReference(_, ast::StringEncoding::Utf8),
..
}
)
}) {
if !comment.is_empty() {
writeln!(comment).unwrap();
writeln!(comment).unwrap();
}
writeln!(comment, "Warning: Passing ill-formed UTF-8 is undefined behavior (and may be memory-unsafe).").unwrap();
}

gen_comment_block(out, &comment)?;
}
let params_to_gen = gen_method_interface(
method,
Expand Down
3 changes: 2 additions & 1 deletion tool/src/cpp/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ fn gen_type_inner<W: fmt::Write>(
}

ast::TypeName::StrReference(_, ast::StringEncoding::UnvalidatedUtf16) => {
write!(out, "const {}<const uint16_t>", library_config.span.expr)?;
let maybe_const = if in_struct { "" } else { "const " };
write!(out, "{maybe_const}{}", library_config.string_view16.expr)?;
}

ast::TypeName::PrimitiveSlice(_, ast::Mutability::Mutable, prim) => {
Expand Down
2 changes: 1 addition & 1 deletion tool/src/cpp2/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<'tcx> Cpp2Formatter<'tcx> {

pub fn fmt_borrowed_utf16_str(&self) -> Cow<'static, str> {
// TODO: This needs to change if an abstraction other than std::u16string_view is used
"std::wstring_view".into()
"std::u16string_view".into()
}

pub fn fmt_owned_str(&self) -> Cow<'static, str> {
Expand Down

0 comments on commit 9dbc3ee

Please sign in to comment.