Skip to content

Commit

Permalink
Merge pull request lfortran#4552 from Pranavchiku/gh4544
Browse files Browse the repository at this point in the history
enh: disable warning when global save attr is present
  • Loading branch information
certik authored Jul 28, 2024
2 parents c489af3 + 69fbb09 commit 4516ba8
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 6 deletions.
34 changes: 29 additions & 5 deletions src/lfortran/semantics/ast_common_visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,9 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
std::map<uint32_t, std::map<std::string, ASR::symbol_t*>> &instantiate_symbols;
std::vector<ASR::stmt_t*> &data_structure;

// global save variable
bool is_global_save_enabled = false;

// implied do loop nesting
int idl_nesting_level = 0;

Expand Down Expand Up @@ -1999,6 +2002,25 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
}
}


template <typename T>
void check_if_global_save_is_enabled(T &x) {
for ( size_t i = 0; i < x.n_decl; i++ ) {
if ( AST::is_a<AST::Declaration_t>(*x.m_decl[i]) ) {
AST::Declaration_t* decl = AST::down_cast<AST::Declaration_t>(x.m_decl[i]);
if ( decl->n_attributes > 0 && decl->n_syms == 0 &&
decl->m_trivia == nullptr &&
AST::is_a<AST::SimpleAttribute_t>(*decl->m_attributes[0]) ) {
AST::SimpleAttribute_t* attr = AST::down_cast<AST::SimpleAttribute_t>(decl->m_attributes[0]);
if ( attr->m_attr == AST::simple_attributeType::AttrSave ) {
is_global_save_enabled = true;
break;
}
}
}
}
}

void create_external_function(std::string sym, Location loc, ASR::ttype_t* determined_type = nullptr) {
if (compiler_options.implicit_interface) {
bool is_subroutine = false;
Expand Down Expand Up @@ -3205,11 +3227,13 @@ class CommonVisitor : public AST::BaseVisitor<Derived> {
}
if (is_Function && implicit_save && !is_save) {
// throw warning to that particular variable
diag.semantic_warning_label(
"Assuming implicit save attribute for variable declaration",
{x.m_syms[i].loc},
"help: add explicit save attribute or parameter attribute or initialize in a separate statement"
);
if ( !is_global_save_enabled ) {
diag.semantic_warning_label(
"Assuming implicit save attribute for variable declaration",
{x.m_syms[i].loc},
"help: add explicit save attribute or parameter attribute or initialize in a separate statement"
);
}
}
if( std::find(excluded_from_symtab.begin(), excluded_from_symtab.end(), sym) == excluded_from_symtab.end() ) {
if ( !is_implicitly_declared && !is_external) {
Expand Down
11 changes: 11 additions & 0 deletions src/lfortran/semantics/ast_symboltable_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
}
}
simd_variables.clear();
bool is_global_save_enabled_copy = is_global_save_enabled;
check_if_global_save_is_enabled( x );
for (size_t i=0; i<x.n_use; i++) {
visit_unit_decl1(*x.m_use[i]);
}
Expand Down Expand Up @@ -559,6 +561,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {

fix_type_info(ASR::down_cast2<ASR::Program_t>(tmp));
mark_common_blocks_as_declared();
is_global_save_enabled = is_global_save_enabled_copy;
}

bool subroutine_contains_entry_function(std::string subroutine_name, AST::stmt_t** body, size_t n_body) {
Expand Down Expand Up @@ -939,6 +942,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
char *bindc_name=nullptr;
extract_bind(x, current_procedure_abi_type, bindc_name);

// iterate over declarations and check if global save is present
bool is_global_save_enabled_copy = is_global_save_enabled;
check_if_global_save_is_enabled( x );
for (size_t i=0; i<x.n_use; i++) {
visit_unit_decl1(*x.m_use[i]);
}
Expand Down Expand Up @@ -1124,6 +1130,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
in_Subroutine = false;
is_template = false;
mark_common_blocks_as_declared();
is_global_save_enabled = is_global_save_enabled_copy;
}

AST::AttrType_t* find_return_type(AST::decl_attribute_t** attributes,
Expand Down Expand Up @@ -1268,6 +1275,9 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
char *bindc_name=nullptr;
extract_bind(x, current_procedure_abi_type, bindc_name);

// iterate over declarations and check if global save is present
bool is_global_save_enabled_copy = is_global_save_enabled;
check_if_global_save_is_enabled( x );
for (size_t i=0; i<x.n_use; i++) {
visit_unit_decl1(*x.m_use[i]);
}
Expand Down Expand Up @@ -1549,6 +1559,7 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
current_function_dependencies = current_function_dependencies_copy;
in_Subroutine = false;
mark_common_blocks_as_declared();
is_global_save_enabled = is_global_save_enabled_copy;
}

void visit_Declaration(const AST::Declaration_t& x) {
Expand Down
13 changes: 13 additions & 0 deletions tests/reference/asr-save3-025da43.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"basename": "asr-save3-025da43",
"cmd": "lfortran --show-asr --no-color {infile} -o {outfile}",
"infile": "tests/save3.f90",
"infile_hash": "73a25f7c7513eff87c70b3f4e4d291178ff58078c3f57c81f356d77f",
"outfile": null,
"outfile_hash": null,
"stdout": "asr-save3-025da43.stdout",
"stdout_hash": "7b1b85844dc907929fc93c75f7d95194967a2bd2c7fbb458fb2fbac4",
"stderr": null,
"stderr_hash": null,
"returncode": 0
}
59 changes: 59 additions & 0 deletions tests/reference/asr-save3-025da43.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(TranslationUnit
(SymbolTable
1
{
f:
(Function
(SymbolTable
2
{
var:
(Variable
2
var
[]
Local
(RealConstant
0.000000
(Real 4)
)
(RealConstant
0.000000
(Real 4)
)
Save
(Real 4)
()
Source
Public
Required
.false.
)
})
f
(FunctionType
[]
()
Source
Implementation
()
.false.
.false.
.false.
.false.
.false.
[]
.false.
)
[]
[]
[]
()
Public
.false.
.false.
()
)
})
[]
)
4 changes: 4 additions & 0 deletions tests/save3.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
subroutine f()
real :: var = 0.0
save
end subroutine
6 changes: 5 additions & 1 deletion tests/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3403,6 +3403,10 @@ asr = true
filename = "save2.f90"
asr = true

[[test]]
filename = "save3.f90"
asr = true

[[test]]
filename = "../integration_tests/c_ptr_02.f90"
asr = true
Expand Down Expand Up @@ -4072,4 +4076,4 @@ asr = true

[[test]]
filename = "errors/intrinsics11.f90"
asr = true
asr = true

0 comments on commit 4516ba8

Please sign in to comment.