-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoding_Conventions.html
330 lines (327 loc) · 25.2 KB
/
Coding_Conventions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Firm - Coding Conventions</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<link rel="stylesheet" type="text/css" href="pygments.css"/>
<link rel="icon" type="image/png" href="logo-simple.png"/>
</head>
<body>
<div class="layout-header">
<a href="index.html"><img src="logo.png" alt="Firm Logo" /></a>
</div>
<div class="layout-content-wrapper">
<div class="layout-content">
<div class="layout-sidebar">
<ul>
<li><a href="index.html">About</a></li>
<li><a href="Features.html">Features</a></li>
<li><a href="Download.html">Download</a></li>
<li><a href="Documentation.html">Documentation</a></li>
<li><a href="Projects.html">Projects</a></li>
<li><a href="Development.html">Development</a></li>
<li><a href="Contact.html">Contact</a></li>
</ul>
<ul class="external">
<li><a href="http://pp.info.uni-karlsruhe.de/projects/firm_publications.php">Publications</a></li>
</ul>
</div>
<div class="layout-document">
<h1>Coding Conventions</h1>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>This document summarizes lib<span class="algo"><span class="algo">Firm</span></span> coding style conventions.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_indentation_and_formatting">Indentation and formatting</h2>
<div class="sectionbody">
<div class="listingblock">
<div class="title">Example</div>
<div class="content"><div class="highlight"><pre><span></span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="nf">maybe_create</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">b</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">10</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">create_node</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">do_something</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">z</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></div></div>
<div class="ulist"><ul>
<li>
<p>
function and variable names are all in lowercase; words are separated with an underscore <em>_</em>
</p>
</li>
<li>
<p>
the star from pointer declarations is put next to the name to be declared (Wrong: ir_node* foo; Right: ir_node *foo;)
</p>
</li>
<li>
<p>
opening curly braces for functions start on a new line
</p>
</li>
<li>
<p>
opening curly braces for if, while, etc. start on the same line!
</p>
</li>
<li>
<p>
in a call there is no space between the function name and the braces for the arguments
</p>
</li>
<li>
<p>
there is a space between builtin keywords (if, while, switch, for) and the following opening braces.
</p>
</li>
<li>
<p>
return is not a function call. So no braces around the return value
</p>
</li>
<li>
<p>
Try to keep lines shorter than 80 characters (breaking this rule is tolerated)
</p>
</li>
<li>
<p>
Don’t break message strings between 2 lines. (so you can grep for messages in the source)
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_indentation">Indentation</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
Indentation at the beginning of the line is done with tabs.
</p>
</li>
<li>
<p>
Indentation to match certain points in the previous line is done with spaces (see next part)
</p>
</li>
</ul></div>
<div class="listingblock">
<div class="title">Indentation Example (using <T> to show tabs)</div>
<div class="content"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">func</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
<span class="o"><</span><span class="n">T</span><span class="o">></span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">bla</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="mi">20</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">foobar_bar</span><span class="p">(</span><span class="n">long_variable_name</span><span class="p">)</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">another_long_thingy</span>
<span class="o"><</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="n">this_was_a_long_condition</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="o"><</span><span class="n">T</span><span class="o">><</span><span class="n">T</span><span class="o">></span><span class="n">print</span><span class="p">();</span>
<span class="o"><</span><span class="n">T</span><span class="o">><</span><span class="n">T</span><span class="o">></span><span class="n">foobar</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="w"> </span><span class="n">b</span><span class="p">,</span><span class="w"> </span><span class="n">more_long_stuff_here</span><span class="p">,</span>
<span class="o"><</span><span class="n">T</span><span class="o">><</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="n">and_this_is</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">another</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="p">(</span><span class="n">longish</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">argument</span><span class="p">);</span>
<span class="o"><</span><span class="n">T</span><span class="o">></span><span class="p">}</span>
<span class="p">}</span>
</pre></div></div></div>
<div class="paragraph"><p>This style allows different people to set tab to different sizes and still get nice layout. Most firm developers tend to set tab to 4 spaces though.</p></div>
<div class="ulist"><ul>
<li>
<p>
Case labels should not get extra indentation. Correct example:
</p>
</li>
</ul></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">foo</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="k">case</span><span class="w"> </span><span class="no">BLA_ENUM_VAL</span><span class="p">:</span>
<span class="w"> </span><span class="n">FooBar</span><span class="p">();</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="k">default</span><span class="o">:</span>
<span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="p">}</span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_documentation">Documentation</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
All functions in the public API (= stuff in the headers in libfirm/include) must have at least a short doxygen documentation
</p>
</li>
<li>
<p>
Documentation for functions should be put in the header (except of course static functions which are only in the C file)
</p>
</li>
<li>
<p>
Each file must have an @brief doxygen comment describing the intention/purpose of the file (except public headers with a detailed description in a @defgroup)
</p>
</li>
<li>
<p>
Each file must have an @author doxygen comment so that we can easily see who has written what code.
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_c99">C99</h2>
<div class="sectionbody">
<div class="paragraph"><p>While alot of firm code has been written for C89, we do use C99 now. Use it:</p></div>
<div class="ulist"><ul>
<li>
<p>
Use the bool type from stdbool.h .
</p>
</li>
<li>
<p>
Move declarations as close as possible to their first assignment.
</p>
</li>
<li>
<p>
But as we don’t want to force our users to use C99 we keep the public API free of C99. So no bool type in the public headers.
</p>
</li>
</ul></div>
<div class="listingblock">
<div class="title">Bad</div>
<div class="content"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">bad_example</span><span class="p">(</span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">arg0</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">swap_directions</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">arity</span><span class="p">;</span>
<span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">right</span><span class="p">;</span>
<span class="w"> </span><span class="n">arity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_irn_arity</span><span class="p">(</span><span class="n">arg0</span><span class="p">);</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">arity</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_irn_n</span><span class="p">(</span><span class="n">arg0</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="n">left</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_binop_left</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="w"> </span><span class="n">right</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_binop_right</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">swap_directions</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">;</span>
<span class="w"> </span><span class="n">left</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">right</span><span class="p">;</span>
<span class="w"> </span><span class="n">right</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">do_something</span><span class="p">(</span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="n">right</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div></div></div>
<div class="listingblock">
<div class="title">Good</div>
<div class="content"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">good_example</span><span class="p">(</span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">arg0</span><span class="p">,</span><span class="w"> </span><span class="kt">bool</span><span class="w"> </span><span class="n">swap_directions</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">arity</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_irn_arity</span><span class="p">(</span><span class="n">arg0</span><span class="p">);</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o"><</span><span class="w"> </span><span class="n">arity</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">n</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_irn_n</span><span class="p">(</span><span class="n">arg0</span><span class="p">,</span><span class="w"> </span><span class="n">i</span><span class="p">);</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">left</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_binop_left</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">right</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">get_binop_right</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">swap_directions</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">ir_node</span><span class="w"> </span><span class="o">*</span><span class="n">tmp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">left</span><span class="p">;</span>
<span class="w"> </span><span class="n">left</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">right</span><span class="p">;</span>
<span class="w"> </span><span class="n">right</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">tmp</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="n">do_something</span><span class="p">(</span><span class="n">left</span><span class="p">,</span><span class="w"> </span><span class="n">right</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="cm">/* even better in this case: foreach_irn_in(arg0, i, n) { ... } */</span>
<span class="p">}</span>
</pre></div></div></div>
</div>
</div>
<div class="sect1">
<h2 id="_tips_amp_tricks">Tips & Tricks</h2>
<div class="sectionbody">
<div class="ulist"><ul>
<li>
<p>
<a href="Subclassing.html">Subclassing</a>
</p>
</li>
<li>
<p>
Allocate memory with the macros from <a class="external text" href="http://pp.ipd.kit.edu/git/libfirm/tree/include/libfirm/adt/xmalloc.h">xmalloc.h</a> to be more robust against type errors.
</p>
</li>
<li>
<p>
Use the const modifier freely, even for non-pointers. It makes program understanding easier if you can immediately see that a variable is only assigned once.
</p>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_tools">Tools</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_astyle">AStyle</h3>
<div class="paragraph"><p>You can use astyle to enforce some of the rules above with the following options (needs astyle 2.00):</p></div>
<div class="listingblock">
<div class="content">
<pre><code>--indent=tab=4
--style=stroustrup
--indent-cases
--indent-col1-comments
--pad-header
--pad-oper
--keep-one-line-statements
--align-pointer=name</code></pre>
</div></div>
</div>
<div class="sect2">
<h3 id="_whitespace_check">Whitespace Check</h3>
<div class="paragraph"><p>A simple python script to check at least the tab/whitespace rules:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="n">file_list</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
<span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="n">file_list</span><span class="p">:</span>
<span class="n">file</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">)</span>
<span class="n">num</span> <span class="o">=</span> <span class="mi">1</span>
<span class="n">lastline</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">file</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
<span class="c1"># Tab characters are only allowed as very first characters on a line!</span>
<span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">"[^</span><span class="se">\t</span><span class="s2">]</span><span class="se">\t</span><span class="s2">"</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="nb">print</span> <span class="s2">"</span><span class="si">%s</span><span class="s2">:</span><span class="si">%d</span><span class="s2">: tabulator not at beginning of line"</span> <span class="o">%</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">num</span><span class="p">)</span>
<span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">" +$"</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="nb">print</span> <span class="s2">"</span><span class="si">%s</span><span class="s2">:</span><span class="si">%d</span><span class="s2">: trailing whitespace"</span> <span class="o">%</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">num</span><span class="p">)</span>
<span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">lastline</span> <span class="o">=</span> <span class="n">line</span>
<span class="k">if</span> <span class="n">lastline</span> <span class="o">!=</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">lastline</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">:]</span> <span class="o">!=</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span><span class="p">:</span>
<span class="nb">print</span> <span class="s2">"</span><span class="si">%s</span><span class="s2">:</span><span class="si">%d</span><span class="s2">: No newline at end of file"</span> <span class="o">%</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">num</span><span class="p">)</span>
</pre></div></div></div>
</div>
<div class="sect2">
<h3 id="_emacs">Emacs</h3>
<div class="paragraph"><p>The following style spec appears to make Emacs obey the coding conventions:</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="p">(</span><span class="nv">c-add-style</span><span class="w"> </span><span class="s">"firm"</span>
<span class="w"> </span><span class="o">'</span><span class="p">(</span><span class="s">"stroustrup"</span>
<span class="w"> </span><span class="p">(</span><span class="nv">case-label</span><span class="w"> </span><span class="o">.</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="nv">statement-case-intro</span><span class="w"> </span><span class="o">.</span><span class="w"> </span><span class="nb">+</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="nv">statement-case-open</span><span class="w"> </span><span class="o">.</span><span class="w"> </span><span class="nb">+</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="nv">indent-tabs-mode</span><span class="w"> </span><span class="o">.</span><span class="w"> </span><span class="no">t</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="nv">c-basic-offset</span><span class="w"> </span><span class="o">.</span><span class="w"> </span><span class="mi">4</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="nv">tab-width</span><span class="w"> </span><span class="o">.</span><span class="w"> </span><span class="mi">4</span><span class="p">))</span><span class="w"> </span><span class="no">nil</span><span class="p">)</span>
</pre></div></div></div>
</div>
</div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<div class="layout-footer">
<span><a href="https://lists.ira.uni-karlsruhe.de/mailman/listinfo/firm">Mailing list</a>: <a href="mailto:[email protected]">[email protected]</a></span>
</div>
</body>
</html>