C++ macro to get full function path and declaration - c++

I want to get full function path and declaration in code via macro or some compiler magic. I have this code (click here to run):
#include <iostream>
namespace NS {
struct Foo {
static int sum(int a, int b) {
std::cout << "This is from " << __FILE__ << ":" << __LINE__ << " # " << __func__ << std::endl;
return a+b;
}
static int sum(int a, int b, int c) {
std::cout << "This is from " << __FILE__ << ":" << __LINE__ << " # " << __func__ << std::endl;
return a+b+c;
}
};
}
int main() {
NS::Foo::sum(1,2);
NS::Foo::sum(1,2, 3);
}
And I get the output:
This is from /some/where/main.cpp:7 # sum
This is from /some/where/main.cpp:12 # sum
My questions are:
How do I get the full path of sum function invoked?(NS::Foo::sum)
How can I get the full function declaration with argument types?( sum(int, int) or sum(int, int, int))
I am interested in mainstream compilers: Clang, GCC, Microsoft C++ compiler

Answering for GCC.
Checkout the __PRETTY_FUNCTION__ macro. I've found it in the defintion of assert macro in <assert.h>. Perhaps same could be found for other compilers and libc implementations.

For Microsoft Visual Studio compiler, __FUNCSIG__ can give you a lot about the function.
__FUNCSIG__ will give you full function signature
__FUNCDNAME__ gives magnled name.
__FUNCTION__ is just for function name.

If you don't want to limit yourself to a single compiler, and don't fancy writing a pre-processor ifdef chain yourself, boost has already defined a macro BOOST_CURRENT_FUNCTION which is defined as the full function signature macro that's defined on the used compiler. __PRETTY_FUNCTION__ in gcc and other compilers that support that, __FUNCSIG__ where that is supported (msvc), also supports some lesser used compilers and falls back to __func__ as defined by the C standard (or a static placeholder if even that's not supported).

Related

Have I found a bug in Clang and GCC?

main.cpp:
#include <iostream>
struct Cls {
static void some_method() {
std::cout << __FILE__ << ": " << __LINE__ << std::endl;
}
};
extern void call_some_method();
void never_caled() {
Cls::some_method(); // (1)
}
int main() {
call_some_method();
return 0;
}
cls.cpp:
#include <iostream>
struct Cls {
static void some_method() {
std::cout << __FILE__ << ": " << __LINE__ << std::endl;
}
};
void call_some_method() {
Cls::some_method();
}
When (1) is commented, call_some_method() write "/home/maxim/CLionProjects/bug/cls.cpp: 5" to std::cout.
When (1) is uncommented, call_some_method() write "/home/maxim/CLionProjects/bug/main.cpp: 5" to std::cout.
How it can be possible for different outputs?
__FILE__ and __LINE__ are macros that are expanded by the pre-processor. Since those macros are in different files, and depend on the files they are used in, they expand to different token sequences.
That means that your definition of Cls::some_method is different across different translation units. This violates the one-definition-rule (ODR) which requires, among other things, that there be exactly one definition of a particular entity in your entire program. If there are multiple definitions, that's fine, so long as the token sequence of those definitions is identical in every translation unit, and those tokens mean the same thing when they are parsed.
This is obviously not true in your case, so you are violating the ODR, which makes the program ill-formed (No Diagnosis Required). This means the compiler is not obliged to give you an error, but if it does produce a program, executing that program invokes undefined behavior (UB). So your program could do anything at all, including producing the output that you are seeing.

Why does #ifndef __func__ return true?

The following code,
#include <iostream>
#ifndef __func__
# ifdef __FUNCTION__
# define __func__ __FUNCTION__
# else
//# error This compiler supports neither __func__ nor __FUNCTION__
# endif
#endif
int main(int argc, char **argv)
{
std::cout << __func__ << std::endl
<< __FUNCTION__ << std::endl
<< __PRETTY_FUNCTION__ << std::endl;
}
gives the following expected output,
main
main
int main(int, char**)
However, if I uncomment the else condition, compilation fails because neither __func__ nor __FUNCTION__ are defined. How can this be? They clearly are defined as seen in the output above. Is there some simple principle regarding #ifdef/#ifndef that I'm missing here?
__func__ is not a macro. It is a magic variable, as are __FUNCTION__ and __PRETTY_FUNCTION__.
From https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Function-Names.html:
GCC provides three magic variables that hold the name of the current function, as a string. The first of these is __func__, which is part of the C99 standard:
The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration
static const char __func__[] = "function-name";
...
__FUNCTION__ is another name for __func__.
...
In C, __PRETTY_FUNCTION__ is yet another name for __func__. However, in C++, __PRETTY_FUNCTION__ contains the type signature of the function as well as its bare name.

