
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
 <channel>
   <title>Gwilym&#39;s blog</title>
   <link>https://gwilym.dev/</link>
   <description>Recent content on Gwilym&#39;s blog</description>
   <generator>Hugo -- gohugo.io</generator>
   <language>en-GB</language>
   <copyright>Copyright &amp;copy; 2020-2021 - Gwilym Inzani [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/)</copyright>
   <lastBuildDate>Wed, 31 Mar 2021 17:02:01 +0100</lastBuildDate>
   
       <atom:link href="https://gwilym.dev/index.xml" rel="self" type="application/rss+xml" />
   
   
     <item>
       <title>Git rebase like a pro</title>
       <link>https://gwilym.dev/2021/03/git-rebase-like-a-pro/</link>
       <pubDate>Wed, 31 Mar 2021 17:02:01 +0100</pubDate>
       
       <guid>https://gwilym.dev/2021/03/git-rebase-like-a-pro/</guid>
       <description>&lt;p&gt;I felt a lot like I was fumbling in the dark when using git for the first year or so.
Tools like &lt;a href=&#34;https://learngitbranching.js.org/&#34;&gt;learngitbranching&lt;/a&gt; were great for getting me going, but I often felt like I didn&amp;rsquo;t have as much control over my commits and the history as I would like.
And when working in a team, merge conflicts become quite common.
The race to be the first to merge the PR so you don&amp;rsquo;t have to deal with the conflict becomes quite heated.&lt;/p&gt;
&lt;p&gt;I used to get upset whenever I had to resolve a merge conflict because it would take ages to work out what I would have to fix.
Or if I realised that I&amp;rsquo;d made a mistake about 10 commits ago and don&amp;rsquo;t want anyone to know.
And sometimes you want to develop on someone else&amp;rsquo;s feature branch rather than off &lt;code&gt;main&lt;/code&gt;.
What do you do when the feature branch you&amp;rsquo;ve been working on gets merged?&lt;/p&gt;
&lt;p&gt;Learning about interactive rebases felt (and still feels like) a super power.&lt;/p&gt;
&lt;p&gt;Note however that rebasing can cause a bit of pain if you&amp;rsquo;re working with people less familiar with git.
But hopefully with these few commands and explanations you&amp;rsquo;ll be git rebasing like a pro in no time!&lt;/p&gt;
&lt;h1 id=&#34;the-most-important-step&#34;&gt;The most important step&lt;/h1&gt;
&lt;p&gt;The most important step before doing a rebase you&amp;rsquo;re not 100% sure will work fine is put down a temporary branch to jump back to.
I&amp;rsquo;ll repeat that in big so that people skipping through this spot it&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Put down a temporary branch before a big rebase&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Just doing &lt;code&gt;git branch tmp/before-rebasing&lt;/code&gt; before you rebase will save you a big headache if you do something horribly wrong.
Yes there are always ways back, but if git decides to garbage collect your commit before you notice you&amp;rsquo;ll be in a difficult space with potential losses of work.&lt;/p&gt;
&lt;p&gt;So, now that you&amp;rsquo;ve done that, I&amp;rsquo;ll continue this article.&lt;/p&gt;
&lt;h1 id=&#34;contents&#34;&gt;Contents&lt;/h1&gt;
&lt;p&gt;This article is quite long, and the subsections and sub-subsections I&amp;rsquo;ve used for some reason make this a little difficult to navigate.
So here are some quick links if you know what you want to do:&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;#1-my-pr-has-a-merge-conflict&#34;&gt;1 My PR has a merge conflict&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;#2-my-branchs-history-makes-no-sense&#34;&gt;2 My branch&amp;rsquo;s history makes no sense&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;#3-someone-rebased-a-branch-i-was-looking-at-locally&#34;&gt;3 Someone rebased a branch I was looking at locally&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;#4-i-branched-of-someone-elses-branch-now-thats-merged-and-i-want-my-branch-off-main&#34;&gt;4 I branched of someone else&amp;rsquo;s branch, now that&amp;rsquo;s merged and I want my branch off main&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;what-does-a-rebase-actually-do&#34;&gt;What does a rebase actually do?&lt;/h1&gt;
&lt;p&gt;A rebase is a kind of &amp;lsquo;history rewriting&amp;rsquo; command.
In essence, it lets you take a group of commits, and apply them to on top of a different commit.&lt;/p&gt;
&lt;p&gt;But, as I will show in some of the examples below, it lets you do all sorts of other operations too.&lt;/p&gt;
&lt;p&gt;Note I will always run a so called &lt;code&gt;interactive&lt;/code&gt; rebase.
This causes git to show you what it plans to do, and gives you a chance to change your mind, cancel the rebase etc.&lt;/p&gt;
&lt;p&gt;I think the easiest way to show what a rebase does and why you&amp;rsquo;d use it is to give some examples of when it is useful and how to use it.&lt;/p&gt;
&lt;h1 id=&#34;1-my-pr-has-a-merge-conflict&#34;&gt;1 My PR has a merge conflict&lt;/h1&gt;
&lt;p&gt;This is probably the easiest and most common time that I would rebase in my day-to-day work.
It is also where I would recommend people get their feet wet and practice before moving on to some of the more advanced use cases below.&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m going to assume your PR is against a branch named &lt;code&gt;main&lt;/code&gt; and that the remote location is called &lt;code&gt;origin&lt;/code&gt;.
Both of these can be swapped for any branches (note that you&amp;rsquo;ll need to refer to a later section if the branch your PR is against has itself been rebased).&lt;/p&gt;
&lt;p&gt;You are currently working on the branch &lt;code&gt;feature&lt;/code&gt; in this example of what we&amp;rsquo;re going to do, and suppose that there is a merge conflict.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/git-rebase/feature-branch.png&#34; alt=&#34;feature branch&#34;&gt;&lt;/p&gt;
&lt;p&gt;Normally the way I see people do this is by first merging &lt;code&gt;main&lt;/code&gt; into their own branch, and then later merging their branch into &lt;code&gt;main&lt;/code&gt;.
I argue that this is ugly and also less useful:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It is harder for you because you have to fix every single merge conflict at the same time. What I mean by that is if you have multiple files conflicting, you have to fix them all. And this can get pretty complex if you have made some deep refactor.&lt;/li&gt;
&lt;li&gt;It is harder for the reviewer because you could change anything in that merge commit, and it can be very difficult to follow that commit specifically.&lt;/li&gt;
&lt;li&gt;Also, looking commit-by-commit at the resulting code requires knowledge of how the system &lt;em&gt;used&lt;/em&gt; to be rather than how it is currently.&lt;/li&gt;
&lt;li&gt;And finally, aesthetically it makes the code graph look really ugly.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This is what your history will end up looking like with the &lt;code&gt;git merge&lt;/code&gt; strategy mentioned above&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/git-rebase/git-merge-diagram.png&#34; alt=&#34;end result of git merging into your branch&#34;&gt;&lt;/p&gt;
&lt;p&gt;And this with &lt;code&gt;git rebase&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/git-rebase/rebase-merge-diagram.png&#34; alt=&#34;end result of git rebasing your branch&#34;&gt;&lt;/p&gt;
&lt;p&gt;Please forgive the strange arrow directions here, not sure what&amp;rsquo;s going on with the visualiser&amp;hellip;&lt;/p&gt;
&lt;h2 id=&#34;11-put-down-a-temporary-branch&#34;&gt;1.1 Put down a temporary branch&lt;/h2&gt;
&lt;p&gt;Okay, I&amp;rsquo;ll admit that in &lt;em&gt;this&lt;/em&gt; case I would normally not.
But if this is the first time you&amp;rsquo;re doing a git rebase, please do it.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git branch tmp/before-rebasing
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Remember that &lt;code&gt;git branch&lt;/code&gt; creates a branch pointing to your current commit, but &lt;strong&gt;doesn&amp;rsquo;t&lt;/strong&gt; switch to it.
You can go back to this if something goes horribly wrong in the next step.&lt;/p&gt;
&lt;h2 id=&#34;12-start-the-interactive-rebase&#34;&gt;1.2 Start the interactive rebase&lt;/h2&gt;
&lt;p&gt;This is the magic of rebasing.
I will always rebase interactively.
If only to know exactly what git is going to do once it starts.&lt;/p&gt;
&lt;p&gt;Firstly, ensure your local pointer to the &lt;code&gt;origin/main&lt;/code&gt; is up to date with&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git fetch
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then, do the rebase&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git rebase -i origin/main
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will open your currently configured text editor (controlled by the &lt;code&gt;EDITOR&lt;/code&gt; environment variable if you want to change it), showing you the list of commits it will replay.&lt;/p&gt;
&lt;p&gt;If you&amp;rsquo;re happy with the list, then save and close the file and git will get to work reapplying your commits one at a time.&lt;/p&gt;
&lt;h2 id=&#34;13-merge-conflict&#34;&gt;1.3 Merge conflict!&lt;/h2&gt;
&lt;p&gt;You&amp;rsquo;re doing this to resolve a merge conflict, so you will get one eventually.
At this point you have to do something you may not be entirely familiar with.
Look at the commit you&amp;rsquo;re in at the moment.
This will be important.&lt;/p&gt;
&lt;p&gt;The current state of your files will be as-if you&amp;rsquo;ve done the commits up to this point on top of main rather than your historical version that you actually did the work in.
So when you fix the merge conflict, you have to leave those files in the state you would&amp;rsquo;ve had it in at &lt;em&gt;this&lt;/em&gt; point in your branch (not the end result) had &lt;code&gt;main&lt;/code&gt; looked like it does now.
This is because after you&amp;rsquo;ve fixed this conflict, the rebase will continue to run the remaining commits.&lt;/p&gt;
&lt;p&gt;Once you&amp;rsquo;re happy with the changes, you can &lt;code&gt;git add&lt;/code&gt; the files you&amp;rsquo;ve updated to tell &lt;code&gt;git&lt;/code&gt; you&amp;rsquo;ve fixed the conflict.&lt;/p&gt;
&lt;p&gt;And then run&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git rebase --continue
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;to continue the rebase.&lt;/p&gt;
&lt;p&gt;Once that&amp;rsquo;s all done, push your branch up to &lt;code&gt;origin&lt;/code&gt; with&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git push --force-with-lease
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and you&amp;rsquo;re good to go.&lt;/p&gt;
&lt;h2 id=&#34;14-help-something-went-wrong&#34;&gt;1.4 Help, something went wrong!&lt;/h2&gt;
&lt;p&gt;It happens sometimes.
How to fix it depends on at what point you&amp;rsquo;re at for your rebase.&lt;/p&gt;
&lt;h3 id=&#34;141-the-rebase-totally-failed-but-it-claims-to-have-finished&#34;&gt;1.4.1 The rebase totally failed but it claims to have finished&lt;/h3&gt;
&lt;p&gt;Good thing you put a temporary branch down at the start.
Simply resetting your git branch to that temporary place will fix the issue.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git reset --hard tmp/before-rebase
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you were a terrible person and didn&amp;rsquo;t put a temporary branch down, all is not lost (but it is definitely slightly harder).
Provided you haven&amp;rsquo;t pushed your changes yet, you can jump to the old location according to where it is in origin, with&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git reset --hard origin/feature
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you&amp;rsquo;ve pushed, you may be able to get an old commit hash for the end of your old feature branch from somewhere, and then you can do&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git reset --hard &amp;lt;commit hash&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And if you&amp;rsquo;ve done none of those things, take this as a lesson to read the text in big writing.
Or look up the git reflog and have a go at working with that.&lt;/p&gt;
&lt;h3 id=&#34;142-the-rebase-is-still-in-progress-but-i-dont-want-to-do-it-any-more&#34;&gt;1.4.2 The rebase is still in progress but I don&amp;rsquo;t want to do it any more&lt;/h3&gt;
&lt;p&gt;This is a nice easy fix.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git rebase --abort
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;will take you back to where you started.&lt;/p&gt;
&lt;h3 id=&#34;143-i-thought-i-wanted-this-commit-but-i-dont-really&#34;&gt;1.4.3 I thought I wanted this commit, but I don&amp;rsquo;t really&lt;/h3&gt;
&lt;p&gt;Running&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git rebase --skip
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;allows you to completely skip this commit and continue with the next ones.
This comes up occasionally when you fix a bug in some existing code because your new feature is exercising it but someone beat you to the fix.
Then it makes no sense to keep this commit, so skip it.&lt;/p&gt;
&lt;h1 id=&#34;2-my-branchs-history-makes-no-sense&#34;&gt;2 My branch&amp;rsquo;s history makes no sense&lt;/h1&gt;
&lt;p&gt;This is probably my favourite usage of git rebasing.
Not only is it super useful in making the reviewer&amp;rsquo;s job easier, but it also hides your silly mistakes which will make you look like a l33t haxxor.&lt;/p&gt;
&lt;p&gt;I do this if I want to&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Change a commit message for a commit a little bit back in the history&lt;/li&gt;
&lt;li&gt;Split some commits into more pieces&lt;/li&gt;
&lt;li&gt;Combine multiple commits into one&lt;/li&gt;
&lt;li&gt;Reorder some commits to makes more sense to a reviewer&lt;/li&gt;
&lt;li&gt;Pretend I wrote the tests first&lt;/li&gt;
&lt;/ol&gt;
&lt;h1 id=&#34;21-put-down-a-temporary-branch&#34;&gt;2.1 Put down a temporary branch&lt;/h1&gt;
&lt;p&gt;You bored of me saying this already?
I&amp;rsquo;ve had more complaints about people not doing this step then anything else with git rebasing.
So hopefully this&amp;rsquo;ll stop that&lt;/p&gt;
&lt;p&gt;Something along the lines of the following will save you.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git branch tmp/before-rebasing
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id=&#34;22-start-the-interactive-rebase&#34;&gt;2.2 Start the interactive rebase&lt;/h1&gt;
&lt;p&gt;In this case, we&amp;rsquo;re not going to be rebasing to the tip of main (although you could decide to do that if you want).
So here we will instead find the commit hash of the oldest commit in your branch.
I like to find it using &lt;code&gt;git log&lt;/code&gt; and scrolling back but you can do this any way you want.&lt;/p&gt;
&lt;p&gt;Lets say my commit hash is &lt;code&gt;deadbeef&lt;/code&gt;, use the command&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;git rebase -i deadbeef
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and this time we&amp;rsquo;re actually going to change the contents of the file a little.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;ll see something along the lines of this in the file.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;# Commands:
# p, pick &amp;lt;commit&amp;gt; = use commit
# r, reword &amp;lt;commit&amp;gt; = use commit, but edit the commit message
# e, edit &amp;lt;commit&amp;gt; = use commit, but stop for amending
# s, squash &amp;lt;commit&amp;gt; = use commit, but meld into previous commit
# f, fixup &amp;lt;commit&amp;gt; = like &amp;quot;squash&amp;quot;, but discard this commit&#39;s log message
# x, exec &amp;lt;command&amp;gt; = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with &#39;git rebase --continue&#39;)
# d, drop &amp;lt;commit&amp;gt; = remove commit
# l, label &amp;lt;label&amp;gt; = label current HEAD with a name
# t, reset &amp;lt;label&amp;gt; = reset HEAD to a label
# m, merge [-C &amp;lt;commit&amp;gt; | -c &amp;lt;commit&amp;gt;] &amp;lt;label&amp;gt; [# &amp;lt;oneline&amp;gt;]
# .       create a merge commit using the original merge commit&#39;s
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c &amp;lt;commit&amp;gt; to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Pretty much the rest of this section will be what these do, ordered from most useful to least.&lt;/p&gt;
&lt;h3 id=&#34;221-pick&#34;&gt;2.2.1 pick&lt;/h3&gt;
&lt;p&gt;You&amp;rsquo;ll see a lot of these by default.
One for every commit hash in your branch.
Reorder them in any order, and importantly remove any for commits you don&amp;rsquo;t want any more.&lt;/p&gt;
&lt;h3 id=&#34;222-fixup&#34;&gt;2.2.2 fixup&lt;/h3&gt;
&lt;p&gt;If you did a commit, and then the next commit is just fixing a typo in something in the previous commit (or you have a commit that fixes a typo in a commit some time in the past), you can follow up your old commit with this fixup.&lt;/p&gt;
&lt;p&gt;As an example, suppose it looked something like this&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pick deadbeef1 Add an article about the C preprocessor
pick deadbeef2 Add an article about PAL video processing
pick deadbeef3 Fix typo in C preprocessor article
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Change the file to the following before rebasing and the end result is your commit adding the article about the C preprocessor will also contain the typo fix.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pick deadbeef1 Add an article about the C preprocessor
fixup deadbeef3 Fix typo in C preprocessor article
pick deadbeef2 Add an article about PAL video processing
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The difference between &lt;code&gt;fixup&lt;/code&gt; and &lt;code&gt;squash&lt;/code&gt; is that &lt;code&gt;squash&lt;/code&gt; awkwardly adds the commit message of the squashed commit to the commit message of the commit it is being squashed into.
So I use &lt;code&gt;fixup&lt;/code&gt; much more often then squash.&lt;/p&gt;
&lt;h3 id=&#34;223-edit&#34;&gt;2.2.3 edit&lt;/h3&gt;
&lt;p&gt;Maybe you want to edit an entire commit.
This is super useful if you accidentally commit something you didn&amp;rsquo;t mean to.
For example, maybe you stuck a &lt;code&gt;debugger&lt;/code&gt; statement in a js file somewhere while you were developing it and accidentally committed it.&lt;/p&gt;
&lt;p&gt;Putting &lt;code&gt;edit&lt;/code&gt; before that commit will allow you to remove the debugger line once git gets to that point in the rebase.
There is something &lt;em&gt;very&lt;/em&gt; important to do here though (and a lot of people get this wrong the first time).&lt;/p&gt;
&lt;p&gt;Once you&amp;rsquo;ve fixed up that commit, &lt;code&gt;git add&lt;/code&gt; the files you&amp;rsquo;ve modified and then run &lt;code&gt;git rebase --continue&lt;/code&gt;.
DO NOT COMMIT THE CHANGES.
The &lt;code&gt;git rebase --continue&lt;/code&gt; will do the commit with the old commit message for you.&lt;/p&gt;
&lt;h3 id=&#34;224-removing-the-line-entirely&#34;&gt;2.2.4 Removing the line entirely&lt;/h3&gt;
&lt;p&gt;If you don&amp;rsquo;t want the commit any more, just don&amp;rsquo;t include the line with that commit in the rebase command list.
Then when git does the rebase, that commit won&amp;rsquo;t be included.&lt;/p&gt;
&lt;h2 id=&#34;23-after-rebasing&#34;&gt;2.3 After rebasing&lt;/h2&gt;
&lt;p&gt;Once you&amp;rsquo;re done, if you have already pushed your branch you&amp;rsquo;ll have to force push it to &lt;code&gt;origin&lt;/code&gt; like the merge conflict example above.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git push --force-with-lease
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And now it looks like you never make any mistakes.
I&amp;rsquo;m sure your colleagues will be incredibly impressed.&lt;/p&gt;
&lt;h2 id=&#34;24-help-something-went-wrong&#34;&gt;2.4 Help, something went wrong!&lt;/h2&gt;
&lt;p&gt;The same advice applies as from the &lt;a href=&#34;#14-help-something-went-wrong&#34;&gt;previous section&lt;/a&gt;&lt;/p&gt;
&lt;h1 id=&#34;3-someone-rebased-a-branch-i-was-looking-at-locally&#34;&gt;3 Someone rebased a branch I was looking at locally&lt;/h1&gt;
&lt;p&gt;This can be annoying if you&amp;rsquo;ve not seen it before.
You run &lt;code&gt;git pull&lt;/code&gt; and you get some strange errors or it is telling you it needs to commit something.&lt;/p&gt;
&lt;p&gt;If you have done some of your own work on this branch too, then put down a temporary branch first, and then after this do step 4 on that temporary branch.&lt;/p&gt;
&lt;p&gt;Afterwards, &lt;code&gt;git fetch&lt;/code&gt; to update your local copy and then &lt;code&gt;git reset --hard origin/branchname&lt;/code&gt; gets you back in place.&lt;/p&gt;
&lt;h1 id=&#34;4-i-branched-of-someone-elses-branch-now-thats-merged-and-i-want-my-branch-off-main&#34;&gt;4 I branched of someone else&amp;rsquo;s branch, now that&amp;rsquo;s merged and I want my branch off main&lt;/h1&gt;
&lt;p&gt;This is a long title, but I couldn&amp;rsquo;t come up with a more descriptive name.
This works very similarly to problem #1 but with a slight modification.&lt;/p&gt;
&lt;p&gt;Just running &lt;code&gt;git rebase origin/main&lt;/code&gt; will cause the rebase to pick up not only your changes but also all of the changes in the branch you branched off.
And git really doesn&amp;rsquo;t like applying a diff twice.&lt;/p&gt;
&lt;p&gt;Firstly, put down a temporary branch to go back to in case things go horribly wrong with &lt;code&gt;git branch tmp/before-rebase&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So, you need to find the point at which you branched off (for example by using &lt;code&gt;git log&lt;/code&gt; or similar), and then run&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;$ git rebase -i &amp;lt;commit hash&amp;gt; --onto&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;origin/main
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;At this point, everything will go ahead like in &lt;a href=&#34;#1-my-pr-has-a-merge-conflict&#34;&gt;#1&lt;/a&gt;.
And of course you could also apply the steps in &lt;a href=&#34;#2-my-branchs-history-makes-no-sense&#34;&gt;#2&lt;/a&gt; to change what your branch history looks like.&lt;/p&gt;
&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;Hopefully with these tips you&amp;rsquo;ll be rebasing like a pro.
This was a difficult article to write because I wasn&amp;rsquo;t sure how to organise it.
However, hopefully once you&amp;rsquo;ve mastered these techniques, you&amp;rsquo;ll really understand not only the power of git, but also how helpful having a clean history in your project is.&lt;/p&gt;
</description>
     </item>
   
     <item>
       <title>Virtual joystick on Linux</title>
       <link>https://gwilym.dev/2021/02/virtual-joystick-on-linux/</link>
       <pubDate>Sun, 28 Feb 2021 00:00:00 +0000</pubDate>
       
       <guid>https://gwilym.dev/2021/02/virtual-joystick-on-linux/</guid>
       <description>&lt;p&gt;A few months ago, I built a home made &lt;a href=&#34;https://retropie.org.uk/&#34;&gt;retropie&lt;/a&gt; based handheld console built around the raspberry pi 3B+.
