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
22 changes: 18 additions & 4 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,11 @@ std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
}

bool Converter::VisitIntegerLiteral(clang::IntegerLiteral *expr) {
if (auto str = GetMappedAsString(expr); !str.empty()) {
StrCat(str);
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
}
StrCat(getIntegerLiteral(expr, Mapper::Map(expr->getType()) != "i32"));
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
Expand Down Expand Up @@ -1774,6 +1779,15 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) {
}
}

bool Converter::IsCastRedundantInRust(clang::Expr *expr,
clang::QualType target_type) {
auto target = GetUnsafeTypeAsString(target_type);
if (const auto *rule = Mapper::GetExprRule(expr)) {
return rule->return_type.type == target;
}
return GetUnsafeTypeAsString(expr->getType()) == target;
}

bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
auto *sub_expr = expr->getSubExpr();
auto type = expr->getType();
Expand Down Expand Up @@ -1873,8 +1887,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
break;
}
// Skip cast if source and target map to the same Rust type.
if (GetUnsafeTypeAsString(sub_expr->getType()) ==
GetUnsafeTypeAsString(type)) {
if (IsCastRedundantInRust(sub_expr, type)) {
Convert(sub_expr);
break;
}
Expand Down Expand Up @@ -3129,7 +3142,7 @@ std::string Converter::GetUnsafeTypeAsString(clang::QualType qual_type) const {
std::string type_as_string;
Converter converter(type_as_string, ctx_);
converter.Convert(qual_type);
return type_as_string;
return std::string(Trim(type_as_string));
}

void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
Expand Down Expand Up @@ -3567,7 +3580,8 @@ void Converter::ConvertDeref(clang::Expr *expr) {

void Converter::ConvertArrow(clang::Expr *expr) { ConvertDeref(expr); }

void Converter::ConvertCast(clang::QualType qual_type) {
void Converter::ConvertCast(clang::QualType qual_type, int line) {
log() << "[ConvertCast] Called from line " << line << "\n";
StrCat(keyword::kAs, GetUnsafeTypeAsString(qual_type));
}

Expand Down
5 changes: 4 additions & 1 deletion cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual void ConvertArrow(clang::Expr *expr);

virtual void ConvertCast(clang::QualType qual_type);
virtual void ConvertCast(clang::QualType qual_type,
int line = __builtin_LINE());

virtual void ConvertLoopVariable(clang::VarDecl *decl,
clang::Expr *range_init);
Expand Down Expand Up @@ -705,6 +706,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

TempMaterializationCtx CollectPrvalueToLRefArgs(clang::CallExpr *expr);

bool IsCastRedundantInRust(clang::Expr *expr, clang::QualType target_type);

private:
void materializeTemplateSpecialization(clang::CXXRecordDecl *decl);

Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ bool SwitchHasFallthrough(clang::SwitchStmt *stmt) {
return false;
}

static std::string_view Trim(std::string_view s) {
std::string_view Trim(std::string_view s) {
auto is_space = [](unsigned char c) { return std::isspace(c); };
auto b = std::find_if_not(s.begin(), s.end(), is_space);
auto e = std::find_if_not(s.rbegin(), s.rend(), is_space).base();
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ std::vector<clang::Stmt *> GetSwitchCaseBody(clang::CompoundStmt *body,

bool SwitchHasFallthrough(clang::SwitchStmt *stmt);

std::string_view Trim(std::string_view s);

void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);

std::string ReplaceAll(std::string str, std::string_view from,
Expand Down
11 changes: 11 additions & 0 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <clang/AST/ExprCXX.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Lexer.h>
#include <llvm/Support/ThreadPool.h>

#include <format>
Expand Down Expand Up @@ -804,6 +805,16 @@ std::string ToString(const clang::Expr *expr) {

expr = expr->IgnoreParenImpCasts();

if (llvm::isa<clang::IntegerLiteral>(expr) &&
expr->getBeginLoc().isMacroID()) {
auto &sm = ctx_->getSourceManager();
auto name = clang::Lexer::getImmediateMacroName(expr->getBeginLoc(), sm,
ctx_->getLangOpts());
if (!name.empty()) {
return name.str();
}
}

if (const auto *CE = llvm::dyn_cast<clang::CallExpr>(expr)) {
if (const auto *decl = CE->getDirectCallee()) {
return ToString(decl);
Expand Down
10 changes: 9 additions & 1 deletion cpp2rust/cpp_rule_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ class Callback : public clang::ast_matchers::MatchFinder::MatchCallback {
add(Mapper::ToString(decl));
return;
}
if (const auto *lit =
R.Nodes.getNodeAs<clang::IntegerLiteral>("macro_int")) {
if (lit->getBeginLoc().isMacroID()) {
add(Mapper::ToString(lit));
}
return;
}
}
}

Expand Down Expand Up @@ -668,7 +675,8 @@ class ActionFactory : public clang::tooling::FrontendActionFactory {
declRefExpr(to(decl(unless(parmVarDecl()))))))
.bind("udeclref"),
cxxDependentScopeMemberExpr().bind("dsme"),
cxxUnresolvedConstructExpr().bind("uctor"))))),
cxxUnresolvedConstructExpr().bind("uctor"),
integerLiteral().bind("macro_int"))))),
hasAncestor(functionDecl(isDefinition(),
matchesName("(^|::)f[0-9]+$"),
isExpansionInMainFile())
Expand Down
3 changes: 2 additions & 1 deletion rules/brotli/brotli/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ typedef enum {
BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_1, -14) SEPARATOR \
BROTLI_ERROR_CODE(_ERROR_FORMAT_, PADDING_2, -15) SEPARATOR \
\
/* -16..-19 codes are reserved */ \
/* -16..-18 codes are reserved */ \
\
BROTLI_ERROR_CODE(_ERROR_, DICTIONARY_NOT_SET, -19) SEPARATOR \
BROTLI_ERROR_CODE(_ERROR_, INVALID_ARGUMENTS, -20) SEPARATOR \
\
/* Memory allocation problems */ \
Expand Down
Loading
Loading