Skip to content

Commit ec736b4

Browse files
d10cCopilot
andcommitted
Add per-file comment markers to the postprocessing InputSig
The `--learn` support that follows needs to render inline-expectation comments in the syntax of the file being edited, and a single CodeQL database can mix source languages (e.g. Java plus XML), so the marker must be chosen per file rather than per database. Extend `TestPostProcessing::InputSig` with `getStartCommentMarker(relativePath)` (no result -> that file is left untouched by `--learn`) and a defaulted `getEndCommentMarker(relativePath)` (`none()` by default: line-comment languages render no closing marker; block-comment languages can override it later). Each language's `InlineExpectationsTestQuery.ql` implements `getStartCommentMarker`, gated to that language's own source extensions so that other file types (XML/YAML/HTML/ERB/Razor) extracted into the same database are not rewritten with the wrong syntax. This commit only wires the marker API through the signature and the per-language inputs; nothing consumes it yet, so behavior is unchanged. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1adfa307-c48e-43f5-94e3-8286e4a8c083
1 parent 04e3acb commit ec736b4

12 files changed

Lines changed: 120 additions & 0 deletions

File tree

‎cpp/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ module Impl implements InlineExpectationsTestSig {
3535
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
3636
)
3737
}
38+
39+
bindingset[relativePath]
40+
string getStartCommentMarker(string relativePath) {
41+
// C/C++ databases can also contain XML (e.g. `.xml`, `.props`), whose block-comment
42+
// syntax is not yet supported, so we only render for C/C++ sources.
43+
relativePath
44+
.regexpMatch(".*\\.(c|cc|cpp|cxx|cp|c\\+\\+|h|hh|hpp|hxx|h\\+\\+|inl|tcc|ipp|tpp|cu|cuh)") and
45+
result = "//"
46+
}
3847
}

‎csharp/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ module Impl implements InlineExpectationsTestSig {
5959
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
6060
)
6161
}
62+
63+
bindingset[relativePath]
64+
string getStartCommentMarker(string relativePath) {
65+
// C# databases can also contain XML (e.g. `.csproj`, `.config`) and Razor markup, whose
66+
// comment syntaxes are not yet supported, so we only render for C# sources.
67+
relativePath.regexpMatch(".*\\.(cs|csx)") and
68+
result = "//"
69+
}
6270
}

‎go/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ module Impl implements InlineExpectationsTestSig {
3030
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
3131
)
3232
}
33+
34+
bindingset[relativePath]
35+
string getStartCommentMarker(string relativePath) {
36+
// Go databases can also contain XML, whose block-comment syntax is not yet supported, so
37+
// we only render for Go sources.
38+
relativePath.matches("%.go") and
39+
result = "//"
40+
}
3341
}

‎java/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,12 @@ module Impl implements InlineExpectationsTestSig {
4545
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
4646
)
4747
}
48+
49+
bindingset[relativePath]
50+
string getStartCommentMarker(string relativePath) {
51+
// Java databases can also contain XML; those files use a different (block) comment
52+
// syntax that is not yet supported, so we only render for Java and Kotlin sources.
53+
relativePath.matches(["%.java", "%.kt"]) and
54+
result = "//"
55+
}
4856
}

‎javascript/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ module Impl implements InlineExpectationsTestSig {
1818
)
1919
}
2020

21+
bindingset[relativePath]
22+
string getStartCommentMarker(string relativePath) {
23+
// JavaScript databases can also contain HTML, whose block-comment syntax is not yet
24+
// supported, so we only render for line-comment source files.
25+
relativePath.regexpMatch(".*\\.(js|cjs|mjs|jsx|ts|cts|mts|tsx)") and
26+
result = "//"
27+
}
28+
2129
abstract private class ExpectationCommentImpl extends Locatable {
2230
abstract string getContents();
2331

‎python/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,12 @@ module Impl implements InlineExpectationsTestSig {
1919
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
2020
)
2121
}
22+
23+
bindingset[relativePath]
24+
string getStartCommentMarker(string relativePath) {
25+
// Python databases can also contain XML, whose block-comment syntax is not yet supported,
26+
// so we only render for Python sources.
27+
relativePath.regexpMatch(".*\\.(py|pyi)") and
28+
result = "#"
29+
}
2230
}