There probably wasn&amp;rsquo;t anything too exciting in the build itself (it was made mainly using standard parts with a 3d printed shell).
However, I believe the way I got the input working isn&amp;rsquo;t as well documented as I believe it should be.&lt;/p&gt;
&lt;p&gt;The end result of this project was a battery powered console that is incredibly uncomfortable to hold, so I&amp;rsquo;m not going to put together a full build explanation.
I&amp;rsquo;m only going to talk about the arduino code and the driver I put together.&lt;/p&gt;
&lt;h1 id=&#34;what-was-i-trying-to-achieve&#34;&gt;What was I trying to achieve?&lt;/h1&gt;
&lt;p&gt;Most people seem to use an arduino leonardo or a teensy microcontroller with their handheld raspberry pi consoles.
Or they would connect the buttons on the console directly to the GPIO pins on the raspberry pi.
This is a perfectly good option, but I wanted my console to have two analog joysticks, and due to the lack of analog to digital converter (ADC) on the raspberry pi, I needed to use something external.&lt;/p&gt;
&lt;p&gt;I had a couple of atmega 328p&amp;rsquo;s, the microcontroller that powers the arduino uno among others, lying around so this seemed like a good option.&lt;/p&gt;
&lt;p&gt;In the end, I wanted 4 analog axes and 14 buttons (up, down, left, right, start, select, A, B, X, Y, L1, L2, R1 and R2).
An atmega 328p has 6 analog inputs and 14 digital IO ports.
So it seems like my desired button scheme would &lt;em&gt;just&lt;/em&gt; fit on a single chip.&lt;/p&gt;
&lt;h1 id=&#34;the-hardware&#34;&gt;The hardware&lt;/h1&gt;
&lt;p&gt;The wiring and hardware for this project wasn&amp;rsquo;t particularly interesting so I&amp;rsquo;ll skip over most of the details here.
I wanted to keep the serial output of the arduino free, so no buttons were hooked up to digital pins 0 and 1 (which are used for this protocol).
The analog joysticks were attached to analog inputs A0 - A3 with the remaining buttons on A4 and A5.
This meant that every single pin was used on the microcontroller (literally the first project I&amp;rsquo;ve done where this is the case).&lt;/p&gt;
&lt;p&gt;For size and cost reasons, this was also built as a minimal system, so no arduino uno inside my games console.
Maybe I&amp;rsquo;ll write an article at some point about the easiest way to solder up an arduino compatible microcontroller on a perf board.&lt;/p&gt;
&lt;h1 id=&#34;the-software&#34;&gt;The software&lt;/h1&gt;
&lt;p&gt;This is where things get exciting.
The code for this post can be found here: &lt;a href=&#34;https://github.com/gwilymk/arduino-joystick&#34;&gt;https://github.com/gwilymk/arduino-joystick&lt;/a&gt;.
I recommend opening this up if you plan on following how this was done at home.&lt;/p&gt;
&lt;p&gt;The atmega chip was programmed using the arduino environment (which I&amp;rsquo;ll refer to as the firmware), and the driver is written in rust.&lt;/p&gt;
&lt;h1 id=&#34;the-joystick-firmware&#34;&gt;The joystick firmware&lt;/h1&gt;
&lt;p&gt;Running on the atmega chip, is a very simple sketch that reads the state of all the buttons and sends packed bytes containing the result along the serial bus every 10ms.
This isn&amp;rsquo;t particularly interesting, but the format of the data will be important when looking at the driver code.&lt;/p&gt;
&lt;p&gt;The data format is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Start marker:
1111111111111111

