Skip to content

check_code is a C linter using spatch program from coccinelle

License

Notifications You must be signed in to change notification settings

Tilatti/mini_cocci_linter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mini_cocci_linter

mini_cocci_linter is a small C linter using spatch program from the coccinelle project.

Usage

Usage:
        Execute requirements on source code of a directory: ./check_code.sh [-r <requirement>] <directory>
        Execute the test cases: ./check_code.sh -t [-r <requirement>]
        Print this help: ./check_code.sh -h

Check all the requirements on the C source code files inside a directory (and its sub-directories).

$ ./check_code.sh <directory>

Same as previously, but its check only the requirement req_3:

$ ./check_code.sh -r req_3 <directory>

Execute the self-tests for all the requirements:

$ ./check_code.sh -t

Dependencies

  • bash
  • spatch
  • clang

Notes

Each requirement checks one rule using a coccinelle script inside the cocci directory.

Some scripts come from the examples of the cocinelle project.

You can implement our own requirement. For example, if we want to detect when the goto jumps to a label preceding this goto, we will add the following file cocci/goto_req.cocci :

//
// DESCRIPTION: Detect use of backward goto.
//

@r@
position p;
identifier label;
@@

label:
...
goto label;@p

@script:python@
p << r.p;
@@
cocci.print_main("ERROR", p)

To check the requirement on a directory containing C files:

$ ./check_code.sh -r goto_req.cocci /home/foo/src

We will also add a test in order to verify if the requirement do what we expect. Add the file cocci/goto_req.c:

void t1(void)
{
  goto F;
F:
  return;
}

void t2(void)
{
  goto F;
  while(0);
F:
  return;
}

void t3(void)
{
F:
  while(0);
  goto F; /* not good */
  return;
}

Note that the place where the requirement shall detect an error is marked with the comment /* not good */. Now we can execute the test case:

$ ./check_code.sh -t -r goto_req.cocci

Bugs

  • Is the cocci/req_2.cocci broken ?
  • Why is cocci/warn_5.cocci disabled ?

About

check_code is a C linter using spatch program from coccinelle

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published