Preprocessor-aware code navigation in IDE for C project

天大地大妈咪最大 提交于 2019-12-21 11:58:52

问题


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.


回答1:


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.




回答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




回答3:


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?



来源:https://stackoverflow.com/questions/38635017/preprocessor-aware-code-navigation-in-ide-for-c-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!