Which buttons are pressed, 1 means pressed in some order
0011101000101111

The state of the 4 joystick axes
0000000101000101
0000001001001000
0000001001001100
0000001001110010
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So a single frame is a total of 12 bytes split into 2 byte sections.
Since there are a total of 14 buttons, the first two bits of the button state will always be 0.
Similarly, since the ADC of the arduino has 10-bit precision, the first 6 bits of the joystick state will always be 0s.&lt;/p&gt;
&lt;p&gt;Therefore, the start marker is unique in that it is the only 2 byte field that starts with 1s (which is how the driver and the firmware line themselves up when the firmware starts up).&lt;/p&gt;
&lt;p&gt;You can have a look at the code for this in the &lt;code&gt;joystick-firmware&lt;/code&gt; folder in the repo if you want to see how this is implemented.&lt;/p&gt;
&lt;h1 id=&#34;the-joystick-driver&#34;&gt;The joystick driver&lt;/h1&gt;
&lt;p&gt;This is where things get more exciting.
Under the hood, we use &lt;code&gt;udev&lt;/code&gt; to emulate the joystick.
The gnarly bits of this are hidden by the &lt;code&gt;input-linux&lt;/code&gt; crate which is used quite heavily.
There are a few potential things that you might run into while trying to emulate a joystick yourself.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;However hard you try to tell Linux that you are emulating a joystick, if it doesn&amp;rsquo;t have a trigger button, it won&amp;rsquo;t believe you. From my list of buttons above, there wasn&amp;rsquo;t a trigger. But I had to include it or my joystick wouldn&amp;rsquo;t show up.&lt;/li&gt;
&lt;li&gt;There seem to be some strange rules about when you have to run as root or as a regular user. On my desktop I could run as my regular user, but on the raspberry pi I had to run it as root.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I&amp;rsquo;m going to quickly describe the code &lt;a href=&#34;https://github.com/gwilymk/arduino-joystick/blob/master/joystick-daemon/src/joystick/mod.rs&#34;&gt;here&lt;/a&gt; in detail.
I will assume some familiarity with rust, but it probably isn&amp;rsquo;t massively important to get the idea across :).&lt;/p&gt;
&lt;h2 id=&#34;creating-the-virtual-joystick&#34;&gt;Creating the virtual joystick&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-rust&#34; data-lang=&#34;rust&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt; uinput_file &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; fs::File::create(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;/dev/uinput&amp;#34;&lt;/span&gt;)&lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt;;
&lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt; device &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; input_linux::UInputHandle::new(uinput_file);

&lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt; input_id &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; input_linux::InputId {
    bustype: &lt;span style=&#34;color:#a6e22e&#34;&gt;sys&lt;/span&gt;::BUS_VIRTUAL,
    vendor: &lt;span style=&#34;color:#ae81ff&#34;&gt;34&lt;/span&gt;,
    product: &lt;span style=&#34;color:#ae81ff&#34;&gt;10&lt;/span&gt;,
    version: &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;,
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The first thing we need to do is open the &lt;code&gt;/dev/uinput&lt;/code&gt; file for writing.
This is the file that udev expects to write to.
We also assign some random vendor, product and version information to our joystick.
It doesn&amp;rsquo;t really matter what you put here, but I guess you don&amp;rsquo;t want to clash with existing ones?
(I&amp;rsquo;m pretty sure I just typed random numbers here myself).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-rust&#34; data-lang=&#34;rust&#34;&gt;device.set_evbit(input_linux::EventKind::Absolute)&lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt;;
device.set_evbit(input_linux::EventKind::Key)&lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt;;
device.set_keybit(input_linux::Key::ButtonTrigger)&lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt;;

&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; button &lt;span style=&#34;color:#66d9ef&#34;&gt;in&lt;/span&gt; Button::all_buttons() {
    device.set_keybit(button.to_evdev_button())&lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Here we set various configuration options for our virtual joystick.
The first says we&amp;rsquo;ll send absolute positioning events (this will be for the analog inputs).
Then that we&amp;rsquo;re going to also send key events (which will be the buttons).&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;keybits&lt;/code&gt; are listing all the key events we could possibly send.
As mentioned above, you have to include &lt;code&gt;ButtonTrigger&lt;/code&gt; otherwise Linux doesn&amp;rsquo;t work out that it is a joystick.
The rest are the remaining buttons which are stored in an enum elsewhere.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-rust&#34; data-lang=&#34;rust&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt; standard_info &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; input_linux::AbsoluteInfo {
    value: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
    minimum: &lt;span style=&#34;color:#f92672&#34;&gt;-&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;512&lt;/span&gt;,
    maximum: &lt;span style=&#34;color:#ae81ff&#34;&gt;512&lt;/span&gt;,
    fuzz: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
    flat: &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
    resolution: &lt;span style=&#34;color:#ae81ff&#34;&gt;50&lt;/span&gt;,
};

