1

I want to slice the unused variables which are shown down with frama-c. But I have no idea which command line should I write to slice all unused variables with one command line

Last login: Thu Nov  9 20:48:42 on ttys000
Recep-MacBook-Pro:~ recepinanir$ cd desktop
Recep-MacBook-Pro:desktop recepinanir$ cat hw.c
#include <stdio.h>

int main()
{
    int x= 10;
    int y= 24;
    int z;

  printf("Hello World\n");

  return 0;
}

Recep-MacBook-Pro:desktop recepinanir$ clang hw.c
Recep-MacBook-Pro:desktop recepinanir$ ./a.out
Hello World
Recep-MacBook-Pro:desktop recepinanir$ clang -Wall hw.c -o result
hw.c:5:9: warning: unused variable 'x' [-Wunused-variable]
    int x= 10;
        ^
hw.c:6:9: warning: unused variable 'y' [-Wunused-variable]
    int y= 24;
        ^
hw.c:7:9: warning: unused variable 'z' [-Wunused-variable]
    int z;
        ^
3 warnings generated.
Recep-MacBook-Pro:desktop recepinanir$ 
glennsl
  • 28,186
  • 12
  • 57
  • 75

1 Answers1

1

As mentioned on https://frama-c.com/slicing.html, slicing is always relative some criterion, and the goal is to produce a program that is smaller to the original one, while presenting the same behavior with respect to the criterion. The Slicing plug-in itself gives several ways to build such criteria, but it seems that you are interested in the result of the Sparecode plugin (https://frama-c.com/sparecode.html): this is a specialized version of slicing, where the criterion is the program state at the end of the entry point of your analysis (i.e. main in your case). In other words, Sparecode will remove everything that does not contribute to the final result of the code under analysis. In your case, frama-c -sparecode-analysis hw.c gives the following result (note that the call to printf has been modified by the Variadic plug-in, and that its argument is not considered as useful for the final state of main. If this is an issue, you'd need to provide more specialized output functions, with an ACSL specification indicating that they have an impact to some global variable)

/* Generated by Frama-C */
#include "stdio.h"
/*@ assigns \result, __fc_stdout->__fc_FILE_data;
    assigns \result
      \from (indirect: __fc_stdout->__fc_FILE_id),
             __fc_stdout->__fc_FILE_data;
    assigns __fc_stdout->__fc_FILE_data
      \from (indirect: __fc_stdout->__fc_FILE_id),
            __fc_stdout->__fc_FILE_data;
 */
int printf_va_1(void);

int main(void)
{
  int __retres;
  printf_va_1();
 __retres = 0;
 return __retres;
}

Finally, note that in the general case, Slicing (hence Sparecode) gives an overapproximation: it will only remove statements for which it is certain that they have no impact on the criterion.

Virgile
  • 9,724
  • 18
  • 42