7

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Andrii Magalich
  • 132
  • 2
  • 8
  • 2
    I think asking for suggestions is off topic. However, what platform are you on? Visual Studio will grey out unused blocks in` ifdef`s. – doctorlove Jul 28 '16 at 11:29
  • I'm on Mac. Will it work if active directives are given at the compile time? – Andrii Magalich Jul 28 '16 at 11:32
  • QT Creator darkens ifdef blocks if they're not relevant – flau Aug 01 '16 at 14:23
  • @iwin where does it get information about the relevant blocks? Is it able to extract it, say, from a Makefile? – Andrii Magalich Aug 01 '16 at 14:24
  • 1
    I'm not sure if it can extract them from a makefile, but I think you can set some defines in the run options and it'll use those for the highlighting if that's any use – flau Aug 01 '16 at 14:26
  • 1
    @AndriiMagalich Personally I often add an `errorerror;` line or something like that to induce a compile error. If there is no compile error, the code is not active. – sashoalm Aug 08 '16 at 05:11
  • @sashoalm that is a robust way to handle this, but unfortunately, my current code compiles for a couple of minutes – Andrii Magalich Aug 08 '16 at 10:32
  • 1
    @sashoalm There is a preprocessor directive that is just for this sort of situation. `#error` will cause the preprocessor to fail, and print an optional message. So you could have some directives that detect invalid combinations of features, and then fail. For example: `#if defined(ACCEL_OPENCL) && defined(ACCEL_CUDA) #error Cannot build with both OpenCL and CUDA acceleration enabled #endif`. – gavinb Aug 08 '16 at 10:42
  • @AndriiMagalich Do you have a way to compile a single file? Some IDEs offer it but AFAIK it's not possible with `make`. – sashoalm Aug 08 '16 at 10:42
  • 1
    @gavinb I know about it and use it personally instead of the way I suggested, but I didn't want to confuse OP in case he didn't know it and have to explain. – sashoalm Aug 08 '16 at 10:43
  • @sashoalm Never tried, but I think it won't be possible — this is a low-level massively distributed code, it is very interconnected – Andrii Magalich Aug 08 '16 at 10:48

3 Answers3

8

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.

Community
  • 1
  • 1
Roman Khimov
  • 4,807
  • 1
  • 27
  • 35
2

I use Vim with several plugins as my IDE of choice (thank you Steve Francia). It is likely this is not your cup of tea, but in case it is, I found a couple suggestions to accomplish this.

Autofold #ifdef..#endif in Vim via .vimrc

C Preprocessor Fold in Vim

Community
  • 1
  • 1
Jeremy Thien
  • 105
  • 1
  • 7
1

I haven't heard about this feature in any IDE. BUT you can still use after preprocessed code to work with. Can gcc output C code after preprocessing?

Community
  • 1
  • 1
Alex Baranowski
  • 1,014
  • 13
  • 22