Hi. As an experiment, and as a way of familiarizing myself with clang's internals, I'd like to implement an extension to provide attributed functions (and methods) with call site information. The primary use for this is for diagnostic logging.
What I envision is this. You declare a function with an __attribute__, perhaps like this:
void FancyLog(const char* inFormat, ...) __attribute__((call_site_info(callInfo)));
And in the implementation, you can reference an implicit parameter named "callInfo" (the implementor can choose whatever name they want; maybe the attribute must be repeated on the function's implementation to get the actual parameter name):
1 void FancyLog(const char* inFormat, ...) __attribute__((call_site_info(callInfo)))
2 {
3 printf("Got called from %s:%lu, func: %s: %s\n", callInfo- >file, callInfo->line, callInfo->prettyFunc, inFormat);
4 }
The function is called based on its formal parameter list:
6 int main()
7 {
8 FancyLog("Now is the time %d %2\n", 1, 2);
9 }
And the output generated would be something like:
Got called from main.c:3 main(): Now is the time 1 2
callInfo is a pointer to a clang-provided struct that looks something like:
struct __call_site_loc_info {
const char* file;
uint32_t line;
const char* prettyFunc;
};
Obviously I've left out some details, but here's what I need to do:
a) parse the new attribute and store the information (the fact that a function is to get this implicit parameter, the parameter's name) with the function information within clang's state
b) Parse the attribute at the function definition, to get the actual parameter name.
c) At each call site, allocate a __call_site_loc_info object on the stack, set the members appropriately (not sure where the __FILE__ and __PRETTY_FUNC__ strings end up, but I'd like to use those),
d) Create the implicit parameter pointing to the __call_site_loc_info struct.
If someone could point me at the right parts of clang to look at, I'd love to experiment with this extension. I've had a bit of discussion with Alex Rosenberg and others on this topic, and I agree that this probably isn't the best formal solution moving forward, but I think it makes for a good experiment, and a GREAT exercise for me in clang.
Any guidance would be much appreciated!
Thanks!