Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
e6ad1a3
Move more rules in src.c
lucic71 Oct 4, 2026
6bbe355
Skip cxxflags for C files
lucic71 Oct 4, 2026
fa3fa23
Ignore diagnostics inside rules epilogue
lucic71 Oct 4, 2026
cfc5f3c
Add rules epilogue for transpile src
lucic71 Oct 4, 2026
b4ea9f7
Append rules epilogue to each file in dir mode
lucic71 Oct 4, 2026
9bef864
Use CPP2RUST_EXPR_RULE/CPP2RUST_TYPE_RULE/CPP2RUST_USE_INCLUDES
lucic71 Oct 4, 2026
7608ea3
ADd rules_epilogue
lucic71 Oct 4, 2026
961bf94
clang-format
lucic71 Oct 4, 2026
86585af
Generate single epilogue file by cmake
lucic71 Oct 4, 2026
a8fcc82
Trigger CI
lucic71 Oct 4, 2026
faf4a0d
Delete epilogue synthetization
lucic71 Oct 5, 2026
bcb639d
Move DefineImplicitMembers in converter_lib
lucic71 Oct 5, 2026
e5c8a68
Revert CPP2RUST macros
lucic71 Oct 5, 2026
6290c74
Handle rules pragma in frontend actions
lucic71 Oct 5, 2026
7dbcd69
Update tests
lucic71 Oct 5, 2026
3bd4dd3
Make ExprKey and TypeKey public
lucic71 Oct 5, 2026
1b8e77d
Extract Init in init.h
lucic71 Oct 5, 2026
c85de17
Add rule loader
lucic71 Oct 5, 2026
07b129e
Merge branch 'master' into rules-in-tu
lucic71 Oct 5, 2026
b3787ea
Update tests
lucic71 Oct 5, 2026
0583cdc
Guard char8_t by standard version
lucic71 Oct 5, 2026
b3ce8fd
Stop supressing errors
lucic71 Oct 5, 2026
d2568bf
Delete ToStringCandidates
lucic71 Oct 5, 2026
a6e322c
Refactor dir struct
lucic71 Oct 5, 2026
dbdc4c1
Ignore builtin_types
lucic71 Oct 5, 2026
e2cdfa8
Merge rule_idnex in cpp_rule_indexer
lucic71 Oct 5, 2026
f2023fb
Include all files from common
lucic71 Oct 5, 2026
a7e1b9e
Move rules in std
lucic71 Oct 5, 2026
dcf4160
Stop depending on cpp-rule-preprocessor
lucic71 Oct 5, 2026
534f49a
Don't depend on string matcher to generate expr/type keys
lucic71 Oct 5, 2026
bb7d22e
Update tests
lucic71 Oct 5, 2026
67e5ed7
Delete RuleKey
lucic71 Oct 5, 2026
3a7a641
Remove IndexAction
lucic71 Oct 5, 2026
93fdcac
Remove rule_preprocessing_lib
lucic71 Oct 5, 2026
82ab7c9
Remove index on every build
lucic71 Oct 5, 2026
e5baa03
Remove type/expr distinction
lucic71 Oct 5, 2026
c6c9040
Drop path dependency
lucic71 Oct 5, 2026
c86c181
Revert moving rules paths
lucic71 Oct 5, 2026
b4eacf4
Update names
lucic71 Oct 5, 2026
83b9a51
Move ListFiles in rules loader
lucic71 Oct 5, 2026
5bd0e06
Build index path on the fly
lucic71 Oct 5, 2026
a3013c5
Merge branch 'master' into rules-in-tu
lucic71 Oct 5, 2026
58b1389
Move index in build/rules_index
lucic71 Oct 5, 2026
e8360f3
Fix build
lucic71 Oct 5, 2026
3dfb5dd
Merge branch 'master' into rules-in-tu
lucic71 Oct 6, 2026
abecbe5
Create a single index
lucic71 Oct 6, 2026
4e7039e
Delete the implicit member definer
lucic71 Oct 6, 2026
ffc74e7
Don't use fielsystem paths
lucic71 Oct 6, 2026
23f3649
Ingore member pointers
lucic71 Oct 6, 2026
c0e9581
Delete Component
lucic71 Oct 6, 2026
8a8fae2
Don't collect user defined types
lucic71 Oct 6, 2026
dc208f3
Check for compile errors
lucic71 Oct 6, 2026
0c8c06a
Simplify the --rules flag
lucic71 Oct 6, 2026
a2581f0
Only analyse functions inside STL
lucic71 Oct 6, 2026
621b093
Add requires field in index to filter unrelated rules
lucic71 Oct 6, 2026
7d0b369
Don't include C specific rules in C++
lucic71 Oct 6, 2026
79920eb
Fix auto rules
lucic71 Oct 6, 2026
002d013
Assert on unhandleded template argument kind
lucic71 Oct 6, 2026
5e98d8a
Assert on unhandled types
lucic71 Oct 6, 2026
02c05eb
Split C and C++ indexes
lucic71 Oct 6, 2026
400758b
Rename
lucic71 Oct 6, 2026
11e24b2
Prefer unordered variants
lucic71 Oct 6, 2026
b83a79e
Skip undeduced types
lucic71 Oct 6, 2026
c451324
Remove inc files from rule indexer
lucic71 Oct 6, 2026
8a34ee8
Ignore template template arguments
lucic71 Oct 6, 2026
acac3d0
clang-format
lucic71 Oct 6, 2026
6ace5f2
Delete pending instantiations
lucic71 Oct 6, 2026
fef866e
Fix required classes
lucic71 Oct 6, 2026
4d62cdd
Add size_type for each container
lucic71 Oct 6, 2026
5864ed7
Fix string matcher to accept size_type rules for each container
lucic71 Oct 6, 2026
23156f4
Add correct rules for std::size_t
lucic71 Oct 6, 2026
43c630d
Handle block pointer
lucic71 Oct 6, 2026
db71b07
Dump type before assert
lucic71 Oct 6, 2026
4a6e673
Stop emitting line directive
lucic71 Oct 6, 2026
7643fa1
Always include common headers
lucic71 Oct 6, 2026
84c0099
Handle vector type
lucic71 Oct 6, 2026
acf54ed
Merge branch 'master' into rules-in-tu
lucic71 Oct 6, 2026
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
27 changes: 25 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,17 @@ add_custom_command(
)

