c – 宏来获取当前的命名空间和函数名(但不是全签名)?

前端之家收集整理的这篇文章主要介绍了c – 宏来获取当前的命名空间和函数名(但不是全签名)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有获取当前命名空间和函数名称的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,"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")
  UT_FUNCTION_CHECK("void Test::Bar<T,N>::parameterized(U) const"
                    " [with unsigned int O = 4u,"
                    " T = Test::Foo,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类似.

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

希望它有帮助.

原文链接:https://www.f2er.com/c/115736.html

猜你在找的C&C++相关文章