Skip to content

Commit

Permalink
Delete dangling source info from macro expansion (#934)
Browse files Browse the repository at this point in the history
* Delete dangling source info from macro expansion

Signed-off-by: Justin King <jcking@google.com>

* Fix go doc

Signed-off-by: Justin King <jcking@google.com>

* Fix test

Signed-off-by: Justin King <jcking@google.com>

---------

Signed-off-by: Justin King <jcking@google.com>
  • Loading branch information
jcking committed Apr 26, 2024
1 parent 65f4686 commit cbe072e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
7 changes: 7 additions & 0 deletions common/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ func (s *SourceInfo) SetOffsetRange(id int64, o OffsetRange) {
s.offsetRanges[id] = o
}

// ClearOffsetRange removes the OffsetRange for the given expression id.
func (s *SourceInfo) ClearOffsetRange(id int64) {
if s != nil {
delete(s.offsetRanges, id)
}
}

// GetStartLocation calculates the human-readable 1-based line and 0-based column of the first character
// of the expression node at the id.
func (s *SourceInfo) GetStartLocation(id int64) common.Location {
Expand Down
8 changes: 0 additions & 8 deletions common/ast/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,18 +284,10 @@ func TestSourceInfoToProto(t *testing.T) {
key: 6
value: 15
}
positions: {
key: 7
value: 28
}
positions: {
key: 8
value: 29
}
positions: {
key: 9
value: 35
}
positions: {
key: 10
value: 36
Expand Down
7 changes: 7 additions & 0 deletions parser/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func (p *parserHelper) id(ctx any) int64 {
return id
}

func (p *parserHelper) deleteId(id int64) {
p.sourceInfo.ClearOffsetRange(id)
if id == p.nextID-1 {
p.nextID--
}
}

func (p *parserHelper) getLocation(id int64) common.Location {
return p.sourceInfo.GetStartLocation(id)
}
Expand Down
9 changes: 6 additions & 3 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,12 @@ func (p *parser) expandMacro(exprID int64, function string, target ast.Expr, arg
expr, err := macro.Expander()(eh, target, args)
// An error indicates that the macro was matched, but the arguments were not well-formed.
if err != nil {
if err.Location != nil {
return p.reportError(err.Location, err.Message), true
loc := err.Location
if loc == nil {
loc = p.helper.getLocation(exprID)
}
return p.reportError(p.helper.getLocation(exprID), err.Message), true
p.helper.deleteId(exprID)
return p.reportError(loc, err.Message), true
}
// A nil value from the macro indicates that the macro implementation decided that
// an expansion should not be performed.
Expand All @@ -929,6 +931,7 @@ func (p *parser) expandMacro(exprID int64, function string, target ast.Expr, arg
if p.populateMacroCalls {
p.helper.addMacroCall(expr.ID(), function, target, args...)
}
p.helper.deleteId(exprID)
return expr, true
}

Expand Down

0 comments on commit cbe072e

Please sign in to comment.