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.
This commit is contained in:
Tim Wojtulewicz 2025-04-16 18:35:26 -07:00
parent 670c4dcbcf
commit 54a0e01805
4 changed files with 12 additions and 0 deletions

View file

@ -117,6 +117,7 @@ void CaseType::GenCleanUpCode(Output* out_cc, Env* env) {
Type::GenCleanUpCode(out_cc, env); Type::GenCleanUpCode(out_cc, env);
env->set_in_branch(true); env->set_in_branch(true);
out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)");
out_cc->println("switch ( %s ) {", env->RValue(index_var_)); out_cc->println("switch ( %s ) {", env->RValue(index_var_));
out_cc->inc_indent(); out_cc->inc_indent();
foreach (i, CaseFieldList, cases_) { foreach (i, CaseFieldList, cases_) {
@ -125,6 +126,7 @@ void CaseType::GenCleanUpCode(Output* out_cc, Env* env) {
} }
out_cc->dec_indent(); out_cc->dec_indent();
out_cc->println("}"); out_cc->println("}");
out_cc->println("// NOLINTEND(bugprone-branch-clone)");
env->set_in_branch(false); 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->SetEvaluated(index_var_);
env->set_in_branch(true); env->set_in_branch(true);
out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)");
out_cc->println("switch ( %s ) {", env->RValue(index_var_)); out_cc->println("switch ( %s ) {", env->RValue(index_var_));
out_cc->inc_indent(); out_cc->inc_indent();
bool has_default_case = false; 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->dec_indent();
out_cc->println("}"); out_cc->println("}");
out_cc->println("// NOLINTEND(bugprone-branch-clone)");
env->set_in_branch(false); env->set_in_branch(false);
if ( compute_size_var ) if ( compute_size_var )
@ -307,6 +311,7 @@ void CaseField::GenPubDecls(Output* out_h, Env* env) {
if ( ! index_ ) if ( ! index_ )
out_h->println("return %s;", lvalue()); out_h->println("return %s;", lvalue());
else { else {
out_h->println("// NOLINTBEGIN(bugprone-branch-clone)");
out_h->println("switch ( %s ) {", env->RValue(index_var_)); out_h->println("switch ( %s ) {", env->RValue(index_var_));
out_h->inc_indent(); out_h->inc_indent();
GenCaseStr(index_, out_h, env, case_type()->IndexExpr()->DataType(env)); 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->dec_indent();
out_h->println("}"); out_h->println("}");
out_h->println("// NOLINTEND(bugprone-branch-clone)");
out_h->println("return %s;", lvalue()); out_h->println("return %s;", lvalue());
} }

View file

@ -209,6 +209,7 @@ void Expr::GenCaseEval(Output* out_cc, Env* env) {
foreach (i, CaseExprList, cases_) foreach (i, CaseExprList, cases_)
(*i)->value()->ForceIDEval(out_cc, env); (*i)->value()->ForceIDEval(out_cc, env);
out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)");
out_cc->println("switch ( %s ) {", operand_[0]->EvalExpr(out_cc, env)); out_cc->println("switch ( %s ) {", operand_[0]->EvalExpr(out_cc, env));
Type* switch_type = operand_[0]->DataType(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->dec_indent();
out_cc->println("}"); out_cc->println("}");
out_cc->println("// NOLINTEND(bugprone-branch-clone)");
env->SetEvaluated(val_var); env->SetEvaluated(val_var);
str_ = env->RValue(val_var); str_ = env->RValue(val_var);

View file

@ -100,6 +100,7 @@ void RecordType::DoGenParseCode(Output* out_cc, Env* env, const DataPtr& data, i
GenBoundaryCheck(out_cc, env, data); GenBoundaryCheck(out_cc, env, data);
if ( incremental_parsing() ) { if ( incremental_parsing() ) {
out_cc->println("// NOLINTBEGIN(bugprone-branch-clone)");
out_cc->println("switch ( %s ) {", env->LValue(parsing_state_id)); out_cc->println("switch ( %s ) {", env->LValue(parsing_state_id));
out_cc->println("case 0:"); 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->println("%s = true;", env->LValue(parsing_complete_var()));
out_cc->dec_indent(); out_cc->dec_indent();
out_cc->println("}"); out_cc->println("}");
out_cc->println("// NOLINTEND(bugprone-branch-clone)");
} }
else { else {
ASSERT(data.id() == begin_of_data && data.offset() == 0); ASSERT(data.id() == begin_of_data && data.offset() == 0);

View file

@ -487,6 +487,7 @@ void Type::GenParseBuffer(Output* out_cc, Env* env, int flags) {
if ( attr_length_expr() ) { if ( attr_length_expr() ) {
ASSERT(buffer_mode() == BUFFER_BY_LENGTH); 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->println("switch ( %s ) {", env->LValue(buffering_state_id));
out_cc->inc_indent(); out_cc->inc_indent();
out_cc->println("case 0:"); 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->dec_indent(); out_cc->dec_indent();
out_cc->println("}"); out_cc->println("}");
out_cc->println("// NOLINTEND(bugprone-branch-clone)");
} }
else if ( attr_restofflow_ ) { else if ( attr_restofflow_ ) {
out_cc->println("BINPAC_ASSERT(%s->eof());", env->RValue(flow_buffer_id)); out_cc->println("BINPAC_ASSERT(%s->eof());", env->RValue(flow_buffer_id));