gpt4 book ai didi

c++ - 获取当前命名空间和函数名(但不是完整签名)的宏?

转载 作者:IT老高 更新时间:2023-10-28 23:02:09 25 4
gpt4 key购买 nike

是否有获取当前命名空间和函数名的 C++ 宏?示例:

namespace foo {
namespace bar {
void baz(int i, double d) {
std::cout << MACRO << std::endl;
}
}
}

将打印 foo::bar::baz。我知道 __FUNCTION__ 但它没有给出命名空间。 BOOST_CURRENT_FUNCTION 给出了整个签名,包括。参数和返回类型:

void foo::bar::baz(int, double)

也许,是否可以编写一个从 BOOST_CURRENT_FUNCTION 中提取命名空间和函数名的宏?

我希望它用于记录目的,以获得类似的记录字符串

foo::bar::baz -- blah logging message blah

最佳答案

据我所知,这是不可能的(不可移植)。但是,您可以从完整签名中提取这些参数。当然它需要解析所说的签名......这并不容易:x

这是我目前使用的功能:

// What we want to consume:
// void
// signed short
// unsigned int
// Test::Bar<T, N>
//
static char const* consumeType(char const* const begin, char const* const end){
static StringRef const Signed("signed");
static StringRef const Unsigned("unsigned");

char const* it = begin;

if (startsWith(it, Signed)) { it += Signed.size() + 1; }
else if (startsWith(it, Unsigned)) { it += Unsigned.size() + 1; }

// jump over the return type
size_t templateNest = 0;
while (it != end) {
if (*it == ' ' and templateNest == 0) { break; }
if (*it == '<') { ++templateNest; }
if (*it == '>' and templateNest > 0) { --templateNest; }

++it;
}

return it;
} // consumeType

//
// \param signature: signature as returned by __func___ on gcc
// \return: full name, included namespace qualifier and class (if any)
//
// void Test::Bar<T, N>::parameterized(U) const
// [with unsigned int O = 4u, U = Test::Foo,
// T = Test::Foo, unsigned int N = 3u]
// -> Test::Bar<T, N>::parameterized
//
StringRef parseFunctionName(StringRef const signature) {
char const* begin = signature.begin();
char const* end = signature.end();

// Jump over the return type
begin = consumeType(begin, end);
if (begin == end) { return signature; }

// skip the space right after the return type
++begin;
if (begin == end) { return signature; }

// if we encounter a '(' then it means that we return a function,
// and we once again need to jump over the return type
if (*begin == '(') {
begin = consumeType(++begin, end);

// skip the space
++begin;
if (begin == end) { return signature; }
}

// and finally, we got the beginning, and we need to get the end, which is
// the first opening '('
char const* e = std::find(begin, end, '(');
return StringRef(begin, e - begin);
} // parseFunctionName

及其伴随的测试:

#define UT_FUNCTION_CHECK(Signature_, Name_) \
UT_CHECK(parseFunctionName(StringRef(Signature_)) == Name_);

void Function() {
// Regular functions
UT_FUNCTION_CHECK("int main()", "main")
UT_FUNCTION_CHECK("int foo(int, double)", "foo")
UT_FUNCTION_CHECK("unsigned int foo(int, double)", "foo")

// Templates
UT_FUNCTION_CHECK("unsigned int Test::Bar<T, N>::print() const"
" [with T = Test::Foo, unsigned int N = 3u]",
"Test::Bar<T, N>::print")
UT_FUNCTION_CHECK("Test::Bar<T, N> Test::Bar<T, N>::print() const"
" [with T = Test::Foo, unsigned int N = 3u]",
"Test::Bar<T, N>::print")
UT_FUNCTION_CHECK("void Test::Bar<T, N>::parameterized(U) const"
" [with unsigned int O = 4u, U = Test::Foo,"
" T = Test::Foo, unsigned int N = 3u]",
"Test::Bar<T, N>::parameterized")

// Functions returning functions
UT_FUNCTION_CHECK("void (* Test::Foo::func() const)()",
"Test::Foo::func")
UT_FUNCTION_CHECK("void (Test::Foo::* Test::Foo::method() const)(int)volatile",
"Test::Foo::method")
UT_FUNCTION_CHECK("void (Test::Foo::* Test::Foo::super())"
"(void (Test::Foo::*)(int)volatile)const",
"Test::Foo::super")
} // Function

它与 gcc 的 __func__ 宏结合使用。

StringRef 类类似于 llvm::StringRef .

如果您负担得起额外的解析,我认为它应该可以满足您的需求。它非常快:没有回溯和动态分配,所以应该不是问题(尤其是与写入文件相比......)。

希望对你有帮助。

关于c++ - 获取当前命名空间和函数名(但不是完整签名)的宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9124856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com