Skip to content

Commit

Permalink
store and output LocationOfOverwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelPFrey committed Apr 2, 2022
1 parent 67240c5 commit b53746c
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/core/Assignment.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Assignment : public ASTNode
{
public:
Assignment(std::string name, const Location& loc)
: ASTNode(loc), name(name) { }
: ASTNode(loc), name(name), locOfOverwrite(Location::NONE) { }
Assignment(std::string name,
shared_ptr<class Expression> expr = shared_ptr<class Expression>(),
const Location& loc = Location::NONE)
: ASTNode(loc), name(name), expr(expr) { }
: ASTNode(loc), name(name), expr(expr), locOfOverwrite(Location::NONE){ }

void print(std::ostream& stream, const std::string& indent) const override;
const std::string& getName() const { return name; }
Expand All @@ -29,10 +29,14 @@ class Assignment : public ASTNode
virtual bool hasAnnotations() const;
virtual const Annotation *annotation(const std::string& name) const;

const Location& locationOfOverwrite() const { return locOfOverwrite; }
void setLocationOfOverwrite(const Location& locOfOverwrite) { this->locOfOverwrite = locOfOverwrite; }

protected:
const std::string name;
shared_ptr<class Expression> expr;
AnnotationMap annotations;
Location locOfOverwrite/*=Location::NONE*/;
};

template <class ... Args> shared_ptr<Assignment> assignment(Args... args) {
Expand Down
7 changes: 6 additions & 1 deletion src/core/ScopeContext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ void ScopeContext::init()
set_variable(assignment->getName(), assignment->getExpr()->evaluate(get_shared_ptr()));
} catch (EvaluationException& e) {
if (e.traceDepth > 0) {
LOG(message_group::Trace, assignment->location(), this->documentRoot(), "assignment to '%1$s'", assignment->getName());
if(assignment->locationOfOverwrite().isNone()){
LOG(message_group::Trace, assignment->location(), this->documentRoot(), "assignment to '%1$s'", assignment->getName());
} else {
LOG(message_group::Trace, assignment->location(), this->documentRoot(), "overwritten assignment to '%1$s' (this is where the assignment is evaluated)", assignment->getName());
LOG(message_group::Trace, assignment->locationOfOverwrite(), this->documentRoot(), "overwritting assignment to '%1$s'", assignment->getName());
}
e.traceDepth--;
}
throw;
Expand Down
2 changes: 1 addition & 1 deletion src/core/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ void handle_assignment(const std::string token, Expression *expr, const Location
warn_reassignment(loc, assignment, mainFilePath, uncPathPrev);
}
assignment->setExpr(shared_ptr<Expression>(expr));
//assignment->setLocation(loc); //do not update the location
assignment->setLocationOfOverwrite(loc);
found = true;
break;
}
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ list(APPEND MISC_FILES
${TEST_SCAD_DIR}/misc/allfunctions.scad
${TEST_SCAD_DIR}/misc/allmodules.scad
${TEST_SCAD_DIR}/misc/special-consts.scad
${TEST_SCAD_DIR}/misc/variable-overwrite.scad
)


Expand Down
12 changes: 12 additions & 0 deletions tests/regression/astdumptest/include-overwrite-main2-expected.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//Description("this works as intuitively expected")
//Parameter("")
overwriteFalse = false;
after = overwriteFalse;
before = overwriteTrue;
//Description("this causes 'before' to be undef")
//Parameter("")
overwriteTrue = true;
echo("after");
echo(after = after);
echo("before");
echo(before = before);
3 changes: 3 additions & 0 deletions tests/regression/astdumptest/variable-overwrite-expected.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a = assert(b);
//Parameter("")
b = true;
1 change: 1 addition & 0 deletions tests/regression/dumptest/variable-overwrite-expected.csg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions tests/regression/echotest/variable-overwrite-expected.echo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
WARNING: a was assigned on line 2 but was overwritten in file variable-overwrite.scad, line 4
WARNING: Ignoring unknown variable 'b' in file variable-overwrite.scad, line 4
ERROR: Assertion 'b' failed in file variable-overwrite.scad, line 4
TRACE: overwritten assignment to 'a' (this is where the assignment is evaluated) in file variable-overwrite.scad, line 2
TRACE: overwritting assignment to 'a' in file variable-overwrite.scad, line 4

0 comments on commit b53746c

Please sign in to comment.