Skip to contentSkip to conversation

docs/demos: add ++ transformation documentation and demos

PATCH Superseded Luis R. Rodriguez 3 participants 5 messages Jun 8, 2016 – Jun 9, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 docs/demos: add ++ transformation documentation and demos

This perhaps is not the best demo for use of ++ but it should suffice. This adds some basic documentation for it and a demo.

---
 demos/vars1.c                |  26 ++++++++
 demos/vars1.cocci            |  28 +++++++++
 demos/vars1.res              |  26 ++++++++
 demos/vars2.c                |  28 +++++++++
 demos/vars2.cocci            |  36 ++++++++++++
 demos/vars2.res              |  28 +++++++++
 docs/manual/cocci_syntax.tex | 137 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 309 insertions(+)
 create mode 100644 demos/vars1.c
 create mode 100644 demos/vars1.cocci
 create mode 100644 demos/vars1.res
 create mode 100644 demos/vars2.c
 create mode 100644 demos/vars2.cocci
 create mode 100644 demos/vars2.res
demos/vars1.c new file +26 -0
new file mode 100644
index 000000000000..b308c05b2013
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+struct foo {
+	int a;
+	int b;
+	int c;
+};
+
+struct foo stuff {
+	a = 1,
+	b = 2,
+	c = 3,
+};
+
+int main(void)
+{
+	struct foo *bar = &stuff;
+	int copy_a = bar->a;
+
+	if (true) {
+		printf("copy_a: %d\n", copy_a);
+	}
+
+	return 0;
+}
demos/vars1.cocci new file +28 -0
new file mode 100644
index 000000000000..055afbc34688
@@ -0,0 +1,28 @@
+/*
+ * This can move unused variables declaration and assignment
+ * which is only used in the branch into the branch. Its
+ * resticted however to only apply to code where this happens
+ * only once. Refer to vars2.[c|cocci] for handling multiple
+ * cases.
+ */
+
+ at simpleplus@
+identifier f, x, i, var;
+expression e1;
+type T;
+@@
+
+f (...) {
+ ... when != x
+     when any
+-T x = i->var;
+ ... when != x
+     when != i = e1
+     when any
+ if (...) {
++  T x = var;
+   <+...x...+>
+  }
+... when != x
+    when any
+}
demos/vars1.res new file +26 -0
new file mode 100644
index 000000000000..98270324eea5
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+struct foo {
+	int a;
+	int b;
+	int c;
+};
+
+struct foo stuff {
+	a = 1,
+	b = 2,
+	c = 3,
+};
+
+int main(void)
+{
+	struct foo *bar = &stuff;
+
+	if (true) {
+		int copy_a = bar->a;
+		printf("copy_a: %d\n", copy_a);
+	}
+
+	return 0;
+}
demos/vars2.c new file +28 -0
new file mode 100644
index 000000000000..b58bb467b388
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+struct foo {
+	int a;
+	int b;
+	int c;
+};
+
+struct foo stuff {
+	a = 1,
+	b = 2,
+	c = 3,
+};
+
+int main(void)
+{
+	struct foo *bar = &stuff;
+	int copy_a = bar->a;
+	int copy_b = bar->b;
+
+	if (true) {
+		printf("copy_a: %d\n", copy_a);
+		printf("copy_b: %d\n", copy_b);
+	}
+
+	return 0;
+}
demos/vars2.cocci new file +36 -0
new file mode 100644
index 000000000000..d42b4acbcb5a
@@ -0,0 +1,36 @@
+/*
+ * This can move more than one unused variables declaration and assignment
+ * which is only used in the branch into the branch.
+ *
+ * This uses ++ to support the fact that the rule may be working
+ * with multiple variables that we need to modify and that order
+ * doe snot matter.
+ *
+ * If you don't use "++" you'll get "already tagged token" error since
+ * Coccinelle is concerned that the user has no way of specifying the order
+ * in which they should appear. By using "++" you are telling Coccinelle
+ *
+ *   "I know that a lot of things can collect here, and I'm OK
+ *    with that.  I'm also OK with things getting added out of order.
+ */
+
+ at plusplus@
+identifier f, x, i, var;
+expression e1;
+type T;
+@@
+
+f (...) {
+ ... when != x
+     when any
+-T x = i->var;
+ ... when != x
+     when != i = e1
+     when any
+ if (...) {
+++  T x = var;
+   <+...x...+>
+  }
+... when != x
+    when any
+}
demos/vars2.res new file +28 -0
new file mode 100644
index 000000000000..e798c8f54529
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <stdbool.h>
+
+struct foo {
+	int a;
+	int b;
+	int c;
+};
+
+struct foo stuff {
+	a = 1,
+	b = 2,
+	c = 3,
+};
+
+int main(void)
+{
+	struct foo *bar = &stuff;
+
+	if (true) {
+		int copy_a = bar->a;
+		int copy_b = bar->b;
+		printf("copy_a: %d\n", copy_a);
+		printf("copy_b: %d\n", copy_b);
+	}
+
+	return 0;
+}
docs/manual/cocci_syntax.tex +137 -0

149 lines not on this page · raw message

Signature · 1 line
2.8.2
#1 lore raw

PATCH 1/1 docs/demos: add ++ transformation documentation and demos

docs/manual/cocci_syntax.tex rule should apply if rule XXX was never matched at all. · line 854

Quoted 5 lines
docs/manual/cocci_syntax.tex +1 -0
@@ -851,6 +851,11 @@ rule should apply if rule XXX was never matched at all.
 
 \section{Transformation}
 
