coding style checker for c (variable names, not indentation) - c

This question asks about a coding style checker, but the focus seems to be on indentation and brace placement. GNU indent deals with indentation (which isn't a problem in this code base, amazingly enough).
I'm working with a pile of code that is full of various naming schemes: camelCase, everythingruntogetherinlowercase, underscores_as_separators, SomeStructsEndWithT, etc.
I'd like to be able to pick a convention and at least have an automatic check that new changes are in line with the convention.
Is there a good tool for checking naming in C? Something like Python's pep8 checker tool, I don't want a beautifier.
Thanks.

It looks like Google's cpplint (a C++ style checker) can be hacked into submission for checking C like I want.
(I'm still interested in knowing if there are any better checkers out there.)

It is an unorthodox choice, but I'd go with cxref, if you are willing to put in half a days work. It is a cross referencer, comes with the source code, it has a clean parser and doesn't build a parse tree. Yet, with a few lines of code you can dump all the variables to examine them, or rewrite them to your favoured style (or if you are as lazy as I am instead of rewriting you could generate replace commands for emacs/sed). I only managed to build it for Mac.

This one has a number of answers already in this thread Coding style checker for C
from which Vera++ might be the most promising, since most of the other suggestions are formatters not validators. There's a webpage about running vera++ at
https://bitbucket.org/verateam/vera/wiki/Running.
There's a download from https://bitbucket.org/verateam/vera/downloads/vera++-1.1.1.tar.gz
Compiling presents a few issues:
sudo apt-get install libboost-dev tcl-dev
An include of tcl.h that should have been tcl/tcl.h
Need a full boost src tree, like that from http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz/download
The build command becomes something like: make BOOST_DIR=/home/fluffy/tmp/boost_1_53_0
vera++ needs a ~/.vera++/profiles/ but doesn't autocreate a default (it can be copied from the one in the distribution, however)
Finally, running it on a C++ file produced output like (with duplicate errors omitted for brevity):
../dllist.c:1: no copyright notice found
../dllist.c:4: horizontal tab used
../dllist.c:10: horizontal tab used
../dllist.c:10: closing curly bracket not in the same line or column
../dllist.c:29: horizontal tab used
../dllist.c:38: keyword 'if' not followed by a single space
../dllist.c:38: negation operator used in its short form
../dllist.c:40: horizontal tab used
../dllist.c:40: full block {} expected in the control structure
../dllist.c:42: horizontal tab used
../dllist.c:71: keyword 'if' not followed by a single space
../dllist.c:71: negation operator used in its short form
../dllist.c:72: horizontal tab used
../dllist.c:72: full block {} expected in the control structure
../dllist.c:73: horizontal tab used

Related

How to fold control statements with preprocessors in them?

Recently I've been working on some legacy code of some coworkers, using visual studio code I've been able to convert it so sane and readable C formatting. However, I'd like to fold some control statements as the ones written by my coworkers are at times larger than 100 lines.
In these lines I've added preprocessor control statements to enable and disable my changes.
In visual studio code I'm able to fold the code nicely, but it stops once it hits a preprocessor statement, at least the #ifdef and #ifndef.
As there is about 1 every 10 lines to fold one control statement I need to fold 10 times, kind of beats the idea behind it right?
I've tried looking for fold and collapse within the settings and some googling, but I can't find anything that might resolve my issues.
For example I have this snippet
if(true)
{
Some functions();
#ifdef DEBUG
Functions with debugging only();
#else
Functions without debugging only();
#endif
Some other functions();
}
I expect, whenever I fold if(true) that everything within this control statement is folded. This is what I'm used to and what will happen in vs2017 in Eclipse. This doesn't happen in VSCODE! Instead only Some functions(); is folded.
How can I get the VSCODE folding behavior to be similar to other IDE's?
It sounds like your editor is using indentation-level folding.
According to https://code.visualstudio.com/docs/editor/codebasics#_folding:
Folding ranges are by default evaluated based on the indentation of
lines. A folding range starts when a line has a smaller indent than
one or more following lines, and ends when there is a line with the
same or smaller indent.
Since the 1.22 release, folding ranges can also be computed based on
syntax tokens of the editor's configured language. The following
languages already provide syntax aware folding:
Markdown, HTML, CSS, LESS, SCSS and JSON
It's possible that there are plugins to support syntax folding for other languages. If not, as a workaround you can indent the preprocessor lines.
This is handled by C/C++ extension ms-vscode.cpptools.
You can force folding mark in options:
"editor.showFoldingControls": "always"
But unfortunately, there doesn't seem to be an option to fold and mask inactive code when opening source file like on Eclipse or on visual studio.

Vim with C Conditional Parsing

Does anyone know if Vim has any way to implement C Conditional Parsing like Source Insight?
comment out or gray out disabled branch?
remove cscope or ctag symbol that in disabled branch?
I've using cscope and ctag with vim to crawl C code. There are a lot of macros, and some code snippets have been disabled by macros. But Vim displays these snippets in the same color as the others. If the disabled code snippet could be shown greyed-out, that would be more convenient to read.
Also, when I search for a symbol or define with cscope, there are a lot of results, but many of them were already disabled by a macro. Is there any way to filter out disabled results?
With the ancient ifdef highlighting plugin, you can :Define or :Undefine preprocessor defines, and the corresponding blocks with be highlighted or not. Apart from that, I'll second #FDinoff's comment: Vim is a text editor, and there are no provisions for extending the tags handling and other lookups. Vim only has a basic understanding of a programming language via syntax scripts, not an entire compiler infrastructure like an IDE.

Why would C files end in /*[]*/

I am looking through some proprietary source code: sample programs in using a library.
The code is written in C and C++, using make for build system.
Each and every file ends in a commented out []: /*[]*/ for source files and #[]# for makefiles. What could be the reason for this?
The code is compiled for ARM with GCC, using extensions.
It is most likely a place holder for some sort of automatic expansion.
Typically something like macrodef (or one of the source code control filters) would expand such items to contain some relevant text. As typically only the comment-protected brackets would expand, the comments would remain in place, protecting the source code from actual expanded items at compilation time.
However, what you are currently looking at is probably the outer containing brackets with all of the internal expansions removed. This may have been done during a code migration from one source code control system to another. Although such an idea is highly speculative, it does not appear that they took the effort to migrate expansion items, instead of just removing them.
On one project I used to work, every C source file contained a comment at the very end:
/* End of file */
The reason for that was the gcc warning
Warning : No new line at end of file
So we had this comment (with a new line after it) to be sure people do not write after the comment :)

ansi c - auto clean up ugly code

I'm dealing with some code written in manner that is really, honestly, ugly. Is there a way to auto enforce proper tabs and remove all of the random multi-line white space entries?
Old Unix people have a solution for everything, including lazy programmers. Enter GNU indent.
You can use any editor but I would prefer using Vim, with vim you can actually give proper tabs by selecting all the code in visual mode and then '='.
You will also have to set sw=3 for only 3 space equal tab - preferred spacing in most of the code. Also set expandtabs this will ensure the code will look the same as it will replace '\t' with 3 space

How to get rid of GVim folding comments in your code?

There is someone in my team that swears by using some kind of GVim feature to do manually code folding.
As I'm using another editor and do not really need the folding feature, I think it only pollutes the source code with tags like:
/* {{{1 */
Convincing the person not to use this folding is not an option (got into some heated discussions before).
I'm not really a GVim guy, I'm wondering if there are not any other ways to do the folding without changing the team's code?
Maybe putting the folding directions in a separate file, or
Doing some kind of smart folding that takes the syntax of the programming language into account without changing the source code?
I would imagine he could just add the following to his .vimrc:
set foldmethod=syntax
Assuming he is using a version of VIM that supports that. :)
It only took a Google for "vim folding" to discover that Vim supports six fold methods.
syntax, indent and diff all mean that the user has little control over where the folding happens. That may or may not be a problem.
marker is a problem for you because you don't like the markers.
expr had a little of each of those problems, depending on the expression you create.
If your man wants to define his own fold points, and store them in an separate file, it seems like set foldmethod=manual combined with :mkview and :loadview would do the trick.
You should try it yourself. Once you start using foldmethod=marker there is just no going back. I am working on a project now where I can't use marker folding and it feels like washing clothes in a time before there were washing machines.

Resources