Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Analysis/Ast/Impl/Analyzer/AnalysisWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ public override bool Walk(ExpressionStatement node) {
case Comprehension comp:
Eval.ProcessComprehension(comp);
return false;
case CallExpression callex when callex.Target is NameExpression nex && !string.IsNullOrEmpty(nex.Name):
Eval.LookupNameInScopes(nex.Name)?.AddReference(Eval.GetLocationOfName(nex));
return true;
case CallExpression callex when callex.Target is MemberExpression mex && !string.IsNullOrEmpty(mex.Name):
var t = Eval.GetValueFromExpression(mex.Target)?.GetPythonType();
t?.GetMember(mex.Name)?.AddReference(Eval.GetLocationOfName(mex));
case CallExpression callex:
Eval.ProcessCallForReferences(callex);
return true;
default:
return base.Walk(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,28 @@ private void DeclareParameter(Parameter p, ParameterInfo pi) {
DeclareVariable(p.Name, paramType.CreateInstance(ArgumentSet.Empty(p.NameExpression, this)),
VariableSource.Declaration, p.NameExpression);
}

internal void ProcessCallForReferences(CallExpression callExpr) {
if (Module.ModuleType != ModuleType.User) {
return;
}

switch (callExpr.Target) {
case NameExpression nex when !string.IsNullOrEmpty(nex.Name):
// Add reference to the function
this.LookupNameInScopes(nex.Name)?.AddReference(GetLocationOfName(nex));
break;
case MemberExpression mex when !string.IsNullOrEmpty(mex.Name): {
var t = GetValueFromExpression(mex.Target)?.GetPythonType();
t?.GetMember(mex.Name)?.AddReference(GetLocationOfName(mex));
break;
}
}

// Add references to all arguments.
foreach (var arg in callExpr.Args) {
GetValueFromExpression(arg.Expression);
}
}
}
}
15 changes: 12 additions & 3 deletions src/Analysis/Ast/Impl/Analyzer/Handlers/FromImportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,29 @@ private void HandleModuleImportStar(PythonVariableModule variableModule, IImport
/// <param name="imports">Import search result.</param>
/// <param name="variableName">Name of the variable to declare, such as 'd' in 'from a.b import c as d'.</param>
/// <param name="importPosition">Position of the import statement.</param>
/// <param name="nameExpression">Name expression of the variable.</param>
private void DeclareVariable(PythonVariableModule variableModule, string memberName, IImportSearchResult imports, string variableName, int importPosition, Node nameExpression) {
/// <param name="nameLocation">Location of the variable name expression.</param>
private void DeclareVariable(PythonVariableModule variableModule, string memberName, IImportSearchResult imports, string variableName, int importPosition, Node nameLocation) {
// First try imports since child modules should win, i.e. in 'from a.b import c'
// 'c' should be a submodule if 'b' has one, even if 'b' also declares 'c = 1'.
var value = GetValueFromImports(variableModule, imports as IImportChildrenSource, memberName);

// First try exported or child submodules.
value = value ?? variableModule.GetMember(memberName);

// Value may be variable or submodule. If it is variable, we need it in order to add reference.
var variable = variableModule.Analysis?.GlobalScope?.Variables[memberName];
value = variable?.Value?.Equals(value) == true ? variable : value;

// If nothing is exported, variables are still accessible.
value = value ?? variableModule.Analysis?.GlobalScope?.Variables[memberName]?.Value ?? Eval.UnknownType;

// Do not allow imported variables to override local declarations
Eval.DeclareVariable(variableName, value, VariableSource.Import, nameExpression, CanOverwriteVariable(variableName, importPosition));
var canOverwrite = CanOverwriteVariable(variableName, importPosition);

// Do not declare references to '*'
var locationExpression = nameLocation is NameExpression nex && nex.Name == "*" ? null : nameLocation;
Eval.DeclareVariable(variableName, value, VariableSource.Import, locationExpression, canOverwrite);

// Make sure module is loaded and analyzed.
if (value is IPythonModule m) {
ModuleResolution.GetOrLoadModule(m.Name);
Expand Down
6 changes: 5 additions & 1 deletion src/Analysis/Ast/Impl/Types/LocatedMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public virtual IReadOnlyList<LocationInfo> References {

public virtual void AddReference(Location location) {
lock (this) {
if(this.DeclaringModule == null || this.DeclaringModule?.ModuleType == ModuleType.Builtins) {
// In order to limit memory consumption we normally don't track references
// to builtin types such as int or list. Exception is functions like 'print'
// since it user may want to find all references to them.
if (this.DeclaringModule == null ||
(this.DeclaringModule?.ModuleType == ModuleType.Builtins && MemberType != PythonMemberType.Function)) {
return;
}
// Don't add references to library code.
Expand Down
8 changes: 0 additions & 8 deletions src/Analysis/Ast/Impl/Types/PythonType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ private PythonType(string name, Location location, BuiltinTypeId typeId) : base(

#region ILocatedMember
public override PythonMemberType MemberType => _typeId.GetMemberId();

public override void AddReference(Location location) {
if (DeclaringModule == null || DeclaringModule.ModuleType == ModuleType.Builtins) {
return;
}

base.AddReference(location);
}
#endregion

#region IPythonType
Expand Down
54 changes: 54 additions & 0 deletions src/Analysis/Ast/Test/ReferencesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,5 +441,59 @@ public async Task ExtendAllAssignment() {
all.References[3].Span.Should().Be(12, 1, 12, 8);
all.References[4].Span.Should().Be(13, 1, 13, 8);
}

[TestMethod, Priority(0)]
public async Task VariableInCallParameters() {
const string code = @"
from constants import *
import constants

print(VARIABLE1)
print(constants.VARIABLE1)
x = print(VARIABLE1)
";
await TestData.CreateTestSpecificFileAsync("constants.py", @"VARIABLE1 = 'afad'");
var analysis = await GetAnalysisAsync(code);
var v1 = analysis.Should().HaveVariable("VARIABLE1").Which;

v1.Definition.Span.Should().Be(1, 1, 1, 10);
v1.Definition.DocumentUri.AbsolutePath.Should().Contain("constants.py");

v1.References.Should().HaveCount(4);
v1.References[0].Span.Should().Be(1, 1, 1, 10);
v1.References[0].DocumentUri.AbsolutePath.Should().Contain("constants.py");

v1.References[1].Span.Should().Be(5, 7, 5, 16);
v1.References[1].DocumentUri.AbsolutePath.Should().Contain("module.py");

v1.References[2].Span.Should().Be(6, 17, 6, 26);
v1.References[2].DocumentUri.AbsolutePath.Should().Contain("module.py");

v1.References[3].Span.Should().Be(7, 11, 7, 20);
v1.References[3].DocumentUri.AbsolutePath.Should().Contain("module.py");
}

[TestMethod, Priority(0)]
public async Task LibraryFunction() {
const string code = @"
print(1)
print(2)
";
var analysis = await GetAnalysisAsync(code);
var b = analysis.Document.Interpreter.ModuleResolution.BuiltinsModule;
var print = b.Analysis.Should().HaveVariable("print").Which;

print.Definition.Span.Should().Be(1, 1, 1, 1);

print.References.Should().HaveCount(3);
print.References[0].Span.Should().Be(1, 1, 1, 1);
print.References[0].DocumentUri.AbsolutePath.Should().Contain("python.pyi");

print.References[1].Span.Should().Be(2, 1, 2, 6);
print.References[1].DocumentUri.AbsolutePath.Should().Contain("module.py");

print.References[2].Span.Should().Be(3, 1, 3, 6);
print.References[2].DocumentUri.AbsolutePath.Should().Contain("module.py");
}
}
}
1 change: 0 additions & 1 deletion src/Parsing/Impl/Ast/CallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Collections;

namespace Microsoft.Python.Parsing.Ast {
Expand Down