Skip to contentSkip to conversation

Error predicate determination with SmPL?

Cocci SF Markus Elfring 2 participants 18 messages Jul 13, 2015

lore
Share & tools
lore
#0 lore raw

Hello,

The semantic patch language can also be used to find specific return statements in the source files. How can this software help to find all of them within a function implementation?

How are the chances to improve static source code analysis possibilities for such a purpose?

Regards,
Markus

#1 lore raw
Quoted 7 lines

The semantic patch language can also be used to find specific return statements in the source files. How can this software help to find all of them within a function implementation?

How are the chances to improve static source code analysis possibilities for such a purpose?

A simple SmPL filter approach like the following works already as expected to some degree.

@returns@
expression express;
identifier work;
type return_type;
@@
return_type work(...)
{
... when any
- return express;
... when any
}

elfring at Sonne:~/Projekte/Coccinelle/Probe> spatch.opt -sp-file show_returns1.cocci API-test1.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: API-test1.c

tmp/cocci-output-6241-92ed9e-API-test1.c renamed from API-test1.c +0 -2
diff = 
@@ -2,10 +2,8 @@
 
 int my_status(void)
 {
-  return 1;
 }
 
 int main(void)
 {
-  return my_status();
 }

I see further software development challenges here.

1. Do I need to specify a SmPL constraint like "non-void" for such a script?

2. The expression which is used for a specific return statement can be assigned to a SmPL metavariable in an other script variant. I would appreciate if I could process the extracted data in a more structured way somehow.

Regards,
Markus

#2 lore raw
Quoted 11 lines · SF Markus Elfring
Quoted 7 lines

The semantic patch language can also be used to find specific return statements in the source files. How can this software help to find all of them within a function implementation?

How are the chances to improve static source code analysis possibilities for such a purpose?

A simple SmPL filter approach like the following works already as expected to some degree.

Quoted 9 lines

@returns@
expression express;
identifier work;
type return_type;
@@
return_type work(...)
{
... when any
- return express;

The when any's are not needed. ... follows the control flow, so there is nothing after a return.

Quoted 23 lines

... when any
}

elfring at Sonne:~/Projekte/Coccinelle/Probe> spatch.opt -sp-file show_returns1.cocci API-test1.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
HANDLING: API-test1.c
diff =

tmp/cocci-output-6241-92ed9e-API-test1.c renamed from API-test1.c +0 -2
@@ -2,10 +2,8 @@
 
 int my_status(void)
 {
-  return 1;
 }
 
 int main(void)
 {
-  return my_status();
 }
Quoted 4 lines

I see further software development challenges here.

1. Do I need to specify a SmPL constraint like "non-void" for such a script?

It seems pointless. If the code is returning something and the return type is void, then the problem is elsewhere.

Quoted 4 lines

2. The expression which is used for a specific return statement can be assigned to a SmPL metavariable in an other script variant. I would appreciate if I could process the extracted data in a more structured way somehow.

I have no idea what you want to do. You can get the AST if you write a script using OCaml:

(stringrep,astrep) << r.express;

julia

#3 lore raw
Quoted 5 lines
Quoted 2 lines

1. Do I need to specify a SmPL constraint like "non-void" for such a script?

It seems pointless. If the code is returning something and the return type is void, then the problem is elsewhere.

A C compiler will usually will react with an error display for this special case.
How should a static source code analysis tool deal with it?

Does the metavariable type "expression" match also to the empty string?

Quoted 9 lines
Quoted 4 lines

2. The expression which is used for a specific return statement can be assigned to a SmPL metavariable in an other script variant. I would appreciate if I could process the extracted data in a more structured way somehow.

I have no idea what you want to do. You can get the AST if you write a script using OCaml:

(stringrep,astrep) << r.express;

I guess that I am going to store elements into another SQL database.

How are the chances that more documentation will become available for safe processing of corresponding abstract syntax graph instances?

Regards,
Markus

#4 lore raw
Quoted 9 lines · SF Markus Elfring
Quoted 5 lines
Quoted 2 lines

1. Do I need to specify a SmPL constraint like "non-void" for such a script?

It seems pointless. If the code is returning something and the return type is void, then the problem is elsewhere.

A C compiler will usually will react with an error display for this special case.
How should a static source code analysis tool deal with it?

Ignore it. Unless your goal is to check for this specific error.

Quoted 1 line

Does the metavariable type "expression" match also to the empty string?

No.

Quoted 9 lines
Quoted 9 lines
Quoted 5 lines

