Frequently, a class/struct is dependent on other classes/structs, which in turn are dependent on other classes/structs. In big projects, this can easily lead to confusion. Is there a tool (preferably a simple text-only command-line tool) that can show how classes or structs are nested/aggregated/related? Support for inheritance is not needed...
I think Doxygen is the best solution, when combinated with graphviz's dot.
http://www.graphviz.org/
http://www.doxygen.org/
The configuration of Doxygen is carried out by a configuration text file, and after that, you can re-launch it as soon as any file has changed, even automatically from another tool.
Using Dot, you'll get diagrams of class relations.
Doxygen does also parse the files and colors its syntax, it is a matter of checking the configuration file for five minutes.
$ doxygen -g app.dox
[...edit app.dox]
$ doxygen app.dox
GDB command - ptype typename
Print inheritance relationships as well as other information for type typename.
Related
Examples found on the web for clang tools are always run on toy examples, which are usually all really trivial C programs.
I am building a tool which performs source-to-source transformations on C++ code, which is obviously a very, very challenging task, but clang is up to this task.
The issue I am facing now is that the AST that clang generates for any C++ code that utilizes the STL is enormous. For example I have some C++ code for which clang++ -ast-dump ... | wc -l is 67,018 lines of horrifying AST gobbledygook!
99% of this is standard library stuff, which I aim to ignore in my source-to-source metaprogramming task. So, to achieve this I want to simply filter out files. Suppose I want to look at only the class definitions in the headers of the project that I'm analyzing (and ignore all standard library headers's stuff), I will need to just figure out which header each of my CXXRecordDecl's came from!
Can this be done?
Edit: Hopefully this is a way to go about it. Trying this out now... The important bit is that it has to tell me the header that the decls came out of, not the cpp file corresponding to the translation unit.
In my experience so far, the "source" of some given AST node is best retrieved by using Locations. For example every node at least has a start location, and when you print this out it will contain the header file path.
Then it's possible to use this path to decide whether it is a system library or part of your application code that you still are interested in examining.
One route I'm looking at is to narrow matches with things like hasName() (as found here. For example:
recordDecl(hasName("MyBaseClass")) // etc.
However your comment above using -ast-dump is something I tried as well to get a lay of the land on my own CLang tool. I found this post to be extremely helpful. Armed with their suggestion, I used clang-check to filter to a specific class name and fed it my top-level CPP file. The output was a much more manageable few hundred lines representing the class declarations and definitions of interest.
Is there any plugin for VIM that I can use to index an C++ project code base?
I would apreciate functionalities like being capable of specifing a class and and may be a method and see what file/line the method/class is defined.
Regarding code navigation (and completion),
I'd take a look at clang_indexer (and clang_complete) --
ctag understanding of C++ code is quite bad, but universal-ctags has greatly improved the situation ; cscope understanding of C++ is non-existent.
Regarding plugins for C++ coding,
I have a suite for C and C++ programming. It is mainly oriented toward C++ programming, however a few, and unique features can be used in C as well:
context sensitive snippets (they require other plugins I'm maintaining);
a way to jump to a function definition from its declaration (or create it on the fly if it doesn't exists yet) (it used to requires the plugin alternate, which is a must have, however that I've forked it for my own needs) -> :GOTOIMPL;
a little tool that lists functions with a declaration and no definition, or functions with a definition and no declaration (NB: I haven't used it against C static function yet) (it requires ctags).
:Override that searches for overridable functions
:DOX that analyses C++ function signature to generate the appropriate (customizable) doxygen comment (with \param, \throw, ...)
a mapping to include the header file where the symbol under the cursor is defined* (which requires an up-to-date ctags base)
and few other things
Otherwise, I also use:
plugins like project/local_vimrc in order to have project specific settings ;
searchInRuntime to open/jump to files without the need to browse the directories of the current project ;
a refactoring plugin (that still lacks a few things ...) ;
a wrapper around :make in order to do background compiling, and to filter &makeprg results (e.g. pathnames conversions between cygwin posix form and dos form ; application of STLfilt ; etc.) (-> BuildToolWrapper which is stable, but still in an alpha stage) ;
and a few other things which have already been mentioned (alternate, ctags, ...).
Other Plugins.
Other people use c.vim, other templating systems (snipmate & co), pyclewn (that I highly recommend for debugging (with gdb) from within vim), other bracket-surrounding-and-expansion systems, ...
PS: I've answered, slightly differently, a question on the same subject on quora.
cscope is a nice tool for browsing. There is nice tutorial here.
ctags is another nice tool, I use it in my projects. Tutorial here. If you are in Ubuntu, you can install ctags by doing:
apt-get install exuberant-ctags
gtags is another tool.
I use taglist extensively.
The "Tag List" plugin is a source code browser for the Vim editor. It provides an overview of the structure of source code files and allows you to efficiently browse through source code files in different programming languages. It is the top-rated and most-downloaded plugin for the Vim editor.
Sometimes I am reading some code and would like to find the definition for a certain symbol, but it is sprinkled throughout the code to such an extent that grep is more or less insufficient for pointing me to its definition.
For example, I am working with Zlib and I want to figure out what FAR means.
Steven#Steven-PC /c/Users/Steven/Desktop/zlib-1.2.5
$ grep "FAR" * -R | wc -l
260
That's a lot to scan through. It turns out it is in fact #defined to nothing but it took me some time to figure it out.
If I was using Eclipse I would have it easy because I can just hover over the symbol and it will tell me what it is.
What kinds of tools out there can I use to analyze code in this way? Can GCC do this for me? clang maybe? I'm looking for something command-line preferably. Some kind of tool that isn't a full fledged IDE at any rate.
You may want to check out cscope, it's basically made for this, and a command line tool (if you like, using ncurses). Also, libclang (part of clang/llvm) can do so - but that's just a library (but took me just ~100 lines of python to use libclang to emulate basic cscope features).
cscope requires you to build a database first. libclang can parse code "live".
If the variable is not declared in your curernt file, it is declared in an included file, i.e. a .h. So you can limit the amount of data by performing a grep only on those files.
Moreover, you can filter whole word matches with -w option of grep.
Try:
grep -w "FAR" *.h -R | wc -l
Our Source Code Search Engine (SCSE) is kind of graphical grep that indexes a large code base according to the tokens of its language(s) (e.g., C, Java, COBOL, ...). Queries are stated in terms of the tokens, not strings, so finding an identifier won't find it in the middle of a comment. This minimizes false positives, and in a big code base these can be a serious waste of time. Found hits are displayed one per line; a click takes to the source text.
One can do queries from the command line and get grep-like responses, too.
A query of the form of
I=foo*
will find all uses of any identifier that starts with the letters "foo".
Queries can compose mulitiple tokens:
I=foo* '[' ... ']' '='
finds assignments to a subscripted foo ("..." means "near").
For C, Java and COBOL, the SCSE can find reads, writes, updates, and declarations of variables.
D=*baz
finds declarations of variables whose names end in "baz". I think this is what OP is looking for.
While SCSE works for C++, it presently can't find reads/writes/updates/declarations in C++. It does everything else.
The SCSE will handle mixed languages with aplomb. An "I" query will search across all langauges that have identifiers, so you can see cross language calls relatively easily, since the source and target identifiers tend to be the same for software engineering reasons.
gcc can output the pre-processing result, with all macro definitions with gcc -E -dD. The output file would be rather larger, often due to the nested system headers. But the first appearance of a symbol is usually the declaration (definition). The output use #line to show the part pre-processed result belong to source/header file, so you can find where it is originally declared.
To get the exact result when the file is compiled, you may need to add all other parameters used to compile the file, like -I, -D, etc. In fact, I always copy a result compilation command line, and add -E -dD to the beginning, and add (or change) -o in case I accidental overwrite anything.
There is gccxml, but I am not aware of tools that build on top of it. clang and LLVM are suited for such stuff, too; equally, I am not aware of standalone tools that build on them.
Apart from that: QtCreator and code::blocks can find the declartion, too.
So what is it about a "full fledged IDE" you don't want? If its a little speed, I found netbeans somewhat usefull when I was in school, but really for power and speed and general utility I would like to reccomend emacs. It has key board shortcuts for things like this. Keep in mind, its a learning curve to be sure, but once you are over the hump there is no going back.
Any suggestions on a quality way to tag and search c++ code. I use cscope/ctags for most stuff, but I have found it insufficient to find my way around some of the overly complex c++ code at work. I have started to switch from vim to Slickedit (which is still not perfect, but better) for browsing code, but would like to go back to exclusively vim.
What I would like is something that can understand scope of class members so, for instance, if I search for references to a member of a class where the same member name exists in other classes (and possibly out of c++ code) it will only give me the relevant references.
I'd prefer something that already works nice with vim, but any open source package such that I might create a plugin myself would be fine.
Any suggestions appreciated, thanks.
Are you sure you called ctags with the right options? For C++, I use:
ctags --c++-kinds=+p --fields=+iaS --extras=+q --language-force=C++
This is what the documentation has to say about the --c++-kinds=+p option:
When parsing a C++ member function definition (e.g.
"className::function"), ctags cannot determine whether the scope specifier
is a class name or a namespace specifier and always lists it as a class name
in the scope portion of the extension fields. Also, if a C++
function is defined outside of the class declaration (the usual case), the
access specification (i.e. public, protected, or private) and
implementation information (e.g. virtual, pure virtual) contained in the function
declaration are not known when the tag is generated for the function
definition. It will, however be available for prototypes
(e.g --c++-kinds=+p).
The --fields=+iaS option:
a Access (or export) of class members
i Inheritance information
S Signature of routine (e.g. prototype or parameter list)
The --extras=+q option:
Because, by default, ctags only generates tags for the separate identifiers found in the source files. If you specify the --extra=+q option, then ctags will also generate a
second, class-qualified tag for each class member (data and function/method) in the form class::member for C++, and in the form class.method for Eiffel and Java.
The --language-force=C++ option:
By default, ctags automatically selects the language of a source file, ignoring those files whose language cannot be determined (see SOURCE FILES, above). This option forces the specified language (case-insensitive; either built-in or user-defined) to be used for every supplied file instead of automatically selecting the language based upon its extension. In addition, the special value auto indicates that the language should be automatically selected (which effectively disables this option).
use doxygen its a great tool to browse code and see classes and members relations with each other. the tool produces clickable html output for your source code with references for each usage. you can compile it to a searchable chm file or use a web server to search the code for keywords.
Try GNU global http://www.gnu.org/software/global/
It can generate navigate:able web pages of your source code as well as having support for vim and a command line interface that is often useful.
I've no experience with this, but I have used Doxygen to browse the source code of complex projects. Just run it with all the settings turned on, and it will generate call graphs, callee graphs, reference and referenced-by relations, template instantiations, etc. Output formats include HTML, LaTeX, CHM, POD
Hope this works!
For a while I have been mixing different tools for this purposes. Vi is a great editor and you can run it over remote computers without hassles, but the completion information is not semantical.
When confronted with a big problem I tend to use either Eclipse CDT or QTCreator, in both cases the latest versions, versions from a year back are not really so nice. QTCreator is a lightweight tool, but I have been quite impressed on its ability to analyze the code. Eclipse CDT is heavier weight, but I am a little more used to the interface, so at the end I tend to use it.
The project I am working on is compiled within a separated sandbox, where none of those two IDEs can be used, but you can get a copy of the repository just for analysis and use the IDE just as a browsing tool.
instead of extras, extra worked for me. Also specified -R to scan all files/folders recursively.
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++
What packages do you use to handle command line options, settings and config files?
I'm looking for something that reads user-defined options from the command line and/or from config files.
The options (settings) should be dividable into different groups, so that I can pass different (subsets of) options to different objects in my code.
I know of boost::program_options, but I can't quite get used to the API. Are there light-weight alternatives?
(BTW, do you ever use a global options object in your code that can be read from anywhere? Or would you consider that evil?)
At Google, we use gflags. It doesn't do configuration files, but for flags, it's a lot less painful than using getopt.
#include <gflags/gflags.h>
DEFINE_string(server, "foo", "What server to connect to");
int main(int argc, char* argv[]) {
google::ParseCommandLineFlags(&argc, &argv, true);
if (!server.empty()) {
Connect(server);
}
}
You put the DEFINE_foo at the top of the file that needs to know the value of the flag. If other files also need to know the value, you use DECLARE_foo in them. There's also pretty good support for testing, so unit tests can set different flags independently.
For command lines and C++, I've been a fan of TCLAP: Templatized Command Line Argument Parser.
http://sourceforge.net/projects/tclap/
Well, you're not going to like my answer. I use boost::program_options. The interface takes some getting used to, but once you have it down, it's amazing. Just make sure to do boatloads of unit testing, because if you get the syntax wrong you will get runtime errors.
And, yes, I store them in a singleton object (read-only). I don't think it's evil in that case. It's one of the few cases I can think of where a singleton is acceptable.
If Boost is overkill for you, GNU Gengetopt is probably, too, but IMHO, it's a fun tool to mess around with.
And, I try to stay away from global options objects, I prefer to have each class read its own config. Besides the whole "Globals are evil" philosophy, it tends to end up becoming an ever-growing mess to have all of your configuration in one place, and also it's harder to tell what configuration variables are being used where. If you keep the configuration closer to where it's being used, it's more obvious what each one is for, and easier to keep clean.
(As to what I use, personally, for everything recently it's been a proprietary command line parsing library that somebody else at my company wrote, but that doesn't help you much, unfortunately)
I've been using TCLAP for a year or two now, but randomly I stumbled across ezOptionParser. ezOptionParser doesn't suffer from "it shouldn't have to be this complex"-syndrome the same way that other option parsers do.
I'm pretty impressed so far and I'll likely be using it going forward, specifically because it supports config files. TCLAP is a more sophisticated library, but the simplicity and extra features from ezOptionParser is very compelling.
Other perks from its website include (as of 0.2.0):
Pretty printing of parsed inputs for debugging.
Auto usage message creation in three layouts (aligned, interleaved or staggered).
Single header file implementation.
Dependent only on STL.
Arbitrary short and long option names (dash '-' or plus '+' prefixes not required).
Arbitrary argument list delimiters.
Multiple flag instances allowed.
Validation of required options, number of expected arguments per flag, datatype ranges, user defined ranges, membership in lists and case for string lists.
Validation criteria definable by strings or constants.
Multiple file import with comments.
Exports to file, either set options or all options including defaults when available.
Option parse index for order dependent contexts.
GNU getopt is pretty nice. If you want a C++ feel, consider getoptpp which is a wrapper around the native getopt.
As far as configuration file is concerned, you should try to make it as stupid as possible so that parsing is easy. If you are bit considerate, you might want to use yaac&lex but that would be really a big bucks for small apps.
I also would like to suggest that you should support both config files and command line options in your application. Config files are better for those options which are to be changed less frequently. Command-line options are good when you want to pass the immediate changing arguments (typically when you are creating a app, which would be called by some other program.)
If you are working with Visual Studio 2005 on x86 and x64 Windows there is some good Command Line Parsing utilities in the SimpleLibPlus library. I have used it and found it very useful.
Not sure about command line argument parsing. I have not needed very rich capabilities in that area and have generally rolled my own to save adding more dependencies to my software. Depending upon what your needs are you may or may not want to try this route. The C++ programs I have written are generally not invoked from the command line.
On the other hand, for a config file you really can't beat an XML based format. It's readable, extensible, structured, etc... :) Plus there are lots of XML parsers out there. Despite the fact it is a C library, I tend to use libxml2 from xmlsoft.org.
Try Apache Ant. Its primary usage is Java projects, but there isn't anything Java about it, and its usable for almost anything.
Usage is fairly simple and you've got a lot of community support too. It's really good at doing things the way you're asking.
As for global options in code, I think they're quite necessary and useful. Don't misuse them, though.