Vim with C Conditional Parsing - c

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.

Related

Document #define without the preprocessor

On the Doxygen documentation I am writting, I have set ENABLE_PREPROCESSING = NO, because I want all of the code to be documented, independently of any #if statements.
The problems is that there is a #define that I need to be documented, but since I have disabled the preprocessor, nothing is generated for it (the other structures on that file are being documented just fine).
One option would be to enable the preprocessor and use the PREDEFINED option to set all the #if, but that is not realistically achievable in my case (too many of them).
Are there any other ways to achieve the intended result?
Thanks!
On the Doxygen documentation I am writting, I have set ENABLE_PREPROCESSING = NO, because I want all of the code to be documented, independently of any #if statements.
That's got some code smell to it. The interface presented by your code should be documented according to how it was built. It's pretty pointless to document features that could have been built but weren't, or to document alternative ways in which your features could have been built. Generally speaking, that means having Doxygen pre-process conditional-compilation directives.
And if you have conditional compilation that you intend for users of your library to trigger when they build their own programs, then I suggest taking a different approach: split your headers, so that your users select which headers to include instead of relying on conditional compilation to customize the content of a single header.
HOWEVER, if you must document all the code in every conditional-compilation branch in a single set of documentation, and you also want to document macros, then you could consider leaving preprocessing on, and filtering out the conditional compilation directives with an input filter. The latter part might be specified like this, for example:
INPUT_FILTER = "sed '/^[ ]*#[ ]*\(if\|el\|endif\)/ d'"
That does not account for line continuations, so as to keep it relatively simple, but even in that form it might be sufficient for your purposes. It could be augmented to handle line continuations if needed.

Expanding a C macro selectively [duplicate]

I was wondering if it is possible, and if yes how, can I run a C preprocessor, like cpp, on a
C++ source file and only process the conditional directives #if #endif etc. I would like other
directives to stay intact in the output file.
I'm doing some analysis on C# code and there is no C# pre-processor. My idea is to run a C preprocessor on C# file and process only conditionals. This way for example, the #region directive, will stay
in the file, but cpp appears to remove #region.
You might be looking for a tool like coan:
Coan is a software engineering tool for analysing preprocessor-based configurations of C or C++ source code. Its principal use is to simplify a body of source code by eliminating any parts that are redundant with respect to a specified configuration.
It's precisely designed to process #if and #ifdef preprocessor lines, and remove code accordingly, but it has a lot of other possible uses.
The linux unifdef command does what you want:
http://linux.die.net/man/1/unifdef
Even if you're not on linux, there is source available on the web.
BTW, this is a duplicate of another question: Way to omit undefined preprocessor branches by default with unifdef?
Oh, this is the same task as I had in the past. I've tried cpp unifdef and coan tools - all of them stumbled upon special C# preprocessor things like #region. In the end I've decided to make my own one:
https://github.com/gaDZella/undefine.
The tool has a pretty simple set of options compared to the mentioned cpp tools but it is fully compatible with C# preprocessor syntax.
You can use g++ -E option to stop after preprocessing stage
-E -> stop after the preprocessing stage.The output is in the form of preprocessed source code, which is sent to the standard output

In vim, how to remove all the C and C++ comments?

How to remove all C and C++ comments in vi?
//
/*
*/
You can't. Parsing C and C++ comments is not something that regular expressions can do. It might work for simple cases, but you never know if the result leaves you with a corrupted source file. E.g. what happens to this:
printf ("//\n");
The proper way is to use an external tool able to parse C. For example some compilers may have an option to strip comments.
Writing a comment stripper is also a basic exercise in lex and yacc programming.
See also this question: Remove comments from C/C++ code
With regular expressions, because of the complexity of C/C++ syntax, you will at best achieve a solution that is 90% correct. Better let the right tool (a compiler) do the job.
Fortunately, Vim integrates nicely with external tools. Based on this answer, you can do:
:%! gcc -fpreprocessed -dD -E "%" 2>/dev/null
Caveats:
requires gcc
it also slightly modifies the formatting (shrunk indent etc.)
Esc:%s/\/\///
Esc:%s/\/\*//
Esc:%s/\*\///
You can use a lexical analyzer like Flex directly applied to source codes. In its manual you can find "How can I match C-style comments?".
If you need an in-depth tutorial, you can find it here; under "Lexical Analysis" section you can find a pdf that introduce you to the tool and an archive with some practical examples, including "c99-comment-eater".

Lines of Code as a function of preprocessor definitions

A project I'm working on (in C) has a lot of sections of code that can be included or omitted based on compile-time configuration, using preprocessor directives.
I'm interested in estimating how many lines of code different configurations are adding to, or subtracting from, my core project. In other words, I'd like to write a few #define and #undef lines somewhere, and get a sense of what that does to the LOC count.
I'm not familiar with LOC counters, but from a cursory search, it doesn't seem like most of the easily-available tools do that. I'm assuming this isn't a difficult problem, but just a rather uncommon metric to measure.
Is there an existing tool that would do what I'm looking for, or some easy way to do it myself? Excluding comments and blank lines would be a major nice-to-have, too.
Run it through a preprocessor. For example, under gcc, use the option -E, I believe, to get just the kind of output you seem to want.
-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of preprocessed source code, which is sent
to the standard output.
You could get the preprocessor output from your compiler, but this might have other unwanted side effects, like expanding complex multi-line macros, and adding to the LOC count in ways you didn't expect.
Why not write your own simple pre-processor, and use your own include/exclude directives? You can make them trivially simple to parse, and then pipe your code through this pre-processor before sending it to a full featured LOC counter like CLOC.

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

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

Resources