Skip to content

Commit 967c086

Browse files
committed
Test: support queries that don't select a Location
1 parent 5b0eb0f commit 967c086

File tree

1 file changed

+109
-12
lines changed

1 file changed

+109
-12
lines changed

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

Lines changed: 109 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -643,21 +643,118 @@ module TestPostProcessing {
643643

644644
module Make<InlineExpectationsTestSig Input, InputSig<Input> Input2> {
645645
private import InlineExpectationsTest as InlineExpectationsTest
646-
private import InlineExpectationsTest::Make<Input>
646+
647+
bindingset[loc]
648+
private predicate parseLocationString(
649+
string loc, string relativePath, int sl, int sc, int el, int ec
650+
) {
651+
relativePath = loc.splitAt(":", 0) and
652+
sl = loc.splitAt(":", 1).toInt() and
653+
sc = loc.splitAt(":", 2).toInt() and
654+
el = loc.splitAt(":", 3).toInt() and
655+
ec = loc.splitAt(":", 4).toInt()
656+
}
657+
658+
pragma[nomagic]
659+
private string getRelativePathTo(string absolutePath) {
660+
exists(Input::Location loc |
661+
loc.hasLocationInfo(absolutePath, _, _, _, _) and
662+
result = Input2::getRelativeUrl(loc).splitAt(":", 0)
663+
)
664+
}
665+
666+
private newtype TTestLocation =
667+
MkInputLocation(Input::Location loc) or
668+
MkResultLocation(string relativePath, int sl, int sc, int el, int ec) {
669+
exists(string data |
670+
queryResults(_, _, _, data) and
671+
parseLocationString(data, relativePath, sl, sc, el, ec) and
672+
not Input2::getRelativeUrl(_) = data // avoid duplicate locations
673+
)
674+
}
675+
676+
/**
677+
* A location that is either an `Input::Location` or a location from an alert.
678+
*
679+
* We use this location type to support queries that select a location that does not correspond
680+
* to an instance of `Input::Location`.
681+
*/
682+
abstract private class TestLocationImpl extends TTestLocation {
683+
string getAbsoluteFile() { this.hasLocationInfo(result, _, _, _, _) }
684+
685+
int getStartLine() { this.hasLocationInfo(_, result, _, _, _) }
686+
687+
int getStartColumn() { this.hasLocationInfo(_, _, result, _, _) }
688+
689+
int getEndLine() { this.hasLocationInfo(_, _, _, result, _) }
690+
691+
int getEndColumn() { this.hasLocationInfo(_, _, _, _, result) }
692+
693+
abstract string getRelativeUrl();
694+
695+
abstract string toString();
696+
697+
abstract predicate hasLocationInfo(string file, int sl, int sc, int el, int ec);
698+
}
699+
700+
private class LocationFromResult extends TestLocationImpl, MkResultLocation {
701+
override string getRelativeUrl() {
702+
exists(string file, int sl, int sc, int el, int ec |
703+
this = MkResultLocation(file, sl, sc, el, ec) and
704+
result = file + ":" + sl + ":" + sc + ":" + el + ":" + ec
705+
)
706+
}
707+
708+
override string toString() { result = this.getRelativeUrl() }
709+
710+
override predicate hasLocationInfo(string file, int sl, int sc, int el, int ec) {
711+
this = MkResultLocation(getRelativePathTo(file), sl, sc, el, ec)
712+
}
713+
}
714+
715+
private class LocationFromInput extends TestLocationImpl, MkInputLocation {
716+
private Input::Location loc;
717+
718+
LocationFromInput() { this = MkInputLocation(loc) }
719+
720+
override string getRelativeUrl() { result = Input2::getRelativeUrl(loc) }
721+
722+
override string toString() { result = this.getRelativeUrl() }
723+
724+
override predicate hasLocationInfo(string file, int sl, int sc, int el, int ec) {
725+
loc.hasLocationInfo(file, sl, sc, el, ec)
726+
}
727+
}
728+
729+
final class TestLocation = TestLocationImpl;
730+
731+
module TestImpl2 implements InlineExpectationsTestSig {
732+
final class Location = TestLocation;
733+
734+
class ExpectationComment instanceof Input::ExpectationComment {
735+
string getContents() { result = super.getContents() }
736+
737+
Location getLocation() { result = MkInputLocation(super.getLocation()) }
738+
739+
string toString() { result = super.toString() }
740+
}
741+
}
742+
743+
private import InlineExpectationsTest::Make<TestImpl2>
647744

648745
/** Holds if the given locations refer to the same lines, but possibly with different column numbers. */
649746
bindingset[loc1, loc2]
650747
pragma[inline_late]
651-
private predicate sameLineInfo(Input::Location loc1, Input::Location loc2) {
748+
private predicate sameLineInfo(TestLocation loc1, TestLocation loc2) {
652749
exists(string file, int line1, int line2 |
653750
loc1.hasLocationInfo(file, line1, _, line2, _) and
654751
loc2.hasLocationInfo(file, line1, _, line2, _)
655752
)
656753
}
657754

658755
pragma[nomagic]
659-
private predicate mainQueryResult(int row, int column, Input::Location loc) {
660-
queryResults(mainResultSet(), row, column, Input2::getRelativeUrl(loc))
756+
private predicate mainQueryResult(int row, int column, TestLocation loc) {
757+
queryResults(mainResultSet(), row, column, loc.getRelativeUrl())
661758
}
662759

663760
/**
@@ -668,7 +765,7 @@ module TestPostProcessing {
668765
*/
669766
private string getSourceTag(int row) {
670767
getQueryKind() = "path-problem" and
671-
exists(Input::Location sourceLoc, Input::Location selectLoc |
768+
exists(TestLocation sourceLoc, TestLocation selectLoc |
672769
mainQueryResult(row, 0, selectLoc) and
673770
mainQueryResult(row, 2, sourceLoc) and
674771
if sameLineInfo(selectLoc, sourceLoc) then result = "Alert" else result = "Source"
@@ -733,7 +830,7 @@ module TestPostProcessing {
733830
}
734831

735832
additional predicate hasPathProblemSource(
736-
int row, Input::Location location, string element, string tag, string value
833+
int row, TestLocation location, string element, string tag, string value
737834
) {
738835
getQueryKind() = "path-problem" and
739836
mainQueryResult(row, 2, location) and
@@ -742,7 +839,7 @@ module TestPostProcessing {
742839
value = ""
743840
}
744841

745-
predicate hasActualResult(Input::Location location, string element, string tag, string value) {
842+
predicate hasActualResult(TestLocation location, string element, string tag, string value) {
746843
hasPathProblemSource(_, location, element, tag, value)
747844
}
748845
}
@@ -770,15 +867,15 @@ module TestPostProcessing {
770867
private predicate hasPathProblemSource = PathProblemSourceTestInput::hasPathProblemSource/5;
771868

772869
private predicate hasPathProblemSink(
773-
int row, Input::Location location, string element, string tag
870+
int row, TestLocation location, string element, string tag
774871
) {
775872
getQueryKind() = "path-problem" and
776873
mainQueryResult(row, 4, location) and
777874
queryResults(mainResultSet(), row, 5, element) and
778875
tag = getSinkTag(row)
779876
}
780877

781-
private predicate hasAlert(int row, Input::Location location, string element, string tag) {
878+
private predicate hasAlert(int row, TestLocation location, string element, string tag) {
782879
getQueryKind() = ["problem", "path-problem"] and
783880
mainQueryResult(row, 0, location) and
784881
queryResults(mainResultSet(), row, 2, element) and
@@ -793,15 +890,15 @@ module TestPostProcessing {
793890
* present).
794891
*/
795892
private string getValue(int row) {
796-
exists(Input::Location location, string element, string tag, string val |
893+
exists(TestLocation location, string element, string tag, string val |
797894
hasPathProblemSource(row, location, element, tag, val) and
798895
result =
799896
PathProblemSourceTest::getAMatchingExpectation(location, element, tag, val, false)
800897
.getValue()
801898
)
802899
}
803900

804-
predicate hasActualResult(Input::Location location, string element, string tag, string value) {
901+
predicate hasActualResult(TestLocation location, string element, string tag, string value) {
805902
exists(int row |
806903
hasPathProblemSource(row, location, element, tag, _)
807904
or
@@ -840,7 +937,7 @@ module TestPostProcessing {
840937
rankedTestFailures(row, f) and
841938
f = MkTestFailure(fl, message)
842939
|
843-
column = 0 and data = Input2::getRelativeUrl(fl.getLocation())
940+
column = 0 and data = fl.getLocation().getRelativeUrl()
844941
or
845942
column = 1 and data = fl.toString()
846943
or

0 commit comments

Comments
 (0)