Skip to content

Commit

Permalink
Merge pull request #630 from galacticusorg/featDirectiveCheck
Browse files Browse the repository at this point in the history
Add functionality to check that all code directives were processed
  • Loading branch information
abensonca committed Jun 17, 2024
2 parents 5e7969c + 1852552 commit 60df160
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 12 deletions.
6 changes: 6 additions & 0 deletions perl/Galacticus/Build/SourceTree.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use Galacticus::Build::SourceTree::Process::ConditionalCall;
use Galacticus::Build::SourceTree::Process::EventHooks;
use Galacticus::Build::SourceTree::Process::Dependencies;
use Galacticus::Build::SourceTree::Process::ClassDocumentation;
use Galacticus::Build::SourceTree::Process::NonProcessed;
use Galacticus::Build::SourceTree::Analyze::UseDuplication;
use Encode;

Expand Down Expand Up @@ -192,6 +193,11 @@ sub ProcessTree {
# Run all defined processors on the tree.
&{$Galacticus::Build::SourceTree::Hooks::processHooks{$_}}($tree,\%options)
foreach ( @processorsOrdered );
# Get a list of all defined postprocessors.
my @postprocessors = sort(keys(%Galacticus::Build::SourceTree::Hooks::postprocessHooks));
# Run all post-processing.
&{$Galacticus::Build::SourceTree::Hooks::postprocessHooks{$_}}($tree,\%options)
foreach ( @postprocessors );
return $tree;
}

Expand Down
1 change: 1 addition & 0 deletions perl/Galacticus/Build/SourceTree/Hooks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use utf8;
# Define a hash into which modules can insert their hooks.
our %parseHooks;
our %processHooks;
our %postprocessHooks;
our %processDependencies;
our %analyzeHooks;

Expand Down
19 changes: 18 additions & 1 deletion perl/Galacticus/Build/SourceTree/Parse/Directives.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use XML::SAX::ParserFactory;
use XML::Validator::Schema;

# Insert hooks for our functions.
$Galacticus::Build::SourceTree::Hooks::parseHooks{'directives'} = \&Parse_Directives;
$Galacticus::Build::SourceTree::Hooks::parseHooks {'directives'} = \&Parse_Directives;
$Galacticus::Build::SourceTree::Hooks::postprocessHooks{'directives'} = \&PostProcess_Directives;

sub Parse_Directives {
# Get the tree.
Expand Down Expand Up @@ -172,4 +173,20 @@ sub Parse_Directives {
}
}

sub PostProcess_Directives {
# Get the tree.
my $tree = shift();
# Walk the tree, looking for directives.
my $node = $tree;
my $depth = 0;
while ( $node ) {
# Find directives.
if ( exists($node->{'directive'}) ) {
die("directive '".$node->{'type'}."' was not processed at line ".$node->{'line'}." in ".$tree->{'name'})
unless ( exists($node->{'directive'}->{'processed'}) && $node->{'directive'}->{'processed'} );
}
$node = &Galacticus::Build::SourceTree::Walk_Tree($node,\$depth);
}
}

