Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preprocessor-aware code navigation in IDE for C project

Background

I spend a lot of time navigating and editing convoluted scientific C codes. Usually they contain hundreds of optional features switched on and off with preprocessor directives. This makes it almost impossible to say at a glance whether the current block of code is activated in my current setup or not. The code itself does not help as every feature is smudged all over the place and everything is usually done using global variables.

Question

Is there an IDE that can handle preprocessor directives by folding/shading the inactive code?

I imagine one can maintain a project with a config of used flags and work with it not being bothered by inactive branches of logic.

like image 718
Andrii Magalich Avatar asked Jul 28 '16 11:07

Andrii Magalich


People also ask

Can GCC output C code after preprocessing?

Yes. Pass gcc the -E option. This will output preprocessed source code.

What are the two most common preprocessor directives in C?

There are 4 Main Types of Preprocessor Directives: Macros. File Inclusion. Conditional Compilation. Other directives.


1 Answers

Looking at similar question it seems like Eclipse CDT has exactly the functionality you need and the other question actually tells where you can set your ifdefs.

Emacs has something similar in form of hide-ifdef-mode.

But you can also try to use IDE-agnostic solution like running the code through unifdef and working with the result. If you just need to read the code, it's almost perfect solution. If you need to make some changes, things become a bit more complicated, but you can use git then to manage changes, like:

  • import whole code base into git
  • run through unifdef
  • commit the result, that's the basis for your patches
  • work with the code base in whatever IDE/editor you prefer, commiting changes as usual
  • if there is a need to produce patches for original code base, just checkout the original import commit and cherry-pick (or rebase) your patches from your branch (of course there is a chance for conflict, but it should be quite easy to resolve as you know your intended change for the code from the patch and you just need to adjust for ifdefs)
  • if there is a need to update code base, just start from original import, apply an update, commit that, run through unifdef, commit that and then rebase your changes on top of that

Of course, whether this approach will work or not depends on particular code base and what you're going to do with it, but it can be useful in some scenarios.

like image 116
Roman Khimov Avatar answered Sep 21 '22 08:09

Roman Khimov