* config/sparc/sparc.h: Correct comment about availability of
[official-gcc.git] / libstdc++-v3 / testsuite / testsuite_abi.cc
blobb94b7eb2df035f6b5bc65ab43f72ed1dd2b70616
1 // -*- C++ -*-
3 // Copyright (C) 2004 Free Software Foundation, Inc.
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2, or (at
8 // your option) any later version.
10 // This library is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this library; see the file COPYING. If not, write to
17 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
18 // MA 02111-1307, USA.
20 // As a special exception, you may use this file as part of a free
21 // software library without restriction. Specifically, if other files
22 // instantiate templates or use macros or inline functions from this
23 // file, or you compile this file and link it with other files to
24 // produce an executable, this file does not by itself cause the
25 // resulting executable to be covered by the GNU General Public
26 // License. This exception does not however invalidate any other
27 // reasons why the executable file might be covered by the GNU General
28 // Public License.
30 // Benjamin Kosnik <bkoz@redhat.com>
32 #include "testsuite_abi.h"
33 #include <sstream>
34 #include <fstream>
35 #include <iostream>
37 using namespace std;
39 void
40 symbol::init(string& data)
42 const char delim = ':';
43 const char version_delim = '@';
44 const string::size_type npos = string::npos;
45 string::size_type n = 0;
47 // Set the type.
48 if (data.find("FUNC") == 0)
49 type = symbol::function;
50 else if (data.find("OBJECT") == 0)
51 type = symbol::object;
52 else
53 type = symbol::error;
54 n = data.find_first_of(delim);
55 if (n != npos)
56 data.erase(data.begin(), data.begin() + n + 1);
58 // Iff object, get size info.
59 if (type == symbol::object)
61 n = data.find_first_of(delim);
62 if (n != npos)
64 string size(data.begin(), data.begin() + n);
65 istringstream iss(size);
66 int x;
67 iss >> x;
68 if (!iss.fail())
69 size = x;
70 data.erase(data.begin(), data.begin() + n + 1);
74 // Set the name.
75 n = data.find_first_of(version_delim);
76 if (n != npos)
78 // Found version string.
79 name = string(data.begin(), data.begin() + n);
80 n = data.find_last_of(version_delim);
81 data.erase(data.begin(), data.begin() + n + 1);
83 // Set version name.
84 version_name = data;
86 else
88 // No versioning info.
89 name = string(data.begin(), data.end());
90 data.erase(data.begin(), data.end());
93 // Set the demangled name.
94 demangled_name = demangle(name);
97 void
98 symbol::print() const
100 const char tab = '\t';
101 cout << tab << name << endl;
102 cout << tab << demangled_name << endl;
103 cout << tab << version_name << endl;
105 string type_string;
106 switch (type)
108 case none:
109 type_string = "none";
110 break;
111 case function:
112 type_string = "function";
113 break;
114 case object:
115 type_string = "object";
116 break;
117 case error:
118 type_string = "error";
119 break;
120 default:
121 type_string = "<default>";
123 cout << tab << type_string << endl;
125 if (type == object)
126 cout << tab << size << endl;
128 string status_string;
129 switch (status)
131 case unknown:
132 status_string = "unknown";
133 break;
134 case added:
135 status_string = "added";
136 break;
137 case subtracted:
138 status_string = "subtracted";
139 break;
140 case compatible:
141 status_string = "compatible";
142 break;
143 case incompatible:
144 status_string = "incompatible";
145 break;
146 default:
147 status_string = "<default>";
149 cout << tab << status_string << endl;
153 bool
154 check_version(const symbol& test, bool added)
156 typedef std::vector<std::string> compat_list;
157 static compat_list known_versions;
158 if (known_versions.empty())
160 known_versions.push_back("GLIBCPP_3.2"); // base version
161 known_versions.push_back("GLIBCPP_3.2.1");
162 known_versions.push_back("GLIBCPP_3.2.2");
163 known_versions.push_back("GLIBCPP_3.2.3"); // gcc-3.3.0
164 known_versions.push_back("GLIBCXX_3.4");
165 known_versions.push_back("GLIBCXX_3.4.1");
166 known_versions.push_back("CXXABI_1.2");
167 known_versions.push_back("CXXABI_1.2.1");
168 known_versions.push_back("CXXABI_1.3");
170 compat_list::iterator begin = known_versions.begin();
171 compat_list::iterator end = known_versions.end();
173 // Check version names for compatibility...
174 compat_list::iterator it1 = find(begin, end, test.version_name);
176 // Check for weak label.
177 compat_list::iterator it2 = find(begin, end, test.name);
179 // Check that added symbols aren't added in the base version.
180 bool compat = true;
181 if (added && test.version_name == known_versions[0])
182 compat = false;
184 if (it1 == end && it2 == end)
185 compat = false;
187 return compat;
190 bool
191 check_compatible(const symbol& lhs, const symbol& rhs, bool verbose)
193 bool ret = true;
194 const char tab = '\t';
196 // Check to see if symbol_objects are compatible.
197 if (lhs.type != rhs.type)
199 ret = false;
200 if (verbose)
201 cout << tab << "incompatible types" << endl;
204 if (lhs.name != rhs.name)
206 ret = false;
207 if (verbose)
208 cout << tab << "incompatible names" << endl;
211 if (lhs.size != rhs.size)
213 ret = false;
214 if (verbose)
216 cout << tab << "incompatible sizes" << endl;
217 cout << tab << lhs.size << endl;
218 cout << tab << rhs.size << endl;
222 if (lhs.version_name != rhs.version_name
223 && !check_version(lhs) && !check_version(rhs))
225 ret = false;
226 if (verbose)
228 cout << tab << "incompatible versions" << endl;
229 cout << tab << lhs.version_name << endl;
230 cout << tab << rhs.version_name << endl;
234 if (verbose)
235 cout << endl;
237 return ret;
241 bool
242 has_symbol(const string& mangled, const symbols& s) throw()
244 const symbol_names& names = s.first;
245 symbol_names::const_iterator i = find(names.begin(), names.end(), mangled);
246 return i != names.end();
249 symbol&
250 get_symbol(const string& mangled, const symbols& s)
252 const symbol_names& names = s.first;
253 symbol_names::const_iterator i = find(names.begin(), names.end(), mangled);
254 if (i != names.end())
256 symbol_objects objects = s.second;
257 return objects[mangled];
259 else
261 ostringstream os;
262 os << "get_symbol failed for symbol " << mangled;
263 throw symbol_error(os.str());
267 void
268 examine_symbol(const char* name, const char* file)
272 symbols s = create_symbols(file);
273 symbol& sym = get_symbol(name, s);
274 sym.print();
276 catch(...)
277 { throw; }
280 void
281 compare_symbols(const char* baseline_file, const char* test_file,
282 bool verbose)
284 // Input both lists of symbols into container.
285 symbols baseline = create_symbols(baseline_file);
286 symbols test = create_symbols(test_file);
287 symbol_names& baseline_names = baseline.first;
288 symbol_objects& baseline_objects = baseline.second;
289 symbol_names& test_names = test.first;
290 symbol_objects& test_objects = test.second;
292 // Sanity check results.
293 const symbol_names::size_type baseline_size = baseline_names.size();
294 const symbol_names::size_type test_size = test_names.size();
295 if (!baseline_size || !test_size)
297 cerr << "Problems parsing the list of exported symbols." << endl;
298 exit(2);
301 // Sort out names.
302 // Assuming baseline_names, test_names are both unique w/ no duplicates.
304 // The names added to missing_names are baseline_names not found in
305 // test_names
306 // -> symbols that have been deleted.
308 // The names added to added_names are test_names are names not in
309 // baseline_names
310 // -> symbols that have been added.
311 symbol_names shared_names;
312 symbol_names missing_names;
313 symbol_names added_names = test_names;
314 for (size_t i = 0; i < baseline_size; ++i)
316 string what(baseline_names[i]);
317 symbol_names::iterator end = added_names.end();
318 symbol_names::iterator it = find(added_names.begin(), end, what);
319 if (it != end)
321 // Found.
322 shared_names.push_back(what);
323 added_names.erase(it);
325 else
326 missing_names.push_back(what);
329 // Check missing names for compatibility.
330 typedef pair<symbol, symbol> symbol_pair;
331 vector<symbol_pair> incompatible;
332 for (size_t j = 0; j < missing_names.size(); ++j)
334 symbol base = baseline_objects[missing_names[j]];
335 incompatible.push_back(symbol_pair(base, base));
338 // Check shared names for compatibility.
339 for (size_t k = 0; k < shared_names.size(); ++k)
341 symbol base = baseline_objects[shared_names[k]];
342 symbol test = test_objects[shared_names[k]];
343 if (!check_compatible(base, test))
344 incompatible.push_back(symbol_pair(base, test));
347 // Check added names for compatibility.
348 for (size_t l = 0; l < added_names.size(); ++l)
350 symbol test = test_objects[added_names[l]];
351 if (!check_version(test, true))
352 incompatible.push_back(symbol_pair(test, test));
355 // Report results.
356 if (verbose && added_names.size())
358 cout << added_names.size() << " added symbols " << endl;
359 for (size_t j = 0; j < added_names.size() ; ++j)
360 test_objects[added_names[j]].print();
363 if (verbose && missing_names.size())
365 cout << missing_names.size() << " missing symbols " << endl;
366 for (size_t j = 0; j < missing_names.size() ; ++j)
367 baseline_objects[missing_names[j]].print();
370 if (verbose && incompatible.size())
372 cout << incompatible.size() << " incompatible symbols " << endl;
373 for (size_t j = 0; j < incompatible.size() ; ++j)
375 // First, report name.
376 const symbol& base = incompatible[j].first;
377 const symbol& test = incompatible[j].second;
378 test.print();
380 // Second, report reason or reasons incompatible.
381 check_compatible(base, test, true);
385 cout << "\n\t\t=== libstdc++-v3 check-abi Summary ===" << endl;
386 cout << endl;
387 cout << "# of added symbols:\t\t " << added_names.size() << endl;
388 cout << "# of missing symbols:\t\t " << missing_names.size() << endl;
389 cout << "# of incompatible symbols:\t " << incompatible.size() << endl;
390 cout << endl;
391 cout << "using: " << baseline_file << endl;
395 symbols
396 create_symbols(const char* file)
398 symbols s;
399 ifstream ifs(file);
400 if (ifs.is_open())
402 // Organize file data into container of symbol objects.
403 symbol_names& names = s.first;
404 symbol_objects& objects = s.second;
405 const string empty;
406 string line = empty;
407 while (getline(ifs, line).good())
409 symbol tmp;
410 tmp.init(line);
411 objects[tmp.name] = tmp;
412 names.push_back(tmp.name);
413 line = empty;
416 else
418 ostringstream os;
419 os << "create_symbols failed for file " << file;
420 throw runtime_error(os.str());
422 return s;
426 const char*
427 demangle(const std::string& mangled)
429 const char* name;
430 if (mangled[0] != '_' || mangled[1] != 'Z')
432 // This is not a mangled symbol, thus has "C" linkage.
433 name = mangled.c_str();
435 else
437 // Use __cxa_demangle to demangle.
438 int status = 0;
439 name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
440 if (!name)
442 switch (status)
444 case 0:
445 name = "error code = 0: success";
446 break;
447 case -1:
448 name = "error code = -1: memory allocation failure";
449 break;
450 case -2:
451 name = "error code = -2: invalid mangled name";
452 break;
453 case -3:
454 name = "error code = -3: invalid arguments";
455 break;
456 default:
457 name = "error code unknown - who knows what happened";
461 return name;