This is an archive of the discontinued LLVM Phabricator instance.

NFC: Port QueryParser to StringRef
ClosedPublic

Authored by steveire on Jan 7 2019, 2:58 PM.

Details

Summary

There is no reason for it to not be a StringRef. Making it one
simplifies existing code, and makes follow-up features easier.

Event Timeline

steveire created this revision.Jan 7 2019, 2:58 PM
kristina added a reviewer: kristina.EditedJan 7 2019, 6:05 PM
kristina added a subscriber: kristina.

LGTM aside from one comment. There's a few style nits however, and I think the lambda on one line could use a few newlines for readability.

clang-query/QueryParser.cpp
36

I don't think this is the best way of handling it, in fact I'm pretty certain you're leaking memory here.

steveire updated this revision to Diff 180626.Jan 8 2019, 1:51 AM

Run clang-format

steveire marked an inline comment as done.Jan 8 2019, 1:52 AM

Thanks, I ran clang-format, and I'd rather let it be the rulemaker, rather than try to add newlines in the lambdas :).

clang-query/QueryParser.cpp
36

Hmm, I didn't know we could get a mem leak with StringRef. Can you explain?

aaron.ballman added inline comments.Jan 8 2019, 8:04 AM
clang-query/QueryParser.cpp
35

Why not just return Line?

36

There's no memory leak here that I can see -- StringRef is non-owning.

37

Why not just return Line here as well?

40–42

Can you use Line.take_until(isWhitespace);?

steveire marked 2 inline comments as done.Jan 8 2019, 8:39 AM
steveire added inline comments.
clang-query/QueryParser.cpp
35

It is the pre-existing behavior to return a zero-length StringRef with a valid Begin pointer in this case. I think the reason is something to do with code completion. I can check later.

37

That's pre-existing. Seems more clear to leave it as is.

aaron.ballman added inline comments.Jan 8 2019, 8:46 AM
clang-query/QueryParser.cpp
35

I believe returning Line is identical -- you should wind up with the same "begin" pointer for the returned string and a length of zero (because Line must be empty by this point).

37

I don't have a strong preference, but I raised the point because it seems odd to explicitly clear a StringRef and then return a new, different, empty StringRef on the next line.

steveire marked an inline comment as done.Jan 8 2019, 8:47 AM
steveire added inline comments.
clang-query/QueryParser.cpp
35

Actually, I can see it by reading the code - P->CompletionPos <= Word.data() in the LexOrCompleteWord ctor woudln't work anymore without this hack. (Working on removing the hack is out of scope of this change).

steveire marked an inline comment as done.Jan 8 2019, 8:49 AM
steveire added inline comments.
clang-query/QueryParser.cpp
35

Ok, I see your new comment now. I guess that will work. I don't know the behavior of ltrim and whether it preserves that property.

steveire marked an inline comment as done.Jan 8 2019, 8:51 AM
steveire added inline comments.
clang-query/QueryParser.cpp
37

Well, code changes, and Line may not always be empty here. As it is, this communicates that we're returning an empty StringRef and always will (until this line has a reason to change).

aaron.ballman added inline comments.Jan 8 2019, 8:56 AM
clang-query/QueryParser.cpp
35

StringRefs are immutable and non-owning, so ltrim() returns Line.begin + someOffset in essence. That should preserve the behavior you need here.

37

Okay, that's good enough for me, thanks!

aaron.ballman accepted this revision.Jan 8 2019, 9:08 AM

LGTM aside from the nits pointed out; you can fix and commit without another round of review.

This revision is now accepted and ready to land.Jan 8 2019, 9:08 AM
This revision was automatically updated to reflect the committed changes.