Skip to content

Commit

Permalink
C#: Take mapped locations into account in Comments.qll
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Oct 28, 2024
1 parent c93e438 commit 7910af1
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 7 deletions.
11 changes: 9 additions & 2 deletions csharp/ql/lib/semmle/code/csharp/Comments.qll
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import Element
import Location

private Location unmapLoc(Location l) {
result.(SourceLocation).getMappedLocation() = l
or
not exists(result.(SourceLocation).getMappedLocation()) and
result = l
}

/**
* A single line of comment.
*
Expand All @@ -19,7 +26,7 @@ class CommentLine extends @commentline {
string toString() { none() }

/** Gets the location of this comment line. */
Location getLocation() { commentline_location(this, result) }
Location getLocation() { commentline_location(this, unmapLoc(result)) }

/** Gets the containing comment block. */
CommentBlock getParent() { result.getAChild() = this }
Expand Down Expand Up @@ -159,7 +166,7 @@ class CommentBlock extends @commentblock {
string toString() { result = this.getChild(0).toString() }

/** Gets the location of this comment block */
Location getLocation() { commentblock_location(this, result) }
Location getLocation() { commentblock_location(this, unmapLoc(result)) }

/** Gets the number of lines in this comment block. */
int getNumLines() { result = count(this.getAChild()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
| comments1.cs:1:1:2:46 | // ... | comments1.cs:9:7:9:7 | C | Start of comment1.cs |
| comments1.cs:1:16:1:46 | // ... | comments1.cs:2:9:2:14 | ... ...; | A mapped single-line comment |
| comments1.cs:4:1:4:25 | // ... | comments1.cs:9:7:9:7 | C | 1) Basic comment types |
| comments1.cs:6:1:6:24 | // ... | comments1.cs:9:7:9:7 | C | A single-line comment |
| comments1.cs:8:1:8:18 | /// ... | comments1.cs:9:7:9:7 | C | An XML comment |
Expand Down Expand Up @@ -57,5 +58,4 @@
| comments2.cs:127:20:127:23 | // ... | comments2.cs:128:9:128:17 | return ...; | x |
| trivia.cs:1:1:3:15 | // ... | trivia.cs:14:7:14:9 | Tr1 | |
| trivia.cs:13:89:13:103 | // ... | trivia.cs:14:7:14:9 | Tr1 | New checksum |
| trivia.cs:19:16:19:46 | // ... | comments1.cs:2:9:2:14 | ... ...; | A mapped single-line comment |
| trivia.cs:25:14:25:38 | // ... | trivia.cs:26:9:26:17 | ... ...; | numbering not affected |
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| comments1.cs:1:16:1:46 | // ... | comments1.cs:1:9:1:14 | ... ...; | A mapped single-line comment |
| comments1.cs:11:1:11:25 | /* ... */ | comments1.cs:9:7:9:7 | C | A multiline comment |
| comments1.cs:13:1:13:20 | // ... | comments1.cs:9:7:9:7 | C | 2) Comment blocks |
| comments1.cs:15:1:15:38 | // ... | comments1.cs:9:7:9:7 | C | A line on its own is a commentblock |
Expand Down Expand Up @@ -48,5 +49,4 @@
| comments2.cs:124:5:124:16 | // ... | comments2.cs:119:11:119:25 | GenericClass`1 | GenericFn |
| comments2.cs:127:20:127:23 | // ... | comments2.cs:127:9:127:18 | ... ...; | x |
| comments2.cs:132:1:132:21 | // ... | comments2.cs:11:7:11:8 | C2 | End of comment2.cs |
| trivia.cs:19:16:19:46 | // ... | comments1.cs:1:9:1:14 | ... ...; | A mapped single-line comment |
| trivia.cs:25:14:25:38 | // ... | trivia.cs:24:9:24:16 | ... ...; | numbering not affected |
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| comments1.cs:1:16:1:46 | // ... | trivia.cs:17:5:29:5 | {...} | A mapped single-line comment |
| comments1.cs:36:5:38:41 | /* ... */ | comments1.cs:34:7:34:9 | Foo | |
| comments1.cs:39:13:40:51 | // ... | comments1.cs:34:7:34:9 | Foo | as this line |
| comments1.cs:42:15:42:36 | // ... | comments1.cs:34:7:34:9 | Foo | These are different |
Expand Down Expand Up @@ -47,7 +48,6 @@
| comments2.cs:121:17:121:20 | // ... | comments2.cs:119:11:119:25 | GenericClass`1 | f |
| comments2.cs:124:5:124:16 | // ... | comments2.cs:11:7:11:8 | C2 | GenericFn |
| comments2.cs:127:20:127:23 | // ... | comments2.cs:126:5:129:5 | {...} | x |
| trivia.cs:19:16:19:46 | // ... | trivia.cs:17:5:29:5 | {...} | A mapped single-line comment |
| trivia.cs:25:14:25:38 | // ... | trivia.cs:17:5:29:5 | {...} | numbering not affected |
| trivia.cs:49:18:49:82 | // ... | trivia.cs:48:5:58:5 | {...} | Sets the nullable annotation and warning contexts to disabled. |
| trivia.cs:50:17:50:80 | // ... | trivia.cs:48:5:58:5 | {...} | Sets the nullable annotation and warning contexts to enabled. |
Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/test/library-tests/comments/Bindings.expected
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
| comments1.cs:1:16:1:46 | // ... | comments1.cs:1:9:1:14 | ... ...; | A mapped single-line comment |
| comments1.cs:8:1:8:18 | /// ... | comments1.cs:9:7:9:7 | C | An XML comment |
| comments1.cs:36:5:38:41 | /* ... */ | comments1.cs:39:9:39:9 | x | |
| comments1.cs:39:13:40:51 | // ... | comments1.cs:39:9:39:9 | x | as this line |
Expand Down Expand Up @@ -50,7 +51,6 @@
| comments2.cs:124:5:124:16 | // ... | comments2.cs:125:9:125:20 | GenericFn`1 | GenericFn |
| comments2.cs:127:20:127:23 | // ... | comments2.cs:127:9:127:18 | ... ...; | x |
| trivia.cs:13:89:13:103 | // ... | trivia.cs:14:7:14:9 | Tr1 | New checksum |
| trivia.cs:19:16:19:46 | // ... | comments1.cs:1:9:1:14 | ... ...; | A mapped single-line comment |
| trivia.cs:25:14:25:38 | // ... | trivia.cs:17:5:29:5 | {...} | numbering not affected |
| trivia.cs:49:18:49:82 | // ... | trivia.cs:48:5:58:5 | {...} | Sets the nullable annotation and warning contexts to disabled. |
| trivia.cs:50:17:50:80 | // ... | trivia.cs:48:5:58:5 | {...} | Sets the nullable annotation and warning contexts to enabled. |
Expand Down
2 changes: 1 addition & 1 deletion csharp/ql/test/library-tests/comments/Comments.expected
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
singlelineComment
| comments1.cs:1:1:2:46 | // ... | comments1.cs:1:1:1:23 | // ... | 2 | Start of comment1.cs | // Start of comment1.cs |
| comments1.cs:1:1:2:46 | // ... | comments1.cs:2:1:2:46 | // ... | 2 | This tests the basic types of comment block | // This tests the basic types of comment block |
| comments1.cs:1:16:1:46 | // ... | comments1.cs:1:16:1:46 | // ... | 1 | A mapped single-line comment | // A mapped single-line comment |
| comments1.cs:4:1:4:25 | // ... | comments1.cs:4:1:4:25 | // ... | 1 | 1) Basic comment types | // 1) Basic comment types |
| comments1.cs:6:1:6:24 | // ... | comments1.cs:6:1:6:24 | // ... | 1 | A single-line comment | // A single-line comment |
| comments1.cs:13:1:13:20 | // ... | comments1.cs:13:1:13:20 | // ... | 1 | 2) Comment blocks | // 2) Comment blocks |
Expand Down Expand Up @@ -74,7 +75,6 @@ singlelineComment
| trivia.cs:1:1:3:15 | // ... | trivia.cs:2:1:2:21 | // ... | 3 | Start of trivia.cs | // Start of trivia.cs |
| trivia.cs:1:1:3:15 | // ... | trivia.cs:3:1:3:15 | // ... | 3 | Unassociated | // Unassociated |
| trivia.cs:13:89:13:103 | // ... | trivia.cs:13:89:13:103 | // ... | 1 | New checksum | // New checksum |
| trivia.cs:19:16:19:46 | // ... | trivia.cs:19:16:19:46 | // ... | 1 | A mapped single-line comment | // A mapped single-line comment |
| trivia.cs:25:14:25:38 | // ... | trivia.cs:25:14:25:38 | // ... | 1 | numbering not affected | // numbering not affected |
| trivia.cs:49:18:49:82 | // ... | trivia.cs:49:18:49:82 | // ... | 1 | Sets the nullable annotation and warning contexts to disabled. | // Sets the nullable annotation and warning contexts to disabled. |
| trivia.cs:50:17:50:80 | // ... | trivia.cs:50:17:50:80 | // ... | 1 | Sets the nullable annotation and warning contexts to enabled. | // Sets the nullable annotation and warning contexts to enabled. |
Expand Down

0 comments on commit 7910af1

Please sign in to comment.