Skip to content

Commit 788a48e

Browse files
authored
[PCompiler] Remove test cases from imported projects (#702)
* [PCompiler] Removes test cases from imported projects Adds logic to filter out all test cases from imported projects, to only retain test cases from the main project * Minor: improve comment * [PCompiler] Addresses suggestions from #702
1 parent 24f64b7 commit 788a48e

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

Src/PCompiler/CompilerCore/Backend/CSharp/CompilationContext.cs

-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ public CompilationContext(ICompilerConfiguration job)
1212
Names = new CSharpNameManager("PGEN_");
1313

1414
FileName = $"{ProjectName}.cs";
15-
ProjectDependencies = job.ProjectDependencies.Count == 0 ? new List<string>() { ProjectName } : job.ProjectDependencies;
1615
GlobalFunctionClassName = "GlobalFunctions";
1716
}
1817

@@ -24,8 +23,6 @@ public CompilationContext(ICompilerConfiguration job)
2423

2524
public string FileName { get; }
2625

27-
public IList<string> ProjectDependencies { get; }
28-
2926
public string GetStaticMethodQualifiedName(Function function)
3027
{
3128
return $"{GlobalFunctionClassName}.{Names.GetNameForDecl(function)}";

Src/PCompiler/CompilerCore/Backend/Java/CompilationContext.cs

-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public CompilationContext(ICompilerConfiguration job)
1111
Types = new TypeManager(Names);
1212

1313
FileName = $"{ProjectName}.java";
14-
ProjectDependencies = job.ProjectDependencies.Count == 0 ? new List<string>() { ProjectName } : job.ProjectDependencies;
1514
}
1615

1716
public NameManager Names { get; }
@@ -21,7 +20,5 @@ public CompilationContext(ICompilerConfiguration job)
2120

2221
public string FileName { get; }
2322