‎ql/ql/src/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ module Impl implements InlineExpectationsTestSig {
3535
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
3636
)
3737
}
38+
39+
bindingset[relativePath]
40+
string getStartCommentMarker(string relativePath) {
41+
// The QL extractor can also extract YAML (e.g. `qlpack.yml`), whose `#` comment syntax
42+
// differs, so we only render for QL sources and dbscheme files.
43+
relativePath.regexpMatch(".*\\.(ql|qll|dbscheme)") and
44+
result = "//"
45+
}
3846
}

‎ruby/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ module Impl implements InlineExpectationsTestSig {
4646
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
4747
)
4848
}
49+
50+
bindingset[relativePath]
51+
string getStartCommentMarker(string relativePath) {
52+
// Ruby databases can also contain ERB, whose comment syntax is not yet supported, so we
53+
// only render for plain Ruby sources.
54+
relativePath.matches("%.rb") and
55+
result = "#"
56+
}
4957
}

‎rust/ql/lib/utils/test/internal/InlineExpectationsTestImpl.qll‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@ module Impl implements InlineExpectationsTestSig {
2121
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
2222
)
2323
}
24+
25+
bindingset[relativePath]
26+
string getStartCommentMarker(string relativePath) {
27+
// Rust databases can also contain YAML, whose `#` comment syntax differs, so we only
28+
// render for Rust sources.
29+
relativePath.matches("%.rs") and
30+
result = "//"
31+
}
2432
}

‎shared/util/codeql/util/test/InlineExpectationsTest.qll‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ signature module InlineExpectationsTestSig {
111111
/** Gets the relative URL of the given location, if any. */
112112
string getRelativeUrl(Location location);
113113

114+
/**
115+
* Gets the marker that starts a line comment (for example `"//"` or `"#"`) in the source
116+
* file with the given `relativePath`, provided that `codeql test run --learn` is able to
117+
* render inline expectations for that file. Files for which this has no result are left
118+
* untouched by `--learn`.
119+
*
120+
* This is keyed on the file rather than on the analyzed language because a single database
121+
* may contain source files in several languages with different comment syntaxes (for
122+
* example Java together with XML). `relativePath` is the path reported by `getRelativeUrl`.
123+
*/
124+
bindingset[relativePath]
125+
string getStartCommentMarker(string relativePath);
126+
127+
/**
128+
* Gets the marker that ends a comment (for example `"-->"`) in the source file with the
129+
* given `relativePath`. Has no result by default, which is correct for languages whose
130+
* inline expectations use line comments (so `--learn` renders no closing marker);
131+
* block-comment languages can override it so that `--learn` renders a closing marker.
132+
*/
133+
bindingset[relativePath]
134+
default string getEndCommentMarker(string relativePath) { none() }
135+
114136
/** A comment that may contain inline expectations. */
115137
class ExpectationComment {
116138
/** Gets the contents of this comment, _excluding_ the comment indicator. */
@@ -748,6 +770,16 @@ module TestPostProcessing {
748770

749771
string getRelativeUrl(Location location) { result = location.getRelativeUrl() }
750772

773+
bindingset[relativePath]
774+
string getStartCommentMarker(string relativePath) {
775+
result = Input::getStartCommentMarker(relativePath)
776+
}
777+
778+
bindingset[relativePath]
779+
string getEndCommentMarker(string relativePath) {
780+
result = Input::getEndCommentMarker(relativePath)
781+
}
782+
751783
final private class ExpectationCommentFinal = Input::ExpectationComment;
752784

753785
class ExpectationComment extends ExpectationCommentFinal {

0 commit comments

Comments
 (0)