项目作者: PjrCodes

项目描述 :
The Code Styliser utility | A way to automatically fix MISRA-C-2012 Rule 15.6. | Adds curly braces around single-line compound statements in C, H files.
高级语言: Python
项目地址: git://github.com/PjrCodes/CodeStyliser.git
创建时间: 2020-05-14T06:33:59Z
项目社区:https://github.com/PjrCodes/CodeStyliser

开源协议:BSD 2-Clause "Simplified" License

下载


CodeStyliser version 0.11-Beta

DISCLAIMER: The code is very messy in it’s current state. I plan to make it cleaner and more efficient in the future.

The Code Styliser utility fixes MISRA-C-2012 Rule 15.6 warnings automatically, by adding curly braces ({}) in C-source (.c) files wherever needed.

It adds braces after single line if(), for(), while(), else and else if() statements.

For example,

  1. if(condition)
  2. statement;

will become

  1. if(condition) {
  2. statement;
  3. }

It is my suggestion to check all the code once this utility is finished running, as there is no guarantee that this software will work out of the box without any issues.

NOTE: This utility will replace all \r\n line endings with \n.

Usage

This utility is written in Python 3.7.7, 64-Bit. However, you dont need to have python installed on your machine. If you cannot use the binary, you must have python installed. There are no external dependencies required. If running with python, replace ./codeStyliser with python codeStyliser.py. You must download the whole repository.

NOTE: The binary file attached works only for 64-Bit Linux systems. It WILL NOT run for any other operating system.

The binary has been tested on Ubuntu 16.04 LTS

The usage is as follows:

  • Download the binary file attached into the directory you want to run the utility from.
  • Now, just run the file in the following manner

    • If you want to run it for a file:

      $ ./codeStyliser -f file-name

    • Or, if you want to run it for a directory:

      $ ./codeStyliser -d directory-name

  • The complete usage of this binary is as follows:

  1. usage: codeStyliser.py [-h] (-f file-name | -d directory-name)
  2. optional arguments:
  3. -h, --help show this help message and exit
  4. -f file-name, --file file-name
  5. file name to format
  6. -d directory-name, --directory directory-name
  7. directory to format files under

Known issues

Encoding issue

The utility only changes UTF-8 encoded files, it will ignore all other file encodings.

Non-keyword block of code

If a block of code, that is not a keyword( for(), if(), while(), do, etc.) is in the code after a keyword, which doesn’t have curly braces ({}), the closing curly brace (}) is added in the wrong position by the utility.

For example,

  1. for (...)
  2. NOT_A_KEYWORD () {
  3. statement 1;
  4. statement 2;
  5. ...
  6. }

becomes:

  1. for (...) {
  2. NOT_A_KEYWORD () {
  3. statement 1;
  4. }
  5. statement 2;
  6. ...
  7. }

This doesnt happen if the block of code starts on a keyword

Tab indentation error

If the characters used to indent keywords, are tabs (\t), then the output code will not be indented properly.

The indented characters must always use spaces to indent.

For example,

  1. for(...)
  2. statement;

becomes

  1. for(...) {
  2. statement;
  3. }

if the characters used to indent the for(...) are tabs, and not spaces.

Hash ignore

The code will ignore all keywords that have a # on the next immediate valid line.

A valid line is a line which has no comment and isnt a blank line.

For example,

  1. for(...)
  2. #endif
  3. for(...)
  4. // comment
  5. #endif
  6. for(...) statement;
  7. #endif

Here, the for(...) is ignored, i.e. NO braces will be added.

  1. for(...)
  2. foo;
  3. #endif

In the above case, the for(...) is not ignored, i.e. braces will be added.

Issues with Comments

Comments like the following will cause issues, as explained in this block:

  1. // the following for(); isnt detected
  2. /*comment*/ for(...)
  3. statement;
  4. // this will result in an error, and this line will be ignored
  5. for( /*comment*/ )
  6. statement;
  7. // this for is also not detected
  8. /*comment*/ for(...) //comment
  9. statement;
  10. // Similiarly, this for is also not detected
  11. /*comment*/ for() /*comment*/
  12. statement;
  13. // Here, the code results in an UnboundLocalError
  14. /*
  15. foo */ for(...) statement; //bar

Copyright (c) 2021, Pranjal Rastogi,

All rights reserved.