<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Shape of Code &#187; Coccinelle</title>
	<atom:link href="http://shape-of-code.coding-guidelines.com/tag/coccinelle/feed/" rel="self" type="application/rss+xml" />
	<link>http://shape-of-code.coding-guidelines.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 20:42:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Unused function parameters</title>
		<link>http://shape-of-code.coding-guidelines.com/2011/05/09/unused-function-parameters/</link>
		<comments>http://shape-of-code.coding-guidelines.com/2011/05/09/unused-function-parameters/#comments</comments>
		<pubDate>Mon, 09 May 2011 02:52:58 +0000</pubDate>
		<dc:creator>Derek-Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Coccinelle]]></category>
		<category><![CDATA[function parameter]]></category>
		<category><![CDATA[measurements]]></category>
		<category><![CDATA[parameter name]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[unused parameter]]></category>

		<guid isPermaLink="false">http://shape-of-code.coding-guidelines.com/?p=417</guid>
		<description><![CDATA[I have started redoing the source code measurements that appear in my C book, this time using a lot more source, upgraded versions of existing tools, plus some new tools such as Coccinelle and R. The intent is to make the code and data available in a form that is easy for others to use [...]]]></description>
			<content:encoded><![CDATA[<p>I have started redoing the source code measurements that appear in my <a href="http://www.knosof.co.uk/cbook">C book</a>, this time using a <a href="http://shape-of-code.coding-guidelines.com/2011/03/31/empirical-software-engineering-is-five-years-old/">lot more source</a>, upgraded versions of existing tools, plus some new tools such as <a href="http://shape-of-code.coding-guidelines.com/2009/01/08/semantic-pattern-matching-coccinelle/">Coccinelle</a> and R.  The intent is to make the code and data available in a form that is easy for others to use (I am hoping that one or more people will measure the same constructs in other languages) and to make some attempt at teasing out relationships in the data (previously the data was simply plotted with little or no explanation).</p>
<p>While it might be possible to write a paper on every language construct measured I don&#8217;t have the time to do the work.  Instead I will use this blog to make a note of the interesting things that crop up during the analysis of each construct I measure.</p>
<p>First up are unused function parameters (<a href="http://www.coding-guidelines.com/code-data/unusedparam.tgz">code and data</a>), which at around 11% of all parameters are slightly more common than unused local variables (<a href="http://www.knosof.co.uk/cbook/usefigtab.pdf">Figure 190.1</a>).  In the following plot black circles are the total number of functions having a given number of parameters, red circles a given number of unused parameters, blue line a linear regression fit of the red circles, and the green line is derived from black circle values using a formula I concocted to fit the data and have no theoretical justification for it.</p>
<p><img src="http://www.coding-guidelines.com/images/unusedparam.png" alt="Function definitions containing a given number of unused parameters." /></p>
<p>The number of functions containing a given number of unused parameters drops by around a factor of three for each additional unused parameter.  Why is this?  The formula I came up with for estimating the number of functions containing <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_e2dba064904f7c6d8d2984bc722ebc9b.png" style="vertical-align:-6.5px; display: inline-block ;" alt="u" title="u"/> unused parameters is: <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_964_fa28659368218a55df4f2c6b29f1aa6a.png" style="vertical-align:-36px; display: inline-block ;" alt="sum{p=u}{8}{F_p/{7p}}" title="sum{p=u}{8}{F_p/{7p}}"/>, where <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_985.5_8b6cd058307db2e619c3cddd145ac2ab.png" style="vertical-align:-14.5px; display: inline-block ;" alt="F_p" title="F_p"/> is the total number of function definitions containing <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_d3d078bf2fc1425e6d4410ccb6ed6df9.png" style="vertical-align:-6.5px; display: inline-block ;" alt="p" title="p"/> parameters.</p>
<p>Which parameter, for those functions defined with more than one, is most likely to be unused?  My thinking was that the first parameter might either hold the most basic information (and so rarely be unused) or hold information likely to be superseded when new parameters are added (and so commonly be unused), either way I considered later parameters as often being put there for later use (and therefore more likely to be unused).  The following plot is for eight programs plus the sum of them all; for all functions defined with between one and eight parameters the percentage of times the <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_a45192846640be85b0edaca33c2a3d3b.png" style="vertical-align:-6.5px; display: inline-block ;" alt="n" title="n"/>&#8216;th parameter is unused is given.  The source measured contained 104,493 function definitions containing more than one parameter with 16,643 of these functions having one or more unused parameter, there were a total of 235,512 parameters with 25,886 parameters being unused.</p>
<p><img src="http://www.coding-guidelines.com/images/unusedparam-pos.png" alt="Percentage of unused parameters at different positions for various function definitions." /></p>
<p>There is no obvious pattern to which parameters are likely to be unused, although a lot of the time the last parameter is more likely to be unused than the penultimate one.</p>
<p>Looking through the raw data I noticed that there seemed to be some clustering of names of unused parameters, in particular if the <img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_993.5_a45192846640be85b0edaca33c2a3d3b.png" style="vertical-align:-6.5px; display: inline-block ;" alt="n" title="n"/>&#8216;th parameter was unused in two adjacent &#8216;unused parameter&#8217; functions they often had the same name.  The following plot is for all measured source and gives the percentage of same name occurrences; the matching process analyses function definitions in the order they occur within a source file and having found one unused parameter its name is compared against the corresponding parameter in the next function definition with the same number of parameters and having an unused parameter at the same position.</p>
<p><img src="http://www.coding-guidelines.com/images/unusedparam-name.png" alt="Probability of unused parameters in adjacent functions having the same name." /></p>
<p>Before getting too excited about this pattern we should ask if something similar exists for the used parameters.  When I get around to redoing the general parameter measurements I will look into this question.  The numbers of occurrences for functions containing eight parameters is close to minimal.</p>
<p>The functions containing these same-name unused parameters appear to also have related function names.  Is this a case of related function being grouped together, often sharing parameter names and when one of them has an unused parameter the corresponding parameter in the others is also unused? </p>
<p>Is there a correlation between number of parameters and number of statements, are functions containing lots of statements less likely to have unused parameters?  What effect does software evolution have (most of this kind of research measures executable code not variable definitions)?</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fshape-of-code.coding-guidelines.com%2F2011%2F05%2F09%2Funused-function-parameters%2F&amp;title=Unused%20function%20parameters" id="wpa2a_2"><img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://shape-of-code.coding-guidelines.com/2011/05/09/unused-function-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Assessing my predictions for 2009</title>
		<link>http://shape-of-code.coding-guidelines.com/2010/01/24/assessing-my-predictions-for-2009/</link>
		<comments>http://shape-of-code.coding-guidelines.com/2010/01/24/assessing-my-predictions-for-2009/#comments</comments>
		<pubDate>Sun, 24 Jan 2010 02:13:10 +0000</pubDate>
		<dc:creator>Derek-Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Coccinelle]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[gcc performance]]></category>
		<category><![CDATA[Gimple]]></category>
		<category><![CDATA[llvm]]></category>
		<category><![CDATA[predictions]]></category>
		<category><![CDATA[static analysis]]></category>
		<category><![CDATA[Treehydra]]></category>

		<guid isPermaLink="false">http://shape-of-code.coding-guidelines.com/?p=171</guid>
		<description><![CDATA[I have been rather remiss in revisiting the predictions I made for 2009 to see how they fared. Only two out of the six predictions were sufficiently precise to enable an assessment one year later; the other four talking more about trends. What of these two predictions? The LLVM project will die. Ok, the project [...]]]></description>
			<content:encoded><![CDATA[<p>I have been rather remiss in revisiting the <a href="http://shape-of-code.coding-guidelines.com/2008/12/predictions-for-2009/">predictions I made for 2009</a> to see how they fared.  Only two out of the six predictions were sufficiently precise to enable an assessment one year later; the other four talking more about trends.  What of these two predictions?</p>
<p><strong>The <a href="http://llvm.org/">LLVM project</a> will die.</strong>  Ok, the project is still going and at the end of December the <a href="http://blog.llvm.org/2009/12/clang-builds-llvm.html">compiler could build itself</a> but the build is not yet in a state to self host (i.e., llvm compiler creates an executable of itself from its own source and this executable can build an executable from source that is identical to itself {modulo date/time stamps etc}). Self hosting is a major event and on some of the projects I have worked on it has been a contractual payment milestone.</p>
<p>Is llvm competition for gcc?  While there might not be much commercial competition as such (would Apple be providing funding to gcc if it were not involved in llvm?), I&#8217;m sure that developers working on both projects want their respective compiler to be the better one.  According to some llvm benchmarks they <a href="http://clang.llvm.org/performance.html">compile code</a> twice as fast as gcc.  If this performance difference holds up across lots of source how might the gcc folk respond?  Is gcc compiler time performance an issue that developers care about or is quality of generated code more important?  For me the latter is more important and I have not been able to find a reliable, recent, performance comparison.  I understand that almost all gcc funding is targeted at code generation related development, so if compile time is an issue gcc may be in a hole.</p>
<p>I still don&#8217;t see the logic behind Apple funding this project and continue to think that this funding will be withdrawn sometime, perhaps I was just a year off.  I do wish the llvm developers well and look forward to them celebrating a self hosted compiler.</p>
<p><strong>Static analysis will go mainstream.</strong>  Ok, I was overly optimistic here.  There do not seem to have been any casualties in 2009 among the commercial vendors selling static analysis tools and the growth of PHP has resulted in a number of companies selling tools that scan for source security vulnerabilities (.e.g, SQL injection attacks).</p>
<p>I was hoping that the availability of <a href="https://developer.mozilla.org/en/Treehydra">Treehydra</a> would spark the development of static analysis plugins for gcc.  Perhaps the neatness of the idea blinded me to what I already knew; many developers dislike the warnings generated by the <code>-Wall</code> option and therefore might be expected to dislike any related kind of warning.  One important usability issue is that Treehydra operates on the abstract syntax tree representation used internally by gcc, <a href="http://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html">GIMPLE</a>.  Learning about this representation requires a big investment before a plugin developer becomes productive.</p>
<p>One tool that did experience a lot of growth in 2009 was <a href="http://shape-of-code.coding-guidelines.com/2009/01/semantic-pattern-matching-coccinelle/">Coccinelle</a>, at least judged by the traffic on its mailing list. The Coccinelle developers continue to improve it and are responsive the questions.  Don&#8217;t be put off by the low version number (currently 0.2), it is much better than most tools with larger version numbers.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fshape-of-code.coding-guidelines.com%2F2010%2F01%2F24%2Fassessing-my-predictions-for-2009%2F&amp;title=Assessing%20my%20predictions%20for%202009" id="wpa2a_4"><img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://shape-of-code.coding-guidelines.com/2010/01/24/assessing-my-predictions-for-2009/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Coccinelle to match if sequences</title>
		<link>http://shape-of-code.coding-guidelines.com/2009/08/31/using-coccinelle-to-match-if-sequences/</link>
		<comments>http://shape-of-code.coding-guidelines.com/2009/08/31/using-coccinelle-to-match-if-sequences/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 15:21:38 +0000</pubDate>
		<dc:creator>Derek-Jones</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Coccinelle]]></category>
		<category><![CDATA[if]]></category>
		<category><![CDATA[measuring]]></category>
		<category><![CDATA[pattern match]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[switch]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://shape-of-code.coding-guidelines.com/?p=116</guid>
		<description><![CDATA[I have been using Coccinelle to obtain measurements of various properties of C if and switch statements. It is rare to find a tool that does exactly what is desired but it is often possible to combine various tools to achieve the desired result. I am interested in measuring sequences of if-else-if statements and one [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using <a href="http://shape-of-code.coding-guidelines.com/2009/01/semantic-pattern-matching-coccinelle/">Coccinelle</a> to obtain measurements of various <a href="http://shape-of-code.coding-guidelines.com/2009/08/108/">properties of C if</a> and switch statements.  It is rare to find a tool that does exactly what is desired but it is often possible to combine various tools to achieve the desired result.</p>
<p>I am interested in measuring sequences of if-else-if statements and one of the things I wanted to know was how many sequences of a given length occurred.  Writing a pattern for each possible sequence was the obvious solution, but what is the longest sequence I should search for?  A better solution is to use a pattern that matches short sequences and writes out the position (line/column number) where they occur in the code, as in the following Coccinelle pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@ if_else_if_else @
expression E_1, E_2<span style="color: #66cc66;">;</span> 
statement S_1, S_2, S_3<span style="color: #66cc66;">;</span>
position p_1, p_2<span style="color: #66cc66;">;</span>
@@
<span style="color: #ff7700;font-weight:bold;">if</span>@p_1 <span style="color: black;">&#40;</span>E_1<span style="color: black;">&#41;</span>
   S_1
<span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #ff7700;font-weight:bold;">if</span>@p_2 <span style="color: black;">&#40;</span>E_2<span style="color: black;">&#41;</span>
   S_2
<span style="color: #ff7700;font-weight:bold;">else</span>
   S_3
@
script:python @ expr_1 <span style="color: #66cc66;">&lt;&lt;</span> if_else_if_else.<span style="color: black;">E_1</span><span style="color: #66cc66;">;</span>
                expr_2 <span style="color: #66cc66;">&lt;&lt;</span> if_else_if_else.<span style="color: black;">E_2</span><span style="color: #66cc66;">;</span>
                loc_1 <span style="color: #66cc66;">&lt;&lt;</span> if_else_if_else.<span style="color: black;">p_1</span><span style="color: #66cc66;">;</span>
                loc_2 <span style="color: #66cc66;">&lt;&lt;</span> if_else_if_else.<span style="color: black;">p_2</span><span style="color: #66cc66;">;</span>
              @@
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;--- ifelseifelse&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> loc_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">line</span>, <span style="color: #483d8b;">&quot; &quot;</span>, loc_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">column</span>, <span style="color: #483d8b;">&quot; &quot;</span>, expr_1
<span style="color: #ff7700;font-weight:bold;">print</span> loc_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">line</span>, <span style="color: #483d8b;">&quot; &quot;</span>, loc_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">column</span>, <span style="color: #483d8b;">&quot; &quot;</span>, expr_2</pre></div></div>

<p>noting that in a sequence of source such as:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x == <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
   stmt_1<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>
   <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x == <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
      stmt_2<span style="color: #66cc66;">;</span>
   <span style="color: #ff7700;font-weight:bold;">else</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>x == <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
         stmt_3<span style="color: #66cc66;">;</span></pre></div></div>

<p>the tokens <code>if (x == 2)</code> will be matched twice, the first setting the position metavariable <code>p_2</code> and then setting <code>p_1</code>.  An <a href="http://en.wikipedia.org/wiki/AWK">awk</a> script was written to read the Coccinelle output and merge together adjacent pairs of matches that were part of a longer if-else-if sequence.</p>
<p>The first pattern did not concern itself with the form of the controlling expression, it simply wrote it out.  A second set of patterns was used to match those forms of controlling expression I was interested in, but first I had to convert the output into syntactically correct C so that it could be processed by Coccinelle.  Again awk came to the rescue, converting the output:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">--- ifelseifelse
<span style="color: #ff4500;">186</span>   <span style="color: #ff4500;">2</span>   op == FFEBLD_opSUBRREF
<span style="color: #ff4500;">191</span>   <span style="color: #ff4500;">7</span>   op == FFEBLD_opFUNCREF
--- ifelseifelse
<span style="color: #ff4500;">1094</span>   <span style="color: #ff4500;">3</span>   anynum <span style="color: #66cc66;">&amp;&amp;</span> commit
<span style="color: #ff4500;">1111</span>   <span style="color: #ff4500;">8</span>   <span style="color: black;">&#40;</span> c <span style="color: black;">&#91;</span> colon + <span style="color: #ff4500;">1</span> <span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'*'</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> commit</pre></div></div>

<p>into a separate function for each matched sequence:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">void f_1<span style="color: black;">&#40;</span>void<span style="color: black;">&#41;</span> <span style="color: black;">&#123;</span>
// --- ifelseifelse
/<span style="color: #66cc66;">*</span> <span style="color: #ff4500;">186</span>   <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">*</span>/   op == FFEBLD_opSUBRREF <span style="color: #66cc66;">;</span>
/<span style="color: #66cc66;">*</span> <span style="color: #ff4500;">191</span>   <span style="color: #ff4500;">7</span> <span style="color: #66cc66;">*</span>/   op == FFEBLD_opFUNCREF <span style="color: #66cc66;">;</span>
<span style="color: black;">&#125;</span>
void f_2<span style="color: black;">&#40;</span>void<span style="color: black;">&#41;</span> <span style="color: black;">&#123;</span>
// --- ifelseifelse
/<span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1094</span>   <span style="color: #ff4500;">3</span> <span style="color: #66cc66;">*</span>/   anynum <span style="color: #66cc66;">&amp;&amp;</span> commit <span style="color: #66cc66;">;</span>
/<span style="color: #66cc66;">*</span> <span style="color: #ff4500;">1111</span>   <span style="color: #ff4500;">8</span> <span style="color: #66cc66;">*</span>/   <span style="color: black;">&#40;</span> c <span style="color: black;">&#91;</span> colon + <span style="color: #ff4500;">1</span> <span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'*'</span> <span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> commit <span style="color: #66cc66;">;</span>
<span style="color: black;">&#125;</span></pre></div></div>

<p>The Coccinelle pattern:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@ if_eq_1 @
expression E_1<span style="color: #66cc66;">;</span>
constant C_1, C_2<span style="color: #66cc66;">;</span>
position p_1, p_2<span style="color: #66cc66;">;</span>
@@
&nbsp;
E_1 == C_1@p_1 <span style="color: #66cc66;">;</span>
E_1 == C_2@p_2 <span style="color: #66cc66;">;</span>
&nbsp;
@
script:python @ expr_1<span style="color: #66cc66;">&lt;&lt;</span> if_eq_1.<span style="color: black;">E_1</span><span style="color: #66cc66;">;</span>
                const_1 <span style="color: #66cc66;">&lt;&lt;</span> if_eq_1.<span style="color: black;">C_1</span><span style="color: #66cc66;">;</span>
                const_2 <span style="color: #66cc66;">&lt;&lt;</span> if_eq_1.<span style="color: black;">C_2</span><span style="color: #66cc66;">;</span>
                loc_1 <span style="color: #66cc66;">&lt;&lt;</span> if_eq_1.<span style="color: black;">p_1</span><span style="color: #66cc66;">;</span>
                loc_2 <span style="color: #66cc66;">&lt;&lt;</span> if_eq_1.<span style="color: black;">p_2</span><span style="color: #66cc66;">;</span>
              @@
<span style="color: #ff7700;font-weight:bold;">print</span> loc_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">line</span>, <span style="color: #483d8b;">&quot; &quot;</span>, loc_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">column</span>, <span style="color: #483d8b;">&quot; 3 &quot;</span>, expr_1, <span style="color: #483d8b;">&quot; == &quot;</span>, const_1
<span style="color: #ff7700;font-weight:bold;">print</span> loc_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">line</span>, <span style="color: #483d8b;">&quot; &quot;</span>, loc_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">column</span>, <span style="color: #483d8b;">&quot; 2 &quot;</span>, expr_1, <span style="color: #483d8b;">&quot; == &quot;</span>, const_2</pre></div></div>

<p>matches a sequence of two statements which consist of an expression being compared for equality against a constant, with the expression being identical in both statements.  Again positions were written out for post-processing, i.e., joining together matched sequences.</p>
<p>I was interested in any sequence of if-else-if that could be converted to an equivalent switch-statement.  Equality tests against a constant is just one form of controlling expression that meets this requirement, another is the <a href="http://shape-of-code.coding-guidelines.com/2009/07/implementing-the-between-operation/">between operation</a>.  Separate patterns could be written and run over the generated C source containing the extracted controlling expressions.</p>
<p>Breaking down the measuring process into smaller steps reduced the amount of time needed to get a final result (with Coccinelle 0.1.19 the first pattern takes round 70 minutes, thanks to <a href="http://www.diku.dk/~julia">Julia Lawall</a>&#8216;s work to speed things up, an overhead that only has to occur once) and allows the same controlling expression patterns to be run against the output of both the if-else-if and if-if patterns.</p>
<p>At the end of this process I ended up with a list information (line numbers in source code and form of controlling expression) on if-statement sequences that could be rewritten as a switch-statement.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fshape-of-code.coding-guidelines.com%2F2009%2F08%2F31%2Fusing-coccinelle-to-match-if-sequences%2F&amp;title=Using%20Coccinelle%20to%20match%20if%20sequences" id="wpa2a_6"><img src="http://shape-of-code.coding-guidelines.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://shape-of-code.coding-guidelines.com/2009/08/31/using-coccinelle-to-match-if-sequences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

