I'm working on a logging mechanism for a project, and currently each API function needs to add
start_api_call(***)
in the beginning of the function
where *** for the function do_something(int foo, int bar) is "foo", foo, "bar",bar
(The log function takes the parameters and form the desired message)
I would like to make this line auto-generated, where the vision is that somehow (mabye clang tools?) the compiler checks for each function in a .cpp if it is an API function (more details later) and if it is, just add the start_api_call(***) to the code.
I have 2 major problems regarding which direction should i go
1) I have never wrote code that it's 'goal' is to parse a source code, hence i don't know which direction should i head, I've read some documentation about the clang tools, but maybe just a python script would be better here?
2) Our design is as follows:
object_foo.h //inside .include/
class foo{
API functions
}
object_foo_impl.h //inside .src/
include "object_foo.h"
class foo_impl :foo{
foo API functions
a lot more functions
}
object_foo_impl.cpp
include "object_foo_impl.h"
{
implementation of all foo_impl functions
}
The start_api_call should be inserted only for the API functions,
thus i need to find a way to query inside the .cpp file, if this function came from the foo.h, or from the foo_impl.h file.
I have a somehow working concept of how to do it using python scripts, that parse all of our source code, identifying using regex which text inside a /include/.h file is a function and then finding all the functions inside .cpp files that implement those functions,
but (if possible) the concept of adding a rule in the compilation(/preprocessor) time is much more attractive.
Any help would be very much appreciated.
Related
I have classes that look like this:
Header (.h):
class DatabaseX ; //forward declare
class DeepClass
{
public:
void DeepClass(DatabaseX* db);
void doStuff();
private:
DatabaseX *m_db;
};
Definition (.cpp)
#include "some/path/that/stretches/into/a/rather/deep/structure/DeepClass.h"
#include "another/somewhat/disturbing/long/path/to/somewhere/distant/DatabaseX.h"
void DeepClass::DeepClass(DatabaseX* db):m_db(db){ m_db->open() }
void DeepClass::~DeepClass(){ m_db->close(); delete m_db; }
void DeepClass::doStuff(){ // <complicated stuff here> }
Now I want a test that checks that doStuff() does the right kind of stuff.
So I write my own mock DatabaseX.
But I have a problem, my own mock database lives in the test directory, it has no place in production code, and what's worse, DatabaseX was never written to be inherited and overloaded.
It's a concrete class, and isn't anything like an interface.
So my question is, how do I write a test, with all these hard-coded include paths everywhere?
Do I for example:
create another duplicate file structure that matches the include paths, and put my mock DatabaseX there in this duplicate file structure?
Somehow rewite each cpp file before the compiler accesses it by some indirection magic or other?
Add macros to eat up the paths?
Write a python/perl/bash script to temporarily remove the include paths prior to compiling my tests?
Just include everything, accept the dependencies of DatabaseX, and just compile the real thing, and all it's dependencies and then replace at link time?
Accept defeat; don't write any tests, and bury my head in the sand.
OR ... ?
I should say there are well over a million lines of code, so changing the source code isn't an option.
Is there a very simple way to overcome this nightmare via a nice simple compiler option or other?
(Perhaps it's not relevant but I'm using Qt's QTest & QtCreator. Maybe there is some magical switch that makes all these gruesome paths go away!).
I am using GCC 4.8.5
I recently got into the situation where i had access to a huge code base, which i couldn't build and i needed to test couple of its functions.
Nevertheless, those functions had references of functions/variables found in other files so it is a big mess trying to extract them manually.
Is there a way to do this automatically?
For example, i want to test function foo in test.c but foo depends on bar function found in file test2.c. The bar function could then be dependent on booz which is found in test3.c.
So in the case above, one could gather foo, bar, and booz in one file and compile.
To generate cross references you can use Doxygen.
You can have a look for PostgreSQL C source code here and search for example definition of main function.
I do a lot of c++ programming in vim and I was wondering if there are any plugins or snippets out there that can generate a source file depending on the contents of the header file.
I.E: test.h
class test {
public:
test();
};
and then going into the test.cpp file and typing "src" and expanding it (using some sort of snippet plugin like UltiSnips) it would look in the test.h file for the funcions and (in this case) make:
test::test() {
//code
}
I got this idea from Derek Wyatt's blog and he does this using XPTemplate so I thought it would be great to do the same in UltiSnips.
Use the xptemplate plugin.
Examples:
http://www.derekwyatt.org/wp-content/uploads/2009/08/my.cpp.xpt.vim
http://www.derekwyatt.org/vim/working-with-vim-and-cpp/cpp-snippets
lh-cpp offers a :GOTOIMPL function that analyses the prototype of a given function, and either jumps to the associated definition or generates it on-the-fly. [NB: it knows what to do with virtual, static, namespace/embedded classes, return type, modifiers, and so on (except templates yet)]
Regarding how to parse a header file and generate all associated functions, the exact same question has been asked on vim mailing list 2-3 weeks ago where another solution has been given (protodef, that you have read about).
I tend to prototype C++ classes as a self-contained class, i.e. like:
class BlahBlahBlah
{
public:
void SomeMethod()
{
// some code here
}
};
and then later when I am happy with the design I will turn it into a .h/.cpp pair. This is mostly a mechanical task so is there any tool out there that can help with that?
Try Lzz.
Visual Assist has a refactor tool that does this. You can bind it to a keyboard shortcut so you can do it case-by-case, rather than robotically.
As an alternative to Lzz you might consider taking a look at Preprocess - A preprocessor for C and C++ modules. Unlike Lzz, it does preserve comments. However, it does have some limitations. It does not support namespaces or nested classes. But since it's written in PERL and I imagine it would not be too difficult to extend and customize.
There is also inlsplit: Inline C++ Source and Header splitter. It is a very simple awk script which parses a single .inl file and places all member functions with a # folowing their prototype into the source file, keeping only the declaration in the header. There is also a #source tag for code to go straight into the implementation. Compared to lzz and preprocess, it is very lightweight. However it has been abandoned and there is no new development.
I'm trying to learn C++ for Qt development, and I'm a little scared of header files. What I'd like to know is, what's the best workflow for keeping *.cpp and *.h files synched? For example, is the norm to write the class file and then copy the relevant info over to the header?
Sorry if this doesn't make any sense...I'm just looking for an efficient workflow for this.
Thanks!
Justin
For example, is the norm to write the class file and then copy the relevant info over to the header?
While there is no single standard approach, its usually a good idea to:
first think about the public interface
put that in the header
implement in the source file accordingly
update the header if needed
Jumping straight into the implementation can make for a painful refactoring later on.
So:
You'll get linker errors if something's in the header, used by a client, and not in the cpp.
You'll get compile errors if something in the cpp is not defined or is defined differently in the .h
What scenario are you worried about?
Create the test code that uses your class/functions (this way you can play with the end results before even writing any code)
Create the header with the class and the methods.
Implement those methods in your cpp file
If you run into the case where you have to change the signature of the methods, you will need to manually change it both place (header + source).
You can shell out a small amount of money and buy Visual Assist X. With Visual Assist X, you right click the method in your cpp file, choose Refactor -> Change Signature. Perform the change and hit Ok. It is changed in both places.
So, in short, there is no way to keep them in sync automatically, but with the right refactor tool, your life will be better.
In general for the first version I write the class file in the .h and when done I copy the method declarations to the .cpp and there change them into definitions (i.e. methods and their bodies). In this phase I declared only the public methods of the class, because I'm more concerned about its interface than the internals.
Later, when I'm implementing the public methods, if I need a new private method I begin by calling it wherever it's needed, so I get the parameters well defined. Then I write its declaration in the .h and go back to the .cpp to write its body.
It may be of interest to you the concepts of Tracer Bullet Design, which fits well with this workflow. It's described in the book The Pragmatic Programmer:
http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X/ref=sr_1_1?ie=UTF8&s=books&qid=1279150854&sr=8-1
This page contains a brief description
http://www.artima.com/intv/tracer2.html
You keep them both open at the same time (I frequently use a horizontal split for this) and when you change one file you change the other file. It's just the same as how you keep the prototypes at the top of a file in sync with the function definitions at the bottom of the file when you're writing a program that fits in a single .cpp file.