This is an archive of the discontinued LLVM Phabricator instance.

SourceManager: Encapsulate line number mapping into SrcMgr::LineOffsetMapping
ClosedPublic

Authored by dexonsmith on Oct 21 2020, 2:39 PM.

Details

Summary

Put the guts of ComputeLineNumbers into LineOffsetMapping::get and
LineOffsetMapping::LineOffsetMapping. As a drive-by, store the number
of lines directly in the bump-ptr-allocated array.

Diff Detail

Event Timeline

dexonsmith created this revision.Oct 21 2020, 2:39 PM
dexonsmith requested review of this revision.Oct 21 2020, 2:39 PM

Renamed getNumLines() to size(), which is more natural given begin() and end().

Fix an off-by-one bug in LineOffsetMapping::LineOffsetMapping, add unit tests, and clang-format.

Add the unittest (constructTwo) that caught the off-by-one bug, somehow missed in the last update.

JDevlieghere accepted this revision.Oct 22 2020, 10:45 PM
JDevlieghere added inline comments.
clang/include/clang/Basic/SourceManager.h
118

I guess it's implicit in the implementation, but maybe it's worth adding a comment describing the layout (first element is the size, elements at index i are at index i+1.

This revision is now accepted and ready to land.Oct 22 2020, 10:45 PM
dexonsmith added inline comments.Oct 23 2020, 9:47 AM
clang/include/clang/Basic/SourceManager.h
118

Thanks, good idea; I'll have that in the commit.

Herald added a project: Restricted Project. · View Herald TranscriptOct 23 2020, 9:56 AM

@dexonsmith thanks for doing that cleanup!

I am currently trying to rebase https://reviews.llvm.org/D33275 on top of our internal infrastructure. We (cling) use SourceManager::overrideFileContents to provide virtual file content. It seems that every call to ContentCache::getBufferOrNone should somehow consider if the content was not overridden. Do you see an easy way to get D33275 aligned to LineOffsetMapping? Alternatively, we could require a SourceManager& argument to ContentCache::getBufferOrNone and return the overridden buffer...