Visual C++ equivalent of __FILE__ , __LINE__ and __PRETTY_FUNCTION__

GCC compiler gives me the following macros:
__FILE__ so that I can print out the file name + directory.
__LINE__ so that I can print out the line number of where I'm printing from.
__PRETTY_FUNCTION__ so that I can print out the pretty function name
Does Visual C++ have the equivalent of these macros? A side question is, are these standard for C++ compilers?
In VS2008, this:
struct A
{
bool Test(int iDummyArg)
{
const char *szFile = __FILE__;
int iLine = __LINE__;
const char *szFunc = __FUNCTION__; // Func name
const char *szFunD = __FUNCDNAME__; // Decorated
const char *szFunS = __FUNCSIG__; // Signature
printf("%s\n", szFile);
printf("%d\n", iLine);
printf("%s\n", szFunc);
printf("%s\n", szFunD);
printf("%s\n", szFunS);
return true;
}
};
int wmain(int argc, TCHAR *lpszArgv[])
{
A a;
a.Test(10);
}
will print this:
c:\source\test_projects\blah\blah.cpp
14
A::Test
?Test#A##QAE_NH#Z
bool __thiscall A::Test(int)
(The line number is "wrong" since there was really some extra stuff at the top of my file.)
__FILE__ and __LINE__ are standard, and I'm rather certain Microsoft compilers have essentially always had them.
__PRETTY_FUNCTION__ is a gcc feature.
For more portability in getting the current function name, you can try BOOST_CURRENT_FUNCTION.
Yes Visual C++ has them or an equivalent. See the responses here:
What's the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?
function-func/4384860#4384860
Also note that despite the upper case used, they aren't macros. They're variables.
I know that MSVC offers __FILE__ and __LINE__, both of which are Standard macros. They also offer __FUNCTION__, which I believe is what you're looking for,
__FILE__ and __LINE__ are standard since C89. __PRETTY_FUNCTION__ is a GCCism. __func__ is a C99ism which (unlike GCCisms) may well be available in Visual C++; it is not precisely the same as __PRETTY_FUNCTION__ but it may be close enough for your purposes.
Yes, Microsoft Visual Studio has __FILE__ and __LINE__. Here are more MSVC macros.
Both are ANSI C++.
MSVC has __FUNCTION__, which is Microsoft-specific.
Using c++14 with constexpr you can use this: WHERE macro.
Based on usage of:
__PRETTY_FUNCTION__
__LINE__
#include "string/ConstexprString.hpp"
#define S1(x) #x
#define S2(x) S1(x)
// WHERE - const char* const should be used as temporary value
#define WHERE (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__))).get()
// It is safe to store e.g. `constexpr auto where = WHERE_STR;`
#define WHERE_STR (string::make(__PRETTY_FUNCTION__) + ":" + string::make(S2(__LINE__)))
Example usage:
// Called: void (anonymous namespace)::exampleUseCaseWhere(int):18
std::cout << "Called: " << WHERE << std::endl;
Full & running example here
See:
src/acme/where.hpp
src/string/ConstexprString.hpp
src/acme/where_test.cpp

g++ __FUNCTION__ replace time

