Skip to content

Commit 84066f3

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 1dcebf1 commit 84066f3

12 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎cpp/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// C/C++ databases can also contain XML (e.g. `.xml`, `.props`), whose block-comment
25+
// syntax is not yet supported, so we only render for C/C++ sources.
26+
relativePath
27+
.regexpMatch(".*\\.(c|cc|cpp|cxx|cp|c\\+\\+|h|hh|hpp|hxx|h\\+\\+|inl|tcc|ipp|tpp|cu|cuh)") and
28+
result = "//"
29+
}
2130
}

‎csharp/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// C# databases can also contain XML (e.g. `.csproj`, `.config`) and Razor markup, whose
25+
// comment syntaxes are not yet supported, so we only render for C# sources.
26+
relativePath.regexpMatch(".*\\.(cs|csx)") and
27+
result = "//"
28+
}
2129
}

‎go/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// Go databases can also contain XML, whose block-comment syntax is not yet supported, so
25+
// we only render for Go sources.
26+
relativePath.matches("%.go") and
27+
result = "//"
28+
}
2129
}

‎java/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// Java databases can also contain XML; those files use a different (block) comment
25+
// syntax that is not yet supported, so we only render for Java and Kotlin sources.
26+
(relativePath.matches("%.java") or relativePath.matches("%.kt")) and
27+
result = "//"
28+
}
2129
}

‎javascript/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// JavaScript databases can also contain HTML, whose (block) comment syntax is not yet
25+
// supported, so we only render for the line-comment source files.
26+
relativePath.regexpMatch(".*\\.(js|cjs|mjs|jsx|ts|cts|mts|tsx)") and
27+
result = "//"
28+
}
2129
}

‎python/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

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

‎ql/ql/src/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// The QL extractor can also extract YAML (e.g. `qlpack.yml`), whose `#` comment syntax
25+
// differs, so we only render for QL sources and dbscheme files.
26+
relativePath.regexpMatch(".*\\.(ql|qll|dbscheme)") and
27+
result = "//"
28+
}
2129
}

‎ruby/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// Ruby databases can also contain ERB, whose comment syntax is not yet supported, so we
25+
// only render for plain Ruby sources.
26+
relativePath.matches("%.rb") and
27+
result = "#"
28+
}
2129
}

‎rust/ql/lib/utils/test/InlineExpectationsTestQuery.ql‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ private module Input implements T::TestPostProcessing::InputSig<Impl> {
1818
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
1919
)
2020
}
21+
22+
bindingset[relativePath]
23+
string getStartCommentMarker(string relativePath) {
24+
// Rust databases can also contain YAML, whose `#` comment syntax differs, so we only
25+
// render for Rust sources.
26+
relativePath.matches("%.rs") and
27+
result = "//"
28+
}
2129
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,28 @@ module TestPostProcessing {
655655

656656
signature module InputSig<InlineExpectationsTestSig Input> {
657657
string getRelativeUrl(Input::Location location);
658+
659+
/**
660+
* Gets the marker that starts a line comment (for example `"//"` or `"#"`) in the source
661+
* file with the given `relativePath`, provided that `codeql test run --learn` is able to
662+
* render inline expectations for that file. Files for which this has no result are left
663+
* untouched by `--learn`.
664+
*
665+
* This is keyed on the file rather than on the analyzed language because a single database
666+
* may contain source files in several languages with different comment syntaxes (for
667+
* example Java together with XML). `relativePath` is the path reported by `getRelativeUrl`.
668+
*/
669+
bindingset[relativePath]
670+
string getStartCommentMarker(string relativePath);
671+
672+
/**
673+
* Gets the marker that ends a comment (for example `"-->"`) in the source file with the
674+
* given `relativePath`. Has no result by default, which is correct for languages whose
675+
* inline expectations use line comments (so `--learn` renders no closing marker);
676+
* block-comment languages can override it so that `--learn` renders a closing marker.
677+
*/
678+
bindingset[relativePath]
679+
default string getEndCommentMarker(string relativePath) { none() }
658680
}
659681

660682
module Make<InlineExpectationsTestSig Input, InputSig<Input> Input2> {

0 commit comments

Comments
 (0)