From 54a0e018059037eb81d59268f2713370e6227d3e Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Wed, 16 Apr 2025 18:35:26 -0700 Subject: [PATCH] binpac: Wrap generated switch statements in NOLINTs for bugprone-branch-clone Binpac generates a lot of switch statements with repeated blocks in them (typically empty blocks). Running clang-tidy on the generated code with bugprone-branch-clone generates a lot of warnings. Instead of doing a ton of analysis in binpac to avoid generating the duplicates, just mark any switch generated with an annotation to avoid reporting them. --- tools/binpac/src/pac_case.cc | 6 ++++++ tools/binpac/src/pac_expr.cc | 2 ++ tools/binpac/src/pac_record.cc | 2 ++ tools/binpac/src/pac_type.cc | 2 ++ 4 files changed, 12 insertions(+) diff --git a/tools/binpac/src/pac_case.cc b/tools/binpac/src/pac_case.cc index 187eaf37c8..cc629d2253 100644 --- a/tools/binpac/src/pac_case.cc +++ b/tools/binpac/src/pac_case.cc @@ -117,6 +117,7 @@ void CaseType::GenCleanUpCode(Output* out_cc, Env* env) { Type::GenCleanUpCode(out_cc, env); env->set_in_branch(true); + out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)"); out_cc->println("switch ( %s ) {", env->RValue(index_var_)); out_cc->inc_indent(); foreach (i, CaseFieldList, cases_) { @@ -125,6 +126,7 @@ void CaseType::GenCleanUpCode(Output* out_cc, Env* env) { } out_cc->dec_indent(); out_cc->println("}"); + out_cc->println("// NOLINTEND(bugprone-branch-clone)"); env->set_in_branch(false); } @@ -141,6 +143,7 @@ void CaseType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, int env->SetEvaluated(index_var_); env->set_in_branch(true); + out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)"); out_cc->println("switch ( %s ) {", env->RValue(index_var_)); out_cc->inc_indent(); bool has_default_case = false; @@ -161,6 +164,7 @@ void CaseType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, int } out_cc->dec_indent(); out_cc->println("}"); + out_cc->println("// NOLINTEND(bugprone-branch-clone)"); env->set_in_branch(false); if ( compute_size_var ) @@ -307,6 +311,7 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) { if ( ! index_ ) out_h->println("return %s;", lvalue()); else { + out_h->println("// NOLINTBEGIN(bugprone-branch-clone)"); out_h->println("switch ( %s ) {", env->RValue(index_var_)); out_h->inc_indent(); GenCaseStr(index_, out_h, env, case_type()->IndexExpr()->DataType(env)); @@ -323,6 +328,7 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) { out_h->dec_indent(); out_h->println("}"); + out_h->println("// NOLINTEND(bugprone-branch-clone)"); out_h->println("return %s;", lvalue()); } diff --git a/tools/binpac/src/pac_expr.cc b/tools/binpac/src/pac_expr.cc index 4f74859f72..fca87cff66 100644 --- a/tools/binpac/src/pac_expr.cc +++ b/tools/binpac/src/pac_expr.cc @@ -209,6 +209,7 @@ void Expr::GenCaseEval(Output* out_cc, Env* env) { foreach (i, CaseExprList, cases_) (*i)->value()->ForceIDEval(out_cc, env); + out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)"); out_cc->println("switch ( %s ) {", operand_[0]->EvalExpr(out_cc, env)); Type* switch_type = operand_[0]->DataType(env); @@ -247,6 +248,7 @@ void Expr::GenCaseEval(Output* out_cc, Env* env) { out_cc->dec_indent(); out_cc->println("}"); + out_cc->println("// NOLINTEND(bugprone-branch-clone)"); env->SetEvaluated(val_var); str_ = env->RValue(val_var); diff --git a/tools/binpac/src/pac_record.cc b/tools/binpac/src/pac_record.cc index a65074751c..145604b13a 100644 --- a/tools/binpac/src/pac_record.cc +++ b/tools/binpac/src/pac_record.cc @@ -100,6 +100,7 @@ void RecordType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, i GenBoundaryCheck(out_cc, env, data); if ( incremental_parsing() ) { + out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)"); out_cc->println("switch ( %s ) {", env->LValue(parsing_state_id)); out_cc->println("case 0:"); @@ -113,6 +114,7 @@ void RecordType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, i out_cc->println("%s = true;", env->LValue(parsing_complete_var())); out_cc->dec_indent(); out_cc->println("}"); + out_cc->println("// NOLINTEND(bugprone-branch-clone)"); } else { ASSERT(data.id() == begin_of_data && data.offset() == 0); diff --git a/tools/binpac/src/pac_type.cc b/tools/binpac/src/pac_type.cc index 9ea73fd339..7a66f65a87 100644 --- a/tools/binpac/src/pac_type.cc +++ b/tools/binpac/src/pac_type.cc @@ -487,6 +487,7 @@ void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) { if ( attr_length_expr() ) { ASSERT(buffer_mode() == BUFFER_BY_LENGTH); + out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)"); out_cc->println("switch ( %s ) {", env->LValue(buffering_state_id)); out_cc->inc_indent(); out_cc->println("case 0:"); @@ -537,6 +538,7 @@ void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) { out_cc->dec_indent(); out_cc->dec_indent(); out_cc->println("}"); + out_cc->println("// NOLINTEND(bugprone-branch-clone)"); } else if ( attr_restofflow_ ) { out_cc->println("BINPAC_ASSERT(%s->eof());", env->RValue(flow_buffer_id));