Giter Club home page Giter Club logo

codestyliser's Introduction

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,

if(condition)
  statement;

will become

if(condition) {
  statement;
}

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:

  usage: codeStyliser.py [-h] (-f  file-name | -d  directory-name)

  optional arguments:
    -h, --help            show this help message and exit
    -f  file-name, --file  file-name
                          file name to format
    -d  directory-name, --directory  directory-name
                          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,

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

becomes:

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

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,

      for(...)
      statement;

becomes

      for(...) {
        statement;
  }

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,

      for(...)
    #endif

      for(...)

      // comment

    #endif

    for(...) statement;
    #endif

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

for(...)
foo;
#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:

// the following for(); isnt detected

/*comment*/ for(...)
              statement;

// this will result in an error, and this line will be ignored

for( /*comment*/ )
  statement;

// this for is also not detected
/*comment*/ for(...) //comment
          statement;

// Similiarly, this for is also not detected
/*comment*/ for() /*comment*/
              statement;

// Here, the code results in an UnboundLocalError

/*
foo */ for(...) statement; //bar

Copyright

Copyright (c) 2021, Pranjal Rastogi,

All rights reserved.

codestyliser's People

Contributors

pjrcodes avatar rishu2575 avatar

Stargazers

 avatar

Watchers

 avatar

codestyliser's Issues

new case for macro

cannot completely ignore the last line in case it as a complete statement ending with ; along with the keyword match

 #define TIMRE(a,b) \
-  if (a->tics < b->tis)  = -1; \
-  else if (a->tims > b->tics)  = 1; \
-  else if (a->tis < b->tis)  = -1; \
-  else if (a->tics > b->tim)  = 1; \
-  else if (a < b)  = -1; \
-  else if (a > b)  = 1; \
+  if (a->tis < b->ts) {   = -1; }  \
+  else if (a->tcs > b->tis) {  a = 1; }  \
+  else if (a->tis < b->tims) {  b = -1; }  \
+  else if (a->tis > b->tim {  rc= 1; }  \
+  else if (a < b) {  d = -1; }  \
+  else if (a > b) {  e= 1; }  \
   else f = 0;

Indent error

indent error

there is a problem:
Big PROBLEM:
indentation is used like this:

/* ap
sapo*/ for() {
a;
}

suggested fix: add comment(len) to index or somethign/

} is added at index of for() in line, and line has no comment in front of it!!!

when there is #else

 {
 #if H
-  if ((uns)
+  if ((un) {
     return NULL;
-  else
+  } else {
     return d;
-#else
+  } else
   FD *node;
   GSR (REE (d), fd, DE, node);
   return node ? &node->map : NULL;
 #endif

MACRO-error

} is added after \ in a macro
this must not happen

{ even this must be added before \

FUNCTION error

after function ended, we see some lines added, something to do with double line if

(very weird error)

Double-comment

if there are two comments on same line, one multiline end and one single line, desired result is not achieved,
example:
asd*/ for() as;//ads

macro check issue

check for the last character to be a \ . don't do a rfind.

         pos = strrchr(value, '"');
-        if (pos == NULL || pos[1] != '\0')
-            return NULL;
+        if (pos == NULL || pos[1] != '\0' { \
+            return NULL; \
+        }

if inside if not working

if ( w ){

  •        if ( f->w == 2 ) d = -d; // some comment
    
  •        if ( d < - f->wm ) d += f->wr;
    
  •        else if ( d > f->wm ) d -= f->wr;
    
  •        if ( d < - f->wm ) {  d += f->wr; } 
    
  •        } else if ( d > f->wm ) {  d -= f->wr; } 
       }
    

for that doesnt close

    for foo                 { \
      if foo                                   \
        {                                                               \
          if foo                                               { \
           bar;                                   \
          } else                                                          \
            {                                                           \
              foo;                                        \
             foo ( bar,            \
                      foo,                           \
                       x,                  \
                        y);       \
              bar;                                       \
            }                                                           \
        }                                           ```

ENDLINE-ERROR

If no end-line at end of file, } is added on same line as the last ;

like:
if()
ask;
becomes
if() {
ask; }

blank line after if statement and else

somefunc()
{

  • if (a)
  • if (b) {
    l;
  • }
  • else if (la)
  • else if (lan) {
    lan;

  • }

    return la;
    }

or a comment

```-    if (se >= 6)
+    if (se >= 6) {
         dz = (*(uint16_t *)&buf[4]);
+    }
         //dz = nt(*(uint16_t *)&buf[4]);
-    else dz = 0;
+    else {  dz = 0; } 
  

NESTED ELSE/ IF

                                                }
-                                           } else
-                                               if (*(void **)&o d)
+                                           } else {
+                                               if (*(void **)&o d) {
                                                    _os r(_g, _p zed, 0L);
-                                               else
+      }
+         } e
                                                    _o r(_g, _zer e, 0L);
                                        }
                                         OP(_oss_c)
                                    }
                                }

