demangle_unittest.cc
Go to the documentation of this file.
1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Satoru Takabayashi
31 //
32 // Unit tests for functions in demangle.c.
33 
34 #include "utilities.h"
35 
36 #include <iostream>
37 #include <fstream>
38 #include <string>
39 #include <glog/logging.h>
40 #include "demangle.h"
41 #include "googletest.h"
42 #include "config.h"
43 
44 #ifdef HAVE_LIB_GFLAGS
45 #include <gflags/gflags.h>
46 using namespace GFLAGS_NAMESPACE;
47 #endif
48 
49 GLOG_DEFINE_bool(demangle_filter, false,
50  "Run demangle_unittest in filter mode");
51 
52 using namespace std;
53 using namespace GOOGLE_NAMESPACE;
54 
55 // A wrapper function for Demangle() to make the unit test simple.
56 static const char *DemangleIt(const char * const mangled) {
57  static char demangled[4096];
58  if (Demangle(mangled, demangled, sizeof(demangled))) {
59  return demangled;
60  } else {
61  return mangled;
62  }
63 }
64 
65 #if defined(GLOG_OS_WINDOWS)
66 
67 #if defined(HAVE_DBGHELP) && !defined(NDEBUG)
68 TEST(Demangle, Windows) {
70  "public: static void __cdecl Foo::func(int)",
71  DemangleIt("?func@Foo@@SAXH@Z"));
73  "public: static void __cdecl Foo::func(int)",
74  DemangleIt("@ILT+1105(?func@Foo@@SAXH@Z)"));
76  "int __cdecl foobarArray(int * const)",
77  DemangleIt("?foobarArray@@YAHQAH@Z"));
78 }
79 #endif
80 
81 #else
82 
83 // Test corner cases of bounary conditions.
84 TEST(Demangle, CornerCases) {
85  const size_t size = 10;
86  char tmp[size] = { 0 };
87  const char *demangled = "foobar()";
88  const char *mangled = "_Z6foobarv";
89  EXPECT_TRUE(Demangle(mangled, tmp, sizeof(tmp)));
90  // sizeof("foobar()") == size - 1
91  EXPECT_STREQ(demangled, tmp);
92  EXPECT_TRUE(Demangle(mangled, tmp, size - 1));
93  EXPECT_STREQ(demangled, tmp);
94  EXPECT_FALSE(Demangle(mangled, tmp, size - 2)); // Not enough.
95  EXPECT_FALSE(Demangle(mangled, tmp, 1));
96  EXPECT_FALSE(Demangle(mangled, tmp, 0));
97  EXPECT_FALSE(Demangle(mangled, NULL, 0)); // Should not cause SEGV.
98 }
99 
100 // Test handling of functions suffixed with .clone.N, which is used by GCC
101 // 4.5.x, and .constprop.N and .isra.N, which are used by GCC 4.6.x. These
102 // suffixes are used to indicate functions which have been cloned during
103 // optimization. We ignore these suffixes.
104 TEST(Demangle, Clones) {
105  char tmp[20];
106  EXPECT_TRUE(Demangle("_ZL3Foov", tmp, sizeof(tmp)));
107  EXPECT_STREQ("Foo()", tmp);
108  EXPECT_TRUE(Demangle("_ZL3Foov.clone.3", tmp, sizeof(tmp)));
109  EXPECT_STREQ("Foo()", tmp);
110  EXPECT_TRUE(Demangle("_ZL3Foov.constprop.80", tmp, sizeof(tmp)));
111  EXPECT_STREQ("Foo()", tmp);
112  EXPECT_TRUE(Demangle("_ZL3Foov.isra.18", tmp, sizeof(tmp)));
113  EXPECT_STREQ("Foo()", tmp);
114  EXPECT_TRUE(Demangle("_ZL3Foov.isra.2.constprop.18", tmp, sizeof(tmp)));
115  EXPECT_STREQ("Foo()", tmp);
116  // Invalid (truncated), should not demangle.
117  EXPECT_FALSE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
118  // Invalid (.clone. not followed by number), should not demangle.
119  EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
120  // Invalid (.clone. followed by non-number), should not demangle.
121  EXPECT_FALSE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
122  // Invalid (.constprop. not followed by number), should not demangle.
123  EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
124 }
125 
126 TEST(Demangle, FromFile) {
127  string test_file = FLAGS_test_srcdir + "/src/demangle_unittest.txt";
128  ifstream f(test_file.c_str()); // The file should exist.
129  EXPECT_FALSE(f.fail());
130 
131  string line;
132  while (getline(f, line)) {
133  // Lines start with '#' are considered as comments.
134  if (line.empty() || line[0] == '#') {
135  continue;
136  }
137  // Each line should contain a mangled name and a demangled name
138  // separated by '\t'. Example: "_Z3foo\tfoo"
139  string::size_type tab_pos = line.find('\t');
140  EXPECT_NE(string::npos, tab_pos);
141  string mangled = line.substr(0, tab_pos);
142  string demangled = line.substr(tab_pos + 1);
143  EXPECT_EQ(demangled, DemangleIt(mangled.c_str()));
144  }
145 }
146 
147 #endif
148 
149 int main(int argc, char **argv) {
150 #ifdef HAVE_LIB_GFLAGS
151  ParseCommandLineFlags(&argc, &argv, true);
152 #endif
153  InitGoogleTest(&argc, argv);
154 
155  FLAGS_logtostderr = true;
156  InitGoogleLogging(argv[0]);
157  if (FLAGS_demangle_filter) {
158  // Read from cin and write to cout.
159  string line;
160  while (getline(cin, line, '\n')) {
161  cout << DemangleIt(line.c_str()) << endl;
162  }
163  return 0;
164  } else if (argc > 1) {
165  cout << DemangleIt(argv[1]) << endl;
166  return 0;
167  } else {
168  return RUN_ALL_TESTS();
169  }
170 }
NULL
NULL
Definition: test_security_zap.cpp:405
TEST
TEST(Demangle, CornerCases)
Definition: demangle_unittest.cc:84
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
InitGoogleTest
_START_GOOGLE_NAMESPACE_ void InitGoogleTest(int *, char **)
Definition: glog/src/googletest.h:124
Demangle
bool Demangle(const char *mangled, char *out, size_t out_size)
Definition: demangle.cc:1327
main
int main(int argc, char **argv)
Definition: demangle_unittest.cc:149
google::protobuf.internal
Definition: python/google/protobuf/internal/__init__.py:1
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: glog/src/googletest.h:156
EXPECT_STREQ
#define EXPECT_STREQ(val1, val2)
Definition: glog/src/googletest.h:184
googletest.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2502
size
#define size
Definition: glcorearb.h:2944
FLAGS_logtostderr
static int FLAGS_logtostderr
Definition: sdk/include/aditof/log.h:67
EXPECT_TRUE
#define EXPECT_TRUE(cond)
Definition: glog/src/googletest.h:137
utilities.h
DemangleIt
static const char * DemangleIt(const char *const mangled)
Definition: demangle_unittest.cc:56
size
GLsizeiptr size
Definition: glcorearb.h:2943
std
EXPECT_FALSE
#define EXPECT_FALSE(cond)
Definition: glog/src/googletest.h:145
demangle.h
f
GLfloat f
Definition: glcorearb.h:3964
GLOG_DEFINE_bool
GLOG_DEFINE_bool(demangle_filter, false, "Run demangle_unittest in filter mode")
benchmark::internal::ParseCommandLineFlags
void ParseCommandLineFlags(int *argc, char **argv)
Definition: benchmark.cc:655
InitGoogleLogging
void InitGoogleLogging(const char *argv0)
Definition: logging.cc:2618


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49