timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
mangler.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25/** \file utility/mangler.hpp
26 * \headerfile utility/mangler.hpp "timemory/utility/mangler.hpp"
27 * Provides mangling for C++ functions
28 *
29 */
30
31#pragma once
32
37
38#include <string>
39#include <tuple>
40
41namespace tim
42{
43//--------------------------------------------------------------------------------------//
44
45namespace impl
46{
47template <typename... Args>
48struct mangler
49{
50 static std::string mangle(std::string func, bool is_memfun, bool is_const, bool nsp)
51 {
52 auto nargs = sizeof...(Args);
53
54 // non-namespaced functions must add "E";
55 auto addE = (func.find("::") < func.find('('));
56
57 std::string ret = "_Z";
58 if(func.length() > 0 && func[0] == '&')
59 func = func.substr(1);
60 auto delim = delimit(func, ":()<>");
61 if(delim.size() > 1)
62 ret += "N";
63 if(is_memfun && is_const)
64 ret += "K";
65 for(const auto& itr : delim)
66 {
67 ret += std::to_string(itr.length());
68 ret += itr;
69 }
70
71 if(addE || nsp)
72 ret += "E";
73 if(nargs == 0)
74 {
75 ret += "v";
76 }
77 else
78 {
79 ret += mpl::apply<std::string>::join("", utility::type_id<Args>::name()...);
80 }
81
82 return ret;
83 }
84};
85
86//--------------------------------------------------------------------------------------//
87
88template <typename... Args>
89struct mangler<std::tuple<Args...>>
90{
91 static std::string mangle(const std::string& func, bool is_memfun, bool is_const,
92 bool nsp = false)
93 {
94 return mangler<Args...>::mangle(func, is_memfun, is_const, nsp);
95 }
96};
97
98//--------------------------------------------------------------------------------------//
99
100} // namespace impl
101
102//--------------------------------------------------------------------------------------//
103
104template <typename FuncT, typename TraitsT = mpl::function_traits<FuncT>>
106mangle(const std::string& func)
107{
108 using _Tuple = typename TraitsT::args_type;
109 constexpr bool is_memfun = TraitsT::is_memfun;
110 constexpr bool is_const = TraitsT::is_const;
111 return impl::mangler<_Tuple>::mangle(func, is_memfun, is_const);
112}
113
114//--------------------------------------------------------------------------------------//
115
116} // namespace tim
STL namespace.
Definition: kokkosp.cpp:39
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
std::string mangle(const std::string &func)
Definition: mangler.hpp:106
ContainerT delimit(const std::string &line, const std::string &delimiters="\"',;: ", PredicateT &&predicate=[](const std::string &s) -> std::string { return s;})
Definition: delimit.hpp:68
static string_t join(SepT &&separator, Tuple &&__tup, index_sequence< Idx... >) noexcept
Definition: apply.hpp:408