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

Handle no return attributes #2259

Merged
merged 12 commits into from
Sep 23, 2022
18 changes: 18 additions & 0 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,24 @@ impl Cursor {
self.has_attr("warn_unused_result", Some(CXCursor_WarnUnusedResultAttr))
}

pub fn has_no_return_attr(&self) -> bool {
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
let mut found_attr = false;
self.visit(|cur| {
found_attr = cur.kind() == CXCursor_UnexposedAttr &&
cur.tokens().iter().any(|t| {
t.kind == CXToken_Keyword && t.spelling() == b"_Noreturn"
});

if found_attr {
CXChildVisit_Break
} else {
CXChildVisit_Continue
}
});

found_attr
}

/// Does this cursor have the given attribute?
///
/// `name` is checked against unexposed attributes.
Expand Down
4 changes: 4 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4704,6 +4704,10 @@ pub mod utils {
ctx: &BindgenContext,
sig: &FunctionSig,
) -> proc_macro2::TokenStream {
if sig.is_divergent() {
return quote! { -> ! };
}

let return_item = ctx.resolve_item(sig.return_type());
if let TypeKind::Void = *return_item.kind().expect_type().kind() {
quote! {}
Expand Down
17 changes: 16 additions & 1 deletion src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub struct FunctionSig {

/// Whether this function is variadic.
is_variadic: bool,
is_divergent: bool,

/// Whether this function's return value must be used.
must_use: bool,
Expand Down Expand Up @@ -358,13 +359,15 @@ impl FunctionSig {
return_type: TypeId,
argument_types: Vec<(Option<String>, TypeId)>,
is_variadic: bool,
is_divergent: bool,
must_use: bool,
abi: Abi,
) -> Self {
FunctionSig {
return_type,
argument_types,
is_variadic,
is_divergent,
must_use,
abi,
}
Expand All @@ -378,6 +381,7 @@ impl FunctionSig {
) -> Result<Self, ParseError> {
use clang_sys::*;
debug!("FunctionSig::from_ty {:?} {:?}", ty, cursor);
let is_divergent = cursor.has_no_return_attr();
pvdrz marked this conversation as resolved.
Show resolved Hide resolved

// Skip function templates
let kind = cursor.kind();
Expand Down Expand Up @@ -528,7 +532,14 @@ impl FunctionSig {
warn!("Unknown calling convention: {:?}", call_conv);
}

Ok(Self::new(ret, args, ty.is_variadic(), must_use, abi))
Ok(Self::new(
ret,
args,
ty.is_variadic(),
is_divergent,
must_use,
abi,
))
}

/// Get this function signature's return type.
Expand Down Expand Up @@ -575,6 +586,10 @@ impl FunctionSig {

matches!(self.abi, Abi::C | Abi::Unknown(..))
}

pub(crate) fn is_divergent(&self) -> bool {
self.is_divergent
}
}

impl ClangSubItemParser for Function {
Expand Down
13 changes: 13 additions & 0 deletions tests/expectations/tests/noreturn.rs

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

3 changes: 3 additions & 0 deletions tests/headers/noreturn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_Noreturn void f(void);
// TODO (pvdrz): figure out how to handle this case.
__attribute__((noreturn)) void g(void);
pvdrz marked this conversation as resolved.
Show resolved Hide resolved