Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
We have a large amount of C/C++ code that's compiled for multiple targets, separated by #ifdefs. One of the targets is very different from the others and it's often important to know if the code you're editing is compiled for that target. Unfortunately the #ifdefs can be very spread out, so it's not always obvious which code is compiled for which targets.
Visual Studio's #ifdef highlighting can be helpful for visually identifying which code is compiled for which target, but changing the highlighting apparently requires modifications to the project file.
I'm interested in finding a tool or method that can help coders quickly recognize which targets are using each line of code. Even if it requires some sort of manual in-source annotation I think it could still be helpful. Best case it's automated, not tied to a specific editor or IDE, and it could be configured to warn in certain conditions (eg "you modified some code on Target X, be sure to test your code on that platform!").
If your code is getting that big that you can't tell what #ifdef your in then it's time to refactor your code. I would recommend that you refactor it into seperate cpp files per platform.
I noramlly only use #idef when the code is only one or two lines long, any longer and I normally refactor into it's only function or class into there own cpp file. That makes it simple to figure out where you are.
Check out Visual SlickEdit. The "Selective Display" option might be what you are looking for. I can't find any on-line documentation on it, but it will allow you to essentially apply a set of macro definitions to the code. So you can tell it to show you the code as the compiler will see it with a set of macros defined. This is a lot more than preprocessor output since it literally hides blocks of code that would be excluded based on the macro definitions.
This doesn't give you the ability to answer the question "Under what preprocessor conditions is this line of code included in compilation" though. The nice thing is that it applies the selective display filter to searches and printing.
I know for a fact that eclipse cdt does it. It has other nice features and some not-so-nice features for an IDE. Now, I code with vi, so I might be biased.
I don't know if there is a tool for this already, but I'd guess would be fairly easy to roll your own by using the precompiler. Precompile your file with a set of specific #defines and the output is what the compiler sees for that platform. I reckon this is not the same as highlighting the current file, but it can be automated and integrated into your IDE, push a button get a temp file with te current edited one under specific #define. Didn't try it myself, is just an idea.
PS. Yes, I had to read couple times your post to searching for where exactly is 'code coverage' involved lol.
Check XRefactory and Cscout.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
I have an application dependent on many libraries. I am building everything from sources on an ubuntu machine. I want to remove any function/class that is not required by an application. Is there any tool to help with that?
P.S. I want to remove source code from the library not just symbols from object files.
Standard strip utility was created exactly for this.
I have now researched this a bit in the context of my own project and decided this was worth a full answer rather than just a comment. This answer is based on Apple's toolchain on macOS (which uses clang, rather than gcc), but I think things work in much the same way for both.
The key to this is enabling 'link time optimization' when building your libraries and executable(s). The mechanics of this are actually very simple - just pass -flto to gcc and ld on the command line. This has two effects:
Code (functions / methods) in object files or archives that is never called is omitted from the final executable.
The linker performs the sort of optimisations that the compiler can perform (such as function inlining), but with knowledge that extends across compilation unit boundaries.
It won't help you if you are linking against a shared library, but it might help if that shared library links with other (static) libraries which contain code that the shared library never calls.
On the upside, this reduced the size of my final executable by about 5%, which I'm pleased about. YMMV.
On the downside, my object files roughly doubled in size and sometimes link times increased dramatically (by something like a factor of 100). Then, if I re-linked, it was much faster. This behaviour might be a peculiarity of Apple's toolchain however. Perhaps it is stashing away some build intermediates somewhere on the first link. In any case, if you only enable this option for release builds it should not be a major issue.
There are more details of the full set of gcc command line options that control optimisation here: https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html. Search that page for flto to narrow down your search.
And for a glimpse behind the scenes, see: https://gcc.gnu.org/onlinedocs/gccint/LTO-Overview.html
Edit:
A bit more information about link times. Apple's linker creates some huge files in a directory called LTOCache when you link. I've not seen these before today so these look to be the build intermediates that speed up linking second time around. As for my initial link being so slow, this may in part be due to the fact that, in my case, these are created on an SMB server. But then again, the CPU was maxed out so maybe not.
OK, now that I understand the OP's requirements better I have another answer for this that I think might better suit his needs. I think the way to tackle this is with a code coverage tool. After all, the problem is identifying what you can safely get rid of it. Actually stripping it out is easy.
My IDE (Visual Studio) has one of these built in but I think the OP is using gcc so the first port of call appears to be gcov. There are a number of commercial options, but they are expensive. There's also a potentially useful post here.
The other thing you need, of course, is a program that exercises all the parts of the library that you want to keep to give you a coverage report to work from, but it sounds like the OP already has that. A good IDE will also help as it makes navigating around the code so much easier. In Visual Studio, I find Jump to Definition and quick and easy 'bookmarking' to be key features.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Languages like Python, MATLAB, E-Lisp have this nice facility for doc-strings.
With this feature using just a few keystrokes in the terminal, you can fetch the documentations of the functions / module you have written and imported into your code.
Now is there any "technique" (library, Editor tricks , whatever to ) to get a similar facility in
C++ / C. Suppose I include the documentation of the function within the source file at
the head of the function,
then I would like to type a command like getinfo at the terminal. (something
like a man page)
I know such a 'man' facility exists for many C functions, but the documentation for these functions are written in separate text files from the source code. I would like the
documentation to be in-place
You can use something like doxygen. It has support for generating man pages, among other formats.
Visual Studio can/will generate popups containing information extracted from DocXml formatted comments. You have to compile with /doc, which will extract the XML from the comments to a .xdc file. Then you have to run xdcmake to compile the .xdc files into an xml file. You'd typically handle all this automatically in the build process, so you don't have to do much manually (except write the comments themselves, of course). The one thing to keep in mind, however, is that the code (at least a declaration) has to build before you get the popups.
I feel obliged to add that IMO, most such comments are generally pretty close to useless. If a corporate standard makes them unavoidable, so be it -- but if they're honestly providing any useful information, I'd consider that an indication of a problem ("Code smell", if you prefer that wording). In most cases, the combination of the name of the function (or whatever) and the names/types of the parameters should make the use of the function quite clear.
If you notate your code with comments in a syntax similar to Javadoc, you can generate a documentation for your code, in various different formats, using Doxygen. It can generate, among other things, man pages, but it seems that the preferred output format people use is HTML pages.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
is there such a thing as a (free) tool that would display a graph of all functions call in a given function?
For instance, if I use it on a complex function fun() I'm trying to understand, it would show me all the functions called by fun() in order, then I would have the possibility to see the same thing for the function called by fun(), and so on.
I'm looking for this for C++ code.
Does such a thing even exist?
edit : I am using VS 2008 if that helps, but I was thinking that such a software would work on the source files anyway
Doxygen can do this. See the CALL_GRAPH configuration option:
If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will generate a call dependency graph for every global function or class method. Note that enabling this option will significantly increase the time of a run. So in most cases it will be better to enable call graphs for selected functions only using the \callgraph command.
Yes, Eclipse CDT Call Hierarchy view provides exactly this. Moreover, this view has 2 options:
Show Callers
Show Callees
You are asking about second one, but I am prefer the first one in code analysis.
Intel(R) Single Event API is free open-source project that utilises GraphVis for call-graph visualisation. It takes a bit of labour to do manual or compiler-automated instrumentation, but beside statistics and call-graphs you will get the overtime views as well.
Yes such things exist. Google under the heading static code analysis. There are, for example, tools such as Understand, and it is extremely likely that your compiler can do this too for which I refer you to its documentation.
You can use callgrind, and it's GUI tool kcachegrind.
I don't know of any tool specially desgined for this. However, there are a few ways of doing it:
Using a IDE (QtCreator is free, Visual Studio Express might also be helpful, Eclipse CDT)
Using (ctags)[http://ctags.sourceforge.net/] and a able text editor.
Using callgrind and the several views it brings. Advantage: you get to see the functions that are really called. Disadvantage: only runs in unixes, and you have to profile.
Using Doxygen... this one is really fancy, as it generates an html "view" of your code, provided that you supply the correct options.
g++ and most compilers can do what you want. It is called profiling. Also there is the oprofile. A profiler gives you the call graph of an application after its execution. This is very useful to study code, you can also walk through the [debug] output as you look at the graph. A code analyzer, in contrast, will give you all possible call paths however, you will not be able to see the significant path easily.
VC++2008/2010 profiler generates among others the file *CallerCalleeSummary.csv, that contains this information. And this is the link to the article explaining how to use it with sample program: Profiling of C++ Applications in Visual Studio
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
The source code of our application is hundreds of thousands of line, thousands of files, and in places very old - the app was first written in 1995 or 1996. Over the past few years my team has greatly improved the quality of the source, but one issue remains that particularly bugs me: a lot of classes have a lot of methods fully defined in their header file.
I have no problem with methods declared inline in a header in some cases - a struct's constructor, a simple method where inlining measurably makes it faster (we have some math functions like this), etc. But the liberal use of inlined methods for no apparent reason is:
Messy
Makes it hard to find the implementation of a method (especially searching through a tree of classes for a virtual function, only to find one class had its version declared in the header...)
Probably increases the compiled code size
Probably causes issues for our linker, which is notoriously flaky for large codebases. To be fair, it has got much better in the past few years, but it's not perfect.
That last reason may now be causing problems for us and it's a good reason to go through the codebase and move most definitions to the source file.
Our codebase is huge. Is there an automated tool that can do (most of) this for us?
Notes:
We use Embarcadero RAD Studio 2010. In other words, the dialect of C++ includes VCL and other extensions, etc.
A few headers are standalone, but most are paired with a corresponding .cpp file, as you normally would. Apart from the extension the filename is the same, i.e., if there are methods defined in X.h, they can be moved to X.cpp. This also means the tool doesn't have to handle parsing the whole project - it could probably just parse individual pairs of .cpp/.h files, ignore the includes, etc, so long as it could reliably recognise a method with a body defined in a class declaration and move it.
You might try Lazy C++. I have not used it, but I believe it is a command line tool to do just what you want.
If the code is working then I would vote against any major automated rewrite.
Lots of work could be involved fixing it up.
Small iterative improvements over time is a better technique as you will be able to test each change in isolation (and add unit tests). Anyway your major complaint about not being able to find the code is not a real problem and is already solved. There are already tools that will index your code base so your editor will jump to the correct function definition without you having to search for it. Take a look at ctags or the equivalent for your editor.
Messy
Subjective
Makes it hard to find the implementation of a method (especially searching through a tree of classes for a virtual function, only to find one class had its version declared in the header...)
There are already tools available for finding the function. ctags will make a file that allows you to jump directly to the function from any decent editor (vim/emacs). I am sure your editor if nto one of these has the equivalent tool.
Probably increases the compiled code size
Unlikely. The compiler will choose to inline or not based on internal metrics not weather it is marked inline in the source.
Probably causes issues for our linker, which is notoriously flaky for large codebases. To be fair, it has got much better in the past few years, but it's not perfect.
Unlikely. If your linker is flakey then it is flakey it is not going to make much difference where the functions are defined as this has no bearing on if they are inlined anyway.
XE2 includes a new static analyzer. It might be worthwhile to give the new version of C++Builer's trial a spin.
You have a number of problems to solve:
How to regroup the source and header files ideally
How to automate the code modifications to carry this out
In both cases, you need a robust C++ parser with full name resolution to determine the dependencies accurately.
Then you need machinery that can reliably modify the C++ source code.
Our DMS Software Reengineering Toolkit with its C++ Front End could be used for this. DMS has been used for large-scale C++ code restructuring; see http://www.semdesigns.com/Company/Publications/ and track down the first paper "Case Study: Re-engineering C++ Component Models Via Automatic Program Transformation". (There's an older version of this paper you can download from there, but the published one is better). AFAIK, DMS is the only tool to have ever been applied to transforming C++ on large scale.
This SO discussion on reorganizing code addresses the problem of grouping directly.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Suppose you have got a bunch of files written in C, C++ or Fortran, by different authors, with different opinions on formatting, how to comment, and so on. I think many people know situations like these.
Are there any free tools for ends like:
uniform format of code (indent, etc.)
create standard comment bodies
rename variables
?
Have a look at
AStyle. It's a command line based formatter/beautifier. It doesn't handle Fortran though it works with C, C++, C# and Java
You can have a look at the indent (unix) command. It doesn't do everything you are asking for , but that's a good start I think
For Fortran there is plusFORT, which can do much more than what you ask for, such as reorganizing code and translating from FORTRAN 77 to Fortran 90. See http://www.polyhedron.com/pf-plusfort0html and http://www.polyhedron.com/pflinux0html
The CDT Plugin for Eclipse has great formatting and refactoring tools for C/C++.
The formatter can be customized to fit almost all needs.
Also the refactoring tools are quite powerful and renaming variables, classes etc. is an easy and safe task with them. (They use the indexer/parser to recognize scope of variables, so its not a simple search and replace. Matching patterns within comments can be changed automatically, too).
However, as far as I know there is no batch processing possible.
Edit: Another - obvious - drawback is, that you have to create a project to make the indexer (and thus the refactoring tools) work. So at least you have to add all include paths and important compiler defines to project settings.
I never tried, but the indexer should work fine without a real compiler available, but it may be necessary to make the project to use the "internal builder", otherwise you cannot set include paths. (I'm unsure about this, because I use the internal builder with gcc in my projects - this works fine.)
I've used Uncrustify with UniversalIndentGui for formatting C++ code. It works pretty well. Uncrustify offers many customization options and UniversalIndentGui "offers a live preview for setting the parameters of nearly any indenter. You change the value of a parameter and directly see how your reformatted code will look like."