device.create(
    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;input_id,
    &lt;span style=&#34;color:#e6db74&#34;&gt;b&amp;#34;arduino-virtual-joystick&amp;#34;&lt;/span&gt;,
    &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;,
    &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;Axis::all_axes()
        .map(&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt;axis&lt;span style=&#34;color:#f92672&#34;&gt;|&lt;/span&gt; input_linux::AbsoluteInfoSetup {
            axis: &lt;span style=&#34;color:#a6e22e&#34;&gt;axis&lt;/span&gt;.to_evdev_axis(),
            info: &lt;span style=&#34;color:#a6e22e&#34;&gt;standard_info&lt;/span&gt;,
        })
        .collect::&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;Vec&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;_&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt;(),
)&lt;span style=&#34;color:#f92672&#34;&gt;?&lt;/span&gt;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we create our device in &lt;code&gt;/dev/uinput&lt;/code&gt;.
The &lt;code&gt;standard_info&lt;/code&gt; stores information about the joystick.
I didn&amp;rsquo;t need to change many values here, but since the 10-bit ADC on the atmega produces numbers between 0 and 1024 with 512 being the neutral position, we scale this to -512 to 512 for each axis.&lt;/p&gt;
&lt;p&gt;For some reason, you have to set up the axis information when creating the device but buttons beforehand.&lt;/p&gt;
&lt;p&gt;At this point, if you run just this code, you&amp;rsquo;ll find a virtual joystick registered in your list of input devices.&lt;/p&gt;
&lt;p&gt;However, in order to do anything you&amp;rsquo;ll need to send &amp;lsquo;events&amp;rsquo; to udev.&lt;/p&gt;
&lt;h2 id=&#34;pressing-buttons&#34;&gt;Pressing buttons&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-rust&#34; data-lang=&#34;rust&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;pub&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;fn&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;button_press&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;self, button: &lt;span style=&#34;color:#a6e22e&#34;&gt;Button&lt;/span&gt;, is_pressed: &lt;span style=&#34;color:#66d9ef&#34;&gt;bool&lt;/span&gt;) -&amp;gt; Result&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;(), Error&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;let&lt;/span&gt; value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; is_pressed {
        input_linux::KeyState::PRESSED
    } &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; {
        input_linux::KeyState::RELEASED
    };

    self.write_event(input_linux::KeyEvent::new(
        empty_event_time(),
        button.to_evdev_button(),
        value,
    ))
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;A button press is actually a key event.
All events come with an &lt;code&gt;EventTime&lt;/code&gt; which didn&amp;rsquo;t seem to mind always being set to 0.
I can&amp;rsquo;t remember if this meant something special (too long since I wrote this code).&lt;/p&gt;
&lt;p&gt;A button press or release is just a key press or release with the correct button code.
However, if you just do this, then not much is going to happen.&lt;/p&gt;
&lt;p&gt;Since you could in theory press multiple buttons at once, and these multiple buttons at once could be important, after every event you send you must also send a synchronise event.
This event will cause all the events since the last synchronise event to be processed.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-rust&#34; data-lang=&#34;rust&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;pub&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;fn&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;synchronise&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;self) -&amp;gt; Result&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;(), Error&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; {
    self.write_event(input_linux::SynchronizeEvent::report(empty_event_time()))
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There isn&amp;rsquo;t much more to say about button events here.&lt;/p&gt;
&lt;h2 id=&#34;moving-axes&#34;&gt;Moving axes&lt;/h2&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-rust&#34; data-lang=&#34;rust&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;pub&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;fn&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;move_axis&lt;/span&gt;(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;self, axis: &lt;span style=&#34;color:#a6e22e&#34;&gt;Axis&lt;/span&gt;, position: &lt;span style=&#34;color:#66d9ef&#34;&gt;i32&lt;/span&gt;) -&amp;gt; Result&lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt;(), Error&lt;span style=&#34;color:#f92672&#34;&gt;&amp;gt;&lt;/span&gt; {
    &lt;span style=&#34;color:#75715e&#34;&gt;// error handling omitted
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;
    self.write_event(input_linux::AbsoluteEvent::new(
        empty_event_time(),
        axis.to_evdev_axis(),
        position,
    ))
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Moving an axis is an &lt;code&gt;AbsoluteEvent&lt;/code&gt;.
In this case, the position should be between -512 and 512 as set up earlier.&lt;/p&gt;
&lt;h1 id=&#34;hooking-up-the-serial-connection&#34;&gt;Hooking up the Serial connection&lt;/h1&gt;
&lt;p&gt;I wanted to minimise the number of external components.
So the atmega&amp;rsquo;s serial connection is connected straight through to the raspberry pi.
Since the raspberry pi runs at 3.3V, to simplify the voltage handling the microcontroller is also run at 3.3V.
This works perfectly well according to the acceptable voltages ranges for an atmega 328p and means that it and the pi can be wired directly to each other.&lt;/p&gt;
&lt;p&gt;Serial (or UART) can be found on GPIO pins 14 and 15 (physical pin 8 and 10) on the raspberry pi&amp;rsquo;s GPIO port.
You need to set it up in the pi configuration as described &lt;a href=&#34;https://www.raspberrypi.org/documentation/configuration/uart.md&#34;&gt;here&lt;/a&gt;.
With that set up, you can access the serial port on &lt;code&gt;/dev/serial0&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So, I copied the executable built on the raspberry pi (or cross compiled) to &lt;code&gt;/usr/local/bin/arduino-joystick-daemon&lt;/code&gt; and then in &lt;code&gt;/etc/rc.local&lt;/code&gt; added &lt;code&gt;/usr/local/bin/arduino-joystick-daemon /dev/serial0 &amp;amp;&lt;/code&gt;.
Retropie immediately recognised the joystick and allowed me to configure it.&lt;/p&gt;
&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;You don&amp;rsquo;t need to hook anything up to the USB port on your raspberry pi if all you need is a few analog inputs.
Also, you should think about ergonomics a little if you want to design a games console with a raspberry pi inside it.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/console-small.jpg&#34; alt=&#34;Horrible to play console&#34;&gt;&lt;/p&gt;
</description>
     </item>
   
     <item>
       <title>Grayscale PAL signals</title>
       <link>https://gwilym.dev/2021/01/grayscale-pal-signals/</link>
       <pubDate>Sat, 30 Jan 2021 11:55:56 +0000</pubDate>
       
       <guid>https://gwilym.dev/2021/01/grayscale-pal-signals/</guid>
       <description>&lt;p&gt;I was inspired by Ben Eater&amp;rsquo;s video on the &lt;a href=&#34;https://www.youtube.com/watch?v=l7rce6IQDWs&#34;&gt;world&amp;rsquo;s worst video card&lt;/a&gt; to make my own.
But in order to prove to myself that I fully understood how it worked, I decided to output composite PAL signals rather than VGA.
This post is going to explain how composite signals work rather than how my implementation of the world&amp;rsquo;s even worsterer graphics card works.&lt;/p&gt;
&lt;p&gt;I live in the UK, so here we use PAL signals rather than NTSC.
When talking about grayscale, both are very similar, but just different enough to be annoying.
I won&amp;rsquo;t be covering how they differ in this post, because I wouldn&amp;rsquo;t be able to confirm its accuracy.&lt;/p&gt;
&lt;h1 id=&#34;how-did-crt-televisions-work&#34;&gt;How did CRT televisions work?&lt;/h1&gt;
&lt;p&gt;Although a strange question to ask first, the whole composite signal standard is based around the behaviour of CRT (or cathode ray tube) televisions.
Technology connections has a wonderful video explaining the general concepts &lt;a href=&#34;https://www.youtube.com/watch?v=l4UgZBs7ZGo&#34;&gt;here&lt;/a&gt;, which I will recommend watching for further detail about how the television would produce the signal.
I am going to go into much more depth about the actual signal itself, rather than the behaviour of the electron beam as the drawing happens.&lt;/p&gt;
&lt;p&gt;Hopefully from this explanation, you should be able to create a terrible graphics card of your own!&lt;/p&gt;
&lt;h1 id=&#34;lines&#34;&gt;Lines&lt;/h1&gt;
&lt;p&gt;A CRT doesn&amp;rsquo;t have pixels.
Instead, the electron beam gets scanned across in lines.
Each line has effectively got an infinite resolution, as you can change the intensity of the electron beam as much as you like (although capacitance will probably stop you from changing it too much).&lt;/p&gt;
&lt;p&gt;Potential epilepsy trigger warning in the following video.&lt;/p&gt;
&lt;video title=&#34;960FPS video of a white screen on a CRT TV, click to play&#34; loop onclick=&#34;javascript:this.paused?this.play():this.pause()&#34; poster=&#34;/post-images/pal-signal/white-screen.png&#34;&gt;
    &lt;source src=&#34;https://gwilym.dev/post-images/pal-signal/white-screen.mp4&#34; type=&#34;video/mp4&#34; /&gt;
&lt;/video&gt;
&lt;p&gt;With a PAL signal, there are a total of 625 lines per frame, running at 25 frames per second.
However, changing data at 25 frames per second produces noticeable flicker, so rather than update the entire screen 25 times per second, the odd lines get updated first and then the even lines.
This is called &lt;code&gt;interlacing&lt;/code&gt;.
So 312.5 lines are updated at 50 frames per second, made up of 313 odd lines, and 312 even lines.
Note that it is no coincidence that the updates happen at exactly the frequency of the AC power.
This provided a very convenient source of a 50Hz timing signal for the TV to use to synchronise with the signal.&lt;/p&gt;
&lt;p&gt;A single line takes 1/625/25 = 64μs to draw.&lt;/p&gt;
&lt;p&gt;To control the brightness of a point in a line, set the voltage to a value between 0.3V and 1V.
With 0.3V being black, and 1V being pure white.&lt;/p&gt;
&lt;p&gt;However, we also need to tell the TV that the line has started, and also give it time to take the beam from the far end of the previous line to the start of the current one.
In order to do that, a line starts with 4.6-4.8μs of 0V.
This tells the TV that the line has started.
There is then something called a &lt;code&gt;back porch&lt;/code&gt;.
This is a period of 0.3V which if you try to draw into, will be off the left of the screen.
This time is around 5.6μs before the beam becomes visible on the screen.
After this, voltages between 0.3V and 1V will draw the various brightnesses on the screen.
Eventually, the line disappears off the end of the right hand side of the screen (slightly before the end of the line), at which point the next line starts.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/pal-signal/line-signal.png&#34; alt=&#34;line signal diagram&#34;&gt;&lt;/p&gt;
&lt;p&gt;So now we understand how a single line is drawn, and how we tell the TV that we have started drawing a line.
Next, we need to cover how to tell the TV that we&amp;rsquo;re starting to draw a frame.&lt;/p&gt;
&lt;h1 id=&#34;synchronisation-pulses&#34;&gt;Synchronisation pulses&lt;/h1&gt;
&lt;p&gt;We need to tell the TV that a frame is about to start.
However, we don&amp;rsquo;t just need to tell it that we&amp;rsquo;re about to start a frame, we also need to tell it that whether it is an odd or ever &amp;lsquo;half frame&amp;rsquo;.
An odd frame will be one which draws all the odd lines, and an even frame one which only sends data for even lines.&lt;/p&gt;
&lt;p&gt;It is helpful at this stage to think about &amp;lsquo;half lines&amp;rsquo; for reasons that will be clear later.
A half line is exactly as the name implies.
It takes 32μs to draw a half line.&lt;/p&gt;
&lt;p&gt;A half line will either be a &lt;code&gt;short sync&lt;/code&gt; pulse or a &lt;code&gt;long sync&lt;/code&gt; pulse.
A long sync pulse is at 0.3V for 30μs and 0V for 2μs.
A short sync pulse is at 0.3V for 2μs and 0V for 30μs.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/pal-signal/sync-pulses.png&#34; alt=&#34;sync pulse diagram&#34;&gt;&lt;/p&gt;
&lt;p&gt;To say you&amp;rsquo;re going to start an odd frame, send 6 short sync pulses, 5 long sync pulses followed by 5 short sync pulses.
At that point, send the normal line information for the remaining 305 lines until the end of this half-frame.
Then start the even frame with 5 short sync pulses, 5 long pulses and 4 short pulses.
And the remaining 305 lines contain the even lines.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/pal-signal/frame-sync.png&#34; alt=&#34;frame sync diagram&#34;&gt;&lt;/p&gt;
&lt;h1 id=&#34;non-interlaced-video&#34;&gt;Non-interlaced video&lt;/h1&gt;
&lt;p&gt;In order to simplify video hardware (and what I ultimately ended up doing for my breadboard video card), you can send the information non-interlaced to the TV.
If doing this, effectively send the odd half frame repeatedly rather than switching between odd and even half frames.
Note that this means your frame rate will be 1 / (64μs × 313) = 49.92Hz.&lt;/p&gt;
&lt;p&gt;However, according to &lt;a href=&#34;http://martin.hinner.info/vga/pal.html&#34;&gt;this source&lt;/a&gt;, the PS2 on a non-interlaced game will use 304 lines rather than 305.
I haven&amp;rsquo;t personally tried 305 lines to see what happens here, so the way I implemented this was to copy the non-interlaced format and use the odd frame cycle but with 304 lines resulting in a frame rate of 1 / (64μs × 313) = 50.0801Hz.
TVs don&amp;rsquo;t seem to have an issue with this (or at least mine doesn&amp;rsquo;t).&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/pal-signal/non-interlaced.png&#34; alt=&#34;non-interlaced diagram&#34;&gt;&lt;/p&gt;
&lt;h1 id=&#34;implementation-notes&#34;&gt;Implementation notes&lt;/h1&gt;
&lt;p&gt;You&amp;rsquo;ll have noticed that I spoke about voltages the entire time here.
One thing to note is that the input impedance for a composite cable is 75Ω (you can measure this with a multimeter).
In order to actually produce these voltages required, you&amp;rsquo;ll have to use a suitable resistor to bring your control signal voltage to 0.3V or the analog value for the brightness.&lt;/p&gt;
&lt;h1 id=&#34;full-demo&#34;&gt;Full demo&lt;/h1&gt;
&lt;p&gt;Using what Ben Eater teaches in his video about the world&amp;rsquo;s worst video card, and my research which unearthed the above information, I was able to put together my own video card.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://gwilym.dev/post-images/pal-signal/breadboard-video-card.JPG&#34; alt=&#34;breadboard video card&#34;&gt;&lt;/p&gt;
&lt;p&gt;And the result is a 128x128 image on a CRT.
Here is a 960FPS video of the output from this circuit.&lt;/p&gt;
&lt;p&gt;Another potential epilepsy trigger&lt;/p&gt;
&lt;video title=&#34;960FPS video of a the resulting display on a CRT TV, click to play&#34; loop onclick=&#34;javascript:this.paused?this.play():this.pause()&#34; poster=&#34;/post-images/pal-signal/resulting-screen.png&#34;&gt;
    &lt;source src=&#34;https://gwilym.dev/post-images/pal-signal/resulting-screen.mp4&#34; type=&#34;video/mp4&#34; /&gt;
