1 // Copyright (c) 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: Satoru Takabayashi
32 // Unit tests for functions in demangle.c.
34 #include "utilities.h"
39 #include "glog/logging.h"
41 #include "googletest.h"
44 #ifdef HAVE_LIB_GFLAGS
45 #include <gflags/gflags.h>
46 using namespace GFLAGS_NAMESPACE;
49 GLOG_DEFINE_bool(demangle_filter, false,
50 "Run demangle_unittest in filter mode");
53 using namespace GOOGLE_NAMESPACE;
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))) {
65 // Test corner cases of bounary conditions.
66 TEST(Demangle, CornerCases) {
68 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, sizeof(tmp)));
69 // sizeof("foobar()") == 9
70 EXPECT_STREQ("foobar()", tmp);
71 EXPECT_TRUE(Demangle("_Z6foobarv", tmp, 9));
72 EXPECT_STREQ("foobar()", tmp);
73 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 8)); // Not enough.
74 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 1));
75 EXPECT_FALSE(Demangle("_Z6foobarv", tmp, 0));
76 EXPECT_FALSE(Demangle("_Z6foobarv", NULL, 0)); // Should not cause SEGV.
79 // Test handling of functions suffixed with .clone.N, which is used by GCC
80 // 4.5.x, and .constprop.N and .isra.N, which are used by GCC 4.6.x. These
81 // suffixes are used to indicate functions which have been cloned during
82 // optimization. We ignore these suffixes.
83 TEST(Demangle, Clones) {
85 EXPECT_TRUE(Demangle("_ZL3Foov", tmp, sizeof(tmp)));
86 EXPECT_STREQ("Foo()", tmp);
87 EXPECT_TRUE(Demangle("_ZL3Foov.clone.3", tmp, sizeof(tmp)));
88 EXPECT_STREQ("Foo()", tmp);
89 EXPECT_TRUE(Demangle("_ZL3Foov.constprop.80", tmp, sizeof(tmp)));
90 EXPECT_STREQ("Foo()", tmp);
91 EXPECT_TRUE(Demangle("_ZL3Foov.isra.18", tmp, sizeof(tmp)));
92 EXPECT_STREQ("Foo()", tmp);
93 EXPECT_TRUE(Demangle("_ZL3Foov.isra.2.constprop.18", tmp, sizeof(tmp)));
94 EXPECT_STREQ("Foo()", tmp);
95 // Invalid (truncated), should not demangle.
96 EXPECT_FALSE(Demangle("_ZL3Foov.clo", tmp, sizeof(tmp)));
97 // Invalid (.clone. not followed by number), should not demangle.
98 EXPECT_FALSE(Demangle("_ZL3Foov.clone.", tmp, sizeof(tmp)));
99 // Invalid (.clone. followed by non-number), should not demangle.
100 EXPECT_FALSE(Demangle("_ZL3Foov.clone.foo", tmp, sizeof(tmp)));
101 // Invalid (.constprop. not followed by number), should not demangle.
102 EXPECT_FALSE(Demangle("_ZL3Foov.isra.2.constprop.", tmp, sizeof(tmp)));
105 TEST(Demangle, FromFile) {
106 string test_file = FLAGS_test_srcdir + "/src/demangle_unittest.txt";
107 ifstream f(test_file.c_str()); // The file should exist.
108 EXPECT_FALSE(f.fail());
111 while (getline(f, line)) {
112 // Lines start with '#' are considered as comments.
113 if (line.empty() || line[0] == '#') {
116 // Each line should contain a mangled name and a demangled name
117 // separated by '\t'. Example: "_Z3foo\tfoo"
118 string::size_type tab_pos = line.find('\t');
119 EXPECT_NE(string::npos, tab_pos);
120 string mangled = line.substr(0, tab_pos);
121 string demangled = line.substr(tab_pos + 1);
122 EXPECT_EQ(demangled, DemangleIt(mangled.c_str()));
126 int main(int argc, char **argv) {
127 #ifdef HAVE_LIB_GFLAGS
128 ParseCommandLineFlags(&argc, &argv, true);
130 InitGoogleTest(&argc, argv);
132 FLAGS_logtostderr = true;
133 InitGoogleLogging(argv[0]);
134 if (FLAGS_demangle_filter) {
135 // Read from cin and write to cout.
137 while (getline(cin, line, '\n')) {
138 cout << DemangleIt(line.c_str()) << endl;
141 } else if (argc > 1) {
142 cout << DemangleIt(argv[1]) << endl;
145 return RUN_ALL_TESTS();