44 * INTERNAL: Do not use.
55 */
66
7+ private import codeql.files.FileSystem
78private import codeql.rust.elements.internal.generated.Function
9+ private import codeql.rust.elements.Comment
810
911/**
1012 * INTERNAL: This module contains the customizable definition of `Function` and should not
@@ -26,5 +28,46 @@ module Impl {
2628 */
2729 class Function extends Generated:: Function {
2830 override string toStringImpl ( ) { result = "fn " + this .getName ( ) .getText ( ) }
31+
32+ pragma [ nomagic]
33+ private predicate hasPotentialCommentAt ( File f , int line ) {
34+ f = this .getLocation ( ) .getFile ( ) and
35+ // When a function is preceded by comments its start line is the line of
36+ // the first comment. Hence all relevant comments are found by including
37+ // comments from the start line and up to the line with the function
38+ // name.
39+ line in [ this .getLocation ( ) .getStartLine ( ) .. this .getName ( ) .getLocation ( ) .getStartLine ( ) ]
40+ }
41+
42+ /**
43+ * Gets a comment preceding this function.
44+ *
45+ * A comment is considered preceding if it occurs immediately before this
46+ * function or if only other comments occur between the comment and this
47+ * function.
48+ */
49+ Comment getAPrecedingComment ( ) {
50+ exists ( File f , int line |
51+ this .hasPotentialCommentAt ( f , line ) and
52+ result .getLocation ( ) .hasLocationFileInfo ( f , line , _, _, _)
53+ )
54+ }
55+
56+ /**
57+ * Gets a comment preceding this function.
58+ *
59+ * A comment is considered preceding if it occurs immediately before this
60+ * function or if only other comments occur between the comment and this
61+ * function.
62+ */
63+ Comment getPrecedingComment ( ) {
64+ result .getLocation ( ) .getFile ( ) = this .getLocation ( ) .getFile ( ) and
65+ // When a function is preceded by comments its start line is the line of
66+ // the first comment. Hence all relevant comments are found by including
67+ // comments from the start line and up to the line with the function
68+ // name.
69+ this .getLocation ( ) .getStartLine ( ) <= result .getLocation ( ) .getStartLine ( ) and
70+ result .getLocation ( ) .getStartLine ( ) <= this .getName ( ) .getLocation ( ) .getStartLine ( )
71+ }
2972 }
3073}
0 commit comments