+Coccinelle allows for transformations to enable modifying C code using

How do you think about to omit the word "for" here?

docs/manual/cocci_syntax.tex write · line 1112

Quoted 9 lines
docs/manual/cocci_syntax.tex +5 -0
@@ -1100,6 +1105,138 @@ write
 Some kinds of terms can only appear in + code.  These include comments,
 ifdefs, and attributes (\texttt{\_\_attribute\_\_((...))}).
 
+\subsection{Advanced transformations}
+
+You may run into the situation where grammar you specificy for
+transformations might associate itself with a few consecutive tokens
+in code, in such cases Coccinelle cannot gaurantee order when making

How would you like to avoid a typo here?

Regards,
Markus

#2 lore raw

PATCH 1/1 docs/demos: add ++ transformation documentation and demos

In reply to Luis R. Rodriguez
Quoted 2 lines

This perhaps is not the best demo for use of ++ but it should suffice.

Since which release of the Coccinelle software is this notation (double plus) supported?

Quoted 1 line

This adds some basic documentation for it and a demo.

Thanks for your approach.

demos/vars1.cocci line 2

Quoted 3 lines
demos/vars1.cocci new file +2 -0
@@ -0,0 +1,28 @@
+/*
+ * This can move unused variables declaration and assignment

Will it be better to refer to "variable declaration"?

demos/vars1.cocci line 3

Quoted 1 line
demos/vars1.cocci new file +1 -0
+ * which is only used in the branch into the branch. Its

Can this wording be confusing?

demos/vars1.cocci line 4

Quoted 1 line
demos/vars1.cocci new file +1 -0
+ * resticted however to only apply to code where this happens

Is the word "restricted" appropriate here?

demos/vars2.cocci line 7

Quoted 1 line
demos/vars2.cocci new file +1 -0
+ * doe snot matter.

Would you like to move a space character here?

demos/vars2.cocci line 9

Quoted 1 line
demos/vars2.cocci new file +1 -0
+ * If you don't use "++" you'll get "already tagged token" error since

Would the addition of a comma (before "you'll") make sense here?

docs/manual/cocci_syntax.tex write · line 1110

Quoted 1 line
docs/manual/cocci_syntax.tex +1 -0
+You may run into the situation where grammar you specificy for

Is such a wording confusing?
Will the word "specify" be more appropriate in this context?

docs/manual/cocci_syntax.tex write · line 1113

Quoted 1 line
docs/manual/cocci_syntax.tex +1 -0
+additions. If you are sure that order does not matter you can use the

Would the addition of a comma (before "you") help in reading?

docs/manual/cocci_syntax.tex write · line 1175

Quoted 2 lines
docs/manual/cocci_syntax.tex +2 -0
+If however you have two consecutive tokens that Coccinelle
+can transform order cannot be guaranteed for how Coccinelle

How do you think about "? transform, order ?"?

Regards,
Markus

#3 lore raw

PATCH 1/1 docs/demos: add ++ transformation documentation and demos

Quoted 5 lines · SF Markus Elfring
Quoted 2 lines

This perhaps is not the best demo for use of ++ but it should suffice.

Since which release of the Coccinelle software is this notation (double plus) supported?

It's many years old. As a random guess I would say 2010.

julia

Quoted 55 lines
Quoted 1 line

This adds some basic documentation for it and a demo.

Thanks for your approach.

demos/vars1.cocci line 2

Quoted 3 lines
demos/vars1.cocci new file +2 -0
@@ -0,0 +1,28 @@
+/*
+ * This can move unused variables declaration and assignment

Will it be better to refer to "variable declaration"?

demos/vars1.cocci line 3

Quoted 1 line
demos/vars1.cocci new file +1 -0
+ * which is only used in the branch into the branch. Its

Can this wording be confusing?

demos/vars1.cocci line 4

Quoted 1 line
demos/vars1.cocci new file +1 -0
+ * resticted however to only apply to code where this happens

Is the word "restricted" appropriate here?

demos/vars2.cocci line 7

Quoted 1 line
demos/vars2.cocci new file +1 -0
+ * doe snot matter.

Would you like to move a space character here?

demos/vars2.cocci line 9

Quoted 1 line
demos/vars2.cocci new file +1 -0
+ * If you don't use "++" you'll get "already tagged token" error since

Would the addition of a comma (before "you'll") make sense here?

docs/manual/cocci_syntax.tex write · line 1110

Quoted 1 line
docs/manual/cocci_syntax.tex +1 -0
+You may run into the situation where grammar you specificy for

Is such a wording confusing?
Will the word "specify" be more appropriate in this context?

docs/manual/cocci_syntax.tex write · line 1113

Quoted 1 line
docs/manual/cocci_syntax.tex +1 -0
+additions. If you are sure that order does not matter you can use the

Would the addition of a comma (before "you") help in reading?

docs/manual/cocci_syntax.tex write · line 1175

Quoted 2 lines
docs/manual/cocci_syntax.tex +2 -0
+If however you have two consecutive tokens that Coccinelle
+can transform order cannot be guaranteed for how Coccinelle

How do you think about "? transform, order ?"?

Regards,
Markus
_______________________________________________
Cocci mailing list
Cocci at systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci

#4 lore raw

PATCH 1/1 docs/demos: add ++ transformation documentation and demos

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

This perhaps is not the best demo for use of ++ but it should suffice.

Since which release of the Coccinelle software is this notation (double plus) supported?

It's many years old. As a random guess I would say 2010.

Is there any more functionality waiting for detailed descriptions?

Regards,
Markus