file(GLOB rule_subdirs CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/rules/*)
file(GLOB rule_common_headers CONFIGURE_DEPENDS ${PROJECT_SOURCE_DIR}/rules/common/*.h)
file(GLOB rule_indexer_sources CONFIGURE_DEPENDS
${PROJECT_SOURCE_DIR}/cpp2rust/rule_preprocessing/*
${PROJECT_SOURCE_DIR}/cpp2rust/converter/rules/rules_loader.*)
set(_common_headers)
foreach(_header IN LISTS rule_common_headers)
list(APPEND _common_headers --common-header=${_header})
endforeach()
set(cpp_rules_ir_outputs)
set(cpp_rules_index_inputs)
set(cpp_rules_index_outputs ${CMAKE_BINARY_DIR}/rules_index/c.json ${CMAKE_BINARY_DIR}/rules_index/cpp.json)
set(rust_rules_ir_outputs)
foreach(_rule_dir IN LISTS rule_subdirs)
file(GLOB _cpp_srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
Expand All @@ -229,7 +239,7 @@ foreach(_rule_dir IN LISTS rule_subdirs)
get_filename_component(_rule_name ${_rule_dir} NAME)
set(_out_dir ${RULES_IR_DIR}/${_rule_name})
set(_cpp_out ${_out_dir}/ir_src.json)
set(_cxxflags --cxxflags=-isystem${_rule_dir})
set(_cxxflags)
set(_cxxflags_file)
if(EXISTS ${_rule_dir}/cxxflags)
set(_cxxflags_file ${_rule_dir}/cxxflags)
Expand All @@ -242,11 +252,15 @@ foreach(_rule_dir IN LISTS rule_subdirs)
OUTPUT ${_cpp_out}
COMMAND ${CMAKE_COMMAND} -E make_directory ${_out_dir}
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir} --out ${_cpp_out} ${_cxxflags}
DEPENDS ${_cpp_srcs} ${_cxxflags_file} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER} ${BROTLI_HEADERS}
DEPENDS ${_cpp_srcs} ${_cxxflags_file} ${rule_common_headers} ${PROJECT_SOURCE_DIR}/cpp2rust/rule_preprocessing/cpp_rule_preprocessor.cpp ${RUSTLS_HEADER} ${BROTLI_HEADERS}
VERBATIM
)
list(APPEND cpp_rules_ir_outputs ${_cpp_out})

if(NOT _rule_name STREQUAL "builtin_types")
list(APPEND cpp_rules_index_inputs ${_cpp_srcs} ${_cxxflags_file})
endif()

set(_rust_srcs)
set(_rust_out)
if(EXISTS ${_rule_dir}/tgt_unsafe.rs)
Expand Down Expand Up @@ -277,6 +291,15 @@ endforeach()

add_custom_target("preprocess-cpp-rules" ALL DEPENDS ${cpp_rules_ir_outputs})

add_custom_command(
OUTPUT ${cpp_rules_index_outputs}
COMMAND $<TARGET_FILE:cpp-rule-indexer> --exclude builtin_types ${_common_headers}
DEPENDS ${cpp_rules_index_inputs} ${rule_common_headers} ${rule_indexer_sources} ${RUSTLS_HEADER} ${BROTLI_HEADERS}
VERBATIM
)

add_custom_target("index-cpp-rules" ALL DEPENDS ${cpp_rules_index_outputs})

add_custom_target("preprocess-rust-rules" ALL
DEPENDS ${rust_rules_ir_outputs})

Expand Down
12 changes: 10 additions & 2 deletions cpp2rust/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
file(GLOB_RECURSE CORE_SOURCES "*.cpp")
file(GLOB RULE_PREPROCESSING_SOURCES "rule_preprocessing/*.cpp")
list(REMOVE_ITEM CORE_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/cpp2rust.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/cpp_rule_preprocessor.cpp"
${RULE_PREPROCESSING_SOURCES}
)

add_library(cpp2rust_core STATIC ${CORE_SOURCES})
Expand All @@ -17,6 +18,8 @@ target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_C_COMPILER=\"${CMAKE_C_
target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_CXX_COMPILER=\"${CMAKE_CXX_COMPILER}\"")
target_compile_definitions(cpp2rust_core PUBLIC "-DCLANG_RESOURCE_DIR=\"${LLVM_LIBRARY_DIR}/clang/${LLVM_VERSION_MAJOR}\"")
target_compile_definitions(cpp2rust_core PUBLIC "-DCOMPAT_INCLUDE_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/compat\"")
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_SRC_DIR=\"${PROJECT_SOURCE_DIR}/rules\"")
target_compile_definitions(cpp2rust_core PUBLIC "-DRULES_INDEX_DIR=\"${CMAKE_BINARY_DIR}/rules_index\"")
target_compile_definitions(cpp2rust_core PUBLIC "-DRUST_STABLE_VERSION=\"${RUST_STABLE_VERSION}\"")

if (APPLE)
Expand All @@ -34,5 +37,10 @@ target_link_libraries(cpp2rust_core PUBLIC ${llvm_libs})
add_clang_executable(cpp2rust PARTIAL_SOURCES_INTENDED cpp2rust.cpp)
target_link_libraries(cpp2rust PRIVATE cpp2rust_core)

add_clang_executable(cpp-rule-preprocessor PARTIAL_SOURCES_INTENDED cpp_rule_preprocessor.cpp)
add_clang_executable(cpp-rule-preprocessor PARTIAL_SOURCES_INTENDED
rule_preprocessing/cpp_rule_preprocessor.cpp)
target_link_libraries(cpp-rule-preprocessor PRIVATE cpp2rust_core)

add_clang_executable(cpp-rule-indexer PARTIAL_SOURCES_INTENDED
rule_preprocessing/cpp_rule_indexer.cpp)
target_link_libraries(cpp-rule-indexer PRIVATE cpp2rust_core)
6 changes: 6 additions & 0 deletions cpp2rust/ast_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@

#include "ast_consumer.h"

#include <llvm/Support/ErrorHandling.h>

#include "converter/converter.h"

namespace cpp2rust {
void ASTConsumer::HandleTranslationUnit(clang::ASTContext &ctx) {
if (CI_.getDiagnostics().hasErrorOccurred()) {
llvm::report_fatal_error(
"the translation unit and its loaded rules do not compile");
}
auto converter = CreateConverter(rs_code_, ctx, model_, rules_dir_);
converter->SetSema(CI_.getSema());
if (first_) {
Expand Down
44 changes: 1 addition & 43 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,49 +965,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
}

void Converter::DefineImplicitMembers(clang::CXXRecordDecl *decl) {
clang::Scope tu_scope(nullptr, clang::Scope::DeclScope,
sema_->getDiagnostics());
tu_scope.setEntity(ctx_.getTranslationUnitDecl());
auto *saved_tu_scope = std::exchange(sema_->TUScope, &tu_scope);
sema_->ForceDeclarationOfImplicitMembers(decl);
for (auto ctor : decl->ctors()) {
if (ctor->isCopyConstructor() && ctor->isImplicit() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted()) {
sema_->DefineImplicitCopyConstructor(decl->getLocation(), ctor);
}
if (ctor->isMoveConstructor() && !ctor->isUserProvided() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted() &&
!HasDefaultedCopyConstructor(decl)) {
sema_->DefineImplicitMoveConstructor(decl->getLocation(), ctor);
}
}
for (auto *method : decl->methods()) {
if (method->isMoveAssignmentOperator() && !method->isUserProvided() &&
!method->doesThisDeclarationHaveABody() && !method->isDeleted() &&
!HasDefaultedCopyAssignment(decl)) {
sema_->DefineImplicitMoveAssignment(decl->getLocation(), method);
}
}
auto define_defaulted_comparison = [&](clang::FunctionDecl *fn) {
if (!fn || !IsComparisonOperator(fn) || !fn->isDefaulted() ||
fn->doesThisDeclarationHaveABody()) {
return;
}
#if CLANG_VERSION_MAJOR >= 24
auto kind = fn->getDefaultedComparisonKind();
#else
auto kind = sema_->getDefaultedComparisonKind(fn);
#endif
sema_->DefineDefaultedComparison(decl->getLocation(), fn, kind);
};
for (auto *method : decl->methods()) {
define_defaulted_comparison(method);
}
for (auto *friend_decl : decl->friends()) {
define_defaulted_comparison(clang::dyn_cast_or_null<clang::FunctionDecl>(
friend_decl->getFriendDecl()));
}
sema_->TUScope = saved_tu_scope;
cpp2rust::DefineImplicitMembers(*sema_, decl);
}

bool Converter::VisitCXXMethodDecl(clang::CXXMethodDecl *decl) {
Expand Down
47 changes: 47 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "converter/converter_lib.h"

#include <clang/AST/DeclFriend.h>
#include <clang/AST/DeclTemplate.h>
#include <clang/AST/ExprCXX.h>
#include <clang/AST/ExprConcepts.h>
Expand Down Expand Up @@ -1270,6 +1271,52 @@ bool IsComparisonOperator(const clang::FunctionDecl *fn) {
}
}

void DefineImplicitMembers(clang::Sema &sema, clang::CXXRecordDecl *decl) {
clang::Scope tu_scope(nullptr, clang::Scope::DeclScope,
sema.getDiagnostics());
tu_scope.setEntity(sema.Context.getTranslationUnitDecl());
auto *saved_tu_scope = std::exchange(sema.TUScope, &tu_scope);
sema.ForceDeclarationOfImplicitMembers(decl);
for (auto ctor : decl->ctors()) {
if (ctor->isCopyConstructor() && ctor->isImplicit() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted()) {
sema.DefineImplicitCopyConstructor(decl->getLocation(), ctor);
}
if (ctor->isMoveConstructor() && !ctor->isUserProvided() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted() &&
!HasDefaultedCopyConstructor(decl)) {
sema.DefineImplicitMoveConstructor(decl->getLocation(), ctor);
}
}
for (auto *method : decl->methods()) {
if (method->isMoveAssignmentOperator() && !method->isUserProvided() &&
!method->doesThisDeclarationHaveABody() && !method->isDeleted() &&
!HasDefaultedCopyAssignment(decl)) {
sema.DefineImplicitMoveAssignment(decl->getLocation(), method);
}
}
auto define_defaulted_comparison = [&](clang::FunctionDecl *fn) {
if (!fn || !IsComparisonOperator(fn) || !fn->isDefaulted() ||
fn->doesThisDeclarationHaveABody()) {
return;
}
#if CLANG_VERSION_MAJOR >= 24
auto kind = fn->getDefaultedComparisonKind();
#else
auto kind = sema.getDefaultedComparisonKind(fn);
#endif
sema.DefineDefaultedComparison(decl->getLocation(), fn, kind);
};
for (auto *method : decl->methods()) {
define_defaulted_comparison(method);
}
for (auto *friend_decl : decl->friends()) {
define_defaulted_comparison(clang::dyn_cast_or_null<clang::FunctionDecl>(
friend_decl->getFriendDecl()));
}
sema.TUScope = saved_tu_scope;
}

bool IsEmittableMethod(clang::CXXMethodDecl *method) {
if (method->isDeleted()) {
return false;
Expand Down
3 changes: 3 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ bool IsConvertibleCXXRecordDecl(const clang::CXXRecordDecl *decl);
bool IsConvertibleCXXMethodDecl(const clang::CXXMethodDecl *decl);

bool IsComparisonOperator(const clang::FunctionDecl *fn);

void DefineImplicitMembers(clang::Sema &sema, clang::CXXRecordDecl *decl);

bool IsEmittableMethod(clang::CXXMethodDecl *method);

bool IsMethodOnPtr(const clang::CXXMethodDecl *method);
Expand Down
15 changes: 14 additions & 1 deletion cpp2rust/converter/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,20 @@ std::string ToString(clang::ASTContext &ctx, clang::QualType qual_type,
}
if (const auto *typedef_type = t->getAs<clang::TypedefType>()) {
if (t.getCanonicalType()->isBuiltinType()) {
return typedef_type->getDecl()->getNameAsString();
auto decl = typedef_type->getDecl();
if (auto record =
clang::dyn_cast<clang::RecordDecl>(decl->getDeclContext());
record && !record->isDependentContext()) {
return ToString(ctx, ctx.getCanonicalTagType(record)) +
"::" + decl->getNameAsString();
}
if (decl->getDeclContext()->getRedeclContext()->isNamespace()) {
std::string name;
llvm::raw_string_ostream os(name);
decl->printQualifiedName(os, getPrintPolicy(ctx));
return name;
}
return decl->getNameAsString();
}
} else if (const auto *predef = t->getAs<clang::PredefinedSugarType>()) {
return predef->getIdentifier()->getName().str();
Expand Down
Loading
Loading