1;
4 changes: 2 additions & 2 deletions perl/Galacticus/Build/SourceTree/Process/Allocate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ sub Process_Allocate {
my $node = $tree;
my $depth = 0;
while ( $node ) {
if ( $node->{'type'} eq "allocate" && ! $node->{'processed'} ) {
if ( $node->{'type'} eq "allocate" && ! $node->{'directive'}->{'processed'} ) {
# Record that node is processed.
$node->{'processed'} = 1;
$node->{'directive'}->{'processed'} = 1;
# Get the declaration and determine rank of the shape variable.
my $rank;
if ( exists($node->{'directive'}->{'rank'}) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ sub Process_DeepCopyActions {
while ( $node ) {
# Capture deepCopyActions directives.
if ( $node->{'type'} eq "deepCopyActions" ) {
$node->{'directive'}->{'processed'} = 1;
# Extract the directive.
push(@directiveNodes,$node);
# Get state storables database if we do not have it.
Expand Down
4 changes: 2 additions & 2 deletions perl/Galacticus/Build/SourceTree/Process/ForEach.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ sub Process_ForEach {
my $node = $tree;
my $depth = 0;
while ( $node ) {
if ( $node->{'type'} eq "forEach" && ! $node->{'processed'} ) {
if ( $node->{'type'} eq "forEach" && ! $node->{'directive'}->{'processed'} ) {
# Record that node is processed.
$node->{'processed'} = 1;
$node->{'directive'}->{'processed'} = 1;
# Get the declaration and determine rank.
my $declaration = &Galacticus::Build::SourceTree::Parse::Declarations::GetDeclaration($node->{'parent'},$node->{'directive'}->{'variable'});
my $rank = 0;
Expand Down
14 changes: 9 additions & 5 deletions perl/Galacticus/Build/SourceTree/Process/FunctionClass.pm
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,28 @@ sub Process_FunctionClass {
our $deepCopyActions;
# Determine if debugging output is required.
our $debugging = exists($ENV{'GALACTICUS_OBJECTS_DEBUG'}) && $ENV{'GALACTICUS_OBJECTS_DEBUG'} eq "yes";
# Get state storables database if we do not have it.
$stateStorables = $xml->XMLin($ENV{'BUILDPATH'}."/stateStorables.xml")
unless ( $stateStorables );
# Walk the tree, looking for code blocks.
my $node = $tree;
my $depth = 0;
while ( $node ) {
while ( $node ) {
if ( grep {$node->{'type'}."Class" eq $_} keys(%{$stateStorables->{'functionClasses'}}) ) {
$node->{'directive'}->{'processed'} = 1;
}
if ( $node->{'type'} eq "functionClass" ) {
# Assert that our parent is a module.
die("Process_FunctionClass: parent node must be a module")
unless ( $node->{'parent'}->{'type'} eq "module" );
my $lineNumber = $node->{'line'};
# Extract the directive.
# Extract the directive and mark as processed.
my $directive = $node->{'directive'};
$directive->{'processed'} = 1;
# Get code directive locations if we do not have them.
$directiveLocations = $xml->XMLin($ENV{'BUILDPATH'}."/directiveLocations.xml")
unless ( $directiveLocations );
# Get state storables database if we do not have it.
$stateStorables = $xml->XMLin($ENV{'BUILDPATH'}."/stateStorables.xml")
unless ( $stateStorables );
# Get state storables database if we do not have it.
$deepCopyActions = $xml->XMLin($ENV{'BUILDPATH'}."/deepCopyActions.xml")
unless ( $deepCopyActions );
# Find methods.
Expand Down
1 change: 1 addition & 0 deletions perl/Galacticus/Build/SourceTree/Process/Generics.pm
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sub Process_Generics {
my $depth = 0;
while ( $node ) {
if ( $node->{'type'} eq "generic" ) {
$node->{'directive'}->{'processed'} = 1;
# Build regExs.
my $genericRegEx = qr/\{$node->{'directive'}->{'identifier'}¦.*\}/;
# Iterate over all sibling nodes.
Expand Down
34 changes: 34 additions & 0 deletions perl/Galacticus/Build/SourceTree/Process/NonProcessed.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Contains a Perl module which implements allocation of variables with some extra, useful functionality.

package Galacticus::Build::SourceTree::Process::NonProcessed;
use strict;
use warnings;
use Cwd;
use lib $ENV{'GALACTICUS_EXEC_PATH'}."/perl";

# Insert hooks for our functions.
$Galacticus::Build::SourceTree::Hooks::processHooks {'nonProcessed'} = \&Process_NonProcessed;
$Galacticus::Build::SourceTree::Hooks::processDependencies{'nonProcessed'} = [ "generics" ];

sub Process_NonProcessed {
# Get the tree.
my $tree = shift();
# Non-processed directives that we simply mark as processed to avoid warnings.
my @nonProcessedDirectives = ( "methods", "workaround", "include", "functionGlobal", "component", "radiusSolverPlausibility", "interTreePositionInsert", "expiry", "scoping", "functionClassType" );
# Walk the tree, looking for our directive.
my $node = $tree;
my $depth = 0;
while ( $node ) {
if (
$node->{'type'} =~ m/Task$/
||
grep {$node->{'type'} eq $_} @nonProcessedDirectives
) {
# Record that node is processed.
$node->{'directive'}->{'processed'} = 1;
}
$node = &Galacticus::Build::SourceTree::Walk_Tree($node,\$depth);
}
}

1;
4 changes: 2 additions & 2 deletions perl/Galacticus/Build/SourceTree/Process/OptionalArgument.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ sub Process_OptionalArguments {
my $node = $tree;
my $depth = 0;
while ( $node ) {
if ( $node->{'type'} eq "optionalArgument" && ! $node->{'processed'} ) {
if ( $node->{'type'} eq "optionalArgument" && ! $node->{'directive'}->{'processed'} ) {
# Record that node is processed.
$node->{'processed'} = 1;
$node->{'directive'}->{'processed'} = 1;
# Generate declaration and setting code for the optional argument.
my $declaration = &Galacticus::Build::SourceTree::Parse::Declarations::GetDeclaration($node->{'parent'},$node->{'directive'}->{'name'});
# Change the name of the declared variable, strip argument-based attributes.
Expand Down

0 comments on commit 60df160

Please sign in to comment.