This is an archive of the discontinued LLVM Phabricator instance.

[lldb][TypeSystemClang][NFC] Make TemplateParameterInfos members private
ClosedPublic

Authored by Michael137 on Dec 14 2022, 8:44 AM.

Details

Summary

This patch makes the members of TemplateParameterInfos only accessible
via public APIs. The motivation for this is that
TemplateParameterInfos attempts to maintain two vectors in tandem
(args for the template arguments and names for the corresponding
name). Working with this structure as it's currently designed makes
it easy to run into out-of-bounds accesses later down the line.

This patch proposes to introduce a new
TemplateParameterInfos::InsertArg which is the only way to
set the TemplateArgument and name of an entry and since we
require both to be specified we maintain the vectors in sync
out-of-the-box.

To avoid adding non-const getters just for unit-tests a new
TemplateParameterInfosManipulatorForTests is introduced
that can be used to control internal state from tests.

Diff Detail

Event Timeline

Michael137 created this revision.Dec 14 2022, 8:44 AM
Herald added a project: Restricted Project. · View Herald Transcript
Michael137 requested review of this revision.Dec 14 2022, 8:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 14 2022, 8:44 AM
aprantl accepted this revision.Dec 14 2022, 9:22 AM
aprantl added inline comments.
lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
2067 ↗(On Diff #482875)

I wish we had a lazy version of StringRef that computes the length only on demand. How long does template_param_infos live? If it's only temporary, maybe we could just make it store StringRefs?

This revision is now accepted and ready to land.Dec 14 2022, 9:22 AM
Michael137 added inline comments.Dec 14 2022, 5:10 PM
lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
2067 ↗(On Diff #482875)

I think that would be feasible. Though for this NFC change I'd prefer just keeping the changes to a minimum. There is this subtle distinction between a nullptr name and an empty string literal which affects how parameter packs are handled. I suspect there's ways to simplify those codepaths and then start using StringRefs instead. I'll look into it after this patch lands

I think that test should just be rewritten to not modify the object in such a way. Instead of cumulatively modifying a single object, it could just create a fresh one each time. That will also make it clearer what is being tested. And I think it's fair game to add some constructors to the production object to make that easy.

For example, instead of

// long sequence of manipulator operations
ExpectNewTemplate("<typename T, long...> (T = int, long = 1)", infos);

it could do something like

ExpectNewTemplate("<typename T, long...> (T = int, long = 1)",
  TemplateParameterInfos({{"T", intType}}, /*pack_name=*/???, /*pack=*/TemplateParameterInfos(???));
  • No need for manipulator wrapper. Add constructors instead

I think that test should just be rewritten to not modify the object in such a way. Instead of cumulatively modifying a single object, it could just create a fresh one each time. That will also make it clearer what is being tested. And I think it's fair game to add some constructors to the production object to make that easy.

For example, instead of

// long sequence of manipulator operations
ExpectNewTemplate("<typename T, long...> (T = int, long = 1)", infos);

it could do something like

ExpectNewTemplate("<typename T, long...> (T = int, long = 1)",
  TemplateParameterInfos({{"T", intType}}, /*pack_name=*/???, /*pack=*/TemplateParameterInfos(???));

@labath good point, looks much better now. Even found a bug I introduced into the test :)

  • Fix tests
labath accepted this revision.Dec 19 2022, 1:07 AM

That looks great. I have just one small nit. I think the test would read better if you made the names of temporary TemplateArgument objects more descriptive -- i.e. encode the kind and value information in the name. For example. int47Arg for an integer template argument whose value is 47, and intTypeArg for a type template argument (where int is the "value").

Got landed in https://reviews.llvm.org/rGa29e06bbeaad7012ac85b221f1aaba3fe1d5fd35

Forgot about your last comment re. variable names in the tests Pavel. Will change in a follow-up commit

Didn't rebase properly and broke the build
Fixing...

Michael137 added a comment.EditedJan 26 2023, 7:15 PM

Argh seems like Phabricator didn't like the fact I linked the revision in a different commit. So to see the actual change that got landed as part of this you need to visit the history tab and exclude the latest commit.