-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java: inline range test #17997
Open
yoff
wants to merge
2
commits into
github:main
Choose a base branch
from
yoff:java/inline-range-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−0
Open
java: inline range test #17997
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
java/ql/test/library-tests/dataflow/range-analysis-inline/B.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
public class B { | ||
public int forloop() { | ||
int result = 0; | ||
for (int i = 0; | ||
i < 10; // $ bound="i in [0..10]" | ||
i++) { // $ bound="i in [0..9]" | ||
result = i; // $ bound="i in [0..9]" | ||
} | ||
return result; // $ bound="result in [0..9]" | ||
} | ||
|
||
public int forloopexit() { | ||
int result = 0; | ||
for (; result < 10;) { // $ bound="result in [0..10]" | ||
result += 1; // $ bound="result in [0..9]" | ||
} | ||
return result; // $ bound="result = 10" | ||
} | ||
|
||
public int forloopexitstep() { | ||
int result = 0; | ||
for (; result < 10;) { // $ bound="result in [0..12]" | ||
result += 3; // $ bound="result in [0..9]" | ||
} | ||
return result; // $ bound="result = 12" | ||
} | ||
|
||
public int forloopexitupd() { | ||
int result = 0; | ||
for (; result < 10; result++) { // $ bound="result in [0..9]" bound="result in [0..10]" | ||
} | ||
return result; // $ bound="result = 10" | ||
} | ||
|
||
public int forloopexitnested() { | ||
int result = 0; | ||
for (; result < 10;) { | ||
int i = 0; | ||
for (; i < 3;) { // $ bound="i in [0..3]" | ||
i += 1; // $ bound="i in [0..2]" | ||
} | ||
result += i; // $ bound="result in [0..9]" bound="i = 3" | ||
} | ||
return result; // $ MISSING:bound="result = 12" | ||
} | ||
|
||
public int emptyforloop() { | ||
int result = 0; | ||
for (int i = 0; i < 0; i++) { // $ bound="i = 0" bound="i in [0..-1]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
result = i; // $ bound="i in [0..-1]" | ||
} | ||
return result; // $ bound="result = 0" | ||
} | ||
|
||
public int noloop() { | ||
int result = 0; | ||
result += 1; // $ bound="result = 0" | ||
return result; // $ bound="result = 1" | ||
} | ||
|
||
public int foreachloop() { | ||
int result = 0; | ||
for (int i : new int[] {1, 2, 3, 4, 5}) { | ||
result = i; | ||
} | ||
return result; | ||
} | ||
|
||
public int emptyforeachloop() { | ||
int result = 0; | ||
for (int i : new int[] {}) { | ||
result = i; | ||
} | ||
return result; | ||
} | ||
|
||
public int whileloop() { | ||
int result = 100; | ||
while (result > 5) { // $ bound="result in [4..100]" | ||
result = result - 2; // $ bound="result in [6..100]" | ||
} | ||
return result; // $ bound="result = 4" | ||
} | ||
|
||
public int oddwhileloop() { | ||
int result = 101; | ||
while (result > 5) { // $ bound="result in [5..101]" | ||
result = result - 2; // $ bound="result in [7..101]" | ||
} | ||
return result; // $ bound="result = 5" | ||
} | ||
} |
Empty file.
35 changes: 35 additions & 0 deletions
35
java/ql/test/library-tests/dataflow/range-analysis-inline/range.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Inline range analysis tests for Java. | ||
* See `shared/util/codeql/dataflow/test/InlineFlowTest.qll` | ||
*/ | ||
|
||
import java | ||
import semmle.code.java.dataflow.RangeAnalysis | ||
private import TestUtilities.InlineExpectationsTest as IET | ||
|
||
module RangeTest implements IET::TestSig { | ||
string getARelevantTag() { result = "bound" } | ||
|
||
predicate hasActualResult(Location location, string element, string tag, string value) { | ||
tag = "bound" and | ||
exists(Expr e, int lower, int upper | | ||
constrained(e, lower, upper) and | ||
e instanceof VarRead and | ||
e.getCompilationUnit().fromSource() | ||
| | ||
location = e.getLocation() and | ||
element = e.toString() and | ||
if lower = upper | ||
then value = "\"" + e.toString() + " = " + lower.toString() + "\"" | ||
else | ||
value = "\"" + e.toString() + " in [" + lower.toString() + ".." + upper.toString() + "]\"" | ||
) | ||
} | ||
|
||
private predicate constrained(Expr e, int lower, int upper) { | ||
bounded(e, any(ZeroBound z), lower, false, _) and | ||
bounded(e, any(ZeroBound z), upper, true, _) | ||
} | ||
} | ||
|
||
import IET::MakeTest<RangeTest> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will get that done.