2. The expression which is used for a specific return statement can be assigned to a SmPL metavariable in an other script variant. I would appreciate if I could process the extracted data in a more structured way somehow.

I have no idea what you want to do. You can get the AST if you write a script using OCaml:

Quoted 6 lines
Quoted 1 line

(stringrep,astrep) << r.express;

I guess that I am going to store elements into another SQL database.

How are the chances that more documentation will become available for safe processing of corresponding abstract syntax graph instances?

About the same as the chance that a user who wants the documentation will write it. I have no plans to do so. In general, if you know enough OCaml to use the abstract syntax tree, you also know enough OCaml to figure out what it is from the source code. Everything is defined in parsing_c/ast_c.ml.

julia

#5 lore raw
Quoted 3 lines
Quoted 1 line

Does the metavariable type "expression" match also to the empty string?

No.

Thanks for your clarification.

I hope then that the shown specification of the SmPL variable will be sufficient for a while to search for function implementations with a non-void return type.

Quoted 8 lines
Quoted 2 lines

How are the chances that more documentation will become available for safe processing of corresponding abstract syntax graph instances?

About the same as the chance that a user who wants the documentation will write it. I have no plans to do so. In general, if you know enough OCaml to use the abstract syntax tree, you also know enough OCaml to figure out what it is from the source code. Everything is defined in parsing_c/ast_c.ml.

I would find it nice if a data structure representation will evolve which is more independent from specific programming languages.

Regards,
Markus

#6 lore raw

[Cocci] Data processing for expression ASTs

In reply to Julia Lawall
Quoted 8 lines
Quoted 2 lines

How are the chances that more documentation will become available for safe processing of corresponding abstract syntax graph instances?

About the same as the chance that a user who wants the documentation will write it. I have no plans to do so. In general, if you know enough OCaml to use the abstract syntax tree, you also know enough OCaml to figure out what it is from the source code. Everything is defined in parsing_c/ast_c.ml.

Does this "tree" data structure correspond to the complete input from the source file?

I imagine that a special mapping for expression data should be sufficient for the discussed SmPL metavariable, shouldn' it?

Regards,
Markus

#7 lore raw
In reply to Julia Lawall
Quoted 3 lines

You can get the AST if you write a script using OCaml:

(stringrep,astrep) << r.express;

I try to get another small SmPL experiment like the following running.

@initialize:ocaml@
@@
let show_positions f_name typ name_places ex_string ex_ASG e_places = (* First loop? *)
Printf.printf "%s|%s\n" f_name typ;
(* Second loop? *)
Printf.printf "%s\n" ex_string

@returns@
expression express;
identifier work;
position e_pos, name_pos;
type return_type;
@@
return_type work at name_pos(...)
{
...
return express at e_pos;
...
}

@script:ocaml display depends on returns@
f_name << returns.work;
typ << returns.return_type;
(ex_string,ex_ASG) << returns.express;
e_places << returns.e_pos;
name_places << returns.name_pos;
@@
show_positions f_name typ name_places ex_string ex_ASG e_places

elfring@Sonne:~/Projekte/Coccinelle/Probe> spatch.opt -sp-file show_returns2.cocci API-test1.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
Using native version of ocamlc/ocamlopt/ocamldep
ocamlopt.opt -shared -o /tmp/ocaml_cocci_368f40.cmxs -g -I /usr/lib64/ocaml -I /usr/local/lib/coccinelle/ocaml /tmp/ocaml_cocci_368f40.ml File "/tmp/ocaml_cocci_368f40.ml", line 15, characters 6-36:
Error: Unbound value Iteration.add_pending_instance
Fatal error: exception Yes_prepare_ocamlcocci.CompileFailure("/tmp/ocaml_cocci_368f40.ml")

Can you better explain the shown error messages than me?

Does my OCaml approach contain mistakes?

How can your software library help with pretty-printing of the syntax graph?

Regards,
Markus

#8 lore raw
Quoted 46 lines · SF Markus Elfring
Quoted 3 lines

You can get the AST if you write a script using OCaml:

(stringrep,astrep) << r.express;

I try to get another small SmPL experiment like the following running.

@initialize:ocaml@
@@
let show_positions f_name typ name_places ex_string ex_ASG e_places = (* First loop? *)
Printf.printf "%s|%s\n" f_name typ;
(* Second loop? *)
Printf.printf "%s\n" ex_string