24-
public IList<string> ProjectDependencies { get; }
25-
2623
}
2724
}

Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private static void ApplyPropagations<T>(IEnumerable<Function> functions, params
161161

162162
private static Scope BuildGlobalScope(ICompilerConfiguration config, PParser.ProgramContext[] programUnits)
163163
{
164-
var globalScope = Scope.CreateGlobalScope(config.Handler);
164+
var globalScope = Scope.CreateGlobalScope(config);
165165
var nodesToDeclarations = new ParseTreeProperty<IPDecl>();
166166

167167
// Add built-in events to the table.

Src/PCompiler/CompilerCore/TypeChecker/DeclarationStubVisitor.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,11 @@ public override object VisitSafetyTestDecl([NotNull] PParser.SafetyTestDeclConte
211211
{
212212
var symbolName = context.testName.GetText();
213213
var decl = CurrentScope.Put(symbolName, context);
214-
decl.Main = context.mainMachine?.GetText();
215-
nodesToDeclarations.Put(context, decl);
214+
if (decl != null)
215+
{
216+
decl.Main = context.mainMachine?.GetText();
217+
nodesToDeclarations.Put(context, decl);
218+
}
216219
return null;
217220
}
218221

Src/PCompiler/CompilerCore/TypeChecker/Scope.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Scope
2020
private readonly IDictionary<string, PEvent> events = new Dictionary<string, PEvent>();
2121
private readonly IDictionary<string, NamedEventSet> eventSets = new Dictionary<string, NamedEventSet>();
2222
private readonly IDictionary<string, Function> functions = new Dictionary<string, Function>();
23-
private readonly ITranslationErrorHandler handler;
23+
private readonly ICompilerConfiguration config;
2424
private readonly IDictionary<string, Implementation> implementations = new Dictionary<string, Implementation>();
2525
private readonly IDictionary<string, Interface> interfaces = new Dictionary<string, Interface>();
2626
private readonly IDictionary<string, Machine> machines = new Dictionary<string, Machine>();
@@ -32,9 +32,9 @@ public class Scope
3232
private readonly IDictionary<string, TypeDef> typedefs = new Dictionary<string, TypeDef>();
3333
private readonly IDictionary<string, Variable> variables = new Dictionary<string, Variable>();
3434

35-
private Scope(ITranslationErrorHandler handler, Scope parent = null)
35+
private Scope(ICompilerConfiguration config, Scope parent = null)
3636
{
37-
this.handler = handler;
37+
this.config = config;
3838
parent?.children.Remove(this);
3939
Parent = parent;
4040
parent?.children.Add(this);
@@ -80,14 +80,14 @@ private Scope(ITranslationErrorHandler handler, Scope parent = null)
8080
public IEnumerable<Implementation> Implementations => implementations.Values;
8181
public IEnumerable<NamedModule> NamedModules => namedModules.Values;
8282

83-
public static Scope CreateGlobalScope(ITranslationErrorHandler handler)
83+
public static Scope CreateGlobalScope(ICompilerConfiguration config)
8484
{
85-
return new Scope(handler);
85+
return new Scope(config);
8686
}
8787

8888
public Scope MakeChildScope()
8989
{
90-
return new Scope(handler, this);
90+
return new Scope(config, this);
9191
}
9292

9393
public IEnumerable<Function> GetAllMethods()
@@ -599,6 +599,16 @@ public Implementation Put(string name, PParser.ImplementationDeclContext tree)
599599

600600
public SafetyTest Put(string name, PParser.SafetyTestDeclContext tree)
601601
{
602+
// check if test is from an imported project, if so, return null
603+
string filePath = config.LocationResolver.GetLocation(tree).File.FullName;
604+
foreach (var dependencyPath in config.ProjectDependencies)
605+
{
606+
if (filePath.StartsWith(dependencyPath))
607+
{
608+
return null;
609+
}
610+
}
611+
602612
var safetyTest = new SafetyTest(tree, name);
603613
CheckConflicts(safetyTest,
604614
Namespace(implementations),
@@ -631,7 +641,7 @@ private void CheckConflicts(IPDecl decl, params TableReader[] namespaces)
631641
IPDecl existingDecl = null;
632642
if (namespaces.Any(table => table(decl.Name, out existingDecl)))
633643
{
634-
throw handler.DuplicateDeclaration(decl.SourceLocation, decl, existingDecl);
644+
throw config.Handler.DuplicateDeclaration(decl.SourceLocation, decl, existingDecl);
635645
}
636646
}
637647

Src/PCompiler/PCommandLine/Parser/ParsePProjectFile.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public void ParseProjectFileForChecker(string projectFile, CheckerConfiguration
115115
var projectDependencies = new HashSet<string>(preProjectDependencies);
116116
var inputFiles = new HashSet<string>(preInputFiles);
117117
var projectXml = XElement.Load(projectFilePath.FullName);
118-
projectDependencies.Add(GetProjectName(projectFilePath));
119118
// add all input files from the current project
120119
inputFiles.UnionWith(ReadAllInputFiles(projectFilePath));
121120

@@ -129,7 +128,9 @@ public void ParseProjectFileForChecker(string projectFile, CheckerConfiguration
129128

130129
CommandLineOutput.WriteInfo($"==== Loading project file: {fullProjectDepenPathName.FullName}");
131130

132-
if (projectDependencies.Contains(GetProjectName(fullProjectDepenPathName))) continue;
131+
if (projectDependencies.Contains(fullProjectDepenPathName.DirectoryName)) continue;
132+
// add path of imported project as project dependency
133+
projectDependencies.Add(fullProjectDepenPathName.DirectoryName);
133134
var inputsAndDependencies = GetAllProjectDependencies(fullProjectDepenPathName, inputFiles, projectDependencies);
134135
projectDependencies.UnionWith(inputsAndDependencies.projectDependencies);
135136
inputFiles.UnionWith(inputsAndDependencies.inputFiles);

0 commit comments

Comments
 (0)