Skip to content

Commit fb1e8ea

Browse files
authored
Disable verbose logging by default (#50)
Logging can be re-enabled using the `--verbose` option
1 parent e3b11db commit fb1e8ea

11 files changed

Lines changed: 102 additions & 65 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ bool Converter::VisitBuiltinType(clang::BuiltinType *type) {
122122
break;
123123
default:
124124
// FIXME: improve error handling
125-
llvm::errs() << "unsupported builtin type\n";
125+
log() << "unsupported builtin type\n";
126126
break;
127127
}
128128
return false;
@@ -151,7 +151,7 @@ bool Converter::VisitRecordType(clang::RecordType *type) {
151151
}
152152

153153
std::string Converter::ConvertPointer(clang::Expr *expr, int line) {
154-
llvm::errs() << "ConvertPointer called from line " << line << "\n";
154+
log() << "ConvertPointer called from line " << line << "\n";
155155
PushExprKind push(*this, ExprKind::AddrOf);
156156
return ToString(expr);
157157
}
@@ -175,7 +175,7 @@ std::string Converter::ConvertLValue(clang::Expr *expr) {
175175
}
176176

177177
std::string Converter::ConvertRValue(clang::Expr *expr, int line) {
178-
llvm::errs() << "ConvertRValue called from line " << line << "\n";
178+
log() << "ConvertRValue called from line " << line << "\n";
179179
PushExprKind push(*this, ExprKind::RValue);
180180
return ToString(expr);
181181
}
@@ -295,7 +295,7 @@ bool Converter::VisitFunctionDecl(clang::FunctionDecl *decl) {
295295
if (!IsInMainFile(decl) && !decl_ids_.insert(GetID(decl)).second) {
296296
return false;
297297
}
298-
decl->dump();
298+
decl->dump(log());
299299
curr_function_ = decl;
300300
std::string function_name;
301301
if (decl->isMain()) {
@@ -582,7 +582,7 @@ static bool recordDerivesCopy(const clang::RecordDecl *decl) {
582582
}
583583

584584
bool Converter::VisitRecordDecl(clang::RecordDecl *decl) {
585-
decl->dumpColor();
585+
decl->dump(log());
586586

587587
// VisitCXXRecordDecl already visited the record
588588
if (clang::isa<clang::CXXRecordDecl>(decl)) {
@@ -694,7 +694,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
694694
materializeTemplateSpecialization(decl);
695695
}
696696

697-
decl->dump();
697+
decl->dump(log());
698698

699699
Mapper::AddRuleForUserDefinedType(decl);
700700
if (!IsConvertibleCXXRecordDecl(decl)) {
@@ -741,7 +741,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
741741
}
742742

743743
bool Converter::VisitCXXMethodDecl(clang::CXXMethodDecl *decl) {
744-
decl->dump();
744+
decl->dump(log());
745745
if (!IsConvertibleCXXMethodDecl(decl)) {
746746
return false;
747747
}
@@ -1081,10 +1081,10 @@ bool Converter::VisitCXXForRangeStmt(clang::CXXForRangeStmt *stmt) {
10811081

10821082
if (!Mapper::Contains(range_init_type.getUnqualifiedType())) {
10831083
// FIXME: improve error handling
1084-
llvm::errs() << "for range stmts only for types in std namespace\n";
1084+
log() << "for range stmts only for types in std namespace\n";
10851085
}
10861086

1087-
llvm::errs() << "GetClassName: " << GetClassName(range_init_type) << "\n";
1087+
log() << "GetClassName: " << GetClassName(range_init_type) << "\n";
10881088

10891089
if (GetClassName(range_init_type) == "std::map") {
10901090
return VisitCXXForRangeStmtMap(stmt);
@@ -2712,7 +2712,7 @@ bool Converter::VisitUnaryExprOrTypeTraitExpr(
27122712
break;
27132713
default:
27142714
// FIXME: improve error handling
2715-
llvm::errs() << "unsupported unary expr or type trait expr\n";
2715+
log() << "unsupported unary expr or type trait expr\n";
27162716
}
27172717
return false;
27182718
}
@@ -3457,7 +3457,7 @@ void Converter::ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *op,
34573457
default:
34583458
// FIXME: improve error handling
34593459
llvm::errs() << "unsupported unsigned binary operator: " << opcode << '\n';
3460-
op->dumpColor();
3460+
op->dump();
34613461
assert(0);
34623462
}
34633463
PushParen paren(*this);
@@ -3767,9 +3767,9 @@ void Converter::SetFreshType(clang::QualType type) {
37673767
}
37683768

37693769
void Converter::dump_expr_kinds() {
3770-
llvm::errs() << "isRValue: " << isRValue() << ", isXValue: " << isXValue()
3771-
<< ", isAddrOf: " << isAddrOf() << ", isObject: " << isObject()
3772-
<< ", isVoid: " << isVoid() << "\n";
3770+
log() << "isRValue: " << isRValue() << ", isXValue: " << isXValue()
3771+
<< ", isAddrOf: " << isAddrOf() << ", isObject: " << isObject()
3772+
<< ", isVoid: " << isVoid() << "\n";
37733773
}
37743774

37753775
void Converter::emplace_back_plugin_construct_arg(

cpp2rust/converter/converter.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "converter/lex.h"
1919
#include "converter/translation_rule.h"
20+
#include "logging.h"
2021

2122
namespace cpp2rust {
2223
class Converter : public clang::RecursiveASTVisitor<Converter> {
@@ -331,8 +332,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
331332

332333
template <typename... Ts>
333334
inline void _StrCat(const char *func, int line, const Ts &...vals) {
334-
llvm::errs() << '[' << func << ':' << line << "] ";
335-
((llvm::errs() << vals << '\n', *rs_code_ += vals, *rs_code_ += ' '), ...);
335+
log() << '[' << func << ':' << line << "] ";
336+
((log() << vals << '\n', *rs_code_ += vals, *rs_code_ += ' '), ...);
336337
}
337338

338339
class Buffer {
@@ -611,13 +612,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
611612
int line = __builtin_LINE())
612613
: c(c) {
613614
c.curr_expr_kind_.push_back(k);
614-
llvm::errs() << "PushExprKind " << file << ':' << line << ' ';
615+
log() << "PushExprKind " << file << ':' << line << ' ';
615616
c.dump_expr_kinds();
616-
llvm::errs() << '[';
617+
log() << '[';
617618
for (const auto k : c.curr_expr_kind_) {
618-
llvm::errs() << c.expr_kind_to_string(k) << ", ";
619+
log() << c.expr_kind_to_string(k) << ", ";
619620
}
620-
llvm::errs() << "]\n";
621+
log() << "]\n";
621622
}
622623
~PushExprKind() { c.curr_expr_kind_.pop_back(); }
623624
};

cpp2rust/converter/converter_lib.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ const char *GetOverloadedOperator(const clang::FunctionDecl *decl) {
418418
return "lt";
419419
default:
420420
// FIXME: improve error handling
421-
llvm::errs() << "unsupported overloaded operator\n";
421+
log() << "unsupported overloaded operator\n";
422422
return "";
423423
}
424424
}

cpp2rust/converter/converter_lib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <string_view>
1515
#include <vector>
1616

17+
#include "logging.h"
18+
1719
namespace cpp2rust {
1820

1921
// Order matters: each category is a superset of the previous one.
@@ -116,7 +118,7 @@ void ForEachTemplateArgument(
116118
break;
117119
default:
118120
// FIXME: improve logging
119-
llvm::errs() << "unsupported template argument kind\n";
121+
log() << "unsupported template argument kind\n";
120122
}
121123
}
122124
}

cpp2rust/converter/mapper.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,21 +384,20 @@ TranslationRule::ExprRule *search(const clang::Expr *expr) {
384384
auto qualified_name = ToString(expr);
385385
auto [rule, subs] =
386386
search(exprs_, qualified_name, GetExprMapKey(qualified_name));
387-
llvm::errs() << "search expr " << qualified_name << ", result:\n";
387+
log() << "search expr " << qualified_name << ", result:\n";
388388
if (rule) {
389389
rule->dump();
390390
} else {
391-
llvm::errs() << "None\n";
391+
log() << "None\n";
392392
}
393393
return rule;
394394
}
395395

396396
TranslationRule::TypeRule *search(clang::QualType qual_type) {
397397
auto type = ToString(qual_type);
398398
auto [rule, subs] = search(types_, type, GetTypeMapKey(type));
399-
llvm::errs() << "search type " << type
400-
<< ", result: " << (rule ? rule->type_info.type : "None")
401-
<< '\n';
399+
log() << "search type " << type
400+
<< ", result: " << (rule ? rule->type_info.type : "None") << '\n';
402401
return rule;
403402
}
404403

@@ -408,7 +407,7 @@ void addRulesFromDirectory(const std::filesystem::path &dir, Model model) {
408407
if (entry.is_regular_file() && path.extension() == ".cpp") {
409408
auto [expr_rules, type_rules] = TranslationRule::Load(path, model);
410409
if (expr_rules.empty() && type_rules.empty()) {
411-
llvm::errs() << "No rules found in " << path << '\n';
410+
log() << "No rules found in " << path << '\n';
412411
continue;
413412
}
414413
for (auto &[_, rule] : expr_rules) {
@@ -885,11 +884,11 @@ void LoadTranslationRules(Model model, clang::ASTContext &ctx,
885884

886885
#if 0
887886
for (auto &[src, rule] : exprs_) {
888-
llvm::errs() << "Expr key: " << src << '\n';
887+
log() << "Expr key: " << src << '\n';
889888
rule.dump();
890889
}
891890
for (auto &[src, rule] : types_) {
892-
llvm::errs() << "Type key: " << src << '\n';
891+
log() << "Type key: " << src << '\n';
893892
rule.dump();
894893
}
895894
#endif

cpp2rust/converter/models/converter_refcount.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,21 +245,21 @@ class ConverterRefCount final : public Converter {
245245
if (pushed) {
246246
c.conversion_kind_.push_back(k);
247247
}
248-
llvm::errs() << "[PushConversionKind:" << line << "] ";
248+
log() << "[PushConversionKind:" << line << "] ";
249249
for (auto ck : c.conversion_kind_) {
250-
llvm::errs() << c.ConversionKindToString(ck) << ", ";
250+
log() << c.ConversionKindToString(ck) << ", ";
251251
}
252-
llvm::errs() << "\n";
252+
log() << "\n";
253253
}
254254
~PushConversionKind() {
255255
if (pushed) {
256256
c.conversion_kind_.pop_back();
257257
}
258-
llvm::errs() << "[PopConversionKind] ";
258+
log() << "[PopConversionKind] ";
259259
for (auto ck : c.conversion_kind_) {
260-
llvm::errs() << c.ConversionKindToString(ck) << ", ";
260+
log() << c.ConversionKindToString(ck) << ", ";
261261
}
262-
llvm::errs() << "\n";
262+
log() << "\n";
263263
}
264264
};
265265

cpp2rust/converter/plugins/emplace_back.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ bool Converter::emplace_back_plugin_convert(clang::CallExpr *call) {
182182
StrCat(GetUnsafeTypeAsString(elem_ty));
183183
}
184184
} else {
185-
call->dumpColor();
185+
call->dump();
186186
assert(0 && "no ctor and no pod type");
187187
return false;
188188
}

cpp2rust/converter/translation_rule.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "compat/platform_flags.h"
3030
#include "converter/mapper.h"
31+
#include "logging.h"
3132

3233
namespace cpp2rust::TranslationRule {
3334

@@ -902,21 +903,19 @@ void BodyFragmentDump(const BodyFragment &frag) {
902903

903904
} // namespace
904905

905-
void TextFragment::dump() const {
906-
llvm::errs() << " text: \"" << text << "\"\n";
907-
}
906+
void TextFragment::dump() const { log() << " text: \"" << text << "\"\n"; }
908907

909908
void PlaceholderFragment::dump() const {
910-
llvm::errs() << " placeholder: " << n;
909+
log() << " placeholder: " << n;
911910
switch (access) {
912911
case Access::kRead:
913-
llvm::errs() << " (read)\n";
912+
log() << " (read)\n";
914913
break;
915914
case Access::kWrite:
916-
llvm::errs() << " (write)\n";
915+
log() << " (write)\n";
917916
break;
918917
case Access::kMove:
919-
llvm::errs() << " (move)\n";
918+
log() << " (move)\n";
920919
break;
921920
}
922921
}
@@ -931,61 +930,59 @@ const PlaceholderFragment *MethodCallFragment::getReceiverPlaceholder() const {
931930
}
932931

933932
void MethodCallFragment::dump() const {
934-
llvm::errs() << " method_call:\n"
935-
" receiver:\n";
933+
log() << " method_call:\n"
934+
" receiver:\n";
936935
for (const auto &frag : receiver) {
937936
BodyFragmentDump(frag);
938937
}
939-
llvm::errs() << " body:\n";
938+
log() << " body:\n";
940939
for (const auto &frag : body) {
941940
BodyFragmentDump(frag);
942941
}
943942
}
944943

945944
void ExprRule::dump() const {
946-
llvm::errs() << "Matching: " << src << '\n';
945+
log() << "Matching: " << src << '\n';
947946
unsigned i = 0;
948947
for (auto &info : params) {
949-
llvm::errs() << " param a" << i++ << ": ";
948+
log() << " param a" << i++ << ": ";
950949
info.dump();
951-
llvm::errs() << '\n';
950+
log() << '\n';
952951
}
953952
if (!return_type.type.empty()) {
954-
llvm::errs() << " return: ";
953+
log() << " return: ";
955954
return_type.dump();
956-
llvm::errs() << '\n';
955+
log() << '\n';
957956
}
958957
i = 0;
959958
for (auto &bounds : generics) {
960-
llvm::errs() << " generic T" << ++i << ':';
959+
log() << " generic T" << ++i << ':';
961960
for (auto &b : bounds) {
962-
llvm::errs() << ' ' << b;
961+
log() << ' ' << b;
963962
}
964-
llvm::errs() << '\n';
963+
log() << '\n';
965964
}
966965
for (const auto &frag : body) {
967966
BodyFragmentDump(frag);
968967
}
969968
}
970969

971-
void GenericFragment::dump() const {
972-
llvm::errs() << " generic: " << n << '\n';
973-
}
970+
void GenericFragment::dump() const { log() << " generic: " << n << '\n'; }
974971

975972
void TypeInfo::dump() const {
976-
llvm::errs() << type;
973+
log() << type;
977974
if (is_refcount_pointer)
978-
llvm::errs() << " [rc_ptr]";
975+
log() << " [rc_ptr]";
979976
if (is_unsafe_pointer)
980-
llvm::errs() << " [unsafe_ptr]";
977+
log() << " [unsafe_ptr]";
981978
}
982979

983980
void TypeRule::dump() const {
984-
llvm::errs() << "name: " << src << "\n Rust type: ";
981+
log() << "name: " << src << "\n Rust type: ";
985982
type_info.dump();
986-
llvm::errs() << '\n';
983+
log() << '\n';
987984
if (!initializer.empty()) {
988-
llvm::errs() << " init: " << initializer << '\n';
985+
log() << " init: " << initializer << '\n';
989986
}
990987
}
991988

0 commit comments

Comments
 (0)