&lt;/video&gt;</description>
     </item>
   
     <item>
       <title>The C preprocessor is awesome part III</title>
       <link>https://gwilym.dev/2020/12/the-c-preprocessor-is-awesome-part-iii/</link>
       <pubDate>Mon, 21 Dec 2020 22:09:06 +0000</pubDate>
       
       <guid>https://gwilym.dev/2020/12/the-c-preprocessor-is-awesome-part-iii/</guid>
       <description>&lt;p&gt;This is the third part of a 3 part series on why the C preprocessor is awesome and showing you some tricks you should never use.
In this part, we&amp;rsquo;re finally going to implement the &lt;code&gt;TRY&lt;/code&gt;, &lt;code&gt;CATCH&lt;/code&gt; and &lt;code&gt;FINALLY&lt;/code&gt; macros I&amp;rsquo;ve been promising you since the beginning.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&#34;https://gwilym.dev/2020/08/the-c-preprocessor-is-awesome-part-i/&#34;&gt;part 1&lt;/a&gt; of this series, we implemented a very nice test harness that let us write unit tests much more simply.
In &lt;a href=&#34;https://gwilym.dev/2020/09/the-c-preprocessor-is-awesome-part-ii/&#34;&gt;part 2&lt;/a&gt;, we implemented a &lt;code&gt;LOCK&lt;/code&gt; macro for making it &amp;lsquo;easier&amp;rsquo; to use mutexes.
This will build on the ideas from both parts, so I recommend reading them first if you haven&amp;rsquo;t already.&lt;/p&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;As you are probably aware, C doesn&amp;rsquo;t really do error handling.
Depending on the function you&amp;rsquo;re calling can vastly change how you do the error handling.
For example, some will return a non-zero value on error.
Some will return a zero value on error.
Some will not return anything and instead set &lt;code&gt;errno&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;In this post we&amp;rsquo;re going to add yet another way to signal errors!
Exactly what everyone needs.&lt;/p&gt;
&lt;p&gt;This would have been much harder without some prior art which I started with.
The initial implementation came from &lt;a href=&#34;http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html&#34;&gt;here&lt;/a&gt; but this didn&amp;rsquo;t handle all the cases I wanted.
I wanted the ability to catch all exceptions as well, so I implemented a solution which allowed either CATCH_ALL or FINALLY or neither.
However, I was never happy with this solution so in the end I now have a mostly original implementation using hacks as never before seen (or at least I&amp;rsquo;ve never seen them before).&lt;/p&gt;
&lt;p&gt;Another limitation of the approach above is it didn&amp;rsquo;t really handle calling other functions that throw exceptions.
And what if you wanted to nest your exceptions a few layers deep?&lt;/p&gt;
&lt;h1 id=&#34;aim&#34;&gt;Aim&lt;/h1&gt;
&lt;p&gt;The end goal here is to have an ergonomic exception system.
Complete with &amp;lsquo;unwinding&amp;rsquo; and no need to check statuses of every method in a slightly different way.&lt;/p&gt;
&lt;p&gt;In the end, we&amp;rsquo;d like to be able to write code such as:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;TRY {
    doSomethingDangerous();
} CATCH(SOME_EXCEPTION) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// this will only run if doSomethingDangerous throws SOME_EXCEPTION
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;} FINALLY {
    &lt;span style=&#34;color:#75715e&#34;&gt;// this code will always run
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Exceptions can be thrown using the &lt;code&gt;THROW&lt;/code&gt; macro.&lt;/p&gt;
&lt;p&gt;We&amp;rsquo;d also like to be able to return within the &lt;code&gt;TRY&lt;/code&gt; and &lt;code&gt;CATCH&lt;/code&gt; blocks, while still having the content of the &lt;code&gt;FINALLY&lt;/code&gt; block run.
We&amp;rsquo;ll try to add support for that too.&lt;/p&gt;
&lt;p&gt;Unfortunately, I wrote this code in 2017 (currently almost 2021), so I&amp;rsquo;m not entirely sure how I came up with some of these hacks.
I am therefore documenting how it was done, and commenting on the implementation, rather than building this up from scratch.&lt;/p&gt;
&lt;p&gt;I recommend loading up &lt;code&gt;exceptions.h&lt;/code&gt; from the repo you can find &lt;a href=&#34;https://github.com/gwilymk/c-exceptions&#34;&gt;here&lt;/a&gt; and referring to it along with the article.&lt;/p&gt;
&lt;h1 id=&#34;the-general-approach&#34;&gt;The general approach&lt;/h1&gt;
&lt;p&gt;At it&amp;rsquo;s heart, the exception system provides a stack of &lt;code&gt;longjmp&lt;/code&gt; locations to jump to.
When we get to a &lt;code&gt;TRY&lt;/code&gt; block, we push onto the stack of exception handling places, and when &lt;code&gt;THROW&lt;/code&gt; gets called we &lt;code&gt;longjmp&lt;/code&gt; back to the most recent TRY block.
However, a few hacks were needed to get the ergonomics that we&amp;rsquo;re used to in other languages.&lt;/p&gt;
&lt;h1 id=&#34;the-try-macro&#34;&gt;The TRY macro&lt;/h1&gt;
&lt;p&gt;Let&amp;rsquo;s have a closer look at the &lt;code&gt;TRY&lt;/code&gt; block (this is the most complicated since it contains the code for all the other cases, but worth understanding how it works).&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define TRY                                                           \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    for (TryData__ tryData__ =                                        \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;             {setjmp(exceptionStack__[try__(__FILE__, __LINE__)]), 0, \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;              (void *)0, (void *)0};                                  \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;         tryData__.runFourTimes &amp;lt;= 3; tryData__.runFourTimes++)       \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        if (tryData__.runFourTimes == 0)                              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        {                                                             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            __label__ continueLabel;                                  \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            tryData__.continueLabel = &amp;amp;&amp;amp;continueLabel;                \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        continueLabel:;                                               \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        }                                                             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        else if (tryData__.runFourTimes == 3)                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        {                                                             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            if (!catchHandled__())                                    \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            {                                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;                RETHROW;                                              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            }                                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            endTry__();                                               \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            if (tryData__.returnTo)                                   \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            {                                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;                goto *tryData__.returnTo;                             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            }                                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        }                                                             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        else if (tryData__.runFourTimes == 1 &amp;amp;&amp;amp; tryData__.tryAttempt == 0)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;We have defined above an &lt;code&gt;exceptionStack__&lt;/code&gt; of &lt;code&gt;jmp_buf&lt;/code&gt;s.
This has some defined size, and externally we keep track of the top of the stack.
The &lt;code&gt;try__&lt;/code&gt; method returns the current index of the top (and prints and terminates if the call stack is exceeded).
It keeps track of the current file and line number to generate nice stack traces (thought they were just for java did you?).&lt;/p&gt;
&lt;p&gt;We now save the current location to the stack.
TryData is a simple struct defined as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;typedef&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt;
{
    &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; tryAttempt;
    &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; runFourTimes;
    &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;returnTo;
    &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;continueLabel;
} TryData__;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is used because we can only define a single variable in the initialiser of a &lt;code&gt;for&lt;/code&gt; loop.
So if I could, these would be defined as individual local variables.
For the rest of this article, I&amp;rsquo;ll be referring to these as local variables, because that&amp;rsquo;s what they really are.
Importantly, when we get &lt;code&gt;longjmp&lt;/code&gt;d back to where we &lt;code&gt;setjmp&lt;/code&gt;d, this is re-initialised to the 0 state.
Except &lt;code&gt;tryAttempt&lt;/code&gt; which set to 0 the first time, and to something non-zero by the &lt;code&gt;longjmp&lt;/code&gt; in the case of an exception.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;runFourTimes&lt;/code&gt; is incredibly well named.
This is our loop counter, and surprisingly, counts from 0 to 4 to cover all the cases (see the &lt;code&gt;LOCK&lt;/code&gt; macro implementation in part 2 of this series for a better explanation of why this is needed).&lt;/p&gt;
&lt;p&gt;There is some massive abuse of the gcc extension &lt;a href=&#34;https://gcc.gnu.org/onlinedocs/gcc-9.3.0/gcc/Local-Labels.html&#34;&gt;local labels&lt;/a&gt;.
We&amp;rsquo;re going to need to be able to jump out of the current block later, so just keep in mind the &lt;code&gt;runFourTimes == 0&lt;/code&gt; case.
Let&amp;rsquo;s skip the &lt;code&gt;runFourTimes == 3&lt;/code&gt; case for now.&lt;/p&gt;
&lt;p&gt;The final case, when &lt;code&gt;runFourTimes&lt;/code&gt; is 1 and &lt;code&gt;tryAttempt&lt;/code&gt; is 0.
Because &lt;code&gt;tryAttempt&lt;/code&gt; is 0, we haven&amp;rsquo;t been &lt;code&gt;longjmp&lt;/code&gt;d to, and because &lt;code&gt;runFourTimes&lt;/code&gt; is 1, the initialisation code has run.&lt;/p&gt;
&lt;p&gt;In this case, we finally execute the code inside the &lt;code&gt;TRY&lt;/code&gt; block.&lt;/p&gt;
&lt;h1 id=&#34;the-catch-macro&#34;&gt;The CATCH macro&lt;/h1&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CATCH(value)                                                                                    \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    else if (tryData__.runFourTimes == 1 &amp;amp;&amp;amp; tryData__.tryAttempt == (value) &amp;amp;&amp;amp; (catchHandled__() || 1))
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is reasonably simple.
We check that &lt;code&gt;runFourTimes&lt;/code&gt; is 1 (so just after the initialisation has run), that &lt;code&gt;tryAttempt&lt;/code&gt; is equal to the exception value we care about and then a slightly confusing call to &lt;code&gt;catchHandled__() || 1&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;catchHandled__()&lt;/code&gt; is a call that informs the exception system that we&amp;rsquo;ve actually handled this exception.
Since all the previous conditions passed, we want to call this state mutating function.
However, we can&amp;rsquo;t actually put that inside the &lt;code&gt;if&lt;/code&gt; statement, because then it wouldn&amp;rsquo;t be able to follow the &lt;code&gt;CATCH&lt;/code&gt; with some braces the way we want to.
So by relying on the short circuiting behaviour of &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, we only call &lt;code&gt;catchHandled__()&lt;/code&gt; if we have actually got a matching &lt;code&gt;CATCH&lt;/code&gt; statement.
Since &lt;code&gt;catchHandled__()&lt;/code&gt; can return both &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;0&lt;/code&gt;, and we would like to ensure that this statement is true regardless, we &lt;code&gt;||&lt;/code&gt; this with 1 to guarantee that it is truthy.&lt;/p&gt;
&lt;p&gt;So the content of this block will only run if we are actually catching the exception with the value we intend to, and if the currently throwing exception is handled, then the &lt;code&gt;catchHandled__()&lt;/code&gt; function is called.&lt;/p&gt;
&lt;h1 id=&#34;the-catch_all-macro&#34;&gt;The CATCH_ALL macro&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;CATCH_ALL&lt;/code&gt; macro is used in place of &lt;code&gt;catch (Throwable e)&lt;/code&gt; in Java.
It will catch any exception not previously caught in the chain (another reason to use &lt;code&gt;else if&lt;/code&gt; here, only one catch statement will ever execute).&lt;/p&gt;
&lt;p&gt;The macro is defined as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CATCH_ALL(e)                                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    else if (tryData__.runFourTimes == 1 &amp;amp;&amp;amp; tryData__.tryAttempt &amp;gt; 0 &amp;amp;&amp;amp;      \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;             (catchHandled__() ||                                            \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;              1)) for (volatile Exception e = {.type = tryData__.tryAttempt, \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;                                               .message = catchMessage__()}; \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;                       (e).type != -1; (e).type = -1)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This similarly contains some hacks to improve ergonomics and also compiler warnings.
We would like to be able to store the exception itself in some local variable, but this variable needs the correct scope.
It works very similarly to the &lt;code&gt;CATCH&lt;/code&gt; macros, except we fetch the construct the exception object.
Since we need a new variable declared, using a &lt;code&gt;for&lt;/code&gt; loop which only executes once does this perfectly.
We have something silly in the test and next parts to ensure the loop runs exactly once.&lt;/p&gt;
&lt;h1 id=&#34;the-finally-macro&#34;&gt;The FINALLY macro&lt;/h1&gt;
&lt;p&gt;The &lt;code&gt;FINALLY&lt;/code&gt; macro is by far the simplest.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define FINALLY else if (tryData__.runFourTimes == 2)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;runFourTimes == 2&lt;/code&gt; has been reserved for this case.
This will run after all &lt;code&gt;CATCH&lt;/code&gt; blocks and the &lt;code&gt;TRY&lt;/code&gt; block which happen on run 1.&lt;/p&gt;
&lt;h1 id=&#34;the-return-macro-and-the-return-of-runfourtimes--3&#34;&gt;The RETURN macro and the return of runFourTimes == 3&lt;/h1&gt;
&lt;p&gt;Let&amp;rsquo;s have a look at the &lt;code&gt;RETURN&lt;/code&gt; macro:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define RETURN(x)                                                              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    do {                                                                       \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        __label__ returnPoint;                                                 \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        __auto_type retValue = (x);                                            \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        tryData__.returnTo = &amp;amp;&amp;amp;returnPoint;                                    \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        goto *tryData__.continueLabel;                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    returnPoint:                                                               \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        return retValue;                                                       \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    } while (0)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Isn&amp;rsquo;t it nice to be back in the wonderful world of matching brackets, terminated statements and the like?
And also a &lt;code&gt;do {} while(0)&lt;/code&gt; loop again?&lt;/p&gt;
&lt;p&gt;This, once again uses local labels, except this time I&amp;rsquo;ll actually explain how they&amp;rsquo;re used.
Notice the &lt;code&gt;__auto_type&lt;/code&gt;.
This is equivalent to C++&#39;s &lt;code&gt;auto&lt;/code&gt; and Java and C#&amp;rsquo;s &lt;code&gt;var&lt;/code&gt;.
You can find &lt;code&gt;__auto_type&lt;/code&gt; mentioned &lt;a href=&#34;https://gcc.gnu.org/onlinedocs/gcc/Typeof.html&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But why is the &lt;code&gt;RETURN&lt;/code&gt; macro so complicated?&lt;/p&gt;
&lt;p&gt;We would like code in the &lt;code&gt;FINALLY&lt;/code&gt; block to run after the code declared in &lt;code&gt;x&lt;/code&gt; has run, but before we actually return.
Mainly this useful for code like:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;Resource someResource &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; createResource();
TRY {
   &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; value &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; doSomeCalculationWith(someResource);
   RETURN(value); 
} FINALLY {
    cleanupResource(someResource);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And in this case, not only will the &lt;code&gt;FINALLY&lt;/code&gt; block run if &lt;code&gt;doSomeCalculationWith&lt;/code&gt; throws an exception, but also just before we return value.&lt;/p&gt;
&lt;p&gt;Unfortunately, because of our limitations with just simple text substitution with C macros, we have to put the return statement where &lt;code&gt;RETURN&lt;/code&gt; is.&lt;/p&gt;
&lt;p&gt;So, in this case, we set &lt;code&gt;returnTo&lt;/code&gt; to the point in the code where we actually return, and then jump to the &lt;code&gt;continueLabel&lt;/code&gt;.
But where &lt;code&gt;continueLabel&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;Remember all the way back in the &lt;code&gt;TRY&lt;/code&gt; macro?&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (tryData__.runFourTimes &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;)                              \
{                                                             \
    __label__ continueLabel;                                  \
    tryData__.continueLabel &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt;continueLabel;                \
continueLabel:;                                               \
}                                                             \
&lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (tryData__.runFourTimes &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;)                         \
{                                                             \
    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (&lt;span style=&#34;color:#f92672&#34;&gt;!&lt;/span&gt;catchHandled__())                                    \
    {                                                         \
        RETHROW;                                              \
    }                                                         \
    endTry__();                                               \
    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (tryData__.returnTo)                                   \
    {                                                         \
        &lt;span style=&#34;color:#66d9ef&#34;&gt;goto&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;tryData__.returnTo;                             \
    }                                                         \
} 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Continue label will take us inside this if statement, where we can then immediately jump out of and do the next stage of the loop.
So now &lt;code&gt;runFourTimes&lt;/code&gt; will be 2 (and run any &lt;code&gt;FINALLY&lt;/code&gt; blocks) and then get to the case where &lt;code&gt;runFourTimes&lt;/code&gt; is 3.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;catchHandled__()&lt;/code&gt; will return 0 if it hasn&amp;rsquo;t already been called and an exception has been thrown.
In this case, we should bubble the exception up the stack.
&lt;code&gt;endTry__()&lt;/code&gt; will pop the top value off the stack without jumping to it, effectively announcing that we&amp;rsquo;re done in this &lt;code&gt;TRY&lt;/code&gt; block.
And then the magic of the local labels will jump us right back to the line above the &lt;code&gt;return&lt;/code&gt; statement and we can actually return.
If we don&amp;rsquo;t want to return, &lt;code&gt;returnTo&lt;/code&gt; will still be &lt;code&gt;NULL&lt;/code&gt; and no jump happens.&lt;/p&gt;
&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;This is a bit of a whirlwind tour of this code I wrote just over 3 years ago.
It was too good to share, but I appologise that this probably isn&amp;rsquo;t the most coherent account of how it works.&lt;/p&gt;
&lt;p&gt;If you have any questions or comments, feel free to contact me, and have a dig around the exception code and the enhanced test helper in the full repo.&lt;/p&gt;
</description>
     </item>
   
     <item>
       <title>The C preprocessor is awesome part II</title>
       <link>https://gwilym.dev/2020/09/the-c-preprocessor-is-awesome-part-ii/</link>
       <pubDate>Wed, 30 Sep 2020 17:14:44 +0100</pubDate>
       
       <guid>https://gwilym.dev/2020/09/the-c-preprocessor-is-awesome-part-ii/</guid>
       <description>&lt;p&gt;This is the second part of a 3 part series on why the C preprocessor is awesome and showing you some tricks you should never use.
This part is going to be the shortest, showing off a really simple trick that makes your macros look like actual keywords.
But this is a very important section for showing how to implement &lt;code&gt;TRY&lt;/code&gt;, &lt;code&gt;CATCH&lt;/code&gt; and &lt;code&gt;FINALLY&lt;/code&gt; in the final part.&lt;/p&gt;
&lt;p&gt;In &lt;a href=&#34;https://gwilym.dev/2020/08/the-c-preprocessor-is-awesome-part-i/&#34;&gt;part 1&lt;/a&gt; of this series, we implemented a very nice test harness that let us write unit tests much more simply.
This will build on the ideas from part 1 so I recommend reading that first if you haven&amp;rsquo;t already.&lt;/p&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;In this installment, I&amp;rsquo;m going to run through creating a &lt;code&gt;LOCK&lt;/code&gt; macro that works like &lt;code&gt;lock&lt;/code&gt; in C# or &lt;code&gt;synchronized&lt;/code&gt; in Java.
Have you ever looked at those languages and wished that C could do that too?
Well look no further.
And also wish you&amp;rsquo;d never asked.
By the end of this post, you&amp;rsquo;ll have a library that lets you use pthread mutexes as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;pthread_mutex_t myMutex;
pthread_mutex_init(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex, NULL);
&lt;span style=&#34;color:#75715e&#34;&gt;// ...
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;LOCK(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// do something
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and because we&amp;rsquo;re writing C and &lt;a href=&#34;https://nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/&#34;&gt;goto fail&lt;/a&gt; is a thing that will never happen, we would like to be able to use our macro as if it were an actual keyword:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;LOCK(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex) functionCall();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and have the mutex get locked and unlocked properly.&lt;/p&gt;
&lt;h1 id=&#34;what-do-we-want-out-of-this&#34;&gt;What do we want out of this?&lt;/h1&gt;
&lt;p&gt;As with part 1, it is useful to decide what we want our macro to expand to in the end.
In this case, we would want the above to expand to something like:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;pthread_mutex_t myMutex;
pthread_mutex_init(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex, NULL);
&lt;span style=&#34;color:#75715e&#34;&gt;// ...
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;pthread_mutex_lock(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex);
{
    &lt;span style=&#34;color:#75715e&#34;&gt;// do something
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
pthread_mutex_unlock(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;which leads us to a small problem.
In our &lt;code&gt;LOCK&lt;/code&gt; usage, the macro is at the beginning and we want code to run both at the start and at the end of our block.
Turns out, there is a really simple way we can get this to work.&lt;/p&gt;
&lt;h1 id=&#34;one-small-trick&#34;&gt;One small trick&lt;/h1&gt;
&lt;p&gt;Let&amp;rsquo;s try rewriting the above in a way that &lt;code&gt;{ // do something }&lt;/code&gt; is at the end.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;; i &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;3&lt;/span&gt;; i&lt;span style=&#34;color:#f92672&#34;&gt;++&lt;/span&gt;) {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (i &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;) {
        pthread_mutex_lock(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex);
    } &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;if&lt;/span&gt; (i &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;) {
        pthread_mutex_unlock(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex);
    } &lt;span style=&#34;color:#66d9ef&#34;&gt;else&lt;/span&gt; {
        &lt;span style=&#34;color:#75715e&#34;&gt;// do something
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and with a careful bit of bracket removal, we have our &lt;code&gt;LOCK&lt;/code&gt; macro:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define LOCK(mutex)                            \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    for (int i__ = 0; i__ &amp;lt; 3; i__++)          \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        if (i__ == 0) {                        \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            pthread_mutex_lock(mutex);        \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        } else if (i__ == 2) {                 \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;            pthread_mutex_unlock(mutex);      \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        } else
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It is almost too good to be true.
You can use this with or without brackets, so both of the above examples of how we&amp;rsquo;d want to use this macro work perfectly.
And both clang and gcc will completely optimise the loop away entirely!
But there are some issues you need to consider (and why you should never use this in production).&lt;/p&gt;
&lt;h1 id=&#34;issues&#34;&gt;Issues&lt;/h1&gt;
&lt;p&gt;Lets start with an unfortunate problem here.
You cannot &lt;code&gt;return&lt;/code&gt; inside the block following &lt;code&gt;LOCK&lt;/code&gt;.
That would cause &lt;code&gt;i = 2&lt;/code&gt; to never happen so the mutex is never unlocked, which is definitely a bad thing.&lt;/p&gt;
&lt;p&gt;Similarly, a &lt;code&gt;break&lt;/code&gt; statement or &lt;code&gt;continue&lt;/code&gt; statement inside the block following &lt;code&gt;LOCK&lt;/code&gt; would effect the for loop defined in the &lt;code&gt;LOCK&lt;/code&gt; macro rather than the outer statements.
For example, the following will execute both &lt;code&gt;// do something&lt;/code&gt; and &lt;code&gt;// do something else&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; leave &lt;code&gt;myMutex&lt;/code&gt; locked!&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;switch&lt;/span&gt; (i) {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;
        LOCK(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;myMutex) {
            &lt;span style=&#34;color:#75715e&#34;&gt;// do something
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;            &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;;
        }
    &lt;span style=&#34;color:#66d9ef&#34;&gt;case&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;:&lt;/span&gt;
        &lt;span style=&#34;color:#75715e&#34;&gt;// do something else
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;        &lt;span style=&#34;color:#66d9ef&#34;&gt;break&lt;/span&gt;;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Given that I recommend never using this in production anyway, it doesn&amp;rsquo;t matter too much.
But we should try to make these compiler errors!&lt;/p&gt;
&lt;h1 id=&#34;nested-functions&#34;&gt;Nested functions&lt;/h1&gt;
&lt;p&gt;Let&amp;rsquo;s dive off ISO C spec once again and delve into the wonderful world of GCC extensions.
We&amp;rsquo;re going to need the &lt;code&gt;CONCAT&lt;/code&gt; macro from last time, and we&amp;rsquo;ll use a feature called &lt;a href=&#34;https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html&#34;&gt;nested functions&lt;/a&gt;.
This lets you define a function inside another function.
And the nested function can access all the variable of the containing function up to the point of its definition.
So in some ways, it is almost indistinguishable from a block!
Except that you can&amp;rsquo;t &lt;code&gt;continue&lt;/code&gt; or &lt;code&gt;break&lt;/code&gt; on the outer scope.&lt;/p&gt;
&lt;p&gt;A useless example of how they can be used is as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; returnTwo(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;) { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;; }

    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; returnTwo();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One thing to note is that if you need to declare the nested function before its definition, you need to use &lt;code&gt;auto&lt;/code&gt;, so as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;auto&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; returnTwo(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;);
    
    &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; returnTwo();
    &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; returnTwo(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;) { &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;2&lt;/span&gt;; }

    &lt;span style=&#34;color:#66d9ef&#34;&gt;return&lt;/span&gt; i;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;rsquo;s all you need to know about nested functions before we show the final macro it all its glory!&lt;/p&gt;
&lt;h1 id=&#34;lock-with-nested-functions&#34;&gt;LOCK with nested functions&lt;/h1&gt;
&lt;p&gt;This unfortunately loses the ability to use the lock statement without braces.
But given my preferences to always include braces, this isn&amp;rsquo;t a massive downside to me.&lt;/p&gt;
&lt;p&gt;I think this is easiest to just show the final macro and explain it afterwards rather than build up to it&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define LOCK(mutex)                                         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    auto void CONCAT(lockNestedFunction__, __LINE__)(void); \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    pthread_mutex_lock(mutex);                              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    CONCAT(lockNestedFunction__, __LINE__)();               \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    pthread_mutex_unlock(mutex);                            \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    void CONCAT(lockNestedFunction__, __LINE__)(void)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This looks very different to before!&lt;/p&gt;
&lt;p&gt;Similar to the previous part, we need a unique name for each function, so using &lt;code&gt;__LINE__&lt;/code&gt; will at least mean you can use a &lt;code&gt;LOCK&lt;/code&gt; once per line.
We lock the mutex, call the newly defined nested function and unlock the mutex on the other side.
The final line starts the definition of the nested function, but leaves it up to the user of the macro to add the braces.&lt;/p&gt;
&lt;p&gt;This has a few advantages over the previous version of this macro with the &lt;code&gt;for&lt;/code&gt; loop.
Trying to compile the switch example above results in the compiler error of:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;error: a label can only be part of a statement and a declaration is not a statement
     auto void CONCAT(lockNestedFunction__, __LINE__)(void); \
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;hmm&amp;hellip; Not quite the error we expected.&lt;/p&gt;
&lt;p&gt;This is a fun side effect of labels in C.
A label must precede a &lt;em&gt;statement&lt;/em&gt; in C.
There is an easy fix for this.
An empty statement counts as a statement, so we can either fix our macro by starting it with a &lt;code&gt;;&lt;/code&gt; or put a &lt;code&gt;;&lt;/code&gt; after the &lt;code&gt;:&lt;/code&gt; in the case statement.&lt;/p&gt;
&lt;p&gt;After doing either of those (at this point, we&amp;rsquo;re so far from doing something sensible that either is okay), we get the compiler error:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;file.c: In function ‘lockNestedFunction__33’:
file.c:36:13: error: break statement not within loop or switch
             break;
             ^~~~~
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;which is what we wanted!&lt;/p&gt;
&lt;p&gt;Unfortunately this doesn&amp;rsquo;t fix the &lt;code&gt;return&lt;/code&gt; issue from before, but at least it won&amp;rsquo;t leave the mutex permanently locked.
And if you try and return something that isn&amp;rsquo;t &lt;code&gt;void&lt;/code&gt;, you&amp;rsquo;ll get a compiler error.&lt;/p&gt;
&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;NEVER DO THIS!&lt;/p&gt;
&lt;p&gt;The C test framework from part I could be considered vaguely sensible.
Especially if it convinced people to write unit tests in their C programs that they wouldn&amp;rsquo;t have before.
But this is so full of issues, for example which key words can&amp;rsquo;t be used within the block, that you&amp;rsquo;ll end up with something hard to debug in the future.
However, this was a fun look into some silly tricks you can use to get some code defined after the macro to run in the middle of the macro, which will be a very useful idea for part III of this series.&lt;/p&gt;
&lt;p&gt;Stay tuned!&lt;/p&gt;
</description>
     </item>
   
     <item>
       <title>The C preprocessor is awesome part I</title>
       <link>https://gwilym.dev/2020/08/the-c-preprocessor-is-awesome-part-i/</link>
       <pubDate>Sat, 15 Aug 2020 16:40:40 +0100</pubDate>
       
       <guid>https://gwilym.dev/2020/08/the-c-preprocessor-is-awesome-part-i/</guid>
       <description>&lt;p&gt;Well&amp;hellip; It probably isn&amp;rsquo;t.
But in this series of posts, I&amp;rsquo;m going to walk you through some terrible ideas which somehow vaguely work.
None of this is a good idea, but it is fun to push simple text preprocessing and the C programming language to it&amp;rsquo;s illogical limit.&lt;/p&gt;
&lt;p&gt;This series of posts was inspired by the crazy definition of &lt;code&gt;ARRAY_SIZE&lt;/code&gt; somewhere floating in the Linux kernel source code, which I first became aware of in &lt;a href=&#34;http://zubplot.blogspot.com/2015/01/gcc-is-wonderful-better-arraysize-macro.html&#34;&gt;a post by a friend of mine&lt;/a&gt;.
And of course I was also inspired by &lt;a href=&#34;https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html&#34;&gt;this classic&lt;/a&gt; which helped me come up with a few solutions to issues I was having while developing these.&lt;/p&gt;
&lt;p&gt;All these examples are compiled with &lt;code&gt;gcc&lt;/code&gt;.
Your success with other compilers may vary.&lt;/p&gt;
&lt;p&gt;I will assume familiarity with the C programming language and some comfort with the C preprocessor for the duration of this series.&lt;/p&gt;
&lt;h1 id=&#34;introduction&#34;&gt;Introduction&lt;/h1&gt;
&lt;p&gt;This series will come in three parts, with this being the first.
I believe I&amp;rsquo;m staying away from undefined behaviour at least for the first 2 parts.
The final part is probably very unsafe and I was only lucky that it worked for me, but it is fun nonetheless!
In this post, I&amp;rsquo;ll show you how to write a simple unit test framework allowing you to write your unit tests in C as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;TEST(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;my fist test&amp;#34;&lt;/span&gt;) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// do some unit testy stuff
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}

TEST(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;my second test&amp;#34;&lt;/span&gt;) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// more unit testy stuff
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In the second part, I&amp;rsquo;ll show you how to write pseudo C# / Java constructs like &lt;code&gt;lock&lt;/code&gt; or &lt;code&gt;using&lt;/code&gt; as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;LOCK(my_mutex) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// do something with threads
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}

USING(my_resource) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// this&amp;#39;ll be disposed after use
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And in the final part, I&amp;rsquo;ll show you how to (probably with loads of undefined behaviour) write a try-catch implementation:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;TRY {
    do_something_dangerous();
} CATCH(SOME_EXCEPTION) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// this code will only throw if do_something_dangerous THROWS(SOME_EXCEPTION)
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;} FINALLY {
    &lt;span style=&#34;color:#75715e&#34;&gt;// this code will always run
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Along the way, I hope you&amp;rsquo;ll learn a few tricks that you should never use in production, but are cool anyway.&lt;/p&gt;
&lt;h1 id=&#34;the-problem-i-was-trying-to-solve&#34;&gt;The problem I was trying to solve&lt;/h1&gt;
&lt;p&gt;Writing unit tests for your code is definitely a very good thing.
Automated regression testing is essential for any project.
But tests are often a pain to write.
And if they are a pain to write, you are tempted to write fewer of them.
This is clearly a bad thing.
So what we&amp;rsquo;re going to put together here is a really simple unit testing framework for your project entirely in GNU C99.
By the end of this, we&amp;rsquo;ll be able to write tests like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#include&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;lt;test_helper.h&amp;gt;&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;
TEST(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;my awesome test&amp;#34;&lt;/span&gt;) {
    assert(&lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;==&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;1&lt;/span&gt;);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and, although I won&amp;rsquo;t spend too long working on the output system for this, you&amp;rsquo;ll be able to run all these tests easily!&lt;/p&gt;
&lt;h1 id=&#34;an-approach-to-automation&#34;&gt;An approach to automation&lt;/h1&gt;
&lt;p&gt;You could try writing crazy macros straight off, but it is probably easier to start with what we want the eventual code to look like first.
So lets take the example above.
We&amp;rsquo;ll assume the existence of some &lt;code&gt;test_main.c&lt;/code&gt; file, which would contain code similar to this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;my_awesome_test&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;) {
    &lt;span style=&#34;color:#75715e&#34;&gt;// equivalent of assert(1 == 1) here
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;}

&lt;span style=&#34;color:#66d9ef&#34;&gt;typedef&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;void&lt;/span&gt; (&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;TestFunction)(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;);

TestFunction testFunctions[] &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; {
    my_awesome_test
};

&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; numTests &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;sizeof&lt;/span&gt;(testFunctions) &lt;span style=&#34;color:#f92672&#34;&gt;/&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;sizeof&lt;/span&gt;(testFunctions[&lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;]);
    &lt;span style=&#34;color:#66d9ef&#34;&gt;for&lt;/span&gt; (&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; i &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#ae81ff&#34;&gt;0&lt;/span&gt;; i &lt;span style=&#34;color:#f92672&#34;&gt;&amp;lt;&lt;/span&gt; numTests; i&lt;span style=&#34;color:#f92672&#34;&gt;++&lt;/span&gt;) {
        testFunctions[i]();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So I guess the test macro can be something like:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define TEST(name) \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void name(void)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;but this forces the test name to be a valid C identifier.
And we might want to use a nicer string name for the test name (just because we can).
So maybe we want to store the test along with its name in the &lt;code&gt;test_main.c&lt;/code&gt; file?&lt;/p&gt;
&lt;p&gt;Also, we want to store the name of the test along with the function anyway, since we would like to print the test names as we run them.
That way, if a test fails, we know exactly which one it is.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#include&lt;/span&gt; &lt;span style=&#34;color:#75715e&#34;&gt;&amp;lt;stdlib.h&amp;gt;&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;
&lt;span style=&#34;color:#66d9ef&#34;&gt;typedef&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;void&lt;/span&gt; (&lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;TestFunction)(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;);

&lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest {
    TestFunction test_function;
    &lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;char&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;test_name;
    &lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;next;
};

&lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;tests;

&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;register_test&lt;/span&gt;(TestFunction test_function, &lt;span style=&#34;color:#66d9ef&#34;&gt;const&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;char&lt;/span&gt; &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;test_name) {
    &lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;test &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; malloc(&lt;span style=&#34;color:#66d9ef&#34;&gt;sizeof&lt;/span&gt;(&lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest));
    test&lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt;test_function &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; test_function;
    test&lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt;test_name &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; test_name;
    test&lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt;next &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; tests;
    tests &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; test;
}

&lt;span style=&#34;color:#66d9ef&#34;&gt;int&lt;/span&gt; &lt;span style=&#34;color:#a6e22e&#34;&gt;main&lt;/span&gt;() {
    register_test(my_awesome_test, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;My awesome test&amp;#34;&lt;/span&gt;);

    &lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;current_test &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; tests;
    &lt;span style=&#34;color:#66d9ef&#34;&gt;while&lt;/span&gt; (current_test) {
        printf(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;Running test %s&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;, current_test&lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt;test_name);
        current_test&lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt;test_function();

        &lt;span style=&#34;color:#66d9ef&#34;&gt;struct&lt;/span&gt; NamedTest &lt;span style=&#34;color:#f92672&#34;&gt;*&lt;/span&gt;next_test &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; current_test&lt;span style=&#34;color:#f92672&#34;&gt;-&amp;gt;&lt;/span&gt;next;
        free(current_test);
        current_test &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; next_test;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is pretty much the final form of the test main file.
But using it does have one pretty major annoyance.
If you want add a new test, you have to manually register it.
So you end up repeating the test function name twice, and the string name is nowhere near the actual test definition.
We will attempt to fix both of these problems.&lt;/p&gt;
&lt;h1 id=&#34;gcc-to-the-rescue&#34;&gt;GCC to the rescue&lt;/h1&gt;
&lt;p&gt;If you have a look through the &lt;a href=&#34;https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes&#34;&gt;GCC function annotations&lt;/a&gt;, you can find some interesting ones.
The one we&amp;rsquo;re interested in here is &lt;code&gt;constructor&lt;/code&gt;.
This lets us state that a function should run before &lt;code&gt;main()&lt;/code&gt;.
If we can somehow register our test function as part of the &lt;code&gt;TEST&lt;/code&gt; declaration, and therefore allow us to not have to repeat ourselves.&lt;/p&gt;
&lt;p&gt;So we end up with a &lt;code&gt;TEST&lt;/code&gt; macro that looks something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define TEST(test_name, test_function_name)                    \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void test_function_name(void);                      \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    __attribute__((constructor))                               \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(register_, test_function_name)(void) {   \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        register_test(&amp;amp;test_function_name, test_name);         \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    }                                                          \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void test_function_name (void)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Ignore the &lt;code&gt;CONCAT&lt;/code&gt; definition for now.
It concatenates two literals, but its definition is non-obvious.
So I&amp;rsquo;ll cover that in the next section.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s a lot going on in the macro above, so it is probably easiest if we expand it&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;// This
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;TEST(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;My awesome test&amp;#34;&lt;/span&gt;, my_awesome_test) {
    &lt;span style=&#34;color:#75715e&#34;&gt;/* ... */&lt;/span&gt;
}

&lt;span style=&#34;color:#75715e&#34;&gt;// Turns into this:
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; my_awesome_test(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;);
__attribute__((constructor))
&lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; register_my_awesome_test(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;) {
    register_test(&lt;span style=&#34;color:#f92672&#34;&gt;&amp;amp;&lt;/span&gt;my_awesome_test, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;My awesome test&amp;#34;&lt;/span&gt;);
}
&lt;span style=&#34;color:#66d9ef&#34;&gt;static&lt;/span&gt; &lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt; my_awesome_test(&lt;span style=&#34;color:#66d9ef&#34;&gt;void&lt;/span&gt;) {
    &lt;span style=&#34;color:#75715e&#34;&gt;/* ... */&lt;/span&gt;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The forward declaration is necessary because we don&amp;rsquo;t want to have to put anything after the final &lt;code&gt;}&lt;/code&gt;.
So all the work needs to be done in the &lt;code&gt;TEST&lt;/code&gt; macro.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s slightly annoying that we still have to repeat ourselves here with both the test name and the function name.
An easy way around that is using the &lt;code&gt;__LINE__&lt;/code&gt; &amp;lsquo;macro&amp;rsquo;.
&lt;code&gt;__LINE__&lt;/code&gt; is defined to be the current line number.
Since we expect users to write tests on different lines, if we put the line number after the test name, we can make all the function names unique without forcing the user to repeat themselves.&lt;/p&gt;
&lt;p&gt;So we could instead write the macro as follows:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define TEST(test_name)                                              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(test_function_, __LINE__)(void);              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    __attribute__((constructor))                                     \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(register_test_, __LINE__)(void) {             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        register_test(&amp;amp;CONCAT(test_function_, __LINE__), test_name); \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    }                                                                \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(test_function_, __LINE__) (void)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id=&#34;aside-the-concat-macro&#34;&gt;Aside: The CONCAT macro&lt;/h1&gt;
&lt;p&gt;The C preprocessor provides the ability to concat two identifiers together.
This is done using the &lt;code&gt;##&lt;/code&gt; operator.
There are some annoyances with it however, which I&amp;rsquo;ll go into detail here.&lt;/p&gt;
&lt;p&gt;Firstly, we had to use the &lt;code&gt;CONCAT&lt;/code&gt; macro in the above example, because if we would have instead written &lt;code&gt;test_function_ ## __LINE__&lt;/code&gt; would end up expanding to &lt;code&gt;test_function___LINE__&lt;/code&gt;.
Which would obviously not be unique for each test in the same file, and also misses the line number which we wanted.&lt;/p&gt;
&lt;p&gt;This behaviour is well documented in this &lt;a href=&#34;https://en.wikipedia.org/wiki/C_preprocessor#Order_of_expansion&#34;&gt;wikipedia article&lt;/a&gt;.
But I&amp;rsquo;ll summarise here.&lt;/p&gt;
&lt;p&gt;The C preprocessor expands macros in the following stages&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Stringification operations are replaced, without performing expansion (this is the &lt;code&gt;#&lt;/code&gt; operator which isn&amp;rsquo;t covered here)&lt;/li&gt;
&lt;li&gt;Parameters are replaced with their replacement, without performing expansion&lt;/li&gt;
&lt;li&gt;Concatenation operations are replaced with the concatenated result without expanding the resulting token&lt;/li&gt;
&lt;li&gt;Tokens originating from parameters are expanded&lt;/li&gt;
&lt;li&gt;The resulting tokens are expanded&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;So if we look through the list above considering &lt;code&gt;test_function_ ## __LINE__&lt;/code&gt;, then we can see that concatenation in step 3 is performed before step 5 which would turn &lt;code&gt;__LINE__&lt;/code&gt; into the current line.&lt;/p&gt;
&lt;p&gt;One way we can get around that is to create a macro which we&amp;rsquo;ll call &lt;code&gt;CONCAT&lt;/code&gt;, which simply concatenates the arguments.
However, this doesn&amp;rsquo;t work:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CONCAT(a, b) a ## b
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Suppose you wrote &lt;code&gt;CONCAT(test_function_ ## __LINE__)&lt;/code&gt;.
Let&amp;rsquo;s follow the substitution pattern above.&lt;/p&gt;
&lt;p&gt;Initially, by rule 5, it expands to &lt;code&gt;test_function_ ## __LINE__&lt;/code&gt;.
Then, by rule 3, the two fields are concatenated.
Before rule 4 where the tokens originated from parameters are expanded.&lt;/p&gt;
&lt;p&gt;The way around this, and the way we define the &lt;code&gt;CONCAT&lt;/code&gt; macro above is as follows&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CONCAT2(a, b) a ## b
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CONCAT(a, b) CONCAT2(a, b)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This seems a bit silly, but it&amp;rsquo;ll now mean that the expansion behaves as you want it to.
Let&amp;rsquo;s try expanding &lt;code&gt;CONCAT(test_function_, __LINE__)&lt;/code&gt; again.
Firstly, by rule 5 this expands to &lt;code&gt;CONCAT2(test_function_, __LINE__)&lt;/code&gt;.
Then, by rule 4, the tokens originating from parameters are expanded, so you get &lt;code&gt;CONCAT2(test_function_, 3)&lt;/code&gt; (say this is on line 3).
This then gets expanded by rule 5 again to &lt;code&gt;test_function_ ## 3&lt;/code&gt; which finally by rule 3 is concatenated to the desired &lt;code&gt;test_function_3&lt;/code&gt;.&lt;/p&gt;
&lt;h1 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h1&gt;
&lt;p&gt;You can create a really simple test framework in C using the macro&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4&#34;&gt;&lt;code class=&#34;language-c&#34; data-lang=&#34;c&#34;&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CONCAT2(a, b) a ## b
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;#define CONCAT(a, b) CONCAT2(a, b)
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;&lt;/span&gt;
&lt;span style=&#34;color:#75715e&#34;&gt;#define TEST(test_name)                                              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(test_function_, __LINE__)(void);              \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    __attribute__((constructor))                                     \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(register_test_, __LINE__)(void) {             \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;        register_test(&amp;amp;CONCAT(test_function_, __LINE__), test_name); \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    }                                                                \
&lt;/span&gt;&lt;span style=&#34;color:#75715e&#34;&gt;    static void CONCAT(test_function_, __LINE__) (void)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;and some simple helper functions.&lt;/p&gt;
&lt;p&gt;It would be reasonably easy to extend the runner to, for example, only run tests who&amp;rsquo;s names matched command line arguments.
I hope this gives you some ideas as to how you can metaprogram in C in ways you never thought were possible.
And maybe persuades you to write a few unit tests for your project that doesn&amp;rsquo;t currently have any.&lt;/p&gt;
&lt;p&gt;Stay tuned for the next two parts of this series!&lt;/p&gt;
</description>
     </item>
   
 </channel>
</rss>
