Skip to content
Merged
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
9 changes: 8 additions & 1 deletion cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ bool Converter::VisitFunctionTemplateDecl(clang::FunctionTemplateDecl *decl) {
}
for (auto *function_decl : decl->specializations()) {
if (function_decl->getTemplateSpecializationKind() ==
clang::TSK_ExplicitSpecialization) {
clang::TSK_ExplicitSpecialization ||
IsLibraryOnlySpecialization(function_decl)) {
continue;
}
VisitFunctionDecl(function_decl);
Expand Down Expand Up @@ -904,6 +905,9 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) {
}
if (auto *nested_tmpl = clang::dyn_cast<clang::ClassTemplateDecl>(d)) {
for (auto *spec : nested_tmpl->specializations()) {
if (IsLibraryOnlySpecialization(spec)) {
continue;
}
inner_structs_[GetID(spec)] = GetRecordName(spec);
VisitCXXRecordDecl(spec);
}
Expand Down Expand Up @@ -4377,6 +4381,9 @@ bool Converter::VisitPredefinedExpr(clang::PredefinedExpr *expr) {

bool Converter::VisitClassTemplateDecl(clang::ClassTemplateDecl *decl) {
for (auto decl : decl->specializations()) {
if (IsLibraryOnlySpecialization(decl)) {
continue;
}
VisitCXXRecordDecl(decl);
}
return false;
Expand Down
59 changes: 57 additions & 2 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,51 @@ bool IsUserDefinedDecl(const clang::Decl *decl) {
!src_mgr.isInSystemMacro(src_loc);
}

static clang::QualType StripPointersAndArrays(clang::QualType type) {
while (type->isPointerType() || type->isReferenceType() ||
type->isArrayType()) {
type = type->isArrayType()
? clang::QualType(type->getArrayElementTypeNoTypeQual(), 0)
: type->getPointeeType();
}
return type;
}

static bool
HasLibraryInternalArgument(clang::ASTContext &ctx,
llvm::ArrayRef<clang::TemplateArgument> args) {
return llvm::any_of(args, [&](const clang::TemplateArgument &arg) {
if (arg.getKind() != clang::TemplateArgument::Type) {
return false;
}
auto tag = StripPointersAndArrays(arg.getAsType().getCanonicalType())
->getAsTagDecl();
return tag && !IsUserDefinedDecl(tag) &&
clang::isReservedInAllContexts(tag->isReserved(ctx.getLangOpts()));
});
}

static bool IsInstantiatedInSystemHeader(clang::ASTContext &ctx,
clang::SourceLocation loc) {
return loc.isValid() && ctx.getSourceManager().isInSystemHeader(
ctx.getSourceManager().getExpansionLoc(loc));
}

bool IsLibraryOnlySpecialization(
const clang::ClassTemplateSpecializationDecl *decl) {
auto &ctx = decl->getASTContext();
return IsInstantiatedInSystemHeader(ctx, decl->getPointOfInstantiation()) &&
HasLibraryInternalArgument(ctx, decl->getTemplateArgs().asArray());
}

bool IsLibraryOnlySpecialization(const clang::FunctionDecl *decl) {
auto &ctx = decl->getASTContext();
auto args = decl->getTemplateSpecializationArgs();
return args &&
IsInstantiatedInSystemHeader(ctx, decl->getPointOfInstantiation()) &&
HasLibraryInternalArgument(ctx, args->asArray());
}

bool IsAbstractUserClass(const clang::RecordDecl *decl) {
auto cxx = llvm::dyn_cast<clang::CXXRecordDecl>(decl);
auto definition = cxx ? cxx->getDefinition() : nullptr;
Expand Down Expand Up @@ -416,7 +461,7 @@ static void ForEachTemplateInstantiation(
llvm::dyn_cast<clang::FunctionTemplateDecl>(d)) {
for (auto s : function_template_decl->specializations()) {
if (auto m = clang::dyn_cast<clang::CXXMethodDecl>(s);
m && m->getDefinition()) {
m && m->getDefinition() && !IsLibraryOnlySpecialization(m)) {
fn(m);
}
}
Expand Down Expand Up @@ -805,7 +850,8 @@ bool IsPassThroughConstructor(const clang::CXXConstructorDecl *ctor) {
}

bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl) {
return decl->isThisDeclarationADefinition() && !decl->isDependentContext();
return decl->isThisDeclarationADefinition() && !decl->isDependentContext() &&
!decl->isInvalidDecl();
}

bool IsConvertibleCXXMethodDecl(const clang::CXXMethodDecl *decl) {
Expand Down Expand Up @@ -1707,6 +1753,15 @@ void DefineImplicitMembers(clang::Sema &sema, clang::CXXRecordDecl *decl) {
tu_scope.setEntity(sema.Context.getTranslationUnitDecl());
auto *saved_tu_scope = std::exchange(sema.TUScope, &tu_scope);
sema.ForceDeclarationOfImplicitMembers(decl);
for (auto *field : decl->fields()) {
if (field->hasInClassInitializer() && !field->getInClassInitializer()) {
RuleDecls::Trial trial(sema);
clang::Sema::GlobalEagerInstantiationScope instantiations(
sema, /*Enabled=*/true, /*AtEndOfTU=*/false);
sema.BuildCXXDefaultInitExpr(decl->getLocation(), field);
instantiations.perform();
}
}
auto define = [&](clang::FunctionDecl *fn, auto &&definer) {
RuleDecls::Trial trial(sema);
clang::DiagnosticErrorTrap errors(sema.getDiagnostics());
Expand Down
5 changes: 5 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ bool IsUserDefinedDecl(const clang::Decl *decl);

bool IsAbstractUserClass(const clang::RecordDecl *decl);

bool IsLibraryOnlySpecialization(
const clang::ClassTemplateSpecializationDecl *decl);

bool IsLibraryOnlySpecialization(const clang::FunctionDecl *decl);

bool ExprHasUserDefinedDecl(const clang::Expr *expr);

bool IsUnsignedArithOp(const clang::BinaryOperator *expr);
Expand Down
30 changes: 18 additions & 12 deletions rules/map/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
#include <type_traits>
#include <utility>

template <typename T1, typename T2> using t1 = std::map<T1, T2>;
template <typename T1, typename T2, typename T3, typename T4>
using t1 = std::map<T1, T2, T3, T4>;

template <typename T1, typename T2>
using t4 = typename std::map<T1, T2>::size_type;
template <typename T1, typename T2, typename T3, typename T4>
using t4 = typename std::map<T1, T2, T3, T4>::size_type;

template <typename T1, typename T2>
using t5 = typename std::map<T1, T2>::key_type;
Expand All @@ -21,8 +22,10 @@ template <typename T1, typename T2> T2 &f1(std::map<T1, T2> &o, const T1 &key) {
return o[key];
}

template <typename T1, typename T2> std::size_t f2(const std::map<T1, T2> &o) {
return o.size();
template <typename T1, typename T2, typename T3, typename T4>
typename std::map<T1, T2, T3, T4>::size_type
f2(const std::map<T1, T2, T3, T4> &a0) {
return a0.size();
}

template <typename T1, typename T2>
Expand All @@ -31,8 +34,9 @@ typename std::map<T1, T2>::iterator f3(std::map<T1, T2> &o,
return o.erase(it);
}

template <typename T1, typename T2> std::map<T1, T2> f5() {
return std::map<T1, T2>();
template <typename T1, typename T2, typename T3, typename T4>
std::map<T1, T2, T3, T4> f5() {
return std::map<T1, T2, T3, T4>();
}

template <typename T1, typename T2>
Expand All @@ -44,8 +48,9 @@ template <typename T1, typename T2> T2 &f7(std::map<T1, T2> &o, const T1 &key) {
return o.at(key);
}

template <typename T1, typename T2> T2 &f8(std::map<T1, T2> &o, T1 &&key) {
return o[std::move(key)];
template <typename T1, typename T2, typename T3, typename T4>
T2 &f8(std::map<T1, T2, T3, T4> &a0, T1 &&a1) {
return a0.operator[](std::move(a1));
}

template <typename T1, typename T2>
Expand Down Expand Up @@ -83,9 +88,10 @@ template <typename T1, typename T2> std::map<T1, T2> f24(std::map<T1, T2> &&o) {
return std::map<T1, T2>(std::move(o));
}

template <typename T1, typename T2>
std::map<T1, T2> &f25(std::map<T1, T2> &dst, std::map<T1, T2> &&src) {
return dst = std::move(src);
template <typename T1, typename T2, typename T3, typename T4>
std::map<T1, T2, T3, T4> &f25(std::map<T1, T2, T3, T4> &a0,
std::map<T1, T2, T3, T4> &&a1) {
return a0.operator=(std::move(a1));
}

template <typename T1, typename T2>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <utility>

// N4950 [allocator.requirements.general]/16: X::rebind<U>::other is the
// allocator of U that corresponds to X.
namespace p16_rebind_member {
template <class T> struct Alloc {
using value_type = T;

int id = 0;

Alloc() = default;

template <class U> Alloc(const Alloc<U> &other) : id(other.id) {}

template <class U> struct rebind {
using other = Alloc<U>;
};

T *allocate(std::size_t n) { return std::allocator<T>().allocate(n); }

void deallocate(T *p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
};

template <class T, class U>
bool operator==(const Alloc<T> &a, const Alloc<U> &b) {
return a.id == b.id;
}

void test() {
std::map<int, int, std::less<int>, Alloc<std::pair<const int, int>>> m;
std::map<int, int, std::less<int>, Alloc<std::pair<const int, int>>> n;
m[1] = 2;
n = std::move(m);
assert(n[1] == 2);
assert(n.size() == 1);
}
} // namespace p16_rebind_member

// N4950 [allocator.requirements.general]/18: if Allocator is SomeAllocator<T>
// and does not supply a rebind member template, rebind_alloc<U> is
// SomeAllocator<U>.
namespace p18_rebind_to_container_node {
template <class T> struct Alloc {
using value_type = T;

Alloc() = default;

template <class U> Alloc(const Alloc<U> &) {}

T *allocate(std::size_t n) { return std::allocator<T>().allocate(n); }

void deallocate(T *p, std::size_t n) { std::allocator<T>().deallocate(p, n); }
};

void test() {
std::map<int, int, std::less<int>, Alloc<std::pair<const int, int>>> m;
m[1] = 2;
assert(m[1] == 2);
assert(m.size() == 1);
}
} // namespace p18_rebind_to_container_node

int main() {
p16_rebind_member::test();
p18_rebind_to_container_node::test();
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct p16_rebind_member_Alloc_std_pair_const_int__int___rebind_std_pair_const_int__int__ {}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)]
#[byte_size(4)]
pub struct p16_rebind_member_Alloc_std_pair_const_int__int__ {
#[offset(0)]
pub id: i32,
}
impl Default for p16_rebind_member_Alloc_std_pair_const_int__int__ {
fn default() -> Self {
{ p16_rebind_member_Alloc_std_pair_const_int__int__ { id: 0 } }
}
}
pub fn test_0() {
let m: Value<Map<i32, (Value<i32>, Value<i32>)>> = Rc::new(RefCell::new(Map::new()));
let n: Value<Map<i32, (Value<i32>, Value<i32>)>> = Rc::new(RefCell::new(Map::new()));
Map::subscript_move(
m.as_pointer(),
Rc::new(RefCell::new(1)).as_pointer(),
|__k| cpp_move!(__k),
)
.write(2);
{
let __src = m
.as_pointer()
.with_mut(|__m: &mut Map<i32, (Value<i32>, Value<i32>)>| std::mem::take(__m));
n.as_pointer().write(__src)
};
assert!(
((Map::subscript_move(
n.as_pointer(),
Rc::new(RefCell::new(1)).as_pointer(),
|__k| cpp_move!(__k)
)
.read())
== 2)
);
assert!(({ ((*n.borrow()).len() == 1_usize) }));
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(1)]
pub struct p18_rebind_to_container_node_Alloc_std_pair_const_int__int__ {}
pub fn test_1() {
let m: Value<Map<i32, (Value<i32>, Value<i32>)>> = Rc::new(RefCell::new(Map::new()));
Map::subscript_move(
m.as_pointer(),
Rc::new(RefCell::new(1)).as_pointer(),
|__k| cpp_move!(__k),
)
.write(2);
assert!(
((Map::subscript_move(
m.as_pointer(),
Rc::new(RefCell::new(1)).as_pointer(),
|__k| cpp_move!(__k)
)
.read())
== 2)
);
assert!(({ ((*m.borrow()).len() == 1_usize) }));
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
({ test_0() });
({ test_1() });
return 0;
}
pub trait p16_rebind_member_Alloc_std_pair_const_int__int__Impl {
fn allocate(&self, mut n: usize) -> Ptr<(Value<i32>, Value<i32>)> {
unimplemented!()
}
fn deallocate(&self, mut p: Ptr<(Value<i32>, Value<i32>)>, mut n: usize) {
unimplemented!()
}
}
impl p16_rebind_member_Alloc_std_pair_const_int__int__Impl
for Ptr<p16_rebind_member_Alloc_std_pair_const_int__int__>
{
}
pub trait p18_rebind_to_container_node_Alloc_std_pair_const_int__int__Impl {
fn allocate(&self, mut n: usize) -> Ptr<(Value<i32>, Value<i32>)> {
unimplemented!()
}
fn deallocate(&self, mut p: Ptr<(Value<i32>, Value<i32>)>, mut n: usize) {
unimplemented!()
}
}
impl p18_rebind_to_container_node_Alloc_std_pair_const_int__int__Impl
for Ptr<p18_rebind_to_container_node_Alloc_std_pair_const_int__int__>
{
}
pub fn __cpp2rust_init_globals() {}
Loading
Loading