On Sat, 8 Feb 2020, Ondřej Surý wrote:

> Hi,
>
> I have a code like this:
>
> ```
> static inline void
> freestruct_in_nsap(ARGS_FREESTRUCT) {
>         dns_rdata_in_nsap_t *nsap;
>
>         REQUIRE(((dns_rdata_in_nsap_t *)source) != NULL);
>         REQUIRE(((dns_rdata_in_nsap_t *)source)->common.rdclass == 
> dns_rdataclass_in);
>         REQUIRE(((dns_rdata_in_nsap_t *)source)->common.rdtype == 
> dns_rdatatype_nsap);
>
>         nsap = source;
> ```
>
> and due to various combination of a C standard requirement and enforced 
> cppcheck rules, I can’t change it to a saner workflow for a legacy code.
>
> But I am trying to replace the specific type case with a generic structure, 
> so it will look like this:
>
> ```
> static inline void
> freestruct_in_nsap(ARGS_FREESTRUCT) {
>         dns_rdata_in_nsap_t *nsap;
>
>         REQUIRE(source != NULL);
>         REQUIRE(((dns_rdatacommon_t *)source)->rdclass == dns_rdataclass_in);
>         REQUIRE(((dns_rdatacommon_t *)source)->rdtype == dns_rdatatype_nsap);
>
>         nsap = source;
>
> ```
>
> These are my rules currently:
>
> ```
> @@
> type T;
> expression source;
> @@
>
> - REQUIRE(((T *)source) != NULL);
> + REQUIRE(source != NULL);
>
> @@
> type T;
> type R;
> identifier common, rdtype;
> T *target;
> expression source;
> @@
>
>   REQUIRE(source != NULL);
>   ...
>   REQUIRE((
> - (T *)
> + (dns_rdatacommon_t *)
>   source)->
> - common.
>   rdtype == ...);
>   <+...
>   target = source;
>   ...+>
> ```
>
> the first one works like a charm, but the second rule fails with:
>
> ```
> init_defs_builtins: 
> /usr/local/bin/../Cellar/coccinelle/1.0.8/bin/../lib/coccinelle/standard.h
> plus: parse error:
>   File "cocci/rdata.spatch", line 21, column 22, charpos = 251
>   around = ')',
>   whole content = + (dns_rdatacommon_t *)
> ```
>
> I must be missing something very obvious as just removing the type works, but 
> adding the `+ (dns_rdatacommon_t *)` line makes the rule fail.

Coccinelle is not able to infer typedefs in casts.  So you should add to
your list of metavariables the declaration:

typedef dns_rdatacommon_t;

julia


>
> Thanks,
> Ondrej
> --
> Ondřej Surý
> [email protected]
>
>
>
> _______________________________________________
> Cocci mailing list
> [email protected]
> https://systeme.lip6.fr/mailman/listinfo/cocci
>
_______________________________________________
Cocci mailing list
[email protected]
https://systeme.lip6.fr/mailman/listinfo/cocci

Reply via email to