Skip to content

Commit fd4da36

Browse files
committed
minor code simplifications
1 parent c7b322b commit fd4da36

5 files changed

Lines changed: 45 additions & 47 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool Converter::Convert(clang::QualType qual_type) {
7878
}
7979

8080
auto mapped = Mapper::Map(qual_type);
81-
if (!mapped.empty() && mapped != ignore_rule_type_) {
81+
if (!mapped.empty() && mapped != token::kIgnoreRule) {
8282
StrCat(mapped);
8383
return false;
8484
}
@@ -89,7 +89,7 @@ bool Converter::Convert(clang::QualType qual_type) {
8989

9090
bool Converter::ConvertMappedType(clang::QualType qual_type) {
9191
std::string type_as_string = Mapper::Map(qual_type);
92-
if (type_as_string == ignore_rule_type_) {
92+
if (type_as_string == token::kIgnoreRule) {
9393
return false;
9494
}
9595
StrCat(type_as_string);
@@ -2723,7 +2723,7 @@ bool Converter::VisitArraySubscriptExpr(clang::ArraySubscriptExpr *expr) {
27232723
}
27242724

27252725
bool Converter::VisitCXXNullPtrLiteralExpr(clang::CXXNullPtrLiteralExpr *expr) {
2726-
StrCat(keyword_default_);
2726+
StrCat(token::kDefault);
27272727
computed_expr_type_ = ComputedExprType::FreshPointer;
27282728
return false;
27292729
}
@@ -2753,7 +2753,7 @@ bool Converter::VisitVAArgExpr(clang::VAArgExpr *expr) {
27532753
}
27542754

27552755
bool Converter::VisitGNUNullExpr(clang::GNUNullExpr *expr) {
2756-
StrCat(keyword_default_);
2756+
StrCat(token::kDefault);
27572757
computed_expr_type_ = ComputedExprType::FreshPointer;
27582758
return false;
27592759
}
@@ -2976,7 +2976,7 @@ void Converter::AddIncDecImpls(clang::EnumDecl *decl) {
29762976

29772977
bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) {
29782978
if (expr->getType()->isPointerType()) {
2979-
StrCat(keyword_default_);
2979+
StrCat(token::kDefault);
29802980
}
29812981
return false;
29822982
}
@@ -3297,7 +3297,7 @@ Converter::GetStructAttributes(const clang::RecordDecl *decl) {
32973297
return {"Copy", "Clone"};
32983298
}
32993299

3300-
std::vector<const char *> struct_attrs = {};
3300+
std::vector<const char *> struct_attrs;
33013301

33023302
if (RecordDerivesCopy(decl)) {
33033303
struct_attrs.emplace_back("Copy");

cpp2rust/converter/converter.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
2626
explicit Converter(std::string &rs_code, clang::ASTContext &ctx,
2727
const char *keyword_unsafe = "unsafe",
2828
const char *keyword_mut = "mut",
29-
const char *keyword_ptr_decay = ".as_mut_ptr()",
3029
const char *keyword_const_fn = keyword::kConst)
3130
: rs_code_(&rs_code), ctx_(ctx), keyword_unsafe_(keyword_unsafe),
32-
keyword_mut_(keyword_mut), keyword_ptr_decay_(keyword_ptr_decay),
33-
keyword_const_fn_(keyword_const_fn) {}
31+
keyword_mut_(keyword_mut), keyword_const_fn_(keyword_const_fn) {}
3432

3533
virtual ~Converter() = default;
3634

@@ -799,13 +797,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
799797

800798
std::string getIntegerLiteral(clang::IntegerLiteral *expr, bool incl_type,
801799
const clang::QualType *type = nullptr);
802-
const char *keyword_default_ = "Default::default()";
803800
const char *keyword_unsafe_;
804801
const char *keyword_mut_;
805-
const char *keyword_ptr_decay_;
806802
const char *keyword_const_fn_;
807-
const char *keyword_ptr_decay_const_ = ".as_ptr()";
808-
const char *ignore_rule_type_ = "libcc2rs::IgnoreRule";
809803
std::vector<ExprKind> curr_expr_kind_;
810804
static std::unordered_map<std::string, std::string> inner_structs_;
811805
static std::unordered_set<std::string> globals_;

cpp2rust/converter/converter_lib.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "converter/mapper.h"
2020

2121
// https://doc.rust-lang.org/reference/keywords.html
22-
static const char *rust_keywords[] = {
22+
static const char rust_keywords[][12] = {
2323
// Strict keywords
2424
"as",
2525
"async",
@@ -433,7 +433,7 @@ const char *AccessSpecifierAsString(clang::AccessSpecifier spec) {
433433
return keyword::kPub;
434434
case clang::AS_protected:
435435
case clang::AS_private:
436-
return token::kEmpty;
436+
return "";
437437
}
438438
std::unreachable();
439439
}

cpp2rust/converter/lex.h

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,58 @@ inline constexpr char kOpenCurlyBracket = '{';
99
inline constexpr char kCloseCurlyBracket = '}';
1010
inline constexpr char kOpenBracket = '[';
1111
inline constexpr char kCloseBracket = ']';
12-
inline constexpr const char *kSemiColon = ";\n";
12+
inline constexpr const char kSemiColon[] = ";\n";
1313
inline constexpr char kComma = ',';
1414
inline constexpr char kColon = ':';
15-
inline constexpr const char *kDoubleColon = "::";
15+
inline constexpr const char kDoubleColon[] = "::";
1616
inline constexpr char kAssign = '=';
1717
inline constexpr char kOpenParen = '(';
1818
inline constexpr char kCloseParen = ')';
1919
inline constexpr char kDot = '.';
2020
inline constexpr char kNot = '!';
21-
inline constexpr const char *kDiff = "!=";
21+
inline constexpr const char kDiff[] = "!=";
2222
inline constexpr char kZero = '0';
2323
inline constexpr char kOne = '1';
2424
inline constexpr char kRef = '&';
25-
inline constexpr const char *kStar = "*";
26-
inline constexpr const char *kArrow = "->";
25+
inline constexpr const char kStar[] = "*";
26+
inline constexpr const char kArrow[] = "->";
2727
inline constexpr char kHash = '#';
2828
inline constexpr char kMinus = '-';
2929
inline constexpr char kDiv = '/';
3030
inline constexpr char kLt = '<';
3131
inline constexpr char kGt = '>';
32-
inline constexpr const char *kEmpty = "";
3332
inline constexpr char kNewLine = '\n';
3433
} // namespace token
3534

3635
namespace keyword {
37-
inline constexpr const char *kAs = "as";
38-
inline constexpr const char *kBreak = "break";
39-
inline constexpr const char *kContinue = "continue";
40-
inline constexpr const char *kConst = "const";
41-
inline constexpr const char *kElse = "else";
42-
inline constexpr const char *kFalse = "false";
43-
inline constexpr const char *kFn = "fn";
44-
inline constexpr const char *kIf = "if";
45-
inline constexpr const char *kImpl = "impl";
46-
inline constexpr const char *kLet = "let";
47-
inline constexpr const char *kLoop = "loop";
48-
inline constexpr const char *kPub = "pub";
49-
inline constexpr const char *kModule = "mod";
50-
inline constexpr const char *kReturn = "return";
51-
inline constexpr const char *kSelfValue = "self";
52-
inline constexpr const char *kStatic = "static";
53-
inline constexpr const char *kStruct = "struct";
54-
inline constexpr const char *kUnion = "union";
55-
inline constexpr const char *kTrue = "true";
56-
inline constexpr const char *kWhile = "while";
57-
inline constexpr const char *kFor = "for";
58-
inline constexpr const char *kIn = "in";
59-
inline constexpr const char *kTrait = "trait";
60-
inline constexpr const char *kDyn = "dyn";
36+
inline constexpr const char kAs[] = "as";
37+
inline constexpr const char kBreak[] = "break";
38+
inline constexpr const char kContinue[] = "continue";
39+
inline constexpr const char kConst[] = "const";
40+
inline constexpr const char kElse[] = "else";
41+
inline constexpr const char kFalse[] = "false";
42+
inline constexpr const char kFn[] = "fn";
43+
inline constexpr const char kIf[] = "if";
44+
inline constexpr const char kImpl[] = "impl";
45+
inline constexpr const char kLet[] = "let";
46+
inline constexpr const char kLoop[] = "loop";
47+
inline constexpr const char kPub[] = "pub";
48+
inline constexpr const char kModule[] = "mod";
49+
inline constexpr const char kReturn[] = "return";
50+
inline constexpr const char kSelfValue[] = "self";
51+
inline constexpr const char kStatic[] = "static";
52+
inline constexpr const char kStruct[] = "struct";
53+
inline constexpr const char kUnion[] = "union";
54+
inline constexpr const char kTrue[] = "true";
55+
inline constexpr const char kWhile[] = "while";
56+
inline constexpr const char kFor[] = "for";
57+
inline constexpr const char kIn[] = "in";
58+
inline constexpr const char kTrait[] = "trait";
59+
inline constexpr const char kDyn[] = "dyn";
6160
} // namespace keyword
61+
62+
namespace token {
63+
inline constexpr const char kDefault[] = "Default::default()";
64+
inline constexpr const char kIgnoreRule[] = "libcc2rs::IgnoreRule";
65+
}
6266
} // namespace cpp2rust

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace cpp2rust {
1717
ConverterRefCount::ConverterRefCount(std::string &rs_code,
1818
clang::ASTContext &ctx)
19-
: Converter(rs_code, ctx, "", "", ".as_pointer()", ""),
19+
: Converter(rs_code, ctx, "", "", ""),
2020
conversion_kind_({ConversionKind::Unboxed}) {}
2121

2222
void ConverterRefCount::EmitFilePreamble() {
@@ -1755,7 +1755,7 @@ ConverterRefCount::ConvertVarDefaultInit(clang::QualType qual_type) {
17551755

17561756
std::vector<const char *>
17571757
ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
1758-
std::vector<const char *> attrs = {};
1758+
std::vector<const char *> attrs;
17591759

17601760
if (RecordDerivesDefault(decl)) {
17611761
attrs.emplace_back("Default");

0 commit comments

Comments
 (0)