Bug related to Nested for

for(;;)
    for(;;)
        a;

will become

for(;;) {
    for(;;) {
}
        a;
    }

possible issue: The Wrong index is being used to check for semicolon and the index of the for(;;) itself is given.

this is also one of the problems behind #36 , CASE 3

HASH-IGNORE-WARN

Dont hash ignore if the semi-colon is found on the same line.

i.e. Dont hash ignore if the curly braces have to be put on the same line!!!

tab-warn

add warning if TAB is found.
Use SPACES instead
give error in RED COLOR

KEYWORD-ERROR

/* KEYWORD-ERROR */
// Here, for() is ignored, as { } of the switch() are detected as for's {}
for()
switch() {
a;
}

// Here, for() is not ignored, but instead {} are added invalidly
for()
switch()
{
a;
}

encoding error

opening files with ISO-81 something encoding fixes this

check the code I sent In email

else in macro

-       if ((elad))                \
+       if ((e(head))                 { \
                e_prev;               \
-       else                                                            \
+ } \
+       else                                                             { \
                ield.cqe_prev =                 \
                    (eev;                              \
-       if ((ed))                \
+ } \
+       if (())                 { \
                (hnext;              \
-       else                                                            \
+ } \
+       else                                                             { \
                (e_next =                 \
                    (e_next;                              \
+ } \
        QUE field)                     \
 } while (/*CO|D*/0)

Double Semicolon erro

If a single line has two semicolons after the keyword, there is a slight problem

The {} must be put after the first semicolon, not the second one.

currently they are put after the second semicolon

another example:

for(;;;;;;) al; a; # put after a;, must be put after al;

else elsec(); eooso(); # put after eooso();, must be put after elsec()

Preprocesser error

# error,
mustnt add { if next line after { curlyBraceIndexLine has a Preprocesser

for nested in another thing, and if-else nested in said for

if() {
    asd;
}
else
   for (a'adsd';)
       if  (*(void **)&od)
            _abr(_g, _pd, 0L);
       else
            _o or(_g, _z_, 0L);

Obviously gives error, No checking for these kind of nested blocks has been put till now in the code at all! These nested blocks have NEVER been tested till now!

Directly after error

Dont know why this ever occurs

if ()
    as;

// else

else if (
            )

    asd;

else

    as;

the first if() puts bracket normally and ignores the else-if in the line.

however,

if ()

    as;

// else

else if (
            )

    asd;

else

    as;

In this, the else-if is not ignored

SLOWNESS-error

TOO SLOW
potential fix:
doont change newlines for every file.

DUAL-COMMENT-ERROR

comments like the following:

/*aoipo*/ for();

for(
/*asoio*/
) 
a;

/*as*/ for() //asoo

/*soi*/ for() /*soio*/


also see
/*
as
asd*/ for() as;//ads


This will also fail

/* asd
asd
asd */ //asd

//asd will be detected as a normal line!

are valid code, but the for()'s are not detected as we are ignoring everything after a single line comment of any type.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.