Skip to content

Commit 85f4447

Browse files
committed
Recognize associated template types and make them opaque
1 parent 4b75a19 commit 85f4447

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/clang.rs

+25
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use cexpr;
88
use clang_sys::*;
9+
use regex;
910
use std::{mem, ptr, slice};
1011
use std::ffi::{CStr, CString};
1112
use std::fmt;
@@ -913,6 +914,30 @@ impl Type {
913914
_ => true,
914915
}
915916
}
917+
918+
/// Is this type an associated template type? Eg `T::Associated` in
919+
/// this example:
920+
///
921+
/// ```c++
922+
/// template <typename T>
923+
/// class Foo {
924+
/// typename T::Associated member;
925+
/// };
926+
/// ```
927+
pub fn is_associated_type(&self) -> bool {
928+
// This is terrible :(
929+
fn hacky_parse_associated_type<S: AsRef<str>>(spelling: S) -> bool {
930+
lazy_static! {
931+
static ref ASSOC_TYPE_RE: regex::Regex =
932+
regex::Regex::new(r"typename type\-parameter\-\d+\-\d+::.+").unwrap();
933+
}
934+
ASSOC_TYPE_RE.is_match(spelling.as_ref())
935+
}
936+
937+
self.kind() == CXType_Unexposed &&
938+
(hacky_parse_associated_type(self.spelling()) ||
939+
hacky_parse_associated_type(self.canonical_type().spelling()))
940+
}
916941
}
917942

918943
/// The `CanonicalTypeDeclaration` type exists as proof-by-construction that its

src/ir/comp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@ impl CompInfo {
552552
}
553553
CXCursor_EnumDecl |
554554
CXCursor_TypeAliasDecl |
555+
CXCursor_TypeAliasTemplateDecl |
555556
CXCursor_TypedefDecl |
556557
CXCursor_StructDecl |
557558
CXCursor_UnionDecl |
@@ -713,7 +714,7 @@ impl CompInfo {
713714
_ => {
714715
warn!("unhandled comp member `{}` (kind {:?}) in `{}` ({})",
715716
cur.spelling(),
716-
cur.kind(),
717+
clang::kind_to_str(cur.kind()),
717718
cursor.spelling(),
718719
cur.location());
719720
}

src/ir/item.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,11 @@ impl ClangItemParser for Item {
11111111

11121112
if ty.kind() == clang_sys::CXType_Unexposed ||
11131113
location.cur_type().kind() == clang_sys::CXType_Unexposed {
1114+
1115+
if ty.is_associated_type() || location.cur_type().is_associated_type() {
1116+
return Ok(Item::new_opaque_type(id, ty, ctx));
1117+
}
1118+
11141119
if let Some(id) = Item::named_type(Some(id), location, ctx) {
11151120
return Ok(id);
11161121
}

tests/expectations/tests/issue-544-stylo-creduce-2.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66

77
#[repr(C)]
8-
#[derive(Debug, Copy, Clone)]
98
pub struct Foo {
10-
pub member: Foo,
9+
pub member: Foo_SecondAlias,
1110
}
11+
pub type Foo_FirstAlias = [u8; 0usize];
12+
pub type Foo_SecondAlias = [u8; 0usize];
1213
impl Default for Foo {
1314
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
1415
}

0 commit comments

Comments
 (0)