[PATCH] D25255: Add a c_str() method to StringRef, similar to data() but asserting if the string isn't null terminated (NFC)

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 16:10:41 PDT 2016


The use case is that MachineInstruction have operands, but the size of their operands is limited. Right now you have the space for one-pointer, but not for a StringRef. So if you create a MachineInstruction that takes a symbol name as an operand, we can only store the “const chat *” from the StringRef.

So what you’re suggesting to do is roughly what I’m trying to achieve here as well, but instead of replicating the logic at each call sites, I try to abstract it behind the API.


> On Oct 4, 2016, at 3:56 PM, Zachary Turner <zturner at google.com> wrote:
> 
> I'm not familiar with those cases, but could you do something like this:
> 
> assert(strlen(s.data()) == s.size() && "Str is not null terminated!");
> 
> at each call site?
> 
> On Tue, Oct 4, 2016 at 3:48 PM Mehdi Amini <mehdi.amini at apple.com <mailto:mehdi.amini at apple.com>> wrote:
> Neat, I didn’t know this one!
> 
> That does not solve cases like the round-trip StringRef -> MO -> StringRef though.
> 
> 
>> On Oct 4, 2016, at 3:38 PM, Zachary Turner <zturner at google.com <mailto:zturner at google.com>> wrote:
>> 
>> Can you use %*s format specifier in those cases?
>> 
>> printf("%*s", s.size(), s.data());
>> On Tue, Oct 4, 2016 at 3:34 PM Mehdi AMINI <mehdi.amini at apple.com <mailto:mehdi.amini at apple.com>> wrote:
>> mehdi_amini added a comment.
>> 
>> In https://reviews.llvm.org/D25255#561566 <https://reviews.llvm.org/D25255#561566>, @zturner wrote:
>> 
>> > Not sure how I feel about this.  It's convenient, but it has potential for abuse.  Where did you run into issues porting code to `StringRef` that this solves?
>> 
>> 
>> I ran into this with "printf" like API at some point, or with places that can't be converted to StringRef because of space constraint, like MachineOperand. So you're out of the IR and you have to use .data() to initialize the MO. Later you may construct a StringRef from this "const char *" again.
>> 
>> >   I've done a lot of `StringRef` porting in LLDB by now, and I've always managed to find a solution to this.  Usually it involves trickling the `StringRef` changes down further
>> 
>> Usually that's what I do, yes. The problem is when you reach some cases like above.
>> 
>> 
>> https://reviews.llvm.org/D25255 <https://reviews.llvm.org/D25255>
>> 
>> 
>> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161004/99b92c66/attachment.html>


More information about the llvm-commits mailing list