Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/topic/etyp/encompass-children-locs'
Browse files Browse the repository at this point in the history
* origin/topic/etyp/encompass-children-locs:
  Encompass child node's location in parent.
  • Loading branch information
rsmmr committed Oct 25, 2024
2 parents 66c8469 + 3eb1b0a commit 012fcb3
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 298 deletions.
6 changes: 5 additions & 1 deletion hilti/toolchain/include/ast/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ class AttributeSet : public Node {
bool has(std::string_view tag) const { return find(tag) != nullptr; }

/** Adds an attribute to the set. */
void add(ASTContext* ctx, Attribute* a) { addChild(ctx, a); }
void add(ASTContext* ctx, Attribute* a) {
addChild(ctx, a);
// Combine this location with the attribute's location so this spans the range
setMeta(meta().mergeLocation(a->location()));
}

/** Removes all attributes of the given tag. */
void remove(std::string_view tag);
Expand Down
6 changes: 6 additions & 0 deletions hilti/toolchain/include/ast/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class Location {
auto from() const { return _from_line; }
auto to() const { return _to_line; }

/**
* Merges this Location with the provided location. Returns new a location
* which encompasses the entire span.
*/
Location merge(const Location& loc) const;

/**
* Returns a string representation of the location.
*
Expand Down
2 changes: 2 additions & 0 deletions hilti/toolchain/include/ast/meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class Meta {
void setLocation(Location l) { _location = std::move(l); }
void setComments(Comments c) { _comments = std::move(c); }

Meta mergeLocation(const Location& l) const { return Meta(location().merge(l), _comments); }

/**
* Returns true if the location does not equal a default constructed
* instance.
Expand Down
1 change: 0 additions & 1 deletion hilti/toolchain/include/ast/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <memory>
#include <optional>
#include <ostream>
#include <set>
#include <string>
#include <unordered_set>
#include <utility>
Expand Down
16 changes: 16 additions & 0 deletions hilti/toolchain/src/ast/location.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.

#include <algorithm>
#include <tuple>

#include <hilti/ast/location.h>
#include <hilti/base/util.h>

Expand All @@ -9,6 +12,19 @@ const Location location::None;

Location::operator bool() const { return _file != location::None._file; }

Location Location::merge(const Location& loc) const {
if ( _file != loc._file )
return *this;

auto [from_line, from_character] =
std::min(std::tie(_from_line, _from_character), std::tie(loc._from_line, loc._from_character));

auto [to_line, to_character] =
std::max(std::tie(_to_line, _to_character), std::tie(loc._to_line, loc._to_character));

return Location(_file, from_line, to_line, from_character, to_character);
}

std::string Location::dump(bool no_path) const {
if ( ! *this )
return "<no location>";
Expand Down
3 changes: 1 addition & 2 deletions spicy/toolchain/src/compiler/validator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ struct VisitorPost : visitor::PreOrder, hilti::validator::VisitorMixIn {
const bool isAlias = n->type()->print() != n->typeID().str();
if ( isAlias ) {
if ( ! n->attributes()->attributes().empty() )
// TODO(#1893): This should diagnose on the attribute set
error("attributes are not allow on type aliases", n);
error("attributes are not allowed on type aliases", n->attributes());
}

if ( n->linkage() == hilti::declaration::Linkage::Public && n->type()->alias() ) {
Expand Down
116 changes: 58 additions & 58 deletions tests/Baseline/hilti.ast.basic-module/debug.log

Large diffs are not rendered by default.

116 changes: 58 additions & 58 deletions tests/Baseline/hilti.ast.coercion/output

Large diffs are not rendered by default.

116 changes: 58 additions & 58 deletions tests/Baseline/hilti.ast.imported-id/output

Large diffs are not rendered by default.

116 changes: 58 additions & 58 deletions tests/Baseline/hilti.ast.types/output

Large diffs are not rendered by default.

116 changes: 58 additions & 58 deletions tests/Baseline/hilti.expressions.ctor-replacement/output

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
[error] <...>/invalid-type-attributes.spicy:11:1-11:23: attributes are not allow on type aliases
[error] <...>/invalid-type-attributes.spicy:12:1-12:23: attributes are not allow on type aliases
[error] <...>/invalid-type-attributes.spicy:15:1-15:46: attributes are not allow on type aliases
[error] <...>/invalid-type-attributes.spicy:16:1-16:53: attributes are not allow on type aliases
[error] <...>/invalid-type-attributes.spicy:11:17-11:22: attributes are not allowed on type aliases
[error] <...>/invalid-type-attributes.spicy:12:15-12:22: attributes are not allowed on type aliases
[error] <...>/invalid-type-attributes.spicy:15:28-15:45: attributes are not allowed on type aliases
[error] <...>/invalid-type-attributes.spicy:16:20-16:52: attributes are not allowed on type aliases
[error] spicyc: aborting after errors

0 comments on commit 012fcb3

Please sign in to comment.