aboutsummaryrefslogtreecommitdiff
path: root/source/DataFormatters/CoreMedia.cpp
blob: 172bdf07d2cf934c5fbc265cd12ce204c2b97e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//===-- CoreMedia.cpp --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/DataFormatters/CXXFormatterFunctions.h"

#include "lldb/Core/Flags.h"
#include "lldb/Symbol/ClangASTContext.h"

#include <inttypes.h>

using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::formatters;

bool
lldb_private::formatters::CMTimeSummaryProvider (ValueObject& valobj, Stream& stream, const TypeSummaryOptions& options)
{
    if (!valobj.GetClangType().IsValid())
        return false;
    ClangASTContext *ast_ctx = valobj.GetClangType().GetTypeSystem()->AsClangASTContext();
    if (!ast_ctx)
        return false;
    
    // fetch children by offset to compensate for potential lack of debug info
    auto int64_ty = ast_ctx->GetIntTypeFromBitSize(64, true);
    auto int32_ty = ast_ctx->GetIntTypeFromBitSize(32, true);
    
    auto value_sp(valobj.GetSyntheticChildAtOffset(0, int64_ty, true));
    auto timescale_sp(valobj.GetSyntheticChildAtOffset(8, int32_ty, true));
    auto flags_sp(valobj.GetSyntheticChildAtOffset(12, int32_ty, true));
    
    if (!value_sp || !timescale_sp || !flags_sp)
        return false;
    
    auto value = value_sp->GetValueAsUnsigned(0);
    auto timescale = (int32_t)timescale_sp->GetValueAsUnsigned(0); // the timescale specifies the fraction of a second each unit in the numerator occupies
    auto flags = Flags(flags_sp->GetValueAsUnsigned(0) & 0x00000000000000FF); // the flags I need sit in the LSB
    
    const unsigned int FlagPositiveInf = 4;
    const unsigned int FlagNegativeInf = 8;
    const unsigned int FlagIndefinite = 16;
    
    if (flags.AnySet(FlagIndefinite))
    {
        stream.Printf("indefinite");
        return true;
    }
    
    if (flags.AnySet(FlagPositiveInf))
    {
        stream.Printf("+oo");
        return true;
    }
    
    if (flags.AnySet(FlagNegativeInf))
    {
        stream.Printf("-oo");
        return true;
    }
    
    if (timescale == 0)
        return false;
    
    switch (timescale)
    {
        case 0:
            return false;
        case 1:
            stream.Printf("%" PRId64 " seconds", value);
            return true;
        case 2:
            stream.Printf("%" PRId64 " half seconds", value);
            return true;
        case 3:
            stream.Printf("%" PRId64 " third%sof a second", value, value == 1 ? " " : "s ");
            return true;
        default:
            stream.Printf("%" PRId64 " %" PRId32 "th%sof a second", value, timescale, value == 1 ? " " : "s ");
            return true;
    }
}