Skip to content

Commit

Permalink
[PCompiler] Remove test cases from imported projects (#702)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
aman-goel committed Mar 7, 2024
1 parent 24f64b7 commit 788a48e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public CompilationContext(ICompilerConfiguration job)
Names = new CSharpNameManager("PGEN_");

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

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

public string FileName { get; }

public IList<string> ProjectDependencies { get; }

public string GetStaticMethodQualifiedName(Function function)
{
return $"{GlobalFunctionClassName}.{Names.GetNameForDecl(function)}";
Expand Down
3 changes: 0 additions & 3 deletions Src/PCompiler/CompilerCore/Backend/Java/CompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public CompilationContext(ICompilerConfiguration job)
Types = new TypeManager(Names);

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

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

public string FileName { get; }

public IList<string> ProjectDependencies { get; }

}
}
2 changes: 1 addition & 1 deletion Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private static void ApplyPropagations<T>(IEnumerable<Function> functions, params

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

// Add built-in events to the table.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,11 @@ public override object VisitSafetyTestDecl([NotNull] PParser.SafetyTestDeclConte
{
var symbolName = context.testName.GetText();
var decl = CurrentScope.Put(symbolName, context);
decl.Main = context.mainMachine?.GetText();
nodesToDeclarations.Put(context, decl);
if (decl != null)
{
decl.Main = context.mainMachine?.GetText();
nodesToDeclarations.Put(context, decl);
}
return null;
}

Expand Down
24 changes: 17 additions & 7 deletions Src/PCompiler/CompilerCore/TypeChecker/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Scope
private readonly IDictionary<string, PEvent> events = new Dictionary<string, PEvent>();
private readonly IDictionary<string, NamedEventSet> eventSets = new Dictionary<string, NamedEventSet>();
private readonly IDictionary<string, Function> functions = new Dictionary<string, Function>();
private readonly ITranslationErrorHandler handler;
private readonly ICompilerConfiguration config;
private readonly IDictionary<string, Implementation> implementations = new Dictionary<string, Implementation>();
private readonly IDictionary<string, Interface> interfaces = new Dictionary<string, Interface>();
private readonly IDictionary<string, Machine> machines = new Dictionary<string, Machine>();
Expand All @@ -32,9 +32,9 @@ public class Scope
private readonly IDictionary<string, TypeDef> typedefs = new Dictionary<string, TypeDef>();
private readonly IDictionary<string, Variable> variables = new Dictionary<string, Variable>();

private Scope(ITranslationErrorHandler handler, Scope parent = null)
private Scope(ICompilerConfiguration config, Scope parent = null)
{
this.handler = handler;
this.config = config;
parent?.children.Remove(this);
Parent = parent;
parent?.children.Add(this);
Expand Down Expand Up @@ -80,14 +80,14 @@ private Scope(ITranslationErrorHandler handler, Scope parent = null)
public IEnumerable<Implementation> Implementations => implementations.Values;
public IEnumerable<NamedModule> NamedModules => namedModules.Values;

public static Scope CreateGlobalScope(ITranslationErrorHandler handler)
public static Scope CreateGlobalScope(ICompilerConfiguration config)
{
return new Scope(handler);
return new Scope(config);
}

public Scope MakeChildScope()
{
return new Scope(handler, this);
return new Scope(config, this);
}

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

public SafetyTest Put(string name, PParser.SafetyTestDeclContext tree)
{
// check if test is from an imported project, if so, return null
string filePath = config.LocationResolver.GetLocation(tree).File.FullName;
foreach (var dependencyPath in config.ProjectDependencies)
{
if (filePath.StartsWith(dependencyPath))
{
return null;
}
}

var safetyTest = new SafetyTest(tree, name);
CheckConflicts(safetyTest,
Namespace(implementations),
Expand Down Expand Up @@ -631,7 +641,7 @@ private void CheckConflicts(IPDecl decl, params TableReader[] namespaces)
IPDecl existingDecl = null;
if (namespaces.Any(table => table(decl.Name, out existingDecl)))
{
throw handler.DuplicateDeclaration(decl.SourceLocation, decl, existingDecl);
throw config.Handler.DuplicateDeclaration(decl.SourceLocation, decl, existingDecl);
}
}

Expand Down
5 changes: 3 additions & 2 deletions Src/PCompiler/PCommandLine/Parser/ParsePProjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ public void ParseProjectFileForChecker(string projectFile, CheckerConfiguration
var projectDependencies = new HashSet<string>(preProjectDependencies);
var inputFiles = new HashSet<string>(preInputFiles);
var projectXml = XElement.Load(projectFilePath.FullName);
projectDependencies.Add(GetProjectName(projectFilePath));
// add all input files from the current project
inputFiles.UnionWith(ReadAllInputFiles(projectFilePath));

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

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

if (projectDependencies.Contains(GetProjectName(fullProjectDepenPathName))) continue;
if (projectDependencies.Contains(fullProjectDepenPathName.DirectoryName)) continue;
// add path of imported project as project dependency
projectDependencies.Add(fullProjectDepenPathName.DirectoryName);
var inputsAndDependencies = GetAllProjectDependencies(fullProjectDepenPathName, inputFiles, projectDependencies);
projectDependencies.UnionWith(inputsAndDependencies.projectDependencies);
inputFiles.UnionWith(inputsAndDependencies.inputFiles);
Expand Down

0 comments on commit 788a48e

Please sign in to comment.