// A of_find_node_by_name, etc is not matched by a of_node_put before an
// error return.
//
// Confidence: Moderate
// Copyright: (C) Gilles Muller, Julia Lawall, EMN, INRIA, DIKU.  GPLv2.
// URL: https://coccinelle.gitlabpages.inria.fr/website/rules/ofname1.html
// Options:

@r@
local idexpression n;
position p1;
@@

n = \(of_find_node_by_name@p1\|of_find_node_by_type@p1\|of_find_node_by_path@p1\|of_find_node_by_phandle@p1\|of_find_matching_node@p1\|of_find_compatible_node@p1\)(...)

// find the missing returns
@t exists@
local idexpression n;
statement S1;
expression E;
expression *ptr != NULL;
type T;
position r.p1,p2;
@@

(
if ((n = \(of_find_node_by_name@p1\|of_find_node_by_type@p1\|of_find_node_by_path@p1\|of_find_node_by_phandle@p1\|of_find_matching_node@p1\|of_find_compatible_node@p1\)(...)) == NULL) S1
|
n = \(of_find_node_by_name@p1\|of_find_node_by_type@p1\|of_find_node_by_path@p1\|of_find_node_by_phandle@p1\|of_find_matching_node@p1\|of_find_compatible_node@p1\)(...)
)
... when != of_node_put(n)
    when != if (...) { <+... of_node_put(n) ...+> }
    when != true n == NULL  || ...
    when != n = (T)E
    when != E = n
(
  return \(0\|<+...n...+>\|ptr\);
|
return@p2 ...;
)

// find the ones with no inconsistent paths
@s@
local idexpression n;
statement S1,S2;
expression E,E1;
expression *ptr != NULL;
type T,T1;
identifier l1,l2;
position r.p1;
@@

(
if ((n = \(of_find_node_by_name@p1\|of_find_node_by_type@p1\|of_find_node_by_path@p1\|of_find_node_by_phandle@p1\|of_find_matching_node@p1\|of_find_compatible_node@p1\)(...)) == NULL) S1
|
n = \(of_find_node_by_name@p1\|of_find_node_by_type@p1\|of_find_node_by_path@p1\|of_find_node_by_phandle@p1\|of_find_matching_node@p1\|of_find_compatible_node@p1\)(...)
)
<... when != of_node_put(n)
    when != if (...) { <+... of_node_put(n) ...+> }
    when != true n == NULL  || ...
    when != n = (T)E
    when != E = n
    when != goto l1;
if (n == NULL || ...) S2
...>
(
  return \(0\|<+...n...+>\|ptr\);
|
+ of_node_put(n);
return ...;
|
of_node_put(n);
|
n = (T1)E1
|
E1 = n
|
goto l2;
)

@script:python depends on !s@
p1 << r.p1;
p2 << t.p2;
@@

print "* file: %s of_find_node_by_name, etc %s return %s" % (p1[0].file,p1[0].line,p2[0].line)