I am using cppcheck 2.6 i an embedded c project .
The software is showing the style issue : "the scope of the variable can be reduced"...
It is showing this message in some variables before a switch ..
If i try to move some variables inside the cases for solving the style issue I get an error from the compiler..
How Can I fix it ?
In an embedded c code it is usual to have all variables at the start of a function. Feel free to keep that programming style. Then you can just suppress these warnings --suppress=variableScope.
a label can only be part of a statement and a declaration is not a statement
Sounds strange!
What happens if you move the variable declaration into the if.
n = K_SENSE_POINTS_NUM;
for (i=0; i!=n; i++) {
if (adc <= machine->uc1_output[chn].ksense.adc[i]) {
break;
}
}
if (i == 0) {
return machine->uc1_output[chn].ksense.k[0];
}
if (i == n) {
return machine->uc1_output[chn].ksense.k[n-1];
}
k1 = machine->uc1_output[chn].ksense.k[i-1];
k2 = machine->uc1_output[chn].ksense.k[i];
k = k1 + ((k2-k1)*((int32_t)(adc-machine->uc1_output[chn].ksense.adc[i-1])))/((int32_t)(machine->uc1_output[chn].ksense.adc[i] - machine->uc1_output[chn].ksense.adc[i-1]));
return k;
}
I don't understand if cpp check is able to detect the possible divion by 0 if k2 = k1 for example .. or if machine->uc1_output[chn].ksense.adc[i] = machine->uc1_output[chn].ksense.adc[i-1]));
Hi forum ,
I am using cppcheck 2.6 i an embedded c project .
The software is showing the style issue : "the scope of the variable can be reduced"...
It is showing this message in some variables before a switch ..
If i try to move some variables inside the cases for solving the style issue I get an error from the compiler..
How Can I fix it ?
Thanks for the suggestion
Last edit: Simone Gasparella 2021-11-26
Hard to say from the snippets you posted, code as text would be better. Maybe you need to open a new scope for a
case
statement?Hi CHR..
Thanks for your answer
if i try to do it I get :
a label can only be part of a statement and a declaration is not a statement
In an embedded c code it is usual to have all variables at the start of a function. Feel free to keep that programming style. Then you can just suppress these warnings
--suppress=variableScope
.Sounds strange!
What happens if you move the variable declaration into the if.
Hi Simone,
A workaround is too put a ';' after the ':'
No need to put braces.
Hi ..
I am using cpp check..
Can you tell me the place where I can ask help for finding this issue ?
static uint64_t CalK(machine_status *machine, uint16_t chn, uint32_t adc)
{
uint16_t i, n;
int32_t k1, k2, k;
n = K_SENSE_POINTS_NUM;
for (i=0; i!=n; i++) {
if (adc <= machine->uc1_output[chn].ksense.adc[i]) {
break;
}
}
if (i == 0) {
return machine->uc1_output[chn].ksense.k[0];
}
if (i == n) {
return machine->uc1_output[chn].ksense.k[n-1];
}
k1 = machine->uc1_output[chn].ksense.k[i-1];
k2 = machine->uc1_output[chn].ksense.k[i];
k = k1 + ((k2-k1)*((int32_t)(adc-machine->uc1_output[chn].ksense.adc[i-1])))/((int32_t)(machine->uc1_output[chn].ksense.adc[i] - machine->uc1_output[chn].ksense.adc[i-1]));
return k;
}
I don't understand if cpp check is able to detect the possible divion by 0 if k2 = k1 for example .. or if machine->uc1_output[chn].ksense.adc[i] = machine->uc1_output[chn].ksense.adc[i-1]));
Is cpp check able to detect this possible issue ?
Thanks a lot for your support
--
Simone Gasparella | Software Engineering Manager
[cid:logo_systel_elettronica_493634fe-166d-4138-a2f2-18892caaa0d1.png]https://www.systel.it/
Systel Elettronica S.r.l.
Via Castellaro 12, 36040 Salcedo (VI), Italy
Phone: +39 0445 888264<tel:+39%200445%20888264> | Direct: +39 0445 1854214<tel:+39%200445%201854214>
simone.gasparella@systel.itsimone.gasparella@systel.it | www.systel.ithttps://www.systel.it/</tel:+39%200445%201854214></tel:+39%200445%20888264>