blob: 0d1ab73c42a69bc98746833944632ef686b490de [file] [log] [blame]
Upstreamcc2ee171970-01-12 13:46:40 +00001/**
2 * @file demangle_symbol.cpp
3 * Demangle a C++ symbol
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 */
10
11#include "config.h"
12
13#include "demangle_symbol.h"
14#include "op_regex.h"
15
16// from libiberty
17/*@{\name demangle option parameter */
18#ifndef DMGL_PARAMS
19# define DMGL_PARAMS (1 << 0) /**< Include function args */
20#endif
21#ifndef DMGL_ANSI
22# define DMGL_ANSI (1 << 1) /**< Include const, volatile, etc */
23#endif
24/*@}*/
25extern "C" char * cplus_demangle(char const * mangled, int options);
26
27using namespace std;
28
29namespace options {
30 extern demangle_type demangle;
31}
32
33string const demangle_symbol(string const & name)
34{
35 if (options::demangle == dmt_none)
36 return name;
37
38 // Do not try to strip leading underscore, this leads to many
39 // C++ demangling failures.
40 char * unmangled = cplus_demangle(name.c_str(), DMGL_PARAMS | DMGL_ANSI);
41
42 if (!unmangled)
43 return name;
44
45 string result(unmangled);
46 free(unmangled);
47
48 if (options::demangle == dmt_smart) {
49 static bool init = false;
50 static regular_expression_replace regex;
51 if (init == false) {
52 setup_regex(regex, OP_DATADIR "/stl.pat");
53 init = true;
54 }
55 // we don't protect against exception here, pattern must be
56 // right and user can easily work-around by using -d
57 regex.execute(result);
58 }
59
60 return result;
61}