Sometimes it may be interesting to apply only a subset of the rules in a semantic patch. This is done in the case of coccicheck, to choose between rules that generate the output in diff and org mode format. It could also be useful when some rules are more precise, but more expensive, than others, and so it is not always desirable to run the more precise version. Essentially, this feature is analogous to #ifdef in C.
Already it is possible in Coccinelle to control whether a rule should be applied based on whether or not other rules have successfully applied, using depends on. For example, the following semantic patch only replaces a call to foo by a call to bar if the file inc.h is explicitly included:
@i@ @@ #include "inc.h" @depends on i@ @@ - foo() + bar()
Virtual rules allow you to control at the command line whether a rule should be applied. For example, if the semantic patch were instead:
virtual i @depends on i@ @@ - foo() + bar()
One could run, eg spatch -sp_file -D i foo.c to cause the replacement of foo by bar to be carried out. On the other hand, in running patch -sp_file foo.c, the rule replacing foo by bar would be ignored.
The set of named rules and virtual rules should be disjoint.
Further examples are available in the distribution of coccicheck, and in demos/virt.cocci in the Coccinelle source code.