Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Emit `&str`, `&CStr`, and arrays thereof as C string literals, and byte-string
(`b"..."`) constants as `uint8_t[]` arrays, instead of dropping them.
* Escape `/*` and `*/` in documentation emitted as C block comments, so a
doc comment containing them no longer breaks the generated header.

# 0.29.4

Expand Down
9 changes: 8 additions & 1 deletion src/bindgen/language_backend/clike.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,14 @@ impl LanguageBackend for CLikeLanguageBackend<'_> {
DocumentationStyle::Auto => unreachable!(), // Auto case should always be covered
}

write!(out, "{line}");
match style {
// C comments do not nest: a `*/` in the text would end the comment
// early, and a `/*` is diagnosed by `-Wcomment`.
DocumentationStyle::C | DocumentationStyle::Doxy => {
write!(out, "{}", line.replace("*/", "*\\/").replace("/*", "/\\*"))
}
_ => write!(out, "{line}"),
}
out.new_line();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
root;
};
12 changes: 12 additions & 0 deletions tests/expectations/documentation_comment_delimiters.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

/**
* Matches glob patterns such as `src/\*\/mod.rs`.
*
* A C comment like /\* this *\/ in the docs must not end the generated comment,
* and neither must `**\/\*.rs`.
*/
void root(void);
20 changes: 20 additions & 0 deletions tests/expectations/documentation_comment_delimiters.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
* Matches glob patterns such as `src/\*\/mod.rs`.
*
* A C comment like /\* this *\/ in the docs must not end the generated comment,
* and neither must `**\/\*.rs`.
*/
void root(void);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
15 changes: 15 additions & 0 deletions tests/expectations/documentation_comment_delimiters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

extern "C" {

/// Matches glob patterns such as `src/*/mod.rs`.
///
/// A C comment like /* this */ in the docs must not end the generated comment,
/// and neither must `**/*.rs`.
void root();

} // extern "C"
13 changes: 13 additions & 0 deletions tests/expectations/documentation_comment_delimiters.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list

cdef extern from *:

# Matches glob patterns such as `src/*/mod.rs`.
#
# A C comment like /* this */ in the docs must not end the generated comment,
# and neither must `**/*.rs`.
void root();
6 changes: 6 additions & 0 deletions tests/rust/documentation_comment_delimiters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Matches glob patterns such as `src/*/mod.rs`.
///
/// A C comment like /* this */ in the docs must not end the generated comment,
/// and neither must `**/*.rs`.
#[no_mangle]
pub extern "C" fn root() {}