The C code that I am working on has lots of macros everywhere. I need to see expanded states of the macros to decrease my coding time.
1) What is the best way of code development considering codes with many macros? For example, there is variable which is probably declared somewhere in the macros and I have to search all macros to find its type.
2) Although using ctags in VIM helps me to jump to the macro definition, is there a VIM plugin that recursively expands and shows C/C++ macros in a C/C++ file while I am editing the file?
P.S. There is a similar question here but its answers are not related with the question:
vim plugin for exploring macros written by c/c++
P.P.S. When I use the solution at Seeing expanded C macros, I got a huge file that contains all other include files and so on. Also the whole file cannot be preprocessed because I have to change the cmake build system.
Related
I have a cmake project. I want to do the following easily
search the declaration, definition and references of any variable, function, etc. under the cursor, which may be declared in an external header file whose path is added using INCLUDE_DIRECTORIES in CMakeLists.txt
rename a variable, function, etc. that is declared in the project
How can I set this up?
You can try to use vim plugin cmake4vim in order to integrate CMake to Vim.
This plugin helps to work with cmake targets and allows to generate compilation database file (compile_commands.json). A lot of plugins use this file for code completion, jump to definition and etc. (for example YCM)
Also you can use vim lsp plugins (for example vim-lsp) these plugins use language servers for code completion, refactoring and another good features.
But CMake project integration (cmake cache generation, project compilation, etc.) and search the declaration, definition and etc are different tasks. And different plugins and tools solve these tasks.
You can tell Vim where to look for includes by adding entries to the path option. I don't have enough experience with Cmake to know how to pull paths from CMakeLists.txt, though.
See :help 'path'.
Assuming a properly set path, it is possible to use the built-in :dsearch and related commands to search for definitions across includes.
The define option has a prescriptive name but it could be used to find any specific pattern so you could alter it to match declarations, too, or really anything.
See :help include-search and :help 'define'.
Vim has no built-in concept of "reference". :isearch and friends should work for that but they will probably be too noisy.
Renaming is usually done with something like:
:grep foo paths
:cwindow
:cdo s/foo/bar/gc
YouCompleteMe will help you. It uses compilation_database.json, witch can be generated by cmake.
This plugin also provides autocompetion for many languages.
I use functions in vim and assign them to a hotkey.
https://developer.ibm.com/tutorials/l-vim-script-2/
it gives you more an IDE feel. but at the end of the day you get a bit more control.
I would like to add a custom preprocessor for .cpp files in my Visual C++ MSBuild project.
Currently I am using a custom buildstep to find all .cpp files and rewrite them (destroying the original content) before they a being compiled. This is no longer acceptable as the preprocessor should make changes "invisible" to the developer (converting lines with a scripting language to real C++ code).
The preprocessing should be done "On the fly". Dependencies should not be affected at all. The custom preprocessor is able to create #line preprocessor directive so compiler errors can be redirected to the original file.
It seems to me that build events and custom build steps are no viable way to do this so.
I tried to replace to compiler binary with my own which calls the custom preprocessor and feeds the output into the original compiler, but this was more of a "proof of concept" and is surely not a production-ready solution.
Q1: Where do I have to look for what I am trying to achieve?
Q2: and is there a possibility to do this in a solution without changing the Visual C++ installation (The custom preprocessor is also part of the solution and compiled before any of the other projects)
I know that you can provide a command line or add switches in your project properties but can you do that from source file? I need to set certain switches for certain source files without going into project properties and manually doing it everytime. So maybe you can do it with preprocessors from C++ source file?
There're some #pragma's that can, for example, alter code generation/optimization for a block of code, but I think that's pretty limited thing.
First: Compilation/linking do not execute your C++ code. So you can't write a C++ piece'o code and make decision (to change some project settings of THIS project) based on some C++ conditions (like if/else/...).
#pragma and #defines are pre-processor directives as you already know. Though compiler understands #pragmas, programming capability is limited and usually compiler specific.
A common use of these is to make the code platform independent.
To answer your original question "how do I modify the project properties progrmatically (not manuall)?": You can write "code" using #pragmas & #defines (even conditional ones) in your code to control various settings. Usually support is compiler-vendor specific, so your code will most probably not be portable (across compilers) unless you're careful.
On my GCC, I always found c++config.h a good example of various cpp directive. On my windows PC it's installed in X:\MinGW\lib\gcc\mingw32\4.5.2\include\c++\mingw32\bits\c++config.h
To find out supported cpp directives, RTM of your compiler.
The other option obviously is to write shell/perl/... script to make modifications to the project settings/Makefiles. I recommend that compared to getting yourself tangled in cpp directives. :)
Of course this option would change the settings before compilation and not during. But if that's not a prob for you, go with this rather than cpp directives.
As has been mentioned, #pragma statements are very vendor specific. For Visual Studio 2005, take a look here for the supported #pragma statements: Pragma Directives
I would also with thekashyap regarding writing a script to modify the project settings for each file for the following reasons:
In the event that you have to add or change a setting, there is a *single* place to modify.
Because there is only one place to look, future maintainers know where to find special settings for a given file. They won't be surprised by something set manually that is hidden deep within the project settings dialogs.
I'm a vi user for coding in Python. I love it: powerful, lowlevel, minimal gui without useless buttons, etc.
Now I'm approaching C++ with an opensource project that—as usual happened in opensource project—has poor documentation and sometimes (well, a lot of times) I have to open declaration and implementation and see what a particular function does.
In Eclipse this is really easy to do due to the Open Declaration (shortcut F3) feature. But I don't like the CDT plugin for Eclipse. I have a makefile project and it indicates some error in importing external .cpp files inside this opensource framework that requires a custom structure for directory and build path (and it's annoying to create a project every time in Eclipse for this custom structure).
I wish to have the same feature in vi. Do you know of anything?
Vim doesn't offer a feature like that by itself. A rudimentary way would be to use gd in command mode. If you want more sophisticated support, take a look at ctags and cscope. Those tools offer which you ask for and integrate nicely into Vim. A quick google search brought me to this link, which explains how to use cscope within Vim. Using ctags quite as easy: generate the ctags database/tags file and use Ctrl+] on an identifier to jump to its declaration. See :help ctags for more information.
One option: Use QT Creator with FakeVim mode enabled. You can follow declarations with F2.
I am analyzing a legacy code which heavily using macro, I am lost in understanding how macro are expanding in code.
Could any one suggest me some tool or technique so that I can study actual code generated from macro expansion.
Platform : Windows XP
Language : C++
Compiler : VC6
To run the GCC as a pre-processor only do:
gcc -E source-file.cc > processed-source-file.cc
It also do all the #includes that you may or may not want.
With visual studio you can use the Generate Preprocessed File option.
In the properties for your project select
C/C++/Preprocessor/
In that tab there is an option to:
generate a preprocessed file.
Select Yes, with numbers.
Run the pre-processor (cpp) on the source file.
Since you're using Visual C this doesn't help you, but it might be helpful to others to mention:
Netbeans 6.7 is able to display the macro expanded version of C/C++ code in a separate window during editing.