1 //===-- CoreMedia.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CoreMedia.h"
10 
11 #include "lldb/Utility/Flags.h"
12 #include "lldb/Utility/Log.h"
13 
14 #include "lldb/Symbol/TypeSystem.h"
15 #include "lldb/Target/Target.h"
16 #include <cinttypes>
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 using namespace lldb_private::formatters;
21 
CMTimeSummaryProvider(ValueObject & valobj,Stream & stream,const TypeSummaryOptions & options)22 bool lldb_private::formatters::CMTimeSummaryProvider(
23     ValueObject &valobj, Stream &stream, const TypeSummaryOptions &options) {
24   CompilerType type = valobj.GetCompilerType();
25   if (!type.IsValid())
26     return false;
27 
28   auto type_system = type.GetTypeSystem();
29   if (!type_system)
30     return false;
31   // fetch children by offset to compensate for potential lack of debug info
32   auto int64_ty =
33       type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 64);
34   auto int32_ty =
35       type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, 32);
36 
37   auto value_sp(valobj.GetSyntheticChildAtOffset(0, int64_ty, true));
38   auto timescale_sp(valobj.GetSyntheticChildAtOffset(8, int32_ty, true));
39   auto flags_sp(valobj.GetSyntheticChildAtOffset(12, int32_ty, true));
40 
41   if (!value_sp || !timescale_sp || !flags_sp)
42     return false;
43 
44   auto value = value_sp->GetValueAsUnsigned(0);
45   auto timescale = (int32_t)timescale_sp->GetValueAsUnsigned(
46       0); // the timescale specifies the fraction of a second each unit in the
47           // numerator occupies
48   auto flags = Flags(flags_sp->GetValueAsUnsigned(0) &
49                      0x00000000000000FF); // the flags I need sit in the LSB
50 
51   const unsigned int FlagPositiveInf = 4;
52   const unsigned int FlagNegativeInf = 8;
53   const unsigned int FlagIndefinite = 16;
54 
55   if (flags.AnySet(FlagIndefinite)) {
56     stream.Printf("indefinite");
57     return true;
58   }
59 
60   if (flags.AnySet(FlagPositiveInf)) {
61     stream.Printf("+oo");
62     return true;
63   }
64 
65   if (flags.AnySet(FlagNegativeInf)) {
66     stream.Printf("-oo");
67     return true;
68   }
69 
70   switch (timescale) {
71   case 0:
72     return false;
73   case 1:
74     stream.Printf("%" PRId64 " seconds", value);
75     return true;
76   case 2:
77     stream.Printf("%" PRId64 " half seconds", value);
78     return true;
79   case 3:
80     stream.Printf("%" PRId64 " third%sof a second", value,
81                   value == 1 ? " " : "s ");
82     return true;
83   default:
84     stream.Printf("%" PRId64 " %" PRId32 "th%sof a second", value, timescale,
85                   value == 1 ? " " : "s ");
86     return true;
87   }
88 }
89