blob: a69e3dc4246bb886a34f17dfc6cf853e6b9ec1c1 [file] [log] [blame]
Enrico Granata5f9d3102015-08-27 21:33:50 +00001//===-- CPlusPlusLanguage.cpp --------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "CPlusPlusLanguage.h"
11
Jim Inghamaa816b82015-09-02 01:59:14 +000012#include <string.h>
13
14#include "llvm/ADT/StringRef.h"
15
Enrico Granata5f9d3102015-08-27 21:33:50 +000016#include "lldb/Core/ConstString.h"
17#include "lldb/Core/PluginManager.h"
Jim Inghamaa816b82015-09-02 01:59:14 +000018#include "lldb/Core/RegularExpression.h"
19#include "lldb/Core/UniqueCStringMap.h"
Enrico Granata5f9d3102015-08-27 21:33:50 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24void
25CPlusPlusLanguage::Initialize()
26{
27 PluginManager::RegisterPlugin (GetPluginNameStatic(),
28 "C++ Language",
29 CreateInstance);
30}
31
32void
33CPlusPlusLanguage::Terminate()
34{
35 PluginManager::UnregisterPlugin (CreateInstance);
36}
37
38lldb_private::ConstString
39CPlusPlusLanguage::GetPluginNameStatic()
40{
41 static ConstString g_name("cplusplus");
42 return g_name;
43}
44
45
46//------------------------------------------------------------------
47// PluginInterface protocol
48//------------------------------------------------------------------
49lldb_private::ConstString
50CPlusPlusLanguage::GetPluginName()
51{
52 return GetPluginNameStatic();
53}
54
55uint32_t
56CPlusPlusLanguage::GetPluginVersion()
57{
58 return 1;
59}
60
61//------------------------------------------------------------------
62// Static Functions
63//------------------------------------------------------------------
64Language *
65CPlusPlusLanguage::CreateInstance (lldb::LanguageType language)
66{
Enrico Granata578c8a72015-09-02 01:28:24 +000067 if (Language::LanguageIsCPlusPlus(language))
68 return new CPlusPlusLanguage();
69 return nullptr;
Enrico Granata5f9d3102015-08-27 21:33:50 +000070}
Jim Inghamaa816b82015-09-02 01:59:14 +000071
72void
73CPlusPlusLanguage::MethodName::Clear()
74{
75 m_full.Clear();
76 m_basename = llvm::StringRef();
77 m_context = llvm::StringRef();
78 m_arguments = llvm::StringRef();
79 m_qualifiers = llvm::StringRef();
80 m_type = eTypeInvalid;
81 m_parsed = false;
82 m_parse_error = false;
83}
84
85bool
86ReverseFindMatchingChars (const llvm::StringRef &s,
87 const llvm::StringRef &left_right_chars,
88 size_t &left_pos,
89 size_t &right_pos,
90 size_t pos = llvm::StringRef::npos)
91{
92 assert (left_right_chars.size() == 2);
93 left_pos = llvm::StringRef::npos;
94 const char left_char = left_right_chars[0];
95 const char right_char = left_right_chars[1];
96 pos = s.find_last_of(left_right_chars, pos);
97 if (pos == llvm::StringRef::npos || s[pos] == left_char)
98 return false;
99 right_pos = pos;
100 uint32_t depth = 1;
101 while (pos > 0 && depth > 0)
102 {
103 pos = s.find_last_of(left_right_chars, pos);
104 if (pos == llvm::StringRef::npos)
105 return false;
106 if (s[pos] == left_char)
107 {
108 if (--depth == 0)
109 {
110 left_pos = pos;
111 return left_pos < right_pos;
112 }
113 }
114 else if (s[pos] == right_char)
115 {
116 ++depth;
117 }
118 }
119 return false;
120}
121
122
123void
124CPlusPlusLanguage::MethodName::Parse()
125{
126 if (!m_parsed && m_full)
127 {
128// ConstString mangled;
129// m_full.GetMangledCounterpart(mangled);
130// printf ("\n parsing = '%s'\n", m_full.GetCString());
131// if (mangled)
132// printf (" mangled = '%s'\n", mangled.GetCString());
133 m_parse_error = false;
134 m_parsed = true;
135 llvm::StringRef full (m_full.GetCString());
136
137 size_t arg_start, arg_end;
138 llvm::StringRef parens("()", 2);
139 if (ReverseFindMatchingChars (full, parens, arg_start, arg_end))
140 {
141 m_arguments = full.substr(arg_start, arg_end - arg_start + 1);
142 if (arg_end + 1 < full.size())
143 m_qualifiers = full.substr(arg_end + 1);
144 if (arg_start > 0)
145 {
146 size_t basename_end = arg_start;
147 size_t context_start = 0;
148 size_t context_end = llvm::StringRef::npos;
149 if (basename_end > 0 && full[basename_end-1] == '>')
150 {
151 // TODO: handle template junk...
152 // Templated function
153 size_t template_start, template_end;
154 llvm::StringRef lt_gt("<>", 2);
155 if (ReverseFindMatchingChars (full, lt_gt, template_start, template_end, basename_end))
156 {
157 // Check for templated functions that include return type like: 'void foo<Int>()'
158 context_start = full.rfind(' ', template_start);
159 if (context_start == llvm::StringRef::npos)
160 context_start = 0;
161
162 context_end = full.rfind(':', template_start);
163 if (context_end == llvm::StringRef::npos || context_end < context_start)
164 context_end = context_start;
165 }
166 else
167 {
168 context_end = full.rfind(':', basename_end);
169 }
170 }
171 else if (context_end == llvm::StringRef::npos)
172 {
173 context_end = full.rfind(':', basename_end);
174 }
175
176 if (context_end == llvm::StringRef::npos)
177 m_basename = full.substr(0, basename_end);
178 else
179 {
180 if (context_start < context_end)
181 m_context = full.substr(context_start, context_end - 1);
182 const size_t basename_begin = context_end + 1;
183 m_basename = full.substr(basename_begin, basename_end - basename_begin);
184 }
185 m_type = eTypeUnknownMethod;
186 }
187 else
188 {
189 m_parse_error = true;
190 return;
191 }
192
193// if (!m_context.empty())
194// printf (" context = '%s'\n", m_context.str().c_str());
195// if (m_basename)
196// printf (" basename = '%s'\n", m_basename.GetCString());
197// if (!m_arguments.empty())
198// printf (" arguments = '%s'\n", m_arguments.str().c_str());
199// if (!m_qualifiers.empty())
200// printf ("qualifiers = '%s'\n", m_qualifiers.str().c_str());
201
202 // Make sure we have a valid C++ basename with optional template args
203 static RegularExpression g_identifier_regex("^~?([A-Za-z_][A-Za-z_0-9]*)(<.*>)?$");
204 std::string basename_str(m_basename.str());
205 bool basename_is_valid = g_identifier_regex.Execute (basename_str.c_str(), NULL);
206 if (!basename_is_valid)
207 {
208 // Check for C++ operators
209 if (m_basename.startswith("operator"))
210 {
211 static RegularExpression g_operator_regex("^(operator)( ?)([A-Za-z_][A-Za-z_0-9]*|\\(\\)|\\[\\]|[\\^<>=!\\/*+-]+)(<.*>)?(\\[\\])?$");
212 basename_is_valid = g_operator_regex.Execute(basename_str.c_str(), NULL);
213 }
214 }
215 if (!basename_is_valid)
216 {
217 // The C++ basename doesn't match our regular expressions so this can't
218 // be a valid C++ method, clear everything out and indicate an error
219 m_context = llvm::StringRef();
220 m_basename = llvm::StringRef();
221 m_arguments = llvm::StringRef();
222 m_qualifiers = llvm::StringRef();
223 m_parse_error = true;
224 }
225 }
226 else
227 {
228 m_parse_error = true;
229// printf ("error: didn't find matching parens for arguments\n");
230 }
231 }
232}
233
234llvm::StringRef
235CPlusPlusLanguage::MethodName::GetBasename ()
236{
237 if (!m_parsed)
238 Parse();
239 return m_basename;
240}
241
242llvm::StringRef
243CPlusPlusLanguage::MethodName::GetContext ()
244{
245 if (!m_parsed)
246 Parse();
247 return m_context;
248}
249
250llvm::StringRef
251CPlusPlusLanguage::MethodName::GetArguments ()
252{
253 if (!m_parsed)
254 Parse();
255 return m_arguments;
256}
257
258llvm::StringRef
259CPlusPlusLanguage::MethodName::GetQualifiers ()
260{
261 if (!m_parsed)
262 Parse();
263 return m_qualifiers;
264}
265
266bool
267CPlusPlusLanguage::IsCPPMangledName (const char *name)
268{
269 // FIXME, we should really run through all the known C++ Language plugins and ask each one if
270 // this is a C++ mangled name, but we can put that off till there is actually more than one
271 // we care about.
272
273 if (name && name[0] == '_' && name[1] == 'Z')
274 return true;
275 else
276 return false;
277}
278
279bool
280CPlusPlusLanguage::ExtractContextAndIdentifier (const char *name, llvm::StringRef &context, llvm::StringRef &identifier)
281{
282 static RegularExpression g_basename_regex("^(([A-Za-z_][A-Za-z_0-9]*::)*)([A-Za-z_][A-Za-z_0-9]*)$");
283 RegularExpression::Match match(4);
284 if (g_basename_regex.Execute (name, &match))
285 {
286 match.GetMatchAtIndex(name, 1, context);
287 match.GetMatchAtIndex(name, 3, identifier);
288 return true;
289 }
290 return false;
291}
292
293class CPPRuntimeEquivalents
294{
295public:
296 CPPRuntimeEquivalents ()
297 {
298
299 m_impl.Append(ConstString("std::basic_string<char, std::char_traits<char>, std::allocator<char> >").AsCString(), ConstString("basic_string<char>"));
300
301 // these two (with a prefixed std::) occur when c++stdlib string class occurs as a template argument in some STL container
302 m_impl.Append(ConstString("std::basic_string<char, std::char_traits<char>, std::allocator<char> >").AsCString(), ConstString("std::basic_string<char>"));
303
304 m_impl.Sort();
305 }
306
307 void
308 Add (ConstString& type_name,
309 ConstString& type_equivalent)
310 {
311 m_impl.Insert(type_name.AsCString(), type_equivalent);
312 }
313
314 uint32_t
315 FindExactMatches (ConstString& type_name,
316 std::vector<ConstString>& equivalents)
317 {
318
319 uint32_t count = 0;
320
321 for (ImplData match = m_impl.FindFirstValueForName(type_name.AsCString());
322 match != NULL;
323 match = m_impl.FindNextValueForName(match))
324 {
325 equivalents.push_back(match->value);
326 count++;
327 }
328
329 return count;
330 }
331
332 // partial matches can occur when a name with equivalents is a template argument.
333 // e.g. we may have "class Foo" be a match for "struct Bar". if we have a typename
334 // such as "class Templatized<class Foo, Anything>" we want this to be replaced with
335 // "class Templatized<struct Bar, Anything>". Since partial matching is time consuming
336 // once we get a partial match, we add it to the exact matches list for faster retrieval
337 uint32_t
338 FindPartialMatches (ConstString& type_name,
339 std::vector<ConstString>& equivalents)
340 {
341
342 uint32_t count = 0;
343
344 const char* type_name_cstr = type_name.AsCString();
345
346 size_t items_count = m_impl.GetSize();
347
348 for (size_t item = 0; item < items_count; item++)
349 {
350 const char* key_cstr = m_impl.GetCStringAtIndex(item);
351 if ( strstr(type_name_cstr,key_cstr) )
352 {
353 count += AppendReplacements(type_name_cstr,
354 key_cstr,
355 equivalents);
356 }
357 }
358
359 return count;
360
361 }
362
363private:
364
365 std::string& replace (std::string& target,
366 std::string& pattern,
367 std::string& with)
368 {
369 size_t pos;
370 size_t pattern_len = pattern.size();
371
372 while ( (pos = target.find(pattern)) != std::string::npos )
373 target.replace(pos, pattern_len, with);
374
375 return target;
376 }
377
378 uint32_t
379 AppendReplacements (const char* original,
380 const char *matching_key,
381 std::vector<ConstString>& equivalents)
382 {
383
384 std::string matching_key_str(matching_key);
385 ConstString original_const(original);
386
387 uint32_t count = 0;
388
389 for (ImplData match = m_impl.FindFirstValueForName(matching_key);
390 match != NULL;
391 match = m_impl.FindNextValueForName(match))
392 {
393 std::string target(original);
394 std::string equiv_class(match->value.AsCString());
395
396 replace (target, matching_key_str, equiv_class);
397
398 ConstString target_const(target.c_str());
399
400// you will most probably want to leave this off since it might make this map grow indefinitely
401#ifdef ENABLE_CPP_EQUIVALENTS_MAP_TO_GROW
402 Add(original_const, target_const);
403#endif
404 equivalents.push_back(target_const);
405
406 count++;
407 }
408
409 return count;
410 }
411
412 typedef UniqueCStringMap<ConstString> Impl;
413 typedef const Impl::Entry* ImplData;
414 Impl m_impl;
415};
416
417static CPPRuntimeEquivalents&
418GetEquivalentsMap ()
419{
420 static CPPRuntimeEquivalents g_equivalents_map;
421 return g_equivalents_map;
422}
423
424
425uint32_t
426CPlusPlusLanguage::FindEquivalentNames(ConstString type_name, std::vector<ConstString>& equivalents)
427{
428 uint32_t count = GetEquivalentsMap().FindExactMatches(type_name, equivalents);
429
430 bool might_have_partials=
431 ( count == 0 ) // if we have a full name match just use it
432 && (strchr(type_name.AsCString(), '<') != NULL // we should only have partial matches when templates are involved, check that we have
433 && strchr(type_name.AsCString(), '>') != NULL); // angle brackets in the type_name before trying to scan for partial matches
434
435 if ( might_have_partials )
436 count = GetEquivalentsMap().FindPartialMatches(type_name, equivalents);
437
438 return count;
439}
440