Skip to content

Commit

Permalink
Extend clang-tidy unit overflow check to watt and kilowatt (#71848)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettDong authored Feb 19, 2024
1 parent bdd316b commit e09b367
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void power::deserialize( const JsonValue &jv )
{
if( jv.test_int() ) {
// Compatibility with old 0.F saves
*this = from_watt( jv.get_int() );
*this = from_watt( static_cast<std::int64_t>( jv.get_int() ) );
return;
}
*this = read_from_json_string( jv, units::power_units );
Expand Down
3 changes: 2 additions & 1 deletion src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5091,7 +5091,8 @@ units::power vehicle::active_reactor_epower() const
// Only count as much power as will be drawn from the reactor to fill the batteries.
const auto [bat_remaining, bat_capacity] = connected_battery_power_level();
// max power flow into batteries for next turn
const units::power max_battery_flow = units::from_kilowatt( bat_capacity - bat_remaining );
const units::power max_battery_flow = units::from_kilowatt( static_cast<std::int64_t>
( bat_capacity - bat_remaining ) );
// power flow by all parts except reactors
const units::power non_reactor_flow = net_battery_charge_rate( false );

Expand Down
5 changes: 3 additions & 2 deletions src/vehicle_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ void vehicle::smart_controller_handle_turn( const std::optional<float> &k_tracti
if( max_battery_level == 0 || !discharge_forbidden_soft ) {
target_charging_rate = 0_W;
} else {
target_charging_rate = units::from_watt( ( max_battery_level * cfg.battery_hi / 100 -
cur_battery_level ) * 10 / ( 6 * 3 ) );
target_charging_rate = units::from_watt( static_cast<std::int64_t>( ( max_battery_level *
cfg.battery_hi / 100 -
cur_battery_level ) * 10 / ( 6 * 3 ) ) );
}
// ( max_battery_level * battery_hi / 100 - cur_battery_level ) * (1000 / (60 * 30)) // originally
// ^ battery_hi% bat to W ^ ^ 30 minutes
Expand Down
2 changes: 2 additions & 0 deletions tools/clang-tidy-plugin/UnitOverflowCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ struct QuantityUnit {
static const std::map<std::string_view, QuantityUnit> FunctionAndQuantityTypes {
{"from_joule", {"energy", 1'000LL}},
{"from_kilojoule", {"energy", 1'000'000LL}},
{"from_watt", {"power", 1'000LL}},
{"from_kilowatt", {"power", 1'000'000LL}},
};

void UnitOverflowCheck::registerMatchers( ast_matchers::MatchFinder *Finder )
Expand Down
45 changes: 45 additions & 0 deletions tools/clang-tidy-plugin/test/unit-overflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,48 @@ units::energy f_energy_6()
// CHECK-MESSAGES: warning: constructing energy quantity from 'int' can overflow in 'from_kilojoule' in multiplying with the conversion factor [cata-unit-overflow]
// CHECK-FIXES: return units::from_kilojoule( static_cast<std::int64_t>( kj ) );
}

units::power f_power_0()
{
return units::from_milliwatt( 3'000 );
}

units::power f_power_1()
{
return units::from_watt( 3'000 );
}

units::power f_power_2()
{
return units::from_watt( 3'000'000 );
// CHECK-MESSAGES: warning: constructing power quantity from 'int' can overflow in 'from_watt' in multiplying with the conversion factor [cata-unit-overflow]
// CHECK-FIXES: return units::from_watt( 3'000'000LL );
}

units::power f_power_3()
{
int watt = 3'000'000;
return units::from_watt( watt );
// CHECK-MESSAGES: warning: constructing power quantity from 'int' can overflow in 'from_watt' in multiplying with the conversion factor [cata-unit-overflow]
// CHECK-FIXES: return units::from_watt( static_cast<std::int64_t>( watt ) );
}

units::power f_power_4()
{
return units::from_kilowatt( 2'000 );
}

units::power f_power_5()
{
return units::from_kilowatt( 3'000 );
// CHECK-MESSAGES: warning: constructing power quantity from 'int' can overflow in 'from_kilowatt' in multiplying with the conversion factor [cata-unit-overflow]
// CHECK-FIXES: return units::from_kilowatt( 3'000LL );
}

units::power f_power_6()
{
int kw = 3'000;
return units::from_kilowatt( kw );
// CHECK-MESSAGES: warning: constructing power quantity from 'int' can overflow in 'from_kilowatt' in multiplying with the conversion factor [cata-unit-overflow]
// CHECK-FIXES: return units::from_kilowatt( static_cast<std::int64_t>( kw ) );
}

0 comments on commit e09b367

Please sign in to comment.