This is an archive of the discontinued LLVM Phabricator instance.

[lldb][formatters] Add support for printing NSConstantDate and fix distantPast value
ClosedPublic

Authored by teemperor on Jul 6 2020, 5:25 AM.

Details

Summary

Certain NSDate constructors return a special NSConstantDate class which currently ends
up being unformatted as it's not in the list of supported classes for the NSDate formatter.
This patch adds that class to the supported class list so LLDB produces a summary for it.

One of these special constructors is [NSDate distantPast] which returns the date for
0001-01-01 00:00:00 UTC. LLDB has a special case for formatting this date but for some
reason we did hardcode the wrong summary string in that special case. Maybe the summary
string was correct back when the code was written but it isn't correct anymore (distantPast
isn't actually defined to be a special date but just some 'a guaranteed temporal boundary.' so
maybe someone changed the value in the last 10 years).

If someone else is wondering why we even have this special case for distantPast but not for
the future. The reason seems to be that our date formatting for really old dates is off by 24
hours. So for example, adding one second to distantPast will cause LLDB to print
0000-12-30 00:00:01 UTC (which is 24 hours behind the expected result). So to make
our code appear to be correct it seems we just hardcoded the most common NSDate result
from that time span. I'll replace that logic with a generic solution in a probably more invasive follow up patch.

I also took the freedom to replace the magic value -63114076800 with some constant
+ documentation. I heard there are some people that don't know from the top of their head that there
are 63114076800 seconds between 1. Jan 0001 and 1. January 2001 in whatever calendar system
NSDate is using.

Diff Detail

Event Timeline

teemperor created this revision.Jul 6 2020, 5:25 AM
teemperor updated this revision to Diff 275683.Jul 6 2020, 5:27 AM
  • Added a FIXME to the hack.
mib added inline comments.Jul 6 2020, 6:59 AM
lldb/source/Plugins/Language/ObjC/Cocoa.cpp
858

Can't we get that value programmatically ?

teemperor marked an inline comment as done.Jul 6 2020, 7:27 AM
teemperor added inline comments.
lldb/source/Plugins/Language/ObjC/Cocoa.cpp
858

I think we could inspect the NSDate _NSDateDistantPast values. But I'm not sure if it's worth it even making a patch for that as this special case should anyway just disappear once we properly format ancient NSDate values (and it's anyway not really in the scope of this patch).

mib accepted this revision.Jul 7 2020, 4:57 AM
mib added inline comments.
lldb/source/Plugins/Language/ObjC/Cocoa.cpp
858

Sounds good to me then.

This revision is now accepted and ready to land.Jul 7 2020, 4:57 AM
teemperor updated this revision to Diff 277788.Jul 14 2020, 5:26 AM
  • Rebase before commit.
davide accepted this revision.Jul 14 2020, 9:16 AM
davide added inline comments.
lldb/source/Plugins/Language/ObjC/Cocoa.cpp
858

I agree with Raph.

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJul 15 2020, 1:28 AM