From d3d6a44277f2527bd5911b67bddfe2503308e8cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 25 Jul 2022 11:17:11 +0200 Subject: [PATCH] ty: Use canonical type to access pointee. Using the canonical type fixes this but changes the output of some tests (in particular, pointer to typedefs now point to the underlying type). So do this only in known-bad cases. Fixes #2244 --- src/ir/ty.rs | 11 ++++++++++- tests/expectations/tests/pointer-attr.rs | 10 ++++++++++ tests/headers/pointer-attr.h | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/expectations/tests/pointer-attr.rs create mode 100644 tests/headers/pointer-attr.h diff --git a/src/ir/ty.rs b/src/ir/ty.rs index d573408c3b..38ab4564bd 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -1031,7 +1031,16 @@ impl Type { CXType_ObjCObjectPointer | CXType_MemberPointer | CXType_Pointer => { - let pointee = ty.pointee_type().unwrap(); + let mut pointee = ty.pointee_type().unwrap(); + if *ty != canonical_ty { + let canonical_pointee = + canonical_ty.pointee_type().unwrap(); + // clang sometimes loses pointee constness here, see + // #2244. + if canonical_pointee.is_const() != pointee.is_const() { + pointee = canonical_pointee; + } + } let inner = Item::from_ty_or_ref(pointee, location, None, ctx); TypeKind::Pointer(inner) diff --git a/tests/expectations/tests/pointer-attr.rs b/tests/expectations/tests/pointer-attr.rs new file mode 100644 index 0000000000..95fe9c5f0d --- /dev/null +++ b/tests/expectations/tests/pointer-attr.rs @@ -0,0 +1,10 @@ +#![allow( + dead_code, + non_snake_case, + non_camel_case_types, + non_upper_case_globals +)] + +extern "C" { + pub fn a(arg1: *const ::std::os::raw::c_char); +} diff --git a/tests/headers/pointer-attr.h b/tests/headers/pointer-attr.h new file mode 100644 index 0000000000..fe0004b8bf --- /dev/null +++ b/tests/headers/pointer-attr.h @@ -0,0 +1 @@ +void a(const char __attribute__((btf_type_tag("a"))) *);