I have a hard time formulating this question. The reason I'm asking is that I want to convert some C++ code with emscripten to java script code, but I don't think I need to convert the whole code base.
Is it possible in C++ to find all the code that a particular function could reach into when executed? Then I would know which part of the code I need to convert and which one I can just ignore.
It is called "call hierarchy" as Eugene said. You can use automatic documentation tools to get this information.
I strongly recommend you to try doxygen because it is really easy to use:
http://www.doxygen.nl/
Related
I saw this post but it was for Python so that doesn't help me too much. I'm programming in C++, working on a code-base that I didn't write. I see some checks like GTK_IS_ENTRY and GTK_IS_COMBO_BOX, but I'm not sure where this person found these or what other GTK_IS_... there are. Is there a reference to these somewhere? I searched online and also on the Gtk/GLib websites but I couldn't find anything. Thanks!
The type checks macros are typically part of the API contract for a GObject, and they are conventionally provided by the library, so they don't end up in the documentation. All they do is call G_TYPE_CHECK_INSTANCE_TYPE with the given GType macro, like GTK_TYPE_ENTRY or GTK_TYPE_COMBO_BOX.
Here's some background of what I'm trying to achieve.
I'm in need of parsing C++ source code to find all instances where function X is called. This seems doable in libclang as mentioned in this post: Find all references of specific function declaration in libclang (Python) (though the answer implies it isn't as simple as you might think).
However, problem with libclang is that using it on Windows is often not recommended by many people. I can't use it on Linux because I'm hoping to use it on existing Visual C++ code that uses winapi.
With this barrier, I asked a colleague and he suggest I just simply search the source code using regular expression. I have my doubts that this is easy.
Can someone tell me if this approach is recommended?
Edit to address the comment of what my goal is: I need to do it programmatically because I'm tryng to integrate it to an infastructure that checks where the code was editted and then gives you an output on which end-user functionality is affected by that edit and thus needs to be rechecked. If I were to do this manually via the "find references" options in IDE, this means "finding references" in multiple levels until I reach the end-user level which is a lot of work for large code and prone to error.
OK, I have been looking for weeks now. I have looked through Eclipse and Visual Studio, but all the plugins for this sort of thing is for Java or C# and not C++. ReSharper does not work, nor does NArrange. How in the world can I sort my methods in a .cpp file without having to go in and cut and paste by hand (there are hundreds of files and there is not enough time in the world to do that)?
I have tried writing the program myself, but I am not very skilled in scripting and have zero experience in Python. Creating the program in C++ I believe is possible but if there is a simpler way then I would like to know.
I didn't use it but take a look at Regionerate. It is a plugin for Visual Studio. I am sorry, I saw now that it is also only for C#. I thought that it worked with C++ too. Sorry.
I have looked for a long time and talked to many co-workers and am now convinced we should not do it. Too many headaches and one of the developers said he didn't want that because of the way he writes his code. Thank the lord he said something!
If anyone else is looking to do this and trying to find a solution, I would just like to let you know that it is not worth the trouble. If you HAVE to do something like this in C++ then you have got to do it by hand. Pray that you don't have to.
I realize you've concluded you don't want to do this, but just in case someone else does, you might be able to use Doxygen to do the "heavy lifting" and extract the functions from your source.
You can configure Doxygen to extract the code structure from undocumented source files.
You'd then have to extract starting line numbers of the functions from Doxygen's output, sort, and reassemble. It gets messy because you might need to introduce forward declarations.
Thankfully you decided against doing it.
learned english as a second lang, sorry for the mistakes & awkwardness
I have given a peculiar project to work on. The company has lost the source code for the app, and I have to make changes to it. Now, reverse engineering the whole thing is impossible for one man, its just too huge, however patching individual functions would be feasible, since the changes are not that monumental.
So, one possible solution would be compiling C code and somehow -after rewriting addresses- patching it into the actual binary, ideally, replacing the code the CALL instruction jumps to, or inserting a JMP to my code.
Is there any way to accomplish this using MingW32? If it is, can you provide a simple example? I'm also interested in books which could help me accomplishing the task.
Thanks for your help
I use OllyDBG for this kind of things. It allows you to see the disassembly and debug it, you can place breakpoints etc, and you can also edit the binary. So, you could edit the PE header of that program adding a code section with your (compiled) code inside, then call it from the original program.
I can't give you any advice since I've never tried, although I thought about it many times. You know, lazyness.. :)
I would disassemble the program with a high-quality disassembler that produces something that can be assembled back into a runnable app, and then replace the parts you need to modify with C code.
Something like this will let you reverse the machine code into source. It won't be pretty but it does work.
http://www.hex-rays.com/idapro/
There are also tools for runtime patching http://www.dyninst.org/ for instance. They really aren't made for patching but they can do the trick.
And of course the last choice is to just use an assembler and write machine code :)
Is there a library for Python that will allow me to parse c++ code?
For example, let's say I want to parse some c++ code and find the names of all classes and their member functions/variables.
I can think of a few ways to hack it together using regular expressions, but if there is an existing library it would be more helpful.
In the past I've used for such purposes gccxml (a C++ parser that emits easily-parseable XML) -- I hacked up my own Python interfaces to it, but now there's a pygccxml which should package that up nicely for you.
Parsing C++ accurately is light-years from something you can do with a regular expression.
You need a full C++ parser, and they're pretty hard to build. I've been involved in building one over several years, and track who is doing it; I don't know of any being attempted in Python.
The one I work on is DMS C++ Front End.
It provides not only parsing, but full name and type resolution. After parsing, you can basically extract detailed information about the code at whatever level of detail you like, including arbittary details about function content.
You might consider using GCCXML, which does contain a parser, and will produce, I believe, the names of all classes, functions, and top-level variables. GCCXML won't give you any information about what's inside a function.
This is a little outside your question's scope perhaps... but depending on what you're trying to achieve, perhaps Exuberant Ctags is worth looking at.
Have not tried, but using the Python bindings from LLVM's Clang parser may work; see here.
How about pyparsing?