dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Make ReferenceLocation.IsWrittenTo public #65408

Open FelixKeck opened 1 year ago

FelixKeck commented 1 year ago

Background and Motivation

I'm trying to find all references of a variable and distinguish them by read/write accesses. Therefore I use SymbolFinder.FindReferencesAsync. The returned reference locations have a property IsWrittenTo which looks like it provides this information. Sadly this property is internal so I cannot use it. Is it possible to make this property public or are there other ways to retrieve this information? Thanks for feedback.

Proposed API

namespace Microsoft.CodeAnalysis.FindSymbols
{
     public readonly struct ReferenceLocation : IComparable<ReferenceLocation>, IEquatable<ReferenceLocation>
     {
-        internal bool IsWrittenTo => SymbolUsageInfo.IsWrittenTo();
+        public bool IsWrittenTo => SymbolUsageInfo.IsWrittenTo();
     }
jasonmalinowski commented 1 year ago

@CyrusNajmabadi @mavasani Any reason we shouldn't do this?

CyrusNajmabadi commented 1 year ago

Generally speaking, because it's not a good representation of things (and really should be removed). References can be bucketed into many disparate concepts. So finding the write way to represetnt that isn't clear.

FelixKeck commented 1 year ago

I see that a ReferenceLocation might not be the correct place for this property but I'm still facing the problem to distinguish read and write accesses of a variable. Is it possible to make SymbolUsageInfo public to retrieve the information directly from a symbol or am I forced to reimplement the functionality? I mean the information is already there but I can't find a way to access it.

CyrusNajmabadi commented 1 year ago

My recommendation would be to use ioperation and see how the reference is being used.