Can anyone tell when g++ replaces the __FUNCTION__ 'macro' with the string containing the function name? It seems it can replace it not until it has check the syntactical correctness of the source code, i.e. the following will not work
#include <whatsneeded>
#define DBG_WHEREAMI __FUNCTION__ __FILE__ __LINE__
int main(int argc, char* argv)
{
printf(DBG_WHEREAMI "\n"); //*
}
since after preprocessing using
g++ -E test.cc
the source looks like
[...]
int main(int argc, char* argv)
{
printf(__FUNCTION__ "test.cc" "6" "\n"); //*
}
and now the compiler rightly throws up because the *ed line is incorrect.
Is there any way to force that replacement with a string to an earlier step so that the line is correct?
Is __FUNCTION__ really replaced with a string after all? Or is it a variable in the compiled code?
Is there any way to force that replacement with a string to an earlier step so that the line is correct?
No. __FUNCTION__ (and its standardized counterpart, __func__) are compiler constructs. __FILE__ and __LINE__ on the other hand, are preprocessor constructs. There is no way to make __FUNCTION__ a preprocessor construct because the preprocessor has no knowledge of the C++ language. When a source file is being preprocessed, the preprocessor has absolutely no idea about which function it is looking at because it doesn't even have a concept of functions.
On the other hand, the preprocessor does know which file it is working on, and it also knows which line of the file it is looking at, so it is able to handle __FILE__ and __LINE__.
This is why __func__ is defined as being equivalent to a static local variable (i.e. a compiler construct); only the compiler can provide this functionality.
You are using __FUNCTION__ like a preprocessor macro, but it's a variable (please read http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html).
Try printf("%s", __FUNCTION__) just for testing and it will print the function name.
__FUNCTION__ is not standard. Use __func__. As the documentation says, it's as if:
<ret-type> function_name( <args> )
{
static const char __func__[] = "function-name";
...
In C/C++, the preprocessor will turn "my " "name " "is " "Bob" into the string literal "my name is Bob"; since __FILE__ and __LINE__ are preprocessor instructions, "We are on line " __LINE__ will pass "We are on line 27" to the compiler.
__FUNCTION__ is normally a synonym for __func__. __func__ can be thought of as a pseudo-function that returns the name of the function in which it is called. This can only be done by the compiler and not by the preprocessor. Because __func__ is not evaluated by the preprocessor, you do not get automatic concatenation. So if you are using printf it must be done by printf("the function name is %s", __func__);
Is this what you want?
#include <stdio.h>
#define DBG_WHEREAMI(X) printf("%s %s(%d): %s\n",__func__,__FILE__,__LINE__,X)
int main(int argc, char* argv)
{
DBG_WHEREAMI("Starting");
}
Note: Since you marked this as C++ you should probably be using the iostreams to make sure it's type safe.
printf("%s" __FILE__ __LINE__ "\n", __FUNCTION__);
Yeah, I know that's not really the same.
Note that if you create a class, you can build a message from any number of types as you'd like which means you have a similar effect to the << operator or the format in a printf(3C). Something like this:
// make sure log remains copyable
class log
{
public:
log(const char *function, const char *filename, int line)
{
f_message << function << ":" << filename << ":" << line << ": ";
}
~log()
{
//printf("%s\n", f_message.str().c_str()); -- printf?!
std::cerr << f_message.str() << std::endl;
}
log& operator () (const char *value)
{
f_message << value;
}
log& operator () (int value)
{
f_message << value;
}
// repeat with all the types you want to support in the base class
// (should be all the basic types at least)
private:
sstream f_message;
};
// start the magic here
log log_error(const char *func, const char *file, int line)
{
log l(func, file, line);
return l;
}
// NOTE: No ';' at the end here!
#define LOG_DEBUG log_error(__func__, __FILE__, __LINE__)
// usage sample:
LOG_DEBUG("found ")(count)(" items");
Note that you could declare the << operators instead of the (). In that case the resulting usage would be something like this:
LOG_DEBUG << "found " << count << " items";
Depends which you prefer to use. I kind of like () because it protects your expressions automatically. i.e. if you want to output "count << 3" then you'd have to write:
LOG_DEBUG << "found " << (count << 3) << " items";

Macro / keyword which can be used to print out method name?

__FILE__ and __LINE__ are well known. There is a __func__ since C99.
#include <iostream>
struct Foo {
void Do(){ std::cout << __func__ << std::endl; }
};
int main()
{
std::cout << __func__ << std::endl;
Foo foo; foo.Do();
return 0;
}
will output
main
Do
Is there any macro / keyword that would output method name like Foo::Do?
Boost has a special utility macro called BOOST_CURRENT_FUNCTION that hides the differences between the compiler implementations.
Following it's implementation we see that there are several macros depending on compiler:
__PRETTY_FUNCTION__ -- GCC, MetroWerks, Digital Mars, ICC, MinGW
__FUNCSIG__ -- MSVC
__FUNCTION__ -- Intel and IBM
__FUNC__ -- Borland
__func__ -- ANSI C99
On GCC you can use __FUNCTION__ and
__PRETTY_FUNCTION__.
On MSVC you can use __FUNCSIG__ and __FUNCTION__.
There's no such macro in standard C++, and that includes the draft C++0x standard I looked at. It would complicate compilation, since parsing (necessary to determine what a function is) comes after preprocessing, and I suspect that's why there's nothing in the standard.
The __func__ you're using is nonstandard, although it apparently works on your compiler.
Not in Standard C++ (and __func__ is not part of C++). Your implementation may have such a feature though - which compiler are you using?
See "Predefined Macros (C/C++)" for a complete list supported by MS Visual Studio.
This might be useful:
http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
#include"stdio.h"
#include"stdlib.h"
int main()
{
printf("line number is %d .. func name is %s, file name is %s",__LINE__,__PRETTY_FUNCTION__,__FILE__);
return 0;
}
This is how to print line number, function name and file name in gcc.