blob: ce31c15678ad4573c98a43ef536e3cb7f115e98e [file] [log] [blame]
Greg Clayton73844aa2012-08-22 17:17:09 +00001//===-- OptionValueProperties.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 "lldb/Interpreter/OptionValueProperties.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/Flags.h"
17#include "lldb/Core/Stream.h"
18#include "lldb/Core/StringList.h"
19#include "lldb/Core/UserSettingsController.h"
20#include "lldb/Interpreter/Args.h"
21#include "lldb/Interpreter/OptionValues.h"
22#include "lldb/Interpreter/Property.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
27
28OptionValueProperties::OptionValueProperties (const ConstString &name) :
29 m_name (name)
30{
31}
32
33OptionValueProperties::OptionValueProperties (const OptionValueProperties &global_properties) :
34 m_name (global_properties.m_name),
35 m_properties (global_properties.m_properties),
36 m_name_to_index (global_properties.m_name_to_index)
37{
38 // We now have an exact copy of "global_properties". We need to now
39 // find all non-global settings and copy the property values so that
40 // all non-global settings get new OptionValue instances created for
41 // them.
42 const size_t num_properties = m_properties.size();
43 for (size_t i=0; i<num_properties; ++i)
44 {
45 // Duplicate any values that are not global when contructing properties from
46 // a global copy
47 if (m_properties[i].IsGlobal() == false)
48 {
49 lldb::OptionValueSP new_value_sp (m_properties[i].GetValue()->DeepCopy());
50 m_properties[i].SetOptionValue(new_value_sp);
51 }
52 }
53}
54
55
56
57size_t
58OptionValueProperties::GetNumProperties() const
59{
60 return m_properties.size();
61}
62
63
64void
65OptionValueProperties::Initialize (const PropertyDefinition *defs)
66{
67 for (size_t i=0; defs[i].name; ++i)
68 {
69 Property property(defs[i]);
70 assert(property.IsValid());
71 m_name_to_index.Append(property.GetName().GetCString(),m_properties.size());
72 property.GetValue()->SetParent(shared_from_this());
73 m_properties.push_back(property);
74 }
75 m_name_to_index.Sort();
76}
77
78void
79OptionValueProperties::AppendProperty(const ConstString &name,
80 const ConstString &desc,
81 bool is_global,
82 const OptionValueSP &value_sp)
83{
84 Property property(name, desc, is_global, value_sp);
85 m_name_to_index.Append(name.GetCString(),m_properties.size());
86 m_properties.push_back(property);
87 value_sp->SetParent (shared_from_this());
88 m_name_to_index.Sort();
89}
90
91
92
93//bool
94//OptionValueProperties::GetQualifiedName (Stream &strm)
95//{
96// bool dumped_something = false;
97//// lldb::OptionValuePropertiesSP parent_sp(GetParent ());
98//// if (parent_sp)
99//// {
100//// parent_sp->GetQualifiedName (strm);
101//// strm.PutChar('.');
102//// dumped_something = true;
103//// }
104// if (m_name)
105// {
106// strm << m_name;
107// dumped_something = true;
108// }
109// return dumped_something;
110//}
111//
112lldb::OptionValueSP
113OptionValueProperties::GetValueForKey (const ExecutionContext *exe_ctx,
114 const ConstString &key,
115 bool will_modify) const
116{
117 lldb::OptionValueSP value_sp;
118 size_t idx = m_name_to_index.Find (key.GetCString(), SIZE_MAX);
119 if (idx < m_properties.size())
120 value_sp = GetPropertyAtIndex(exe_ctx, will_modify, idx)->GetValue();
121 return value_sp;
122}
123
124lldb::OptionValueSP
125OptionValueProperties::GetSubValue (const ExecutionContext *exe_ctx,
126 const char *name,
127 bool will_modify,
128 Error &error) const
129{
130 lldb::OptionValueSP value_sp;
131
132 if (name && name[0])
133 {
134 const char *sub_name = NULL;
135 ConstString key;
136 size_t key_len = ::strcspn (name, ".[{");
137
138 if (name[key_len])
139 {
140 key.SetCStringWithLength (name, key_len);
141 sub_name = name + key_len;
142 }
143 else
144 key.SetCString (name);
145
146 value_sp = GetValueForKey (exe_ctx, key, will_modify);
147 if (sub_name && value_sp)
148 {
149 switch (sub_name[0])
150 {
151 case '.':
152 return value_sp->GetSubValue (exe_ctx, sub_name + 1, will_modify, error);
153
154 case '{':
155 // Predicate matching for predicates like
156 // "<setting-name>{<predicate>}"
157 // strings are parsed by the current OptionValueProperties subclass
158 // to mean whatever they want to. For instance a subclass of
159 // OptionValueProperties for a lldb_private::Target might implement:
160 // "target.run-args{arch==i386}" -- only set run args if the arch is i386
161 // "target.run-args{path=/tmp/a/b/c/a.out}" -- only set run args if the path matches
162 // "target.run-args{basename==test&&arch==x86_64}" -- only set run args if exectable basename is "test" and arch is "x86_64"
163 if (sub_name[1])
164 {
165 const char *predicate_start = sub_name + 1;
166 const char *predicate_end = strchr(predicate_start, '}');
167 if (predicate_end)
168 {
169 std::string predicate(predicate_start, predicate_end);
170 if (PredicateMatches(exe_ctx, predicate.c_str()))
171 {
172 if (predicate_end[1])
173 {
174 // Still more subvalue string to evaluate
175 return value_sp->GetSubValue (exe_ctx, predicate_end + 1, will_modify, error);
176 }
177 else
178 {
179 // We have a match!
180 break;
181 }
182 }
183 }
184 }
185 // Predicate didn't match or wasn't correctly formed
186 value_sp.reset();
187 break;
188
189 case '[':
190 // Array or dictionary access for subvalues like:
191 // "[12]" -- access 12th array element
192 // "['hello']" -- dictionary access of key named hello
193 return value_sp->GetSubValue (exe_ctx, sub_name, will_modify, error);
194
195 default:
196 value_sp.reset();
197 break;
198 }
199 }
200 }
201 return value_sp;
202}
203
204Error
205OptionValueProperties::SetSubValue (const ExecutionContext *exe_ctx,
206 VarSetOperationType op,
207 const char *name,
208 const char *value)
209{
210 Error error;
211 const bool will_modify = true;
212 lldb::OptionValueSP value_sp (GetSubValue (exe_ctx, name, will_modify, error));
213 if (value_sp)
214 error = value_sp->SetValueFromCString(value, op);
215 else
216 {
217 if (error.AsCString() == NULL)
218 error.SetErrorStringWithFormat("invalid value path '%s'", name);
219 }
220 return error;
221}
222
223
224ConstString
225OptionValueProperties::GetPropertyNameAtIndex (uint32_t idx) const
226{
227 const Property *property = GetPropertyAtIndex(NULL, false, idx);
228 if (property)
229 return property->GetName();
230 return ConstString();
231
232}
233
234const char *
235OptionValueProperties::GetPropertyDescriptionAtIndex (uint32_t idx) const
236{
237 const Property *property = GetPropertyAtIndex(NULL, false, idx);
238 if (property)
239 return property->GetDescription();
240 return NULL;
241}
242
243uint32_t
244OptionValueProperties::GetPropertyIndex (const ConstString &name) const
245{
246 return m_name_to_index.Find (name.GetCString(), SIZE_MAX);
247}
248
249const Property *
250OptionValueProperties::GetProperty (const ExecutionContext *exe_ctx, bool will_modify, const ConstString &name) const
251{
252 return GetPropertyAtIndex (exe_ctx, will_modify, m_name_to_index.Find (name.GetCString(), SIZE_MAX));
253}
254
255const Property *
256OptionValueProperties::GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
257{
258 return ProtectedGetPropertyAtIndex (idx);
259}
260
261lldb::OptionValueSP
262OptionValueProperties::GetPropertyValueAtIndex (const ExecutionContext *exe_ctx,
263 bool will_modify,
264 uint32_t idx) const
265{
266 const Property *setting = GetPropertyAtIndex (exe_ctx, will_modify, idx);
267 if (setting)
268 return setting->GetValue();
269 return OptionValueSP();
270}
271
272OptionValuePathMappings *
273OptionValueProperties::GetPropertyAtIndexAsOptionValuePathMappings (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
274{
275 OptionValueSP value_sp(GetPropertyValueAtIndex (exe_ctx, will_modify, idx));
276 if (value_sp)
277 return value_sp->GetAsPathMappings();
278 return NULL;
279}
280
281OptionValueFileSpecList *
282OptionValueProperties::GetPropertyAtIndexAsOptionValueFileSpecList (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
283{
284 OptionValueSP value_sp(GetPropertyValueAtIndex (exe_ctx, will_modify, idx));
285 if (value_sp)
286 return value_sp->GetAsFileSpecList();
287 return NULL;
288}
289
290OptionValueArch *
291OptionValueProperties::GetPropertyAtIndexAsOptionValueArch (const ExecutionContext *exe_ctx, uint32_t idx) const
292{
293 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
294 if (property)
295 return property->GetValue()->GetAsArch();
296 return NULL;
297}
298
299bool
300OptionValueProperties::GetPropertyAtIndexAsArgs (const ExecutionContext *exe_ctx, uint32_t idx, Args &args) const
301{
302 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
303 if (property)
304 {
305 OptionValue *value = property->GetValue().get();
306 if (value)
307 {
308 const OptionValueArray *array = value->GetAsArray();
309 if (array)
310 return array->GetArgs(args);
311 else
312 {
313 const OptionValueDictionary *dict = value->GetAsDictionary();
314 if (dict)
315 return dict->GetArgs(args);
316 }
317 }
318 }
319 return false;
320}
321
322bool
323OptionValueProperties::SetPropertyAtIndexFromArgs (const ExecutionContext *exe_ctx, uint32_t idx, const Args &args)
324{
325 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
326 if (property)
327 {
328 OptionValue *value = property->GetValue().get();
329 if (value)
330 {
331 OptionValueArray *array = value->GetAsArray();
332 if (array)
333 return array->SetArgs(args, eVarSetOperationAssign).Success();
334 else
335 {
336 OptionValueDictionary *dict = value->GetAsDictionary();
337 if (dict)
338 return dict->SetArgs(args, eVarSetOperationAssign).Success();
339 }
340 }
341 }
342 return false;
343}
344
345bool
346OptionValueProperties::GetPropertyAtIndexAsBoolean (const ExecutionContext *exe_ctx, uint32_t idx, bool fail_value) const
347{
348 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
349 if (property)
350 {
351 OptionValue *value = property->GetValue().get();
352 if (value)
353 return value->GetBooleanValue(fail_value);
354 }
355 return fail_value;
356}
357
358bool
359OptionValueProperties::SetPropertyAtIndexAsBoolean (const ExecutionContext *exe_ctx, uint32_t idx, bool new_value)
360{
361 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
362 if (property)
363 {
364 OptionValue *value = property->GetValue().get();
365 if (value)
366 {
367 value->SetBooleanValue(new_value);
368 return true;
369 }
370 }
371 return false;
372}
373
374OptionValueDictionary *
375OptionValueProperties::GetPropertyAtIndexAsOptionValueDictionary (const ExecutionContext *exe_ctx, uint32_t idx) const
376{
377 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
378 if (property)
379 return property->GetValue()->GetAsDictionary();
380 return NULL;
381}
382
383int64_t
384OptionValueProperties::GetPropertyAtIndexAsEnumeration (const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const
385{
386 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
387 if (property)
388 {
389 OptionValue *value = property->GetValue().get();
390 if (value)
391 return value->GetEnumerationValue(fail_value);
392 }
393 return fail_value;
394}
395
396bool
397OptionValueProperties::SetPropertyAtIndexAsEnumeration (const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value)
398{
399 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
400 if (property)
401 {
402 OptionValue *value = property->GetValue().get();
403 if (value)
404 return value->SetEnumerationValue(new_value);
405 }
406 return false;
407}
408
409
410FileSpec
411OptionValueProperties::GetPropertyAtIndexAsFileSpec (const ExecutionContext *exe_ctx, uint32_t idx) const
412{
413 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
414 if (property)
415 {
416 OptionValue *value = property->GetValue().get();
417 if (value)
418 return value->GetFileSpecValue();
419 }
420 return FileSpec();
421}
422
423
424bool
425OptionValueProperties::SetPropertyAtIndexAsFileSpec (const ExecutionContext *exe_ctx, uint32_t idx, const FileSpec &new_file_spec)
426{
427 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
428 if (property)
429 {
430 OptionValue *value = property->GetValue().get();
431 if (value)
432 return value->SetFileSpecValue(new_file_spec);
433 }
434 return false;
435}
436
437const RegularExpression *
438OptionValueProperties::GetPropertyAtIndexAsOptionValueRegex (const ExecutionContext *exe_ctx, uint32_t idx) const
439{
440 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
441 if (property)
442 {
443 OptionValue *value = property->GetValue().get();
444 if (value)
445 return value->GetRegexValue();
446 }
447 return NULL;
448}
449
450OptionValueSInt64 *
451OptionValueProperties::GetPropertyAtIndexAsOptionValueSInt64 (const ExecutionContext *exe_ctx, uint32_t idx) const
452{
453 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
454 if (property)
455 {
456 OptionValue *value = property->GetValue().get();
457 if (value)
458 return value->GetAsSInt64();
459 }
460 return NULL;
461}
462
463int64_t
464OptionValueProperties::GetPropertyAtIndexAsSInt64 (const ExecutionContext *exe_ctx, uint32_t idx, int64_t fail_value) const
465{
466 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
467 if (property)
468 {
469 OptionValue *value = property->GetValue().get();
470 if (value)
471 return value->GetSInt64Value(fail_value);
472 }
473 return fail_value;
474}
475
476bool
477OptionValueProperties::SetPropertyAtIndexAsSInt64 (const ExecutionContext *exe_ctx, uint32_t idx, int64_t new_value)
478{
479 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
480 if (property)
481 {
482 OptionValue *value = property->GetValue().get();
483 if (value)
484 return value->SetSInt64Value(new_value);
485 }
486 return false;
487}
488
489const char *
490OptionValueProperties::GetPropertyAtIndexAsString (const ExecutionContext *exe_ctx, uint32_t idx, const char *fail_value) const
491{
492 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
493 if (property)
494 {
495 OptionValue *value = property->GetValue().get();
496 if (value)
497 return value->GetStringValue(fail_value);
498 }
499 return fail_value;
500}
501
502bool
503OptionValueProperties::SetPropertyAtIndexAsString (const ExecutionContext *exe_ctx, uint32_t idx, const char *new_value)
504{
505 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
506 if (property)
507 {
508 OptionValue *value = property->GetValue().get();
509 if (value)
510 return value->SetStringValue(new_value);
511 }
512 return false;
513}
514
515uint64_t
516OptionValueProperties::GetPropertyAtIndexAsUInt64 (const ExecutionContext *exe_ctx, uint32_t idx, uint64_t fail_value) const
517{
518 const Property *property = GetPropertyAtIndex (exe_ctx, false, idx);
519 if (property)
520 {
521 OptionValue *value = property->GetValue().get();
522 if (value)
523 return value->GetUInt64Value(fail_value);
524 }
525 return fail_value;
526}
527
528bool
529OptionValueProperties::SetPropertyAtIndexAsUInt64 (const ExecutionContext *exe_ctx, uint32_t idx, uint64_t new_value)
530{
531 const Property *property = GetPropertyAtIndex (exe_ctx, true, idx);
532 if (property)
533 {
534 OptionValue *value = property->GetValue().get();
535 if (value)
536 return value->SetUInt64Value(new_value);
537 }
538 return false;
539}
540
541bool
542OptionValueProperties::Clear ()
543{
544 const size_t num_properties = m_properties.size();
545 for (size_t i=0; i<num_properties; ++i)
546 m_properties[i].GetValue()->Clear();
547 return true;
548}
549
550
551Error
552OptionValueProperties::SetValueFromCString (const char *value, VarSetOperationType op)
553{
554 Error error;
555
556// Args args(value_cstr);
557// const size_t argc = args.GetArgumentCount();
558 switch (op)
559 {
560 case eVarSetOperationClear:
561 Clear ();
562 break;
563
564 case eVarSetOperationReplace:
565 case eVarSetOperationAssign:
566 case eVarSetOperationRemove:
567 case eVarSetOperationInsertBefore:
568 case eVarSetOperationInsertAfter:
569 case eVarSetOperationAppend:
570 case eVarSetOperationInvalid:
571 error = OptionValue::SetValueFromCString (value, op);
572 break;
573 }
574
575 return error;
576}
577
578void
579OptionValueProperties::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
580{
581 const size_t num_properties = m_properties.size();
582 for (size_t i=0; i<num_properties; ++i)
583 {
584 const Property *property = GetPropertyAtIndex(exe_ctx, false, i);
585 if (property)
586 {
587 OptionValue *option_value = property->GetValue().get();
588 assert (option_value);
589 const bool transparent_value = option_value->ValueIsTransparent ();
590 property->Dump (exe_ctx,
591 strm,
592 dump_mask);
593 if (!transparent_value)
594 strm.EOL();
595 }
596 }
597}
598
599Error
600OptionValueProperties::DumpPropertyValue (const ExecutionContext *exe_ctx,
601 Stream &strm,
602 const char *property_path,
603 uint32_t dump_mask)
604{
605 Error error;
606 const bool will_modify = false;
607 lldb::OptionValueSP value_sp (GetSubValue (exe_ctx, property_path, will_modify, error));
608 if (value_sp)
609 {
610 if (!value_sp->ValueIsTransparent ())
611 {
612 if (dump_mask & eDumpOptionName)
613 strm.PutCString (property_path);
614 if (dump_mask & ~eDumpOptionName)
615 strm.PutChar (' ');
616 }
617 value_sp->DumpValue (exe_ctx, strm, dump_mask);
618 }
619 return error;
620}
621
622lldb::OptionValueSP
623OptionValueProperties::DeepCopy () const
624{
625 assert(!"this shouldn't happen");
626}
627
628const Property *
629OptionValueProperties::GetPropertyAtPath (const ExecutionContext *exe_ctx,
630 bool will_modify,
631 const char *name) const
632{
633 const Property *property = NULL;
634 if (name && name[0])
635 {
636 const char *sub_name = NULL;
637 ConstString key;
638 size_t key_len = ::strcspn (name, ".[{");
639
640 if (name[key_len])
641 {
642 key.SetCStringWithLength (name, key_len);
643 sub_name = name + key_len;
644 }
645 else
646 key.SetCString (name);
647
648 property = GetProperty (exe_ctx, will_modify, key);
649 if (sub_name && property)
650 {
651 if (sub_name[0] == '.')
652 {
653 OptionValueProperties *sub_properties = property->GetValue()->GetAsProperties();
654 if (sub_properties)
655 return sub_properties->GetPropertyAtPath(exe_ctx, will_modify, sub_name + 1);
656 }
657 property = NULL;
658 }
659 }
660 return property;
661}
662
663void
664OptionValueProperties::DumpAllDescriptions (CommandInterpreter &interpreter,
665 Stream &strm) const
666{
667 size_t max_name_len = 0;
668 const size_t num_properties = m_properties.size();
669 for (size_t i=0; i<num_properties; ++i)
670 {
671 const Property *property = ProtectedGetPropertyAtIndex(i);
672 if (property)
673 max_name_len = std::max<size_t>(property->GetName().GetLength(), max_name_len);
674 }
675 for (size_t i=0; i<num_properties; ++i)
676 {
677 const Property *property = ProtectedGetPropertyAtIndex(i);
678 if (property)
679 property->DumpDescription (interpreter, strm, max_name_len, false);
680 }
681}
682
683void
684OptionValueProperties::Apropos (const char *keyword, std::vector<const Property *> &matching_properties) const
685{
686 const size_t num_properties = m_properties.size();
687 StreamString strm;
688 for (size_t i=0; i<num_properties; ++i)
689 {
690 const Property *property = ProtectedGetPropertyAtIndex(i);
691 if (property)
692 {
693 const OptionValueProperties *properties = property->GetValue()->GetAsProperties();
694 if (properties)
695 {
696 properties->Apropos (keyword, matching_properties);
697 }
698 else
699 {
700 bool match = false;
701 const char *name = property->GetName().GetCString();
702 if (name && ::strcasestr(name, keyword))
703 match = true;
704 else
705 {
706 const char *desc = property->GetDescription();
707 if (desc && ::strcasestr(desc, keyword))
708 match = true;
709 }
710 if (match)
711 {
712 matching_properties.push_back (property);
713 }
714 }
715 }
716 }
717}
718
719