mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Merge pull request #20736 from github/copilot/add-constaccess-class
Rust: Add ConstAccess class with getConst() predicate
This commit is contained in:
1
rust/ql/.generated.list
generated
1
rust/ql/.generated.list
generated
@@ -247,7 +247,6 @@ lib/codeql/rust/elements/internal/ConstArgImpl.qll dc7e7b5fe1a6eeb61dd30a55a3ed2
|
||||
lib/codeql/rust/elements/internal/ConstBlockPatConstructor.qll ddb4a0045635d477e87360ecafec0ba90ddcffc6e62996eb6e7edd5a5d65b860 442061d0497a615b3f008b990f5e3c4f045110f76500eff81a7f44ffd1319acf
|
||||
lib/codeql/rust/elements/internal/ConstBlockPatImpl.qll 2082a3244c21e03b6dadfba9b3f97a00981324e10d1465d3a51cf3c921eb89e4 889e347834d8c6e90dfef9714af073b3b2193f6830f1c8356cee9c6573b3ecb4
|
||||
lib/codeql/rust/elements/internal/ConstConstructor.qll 72a31fd9b8b3fd910e35af1b2b30fa54cc4d9e14e7eabdb94b4cd2af95b2df38 3edc0a82a7b446fdfd3e71947801f3c7cac010b2a217b8accb69980387bdd67a
|
||||
lib/codeql/rust/elements/internal/ConstImpl.qll 058b474b9aaf2ad687ab1e62ebc8a51ba93d9ea4340c2f41768b71613ac330c1 c2c5d4746a588096cbbdfa4355ee73d806c7a4ac9507930a120e49060f9d5347
|
||||
lib/codeql/rust/elements/internal/ConstParamConstructor.qll f6645f952aac87c7e00e5e9661275312a1df47172088b4de6b5a253d5c4ed048 eda737470a7b89cf6a02715c9147d074041d6d00fd50d5b2d70266add6e4b571
|
||||
lib/codeql/rust/elements/internal/ConstParamImpl.qll c6995be58f84d1df65897c80f7ee3dd8eb410bb3e634ff1bfe1be94dfb3fdf32 bcfb5547b40f24bcec20056fe1d36724b734c920b0bc7538fe2974b03f4478fe
|
||||
lib/codeql/rust/elements/internal/ContinueExprConstructor.qll cd93f1b35ccdb031d7e8deba92f6a76187f6009c454f3ea07e89ba459de57ca6 6f658e7d580c4c9068b01d6dd6f72888b8800860668a6653f8c3b27dc9996935
|
||||
|
||||
1
rust/ql/.gitattributes
generated
vendored
1
rust/ql/.gitattributes
generated
vendored
@@ -249,7 +249,6 @@
|
||||
/lib/codeql/rust/elements/internal/ConstBlockPatConstructor.qll linguist-generated
|
||||
/lib/codeql/rust/elements/internal/ConstBlockPatImpl.qll linguist-generated
|
||||
/lib/codeql/rust/elements/internal/ConstConstructor.qll linguist-generated
|
||||
/lib/codeql/rust/elements/internal/ConstImpl.qll linguist-generated
|
||||
/lib/codeql/rust/elements/internal/ConstParamConstructor.qll linguist-generated
|
||||
/lib/codeql/rust/elements/internal/ConstParamImpl.qll linguist-generated
|
||||
/lib/codeql/rust/elements/internal/ContinueExprConstructor.qll linguist-generated
|
||||
|
||||
7
rust/ql/lib/codeql/rust/elements/ConstAccess.qll
Normal file
7
rust/ql/lib/codeql/rust/elements/ConstAccess.qll
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* This module provides the public class `ConstAccess`.
|
||||
*/
|
||||
|
||||
private import internal.ConstImpl
|
||||
|
||||
final class ConstAccess = Impl::ConstAccess;
|
||||
@@ -1,4 +1,3 @@
|
||||
// generated by codegen, remove this comment if you wish to edit this file
|
||||
/**
|
||||
* This module provides a hand-modifiable wrapper around the generated class `Const`.
|
||||
*
|
||||
@@ -6,12 +5,15 @@
|
||||
*/
|
||||
|
||||
private import codeql.rust.elements.internal.generated.Const
|
||||
private import codeql.rust.elements.internal.PathExprImpl::Impl as PathExprImpl
|
||||
private import codeql.rust.internal.PathResolution
|
||||
|
||||
/**
|
||||
* INTERNAL: This module contains the customizable definition of `Const` and should not
|
||||
* be referenced directly.
|
||||
*/
|
||||
module Impl {
|
||||
// the following QLdoc is generated: if you need to edit it, do it in the schema file
|
||||
/**
|
||||
* A constant item declaration.
|
||||
*
|
||||
@@ -21,4 +23,27 @@ module Impl {
|
||||
* ```
|
||||
*/
|
||||
class Const extends Generated::Const { }
|
||||
|
||||
/**
|
||||
* A constant access.
|
||||
*
|
||||
* For example:
|
||||
* ```rust
|
||||
* const X: i32 = 42;
|
||||
*
|
||||
* fn main() {
|
||||
* println!("{}", X);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
class ConstAccess extends PathExprImpl::PathExpr {
|
||||
private Const c;
|
||||
|
||||
ConstAccess() { c = resolvePath(this.getPath()) }
|
||||
|
||||
/** Gets the constant being accessed. */
|
||||
Const getConst() { result = c }
|
||||
|
||||
override string getAPrimaryQlClass() { result = "ConstAccess" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import codeql.rust.elements.ArithmeticOperation
|
||||
import codeql.rust.elements.AssignmentOperation
|
||||
import codeql.rust.elements.BitwiseOperation
|
||||
import codeql.rust.elements.ComparisonOperation
|
||||
import codeql.rust.elements.ConstAccess
|
||||
import codeql.rust.elements.DerefExpr
|
||||
import codeql.rust.elements.LiteralExprExt
|
||||
import codeql.rust.elements.LogicalOperation
|
||||
|
||||
@@ -1043,7 +1043,7 @@ macro_expansion.rs:
|
||||
# 98| getParamList(): [ParamList] ParamList
|
||||
# 99| getFunctionBody(): [BlockExpr] { ... }
|
||||
# 99| getStmtList(): [StmtList] StmtList
|
||||
# 99| getTailExpr(): [PathExpr] CONST_MyDeriveUnion
|
||||
# 99| getTailExpr(): [ConstAccess] CONST_MyDeriveUnion
|
||||
# 99| getPath(): [Path] CONST_MyDeriveUnion
|
||||
# 99| getSegment(): [PathSegment] CONST_MyDeriveUnion
|
||||
# 99| getIdentifier(): [NameRef] CONST_MyDeriveUnion
|
||||
|
||||
7
rust/ql/test/library-tests/const_access/Cargo.lock
generated
Normal file
7
rust/ql/test/library-tests/const_access/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "test"
|
||||
version = "0.0.1"
|
||||
@@ -0,0 +1,8 @@
|
||||
testFailures
|
||||
constAccess
|
||||
| main.rs:17:13:17:24 | GLOBAL_CONST | main.rs:1:1:1:29 | Const |
|
||||
| main.rs:19:13:19:24 | STRING_CONST | main.rs:2:1:2:35 | Const |
|
||||
| main.rs:21:13:21:33 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const |
|
||||
| main.rs:23:13:23:35 | ...::MODULE_CONST | main.rs:13:5:13:38 | Const |
|
||||
| main.rs:25:8:25:19 | GLOBAL_CONST | main.rs:1:1:1:29 | Const |
|
||||
| main.rs:29:16:29:36 | ...::ASSOC_CONST | main.rs:9:5:9:33 | Const |
|
||||
21
rust/ql/test/library-tests/const_access/const_access.ql
Normal file
21
rust/ql/test/library-tests/const_access/const_access.ql
Normal file
@@ -0,0 +1,21 @@
|
||||
import rust
|
||||
import utils.test.InlineExpectationsTest
|
||||
import TestUtils
|
||||
|
||||
query predicate constAccess(ConstAccess ca, Const c) { toBeTested(ca) and c = ca.getConst() }
|
||||
|
||||
module ConstAccessTest implements TestSig {
|
||||
string getARelevantTag() { result = "const_access" }
|
||||
|
||||
predicate hasActualResult(Location location, string element, string tag, string value) {
|
||||
exists(ConstAccess ca |
|
||||
toBeTested(ca) and
|
||||
location = ca.getLocation() and
|
||||
element = ca.toString() and
|
||||
tag = "const_access" and
|
||||
value = ca.getConst().getName().getText()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
import MakeTest<ConstAccessTest>
|
||||
34
rust/ql/test/library-tests/const_access/main.rs
Normal file
34
rust/ql/test/library-tests/const_access/main.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
const GLOBAL_CONST: i32 = 42;
|
||||
const STRING_CONST: &str = "hello";
|
||||
|
||||
struct MyStruct {
|
||||
value: i32,
|
||||
}
|
||||
|
||||
impl MyStruct {
|
||||
const ASSOC_CONST: i32 = 100;
|
||||
}
|
||||
|
||||
mod my_module {
|
||||
pub const MODULE_CONST: i32 = 200;
|
||||
}
|
||||
|
||||
fn use_consts() {
|
||||
let x = GLOBAL_CONST; // $ const_access=GLOBAL_CONST
|
||||
|
||||
let s = STRING_CONST; // $ const_access=STRING_CONST
|
||||
|
||||
let y = MyStruct::ASSOC_CONST; // $ const_access=ASSOC_CONST
|
||||
|
||||
let z = my_module::MODULE_CONST; // $ const_access=MODULE_CONST
|
||||
|
||||
if GLOBAL_CONST > 0 { // $ const_access=GLOBAL_CONST
|
||||
println!("positive");
|
||||
}
|
||||
|
||||
let arr = [MyStruct::ASSOC_CONST; 5]; // $ const_access=ASSOC_CONST
|
||||
}
|
||||
|
||||
fn main() {
|
||||
use_consts();
|
||||
}
|
||||
Reference in New Issue
Block a user