@returns@
expression express;
identifier work;
position e_pos, name_pos;
type return_type;
@@
return_type work at name_pos(...)
{
...
return express at e_pos;
...
}

@script:ocaml display depends on returns@
f_name << returns.work;
typ << returns.return_type;
(ex_string,ex_ASG) << returns.express;
e_places << returns.e_pos;
name_places << returns.name_pos;
@@
show_positions f_name typ name_places ex_string ex_ASG e_places

elfring at Sonne:~/Projekte/Coccinelle/Probe> spatch.opt -sp-file show_returns2.cocci API-test1.c
init_defs_builtins: /usr/local/lib/coccinelle/standard.h
Using native version of ocamlc/ocamlopt/ocamldep
ocamlopt.opt -shared -o /tmp/ocaml_cocci_368f40.cmxs -g -I /usr/lib64/ocaml -I /usr/local/lib/coccinelle/ocaml /tmp/ocaml_cocci_368f40.ml File "/tmp/ocaml_cocci_368f40.ml", line 15, characters 6-36:
Error: Unbound value Iteration.add_pending_instance
Fatal error: exception Yes_prepare_ocamlcocci.CompileFailure("/tmp/ocaml_cocci_368f40.ml")

Quoted 3 lines

Can you better explain the shown error messages than me?

Does my OCaml approach contain mistakes?

It looks like there is a problem with your installation of coccinelle. Perhaps you have a quite old version of ocaml?

Quoted 1 line

How can your software library help with pretty-printing of the syntax graph?

All it offers is the string version that is already available in the metavariable,

julia

#9 lore raw
Quoted 2 lines

It looks like there is a problem with your installation of coccinelle. Perhaps you have a quite old version of ocaml?

I hope not ?

elfring at Sonne:~/Projekte/Coccinelle/Probe> spatch.opt -version spatch version 1.0.1 with Python support and with PCRE support

Regards,
Markus

#11 lore raw
Quoted 3 lines · SF Markus Elfring
Quoted 1 line

Perhaps you have a quite old version of ocaml?

Is the software "OCaml 4.02.1-1.2" recent enough?

Yes, the version seems fine. It seems that you are not getting the proper dynamic linking with ocaml. Perhaps send the result of running configure.

julia

#12 lore raw

[Cocci] Checking of OCaml functionality

Quoted 2 lines

It seems that you are not getting the proper dynamic linking with ocaml.

Do you know a small test to check this aspect better?

Quoted 1 line

Perhaps send the result of running configure.

What do you hope to see there?

Regards,
Markus

#13 lore raw

[Cocci] Checking of OCaml functionality

Quoted 4 lines · SF Markus Elfring
Quoted 2 lines

It seems that you are not getting the proper dynamic linking with ocaml.

Do you know a small test to check this aspect better?

No. Your test is already as small as it can be.

Quoted 3 lines
Quoted 1 line

Perhaps send the result of running configure.

What do you hope to see there?

I would do a diff with my configuration and see if there is anything different with respect to ocaml.

julia

#14 lore raw

[Cocci] Checking of OCaml functionality

Quoted 2 lines

I would do a diff with my configuration and see if there is anything different with respect to ocaml.

The software "OCaml 4.02.1-1.2" was provided by a RPM package from my Linux distribution.
https://build.opensuse.org/package/show?project=openSUSE%3AFactory&package=ocaml

Your tool "spatch 1.0.1" was built on my own here.

Where do you imagine to find interesting differences?

Regards,
Markus

#15 lore raw

[Cocci] Checking of OCaml functionality

Quoted 11 lines · SF Markus Elfring
Quoted 2 lines

I would do a diff with my configuration and see if there is anything different with respect to ocaml.

The software "OCaml 4.02.1-1.2" was provided by a RPM package from my Linux distribution.
https://build.opensuse.org/package/show?project=openSUSE%3AFactory&package=ocaml

Your tool "spatch 1.0.1" was built on my own here.

Where do you imagine to find interesting differences?

I asked about the output of the configure program. The software can be fine, but it will not do the right thing if it is configured properly.

julia

#16 lore raw

[Cocci] Checking of OCaml functionality

Quoted 1 line

I asked about the output of the configure program.

Do you refer to the log from Coccinelle's shell
script "configure" here?

Regards,
Markus

#17 lore raw

[Cocci] Checking of OCaml functionality

Quoted 4 lines · SF Markus Elfring
Quoted 1 line

I asked about the output of the configure program.

Do you refer to the log from Coccinelle's shell
script "configure" here?

Yes.

julia