Unfortunately, the only way to know the path to the Location object is to 
know the path to the descriptor proto object in question.
Alternatively, you could iterate through all the sourcecodeinfo elements 
and use their paths to navigate to the correct descriptor object.
One technique I have used in the past is to iterate through all the 
sourcecodeinfo elements and store the location object in a custom option 
extension on the object in question (or the parent object if it something 
that doesn't have options).

Also, as shaod@ points out, some comments will not show up in 
sourcecodeinfo.

On Wednesday, September 7, 2022 at 4:11:32 PM UTC-6 [email protected] wrote:

> First keep in mind that some comments are detached and thus ignored by 
> SourceCodeInfo.
>
> That being said, IIRC I've seen a very hacky way to achieve similar goals: 
> https://github.com/protocolbuffers/protobuf/blob/3322c0b92a5001ade92608d75891d63c749d624d/src/google/protobuf/compiler/parser_unittest.cc#L2472
> On Thursday, September 1, 2022 at 7:16:52 AM UTC-7 [email protected] 
> wrote:
>
>> I'm parsing a large number of protobuf files and am using the Source Code 
>> Info descriptor to extract comment data from the source files as well. I 
>> currently use the FileDescriptorProto.ListFields() method to extract the 
>> DescriptorProto objects I care about as well as the SourceCodeInfo.
>>
>> To my knowledge, the only way to pair up Location fields with the 
>> corresponding objects is via the path attribute 
>> <https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor.pb>.
>>  
>> This is fine; except for the fact that involves me manually stepping 
>> through said path to land at my parsed Protobuf Object. This gets 
>> complicated when dealing with layers of nested_types and I am convinced 
>> there must be a way for me to extract the path from the particular 
>> DescriptorProto Object and then use that to match up the object with the 
>> path specified in the corresponding Location field.
>>
>> In short: How can I easily pair up DescriptorProto objects with the 
>> Location objects that correspond to them? Specifically for comment parsing 
>> purposes.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/3c8e8e15-4c36-4957-bcb4-0dd1430854a4n%40googlegroups.com.

Reply via email to