Skip to content

Commit

Permalink
Recognize associated template types and make them opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
fitzgen committed Mar 6, 2017
1 parent 4b75a19 commit 85f4447
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use cexpr;
use clang_sys::*;
use regex;
use std::{mem, ptr, slice};
use std::ffi::{CStr, CString};
use std::fmt;
Expand Down Expand Up @@ -913,6 +914,30 @@ impl Type {
_ => true,
}
}

/// Is this type an associated template type? Eg `T::Associated` in
/// this example:
///
/// ```c++
/// template <typename T>
/// class Foo {
/// typename T::Associated member;
/// };
/// ```
pub fn is_associated_type(&self) -> bool {
// This is terrible :(
fn hacky_parse_associated_type<S: AsRef<str>>(spelling: S) -> bool {
lazy_static! {
static ref ASSOC_TYPE_RE: regex::Regex =
regex::Regex::new(r"typename type\-parameter\-\d+\-\d+::.+").unwrap();
}
ASSOC_TYPE_RE.is_match(spelling.as_ref())
}

self.kind() == CXType_Unexposed &&
(hacky_parse_associated_type(self.spelling()) ||
hacky_parse_associated_type(self.canonical_type().spelling()))
}
}

/// The `CanonicalTypeDeclaration` type exists as proof-by-construction that its
Expand Down
3 changes: 2 additions & 1 deletion src/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ impl CompInfo {
}
CXCursor_EnumDecl |
CXCursor_TypeAliasDecl |
CXCursor_TypeAliasTemplateDecl |
CXCursor_TypedefDecl |
CXCursor_StructDecl |
CXCursor_UnionDecl |
Expand Down Expand Up @@ -713,7 +714,7 @@ impl CompInfo {
_ => {
warn!("unhandled comp member `{}` (kind {:?}) in `{}` ({})",
cur.spelling(),
cur.kind(),
clang::kind_to_str(cur.kind()),
cursor.spelling(),
cur.location());
}
Expand Down
5 changes: 5 additions & 0 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,11 @@ impl ClangItemParser for Item {

if ty.kind() == clang_sys::CXType_Unexposed ||
location.cur_type().kind() == clang_sys::CXType_Unexposed {

if ty.is_associated_type() || location.cur_type().is_associated_type() {
return Ok(Item::new_opaque_type(id, ty, ctx));
}

if let Some(id) = Item::named_type(Some(id), location, ctx) {
return Ok(id);
}
Expand Down
5 changes: 3 additions & 2 deletions tests/expectations/tests/issue-544-stylo-creduce-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@


#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
pub member: Foo,
pub member: Foo_SecondAlias,
}
pub type Foo_FirstAlias = [u8; 0usize];
pub type Foo_SecondAlias = [u8; 0usize];
impl Default for Foo {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}

0 comments on commit 85f4447

Please sign in to comment.