Skip to content

Commit ce8bd55

Browse files
committed
Merge branch 'topic/final_package' into 'master'
First implementation for `final_package` check See merge request eng/libadalang/langkit-query-language!122
2 parents 2c178ef + 3c32733 commit ce8bd55

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
fun is_final_pkg(pkg) =
2+
|" Whether pkg is a final package; for the moment this means only packages
3+
|" with the `Annotate => (GNATcheck, Final)` aspect, but ultimately this
4+
|" will also include packages annotated with the `Final` aspect, as such:
5+
|"
6+
|" .. code-block:: ada
7+
|"
8+
|" package Pkg with Final is
9+
|" end Pkg;
10+
pkg is BasePackageDecl when {
11+
val aspect = pkg.p_get_aspect("Annotate");
12+
aspect.exists and aspect.value is Aggregate(
13+
f_assocs is a@* when a[1] is "GNATcheck" and a[2] is "Final"
14+
)
15+
}
16+
17+
@check(message="child package cannot have a final parent package",
18+
category="Style", subcategory="Programming Practice")
19+
fun final_package(node) =
20+
|" Check that package declarations annotated as final don't have child
21+
|" packages
22+
|"
23+
|" .. note:: We don't do a transitive check, so grandchild packages won't
24+
|" be flagged. We consider this is not necessary, because the child
25+
|" package will be flagged anyway.
26+
|"
27+
|" Here is an example:
28+
|"
29+
|" .. code-block:: ada
30+
|"
31+
|" package Pkg with Annotate => (GNATcheck, Final) is
32+
|" end Pkg;
33+
|"
34+
|" package Pkg.Child is -- FLAGGED
35+
|" end Pkg.Child;
36+
|"
37+
|" package Pkg.Child.Grandchild is -- NOT FLAGGED
38+
|" end Pkg.Child.Grandchild;
39+
node is BasePackageDecl(parent is LibraryItem)
40+
when is_final_pkg(node.p_semantic_parent())
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- No need to explicitly flag grandchild package, since it cannot exist when
2+
-- you remove the child one
3+
4+
package Foo.Child.Grandchild is -- NOFLAG
5+
end Foo.Child.Grandchild;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package Foo.Child is -- FLAG
2+
end Foo.Child;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package Foo with Annotate => (GNATcheck, Final) is
2+
end Foo;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
foo-child.ads:1:9: rule violation: child package cannot have a final parent package
2+
1 | package Foo.Child is -- FLAG
3+
| ^^^^^^^^^
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
driver: 'checker'
2+
rule_name: Final_Package
3+
input_sources: ['foo.ads', 'foo-child.ads', 'foo-child-grandchild.ads']

0 commit comments

Comments
 (0)