OSDN Git Service

2002-09-16 Phil Edwards <pme@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / abi_check.cc
1 // Utility for libstdc++ ABI analysis -*- C++ -*-
2
3 // Copyright (C) 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // Benjamin Kosnik  <bkoz@redhat.com>
31 // Blame subsequent hacks on Loren J. Rittle <ljrittle@acm.org>, Phil
32 // Edwards <pme@gcc.gnu.org>, and a cast of dozens at libstdc++@gcc.gnu.org.
33
34 #include <string>
35 #include <ext/hash_map>
36 #include <deque>
37 #include <sstream>
38 #include <fstream>
39 #include <iostream>
40 #include <cxxabi.h>
41 #include <stdlib.h>    // for system(3)
42 #include <unistd.h>    // for access(2)
43
44 struct symbol_info
45 {
46   enum category { none, function, object, error };
47   category      type;
48   std::string   name;
49   std::string   name_demangled;
50   std::string   version;
51   int           size;
52
53   symbol_info() : type(none), size(0) { }
54
55   symbol_info(const symbol_info& other) 
56   : type(other.type), name(other.name), name_demangled(other.name_demangled), 
57   version(other.version), size(other.size) { }
58 };
59
60 bool 
61 check_compatible(const symbol_info& lhs, const symbol_info& rhs, 
62                  bool verbose = false)
63 {
64   using namespace std;
65   bool ret = true;
66   const char tab = '\t';
67
68   // Check to see if symbol_infos are compatible.
69   if (lhs.type != rhs.type)
70     {
71       ret = false;
72       if (verbose)
73         {
74           cout << tab << "incompatible types" << endl;
75         }
76     }
77   
78   if (lhs.name != rhs.name)
79     {
80       ret = false;
81       if (verbose)
82         {
83           cout << tab << "incompatible names" << endl;
84         }
85     }
86
87   if (lhs.size != rhs.size)
88     {
89       ret = false;
90       if (verbose)
91         {
92           cout << tab << "incompatible sizes" << endl;
93         }
94     }
95
96   // Expect something more sophisticated eventually.
97   if (lhs.version != rhs.version)
98     {
99       ret = false;
100       if (verbose)
101         {
102           cout << tab << "incompatible versions" << endl;
103         }
104     }
105
106   return ret;
107 }
108
109 template<typename _CharT, typename _Traits>
110   std::basic_ostream<_CharT, _Traits>&
111   operator<<(std::basic_ostream<_CharT, _Traits>& os, symbol_info& si)
112   {
113     using namespace std;
114     os << si.type << endl;
115     os << si.name << endl;
116     os << si.name_demangled << endl;
117     os << si.version << endl;
118     os << si.size << endl;
119     return os;
120   }
121  
122 const char*
123 demangle(const std::string& mangled)
124 {
125   const char* name;
126   if (mangled[0] != '_' && mangled[1] != 'Z')
127     {
128       // This is not a mangled symbol, thus has "C" linkage.
129       name = mangled.c_str();
130     }
131   else
132     {
133       // Use __cxa_demangle to demangle.
134       int status = 0;
135       name = abi::__cxa_demangle(mangled.c_str(), 0, 0, &status);
136       if (!name)
137         {
138           switch (status)
139             {
140             case 0:
141               name = "error code = 0: success";
142               break;
143             case -1:
144               name = "error code = -1: memory allocation failure";
145               break;
146             case -2:
147               name = "error code = -2: invalid mangled name";
148               break;
149             case -3:
150               name = "error code = -3: invalid arguments";
151               break;
152             default:
153               name = "error code unknown - who knows what happened";
154             }
155         }
156     }
157   return name;
158 }
159
160 void 
161 line_to_symbol_info(std::string& input, symbol_info& output)
162 {
163   using namespace std;
164   const char delim = ':';
165   const char version_delim = '@';
166   const string::size_type npos = string::npos;
167   string::size_type n = 0;
168
169   // Set the type.
170   if (input.find("FUNC") == 0)
171     output.type = symbol_info::function;
172   else if (input.find("OBJECT") == 0)
173     output.type = symbol_info::object;
174   else
175     output.type = symbol_info::error;
176   n = input.find_first_of(delim);
177   if (n != npos)
178     input.erase(input.begin(), input.begin() + n + 1);
179
180   // Iff object, get size info.
181   if (output.type == symbol_info::object)
182     {
183       n = input.find_first_of(delim);
184       if (n != npos)
185         {
186           string size(input.begin(), input.begin() + n);
187           istringstream iss(size);
188           int x;
189           iss >> x;
190           if (iss.good())
191             output.size = x;
192           input.erase(input.begin(), input.begin() + n + 1);
193         }
194     }
195
196   // Set the name.
197   n = input.find_first_of(version_delim);
198   if (n != npos)
199     {
200       // Found version string.
201       output.name = string(input.begin(), input.begin() + n);
202       n = input.find_last_of(version_delim);
203       input.erase(input.begin(), input.begin() + n + 1);
204
205       // Set version name.
206       output.version = input;
207     }
208   else
209     {
210       // No versioning info.
211       output.name = string(input.begin(), input.end());
212       input.erase(input.begin(), input.end());
213     }
214
215   // Set the demangled name.
216   output.name_demangled = demangle(output.name);
217 }
218
219 typedef std::deque<std::string>                         symbol_names;
220 typedef __gnu_cxx::hash_map<const char*, symbol_info>   symbol_infos;
221
222 void
223 create_symbol_data(const char* file, symbol_infos& symbols, 
224                    symbol_names& names)
225 {
226   // Parse list of symbols in file into vectors of symbol_info.
227   // For 3.2.0 on x86/linux, this usually is
228   // 947 non-weak symbols
229   // 2084 weak symbols
230   using namespace std;
231   ifstream ifs(file); 
232   if (ifs.is_open())
233     {
234       // Organize input into container of symbol_info objects.
235       const string empty;
236       string line = empty;
237       while (getline(ifs, line).good())
238         {
239           symbol_info symbol;
240           line_to_symbol_info(line, symbol);
241           symbols[symbol.name.c_str()] = symbol;
242           names.push_back(symbol.name);
243           line = empty;
244         }
245     }
246 }
247
248 void
249 report_symbol_info(const symbol_info& symbol, std::size_t n)
250 {
251   using namespace std;
252   const char tab = '\t';
253   cout << tab << n << endl;
254   cout << tab << "symbol"<< endl;
255   cout << tab << symbol.name << endl;
256
257   // Add any other information to display here.
258   cout << tab << "demangled symbol"<< endl;
259   cout << tab << symbol.name_demangled << endl;
260
261   cout << endl;
262 }
263
264
265 int
266 main(int argc, char** argv)
267 {
268   using namespace std;
269
270   // Get arguments.  (Heading towards getopt_long, I can feel it.)
271   string argv1;
272   if (argc < 4 || (string("--help") == (argv1 = argv[1])))
273     {
274       cerr << "Usage:  abi_check --check    cur baseline\n"
275               "                  --help\n\n"
276               "Where CUR is a file containing the current results from\n"
277               "extract_symvers, and BASELINE is one from config/abi.\n"
278            << endl;
279       exit(1);
280     }
281
282   const char* test_file = argv[2];
283   const char* baseline_file = argv[3];
284
285   // Quick sanity/setup check
286   if (access(test_file, R_OK) != 0)
287     {
288       cerr << "Cannot read symbols file " << test_file
289            << ", did you forget to build first?" << endl;
290       exit(1);
291     }
292   if (access(baseline_file, R_OK) != 0)
293     {
294       cerr << "Cannot read baseline file " << baseline_file << endl;
295       exit(1);
296     }
297
298   // Input both lists of symbols into container.
299   symbol_infos  baseline_symbols;
300   symbol_names  baseline_names;
301   symbol_infos  test_symbols;
302   symbol_names  test_names;
303   create_symbol_data(baseline_file, baseline_symbols, baseline_names);
304   create_symbol_data(test_file, test_symbols, test_names);
305
306   // More sanity checking.
307   const symbol_names::size_type baseline_size = baseline_names.size();
308   const symbol_names::size_type test_size = test_names.size();
309   if (!baseline_size || !test_size)
310     {
311       cerr << "Problems parsing the list of exported symbols." << endl;
312       exit(2);
313     }
314
315   // Sort out names.
316   // Assuming baseline_names, test_names are both unique w/ no duplicates.
317   //
318   // The pairs of names in shared_names are needed to do lookups on
319   // the hash tables of common symbols to do compares.
320   //
321   // The names added to missing_names are baseline_names not found in
322   // test_names 
323   // -> symbols that have been deleted.
324   //
325   // The names left in test_names are names not in baseline_names
326   // -> symbols that have been added.
327   typedef pair<string, string> string_pair;
328   vector<string_pair> shared_names;
329   symbol_names missing_names;
330   for (size_t i = 0; i < baseline_size; ++i)
331     {
332       symbol_names::iterator end = test_names.end();
333       symbol_names::iterator it = find(test_names.begin(), end, 
334                                        baseline_names[i]);
335       if (it != end)
336         {
337           // Found.
338           shared_names.push_back(string_pair(baseline_names[i], *it));
339           test_names.erase(it);
340         }
341       else
342         missing_names.push_back(baseline_names[i]);
343     }
344
345   // Check common names for detailed compatibility.
346   const vector<string_pair>::size_type shared_size = shared_names.size();
347   typedef pair<symbol_info, symbol_info> symbol_pair;
348   vector<symbol_pair> incompatible;
349   for (size_t i = 0; i < shared_size; ++i)
350     {
351       symbol_info base = baseline_symbols[shared_names[i].first.c_str()];
352       symbol_info test = test_symbols[shared_names[i].second.c_str()];
353       if (!check_compatible(base, test))
354         incompatible.push_back(symbol_pair(base, test));
355     }
356
357   // Report results.
358   cout << test_names.size() << " added symbols " << endl;
359   for (size_t j = 0; j < test_names.size() ; ++j)
360     report_symbol_info(test_symbols[test_names[j].c_str()], j + 1);
361
362   cout << missing_names.size() << " missing symbols " << endl;
363   for (size_t j = 0; j < missing_names.size() ; ++j)
364     report_symbol_info(baseline_symbols[missing_names[j].c_str()], j + 1);
365
366   cout << incompatible.size() << " incompatible symbols " << endl;
367   for (size_t j = 0; j < incompatible.size() ; ++j)
368     {
369       // First, report name.
370       const symbol_info& base = incompatible[j].first;
371       const symbol_info& test = incompatible[j].second;
372       report_symbol_info(test, j + 1);
373
374       // Second, report reason or reasons incompatible.
375       check_compatible(base, test, true);
376     }
377
378   return 0;
379 }