Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check unit empty #883

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion src/parser/nmodl.yy
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ units : {

unit : "(" { scanner.scan_unit(); } ")"
{
// @todo Empty units should be handled in semantic analysis
auto unit = scanner.get_unit();
auto text = unit->eval();
$$ = new ast::Unit(unit);
Expand Down
9 changes: 9 additions & 0 deletions src/visitors/semantic_analysis_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ast/string.hpp"
#include "ast/suffix.hpp"
#include "ast/table_statement.hpp"
#include "ast/unit.hpp"
#include "symtab/symbol_properties.hpp"
#include "utils/logger.hpp"
#include "visitors/visitor_utils.hpp"
Expand Down Expand Up @@ -163,5 +164,13 @@ void SemanticAnalysisVisitor::visit_mutex_unlock(const ast::MutexUnlock& /* node
/// -->
}

void SemanticAnalysisVisitor::visit_unit(const ast::Unit& node) {
/// <-- This code is for check 8
if (node.get_name()->get_value().empty()) {
logger->error("SemanticAnalysisVisitor:: An unit cannot be created without name.");
check_fail = true;
}
}

} // namespace visitor
} // namespace nmodl
6 changes: 5 additions & 1 deletion src/visitors/semantic_analysis_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
* 3. A TABLE statement in functions cannot have name list, and should have one in procedures.
* 4. Check if ion variables from a `USEION` statement are not declared in `CONSTANT` block.
* 5. Check if an independent variable is not 't'.
* 6. Check that mutex are not badly use
* 6. Check that mutex are not badly use.
* 7. Check than function table got at least one argument.
* 8. Check that unit always have a name.
*/
#include "ast/ast.hpp"
#include "visitors/ast_visitor.hpp"
Expand Down Expand Up @@ -82,6 +83,9 @@ class SemanticAnalysisVisitor: public ConstAstVisitor {
/// Look if MUTEXUNLOCK is outside a locked block
void visit_mutex_unlock(const ast::MutexUnlock& node) override;

/// Visit an unit and check that name is not empty
void visit_unit(const ast::Unit& node) override;

public:
SemanticAnalysisVisitor(bool accel_backend = false)
: accel_backend(accel_backend) {}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/visitor/semantic_analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,34 @@ SCENARIO("FUNCTION_TABLE block", "[visitor][semantic_analysis]") {
}
}
}

SCENARIO("UNITS block", "[visitor][semantic_analysis]") {
GIVEN("A mod file with UNITS empty") {
std::string nmodl_text = R"(
UNITS {}
)";
THEN("Semantic analysis should success") {
REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
}
}
GIVEN("A mod file with UNITS with space as name") {
std::string nmodl_text = R"(
UNITS {
( ) = (millivolt)
}
)";
THEN("Semantic analysis should success") {
REQUIRE_FALSE(run_semantic_analysis_visitor(nmodl_text));
}
}
GIVEN("A mod file with UNITS with no name") {
std::string nmodl_text = R"(
UNITS {
() = (millivolt)
}
)";
THEN("Semantic analysis should fail") {
REQUIRE(run_semantic_analysis_visitor(nmodl_text));
}
}
}