GSoC 2019 Report: Adding NetBSD KNF to clang-format, Part 1

June 29, 2019 posted by Michał Górny

Prepared by Manikishan Ghantasala (shannu) as a part of Google Summer of Code 2019.

Greetings everyone, I am Manikishan an Undergraduate pursuing my Bachelors Degree in Computer Science from Amrita Vishwa Vidyapeetham, Amritapuri, Kerala, India. I have been very interested in working on the lower level development such as Operating Systems, Kernels, and Compilers. I have also worked on building a computer from scratch. The project is named From Nand To Tetris . It had helped me elevate my interest in the field of Operating Systems and to apply for this organization. I am very grateful to be a part of this program and also would like to thank the community and my mentors for granting me the opportunity and being supportive at all times.

Regarding the first evaluation, it has been quite interesting working on Add KNF (NetBSD style) in clang-format project. I love the NetBSD community and look forward to continue. It has helped me to learn a lot during this period. It has been challenging and amazing so far.

This is a blog post about the work I have done prior to the first evaluation period.

What is clang-format?

Clang-format is a set of tools that format code built upon LibFormat. It supports some of the coding styles such as LLVM, Google, Chromium, Mozilla, Webkit which can be chosen by -style=<StyleName>. There is another option of writing a config file named .clang-format with custom style rules. My project is to add NetBSD KNF support to clang-format.

With the added support to clang-format, it will be able to format the code according to the NetBSD Style when run with -style=NetBSD.

Getting familiar to LLVM Source

For the first week, I went on exploring LLVM source to find out implementations for the similar style rules I have listed in my proposal. I have managed to figure out a way for the same with the help of my supportive mentors. I have implemented two styles:

  1. BitFieldDeclarationsOnePerLine
  2. SortNetBSDIncludes
in the first phase.

About BitFieldDeclarationsOnePerLine

This rule lines up BitField declarations on consecutive lines with correct indentation.



          unsigned int bas :3,  hh : 4, jjj : 8;

          unsigned int baz:1,


          unsigned int bas:3,
          unsigned int baz:1,

Submitted a patch regarding this in the differential for review.

-> Patch:

There is a bug in the implementation where the indentation breaks when there is a comment in between the bitfields, which I kept aside for getting on to go with the next style and will fix it in the coming weeks.

About SortNetBSDIncludes

Clang-format has a native SortIncludes style, which sorts all the headers in alphabetical order where NetBSD headers follow a special order due to dependencies between each header. After discussing with my mentors and in the tech-toolchain we have come up with more precise order in which we should follow for headers:

  1. <sys/param.h>
  2. <sys/types.h>
  3. <sys/*> -- kernel headers
  4. <uvm/*> -- vm headers
  5. <net*/*> -- network protocol headers
  6. <*fs/*> -- filesystem headers
  7. <dev/*> -- device driver headers
  8. <protocols/.h>
  9. <machine/*>
  10. <[arch]/*>
  11. < /usr includes next>
  12. <paths.h>
  13. User include files

I have made more smarter version with Regex with necessary changes from the approach I had before which was a harcoded one, the patch for this will be up by this weekend.

Plan for the next phase

I was a bit behind the proposed schedule because understanding the LLVM source took more time than expected. I am confident that I will be speeding up my progress in the upcoming phase and complete as many styles possible. The final plan is to add support for NetBSD in clang-format which can be used by --style=NetBSD.


In the coming weeks, I will fix and add all the missing features to existing styles and new ones, and concentrate on optimizing and testing following all guidelines of NetBSD and make sure a formatted code doesn't cause a build fail.

Lastly, I would like to thank my wonderful mentors Michal and Christos for helping me to the process and guiding me whenever needed.



Post a Comment:
Comments are closed for this entry.