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

2024-02-29

是否有获取当前命名空间和函数名的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

据我所知,这是不可能的(不可移植)。但是,从完整签名中,您可以extract那些论点。当然,它需要解析所述签名......这并不那么容易: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__ macro.

The StringRef类类似于llvm::StringRef http://llvm.org/docs/doxygen/html/StringRef_8h.html.

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

希望能帮助到你。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

获取当前命名空间和函数名称的宏(但不是完整签名)? 的相关文章

随机推荐