Skip to content

Commit a1c4c53

Browse files
committed
debug
1 parent 989432c commit a1c4c53

File tree

11 files changed

+393
-268
lines changed

11 files changed

+393
-268
lines changed

rust/ql/lib/codeql/rust/elements/internal/AstNodeImpl.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@ module Impl {
2929
result = getImmediateParent(e)
3030
}
3131

32+
/**
33+
* Holds if `n` is superseded by an attribute macro expansion. That is, `n` is
34+
* an item or a transitive child of an item with an attribute macro expansion.
35+
*/
36+
predicate supersededByAttributeMacroExpansion(AstNode n) {
37+
n.(Item).hasAttributeMacroExpansion()
38+
or
39+
exists(AstNode parent |
40+
n.getParentNode() = parent and
41+
supersededByAttributeMacroExpansion(parent) and
42+
// Don't exclude expansions themselves as they supercede other nodes.
43+
not n = parent.(Item).getAttributeMacroExpansion() and
44+
// Don't consider attributes themselves to be superseded. E.g., in `#[a] fn
45+
// f() {}` the macro expansion supercedes `fn f() {}` but not `#[a]`.
46+
not n instanceof Attr
47+
)
48+
}
49+
3250
class AstNode extends Generated::AstNode {
3351
/**
3452
* Gets the nearest enclosing parent of this node, which is also an `AstNode`,

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ private import rust
22
private import codeql.rust.controlflow.ControlFlowGraph
33
private import codeql.rust.internal.PathResolution as PathResolution
44
private import codeql.rust.elements.internal.generated.ParentChild as ParentChild
5+
private import codeql.rust.elements.internal.AstNodeImpl::Impl as AstNodeImpl
56
private import codeql.rust.elements.internal.PathImpl::Impl as PathImpl
67
private import codeql.rust.elements.internal.PathExprBaseImpl::Impl as PathExprBaseImpl
78
private import codeql.rust.elements.internal.FormatTemplateVariableAccessImpl::Impl as FormatTemplateVariableAccessImpl
@@ -104,29 +105,32 @@ module Impl {
104105
cached
105106
predicate variableDecl(AstNode definingNode, Name name, string text) {
106107
Cached::ref() and
107-
exists(SelfParam sp |
108-
name = sp.getName() and
109-
definingNode = name and
110-
text = name.getText() and
111-
// exclude self parameters from functions without a body as these are
112-
// trait method declarations without implementations
113-
not exists(Function f | not f.hasBody() and f.getSelfParam() = sp)
114-
)
115-
or
116-
exists(IdentPat pat |
117-
name = pat.getName() and
118-
(
119-
definingNode = getOutermostEnclosingOrPat(pat)
120-
or
121-
not exists(getOutermostEnclosingOrPat(pat)) and definingNode = name
122-
) and
123-
text = name.getText() and
124-
not PathResolution::identPatIsResolvable(pat) and
125-
// exclude parameters from functions without a body as these are trait method declarations
126-
// without implementations
127-
not exists(Function f | not f.hasBody() and f.getAParam().getPat() = pat) and
128-
// exclude parameters from function pointer types (e.g. `x` in `fn(x: i32) -> i32`)
129-
not exists(FnPtrTypeRepr fp | fp.getParamList().getAParam().getPat() = pat)
108+
not AstNodeImpl::supersededByAttributeMacroExpansion(definingNode) and
109+
(
110+
exists(SelfParam sp |
111+
name = sp.getName() and
112+
definingNode = name and
113+
text = name.getText() and
114+
// exclude self parameters from functions without a body as these are
115+
// trait method declarations without implementations
116+
not exists(Function f | not f.hasBody() and f.getSelfParam() = sp)
117+
)
118+
or
119+
exists(IdentPat pat |
120+
name = pat.getName() and
121+
(
122+
definingNode = getOutermostEnclosingOrPat(pat)
123+
or
124+
not exists(getOutermostEnclosingOrPat(pat)) and definingNode = name
125+
) and
126+
text = name.getText() and
127+
not PathResolution::identPatIsResolvable(pat) and
128+
// exclude parameters from functions without a body as these are trait method declarations
129+
// without implementations
130+
not exists(Function f | not f.hasBody() and f.getAParam().getPat() = pat) and
131+
// exclude parameters from function pointer types (e.g. `x` in `fn(x: i32) -> i32`)
132+
not exists(FnPtrTypeRepr fp | fp.getParamList().getAParam().getPat() = pat)
133+
)
130134
)
131135
}
132136

rust/ql/lib/codeql/rust/internal/Definitions.qll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ private import codeql.rust.internal.PathResolution
1717

1818
/** An element with an associated definition. */
1919
abstract class Use extends Locatable {
20-
Use() { not this.(AstNode).isFromMacroExpansion() }
21-
2220
/** Gets the definition associated with this element. */
2321
abstract Definition getDefinition();
2422

@@ -37,14 +35,17 @@ private module Cached {
3735
TFormatArgsArgIndex(Expr e) { e = any(FormatArgsArg a).getExpr() } or
3836
TItemNode(ItemNode i)
3937

38+
pragma[nomagic]
39+
private predicate isMacroCallLocation(Location loc) { loc = any(MacroCall m).getLocation() }
40+
4041
/**
4142
* Gets an element, of kind `kind`, that element `use` uses, if any.
4243
*/
4344
cached
4445
Definition definitionOf(Use use, string kind) {
4546
result = use.getDefinition() and
4647
kind = use.getUseType() and
47-
not result.getLocation() = any(MacroCall m).getLocation()
48+
not isMacroCallLocation(result.getLocation())
4849
}
4950
}
5051

rust/ql/lib/codeql/rust/internal/PathResolution.qll

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
private import rust
66
private import codeql.rust.elements.internal.generated.ParentChild
7+
private import codeql.rust.elements.internal.AstNodeImpl::Impl as AstNodeImpl
78
private import codeql.rust.elements.internal.CallExprImpl::Impl as CallExprImpl
89
private import codeql.rust.internal.CachedStages
910
private import codeql.rust.frameworks.stdlib.Builtins as Builtins
@@ -90,24 +91,6 @@ private module UseOption = Option<Use>;
9091

9192
private class UseOption = UseOption::Option;
9293

93-
/**
94-
* Holds if `n` is superseded by an attribute macro expansion. That is, `n` is
95-
* an item or a transitive child of an item with an attribute macro expansion.
96-
*/
97-
predicate supersededByAttributeMacroExpansion(AstNode n) {
98-
n.(Item).hasAttributeMacroExpansion()
99-
or
100-
exists(AstNode parent |
101-
n.getParentNode() = parent and
102-
supersededByAttributeMacroExpansion(parent) and
103-
// Don't exclude expansions themselves as they supercede other nodes.
104-
not n = parent.(Item).getAttributeMacroExpansion() and
105-
// Don't consider attributes themselves to be superseded. E.g., in `#[a] fn
106-
// f() {}` the macro expansion supercedes `fn f() {}` but not `#[a]`.
107-
not n instanceof Attr
108-
)
109-
}
110-
11194
/**
11295
* An item that may be referred to by a path, and which is a node in
11396
* the _item graph_.
@@ -188,7 +171,7 @@ predicate supersededByAttributeMacroExpansion(AstNode n) {
188171
abstract class ItemNode extends Locatable {
189172
ItemNode() {
190173
// Exclude items that are superseded by the expansion of an attribute macro.
191-
not supersededByAttributeMacroExpansion(this)
174+
not AstNodeImpl::supersededByAttributeMacroExpansion(this)
192175
}
193176

194177
/** Gets the (original) name of this item. */
@@ -1531,6 +1514,8 @@ private predicate declares(ItemNode item, Namespace ns, string name) {
15311514
* to constructors in patterns.
15321515
*/
15331516
abstract class PathExt extends AstNode {
1517+
PathExt() { not AstNodeImpl::supersededByAttributeMacroExpansion(this) }
1518+
15341519
abstract string getText();
15351520

15361521
/** Holds if this is an unqualified path with the textual value `name`. */
@@ -1975,7 +1960,10 @@ private ItemNode resolvePathCand(PathExt path) {
19751960
then result instanceof TypeItemNode
19761961
else
19771962
if path instanceof IdentPat
1978-
then result instanceof VariantItemNode or result instanceof StructItemNode
1963+
then
1964+
result instanceof VariantItemNode or
1965+
result instanceof StructItemNode or
1966+
result instanceof ConstItemNode
19791967
else any()
19801968
|
19811969
pathUsesNamespace(path, ns)
@@ -2066,6 +2054,13 @@ private ItemNode resolveUseTreeListItem(Use use, UseTree tree, PathExt path, Suc
20662054
result = q.getASuccessor(name, kind, useOpt)
20672055
)
20682056
)
2057+
// or
2058+
// // use {std::cmp::Ordering::*, AdjustmentHintsMode::*};
2059+
// tree = use.getUseTree() and
2060+
// not tree.hasPath() and
2061+
// isUseTreeSubPathUnqualified(tree, path, _) and
2062+
// result = resolvePathCand(path) and
2063+
// kind.isBoth() // todo
20692064
}
20702065

20712066
pragma[nomagic]
@@ -2093,6 +2088,7 @@ private ItemNode resolveUseTreeListItem(Use use, UseTree tree) {
20932088
result = resolveUseTreeListItem(use, tree, path, _)
20942089
)
20952090
or
2091+
// use foo::{bar, *}
20962092
exists(UseTree midTree |
20972093
result = resolveUseTreeListItem(use, midTree) and
20982094
tree = getAUseTreeUseTree(midTree) and
@@ -2217,8 +2213,12 @@ private module Debug {
22172213
Locatable getRelevantLocatable() {
22182214
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
22192215
result.getLocation().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
2220-
filepath.matches("%/main.rs") and
2221-
startline = 800
2216+
// filepath.matches("%/src/tools/rust-analyzer/crates/ide/src/inlay_hints/adjustment.rs") and
2217+
// startline = [6, 227]
2218+
// filepath.matches("%/compiler/rustc_middle/src/ty/layout.rs") and
2219+
// startline = 36
2220+
filepath.matches("%/cranelift-codegen-943c3c0c33bda67e/out/isle_riscv64.rs") and
2221+
startline = 11
22222222
)
22232223
}
22242224

@@ -2233,9 +2233,14 @@ private module Debug {
22332233
path = p.toStringDebug()
22342234
}
22352235

2236+
ItemNode debugUnqualifiedPathLookup(PathExt p, Namespace ns, SuccessorKind kind) {
2237+
p = getRelevantLocatable() and
2238+
result = unqualifiedPathLookup(p, ns, kind)
2239+
}
2240+
22362241
predicate debugItemNode(ItemNode item) { item = getRelevantLocatable() }
22372242

2238-
ItemNode debugResolvePath(PathExt path) {
2243+
ItemNode debugResolvePath(Path path) {
22392244
path = getRelevantLocatable() and
22402245
result = resolvePath(path)
22412246
}

rust/ql/lib/utils/test/PathResolutionInlineExpectationsTest.qll

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
private import rust
6+
private import codeql.rust.elements.internal.AstNodeImpl::Impl as AstNodeImpl
67
private import codeql.rust.internal.PathResolution
78
private import codeql.rust.internal.TypeInference
89
private import utils.test.InlineExpectationsTest
@@ -37,13 +38,15 @@ private module ResolveTest implements TestSig {
3738
)
3839
}
3940

40-
predicate hasActualResult(Location location, string element, string tag, string value) {
41+
private predicate hasResult(
42+
Location location, string element, string tag, string value, boolean inMacro
43+
) {
4144
exists(AstNode n |
4245
not n = any(Path parent).getQualifier() and
4346
location = n.getLocation() and
4447
n.fromSource() and
4548
not location.getFile().getAbsolutePath().matches("%proc_macro.rs") and
46-
not n.isFromMacroExpansion() and
49+
(if n.isFromMacroExpansion() then inMacro = true else inMacro = false) and
4750
element = n.toString() and
4851
tag = "item"
4952
|
@@ -52,6 +55,14 @@ private module ResolveTest implements TestSig {
5255
item(n.(MethodCallExpr).getStaticTarget(), value)
5356
)
5457
}
58+
59+
predicate hasActualResult(Location location, string element, string tag, string value) {
60+
hasResult(location, element, tag, value, false)
61+
}
62+
63+
predicate hasOptionalResult(Location location, string element, string tag, string value) {
64+
hasResult(location, element, tag, value, true)
65+
}
5566
}
5667

5768
import MakeTest<ResolveTest>

rust/ql/test/library-tests/path-resolution/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,24 @@ mod patterns {
813813
let test = test_alias();
814814
test
815815
}
816+
817+
#[rustfmt::skip]
818+
const z: i32 // $ item=i32
819+
= 0; // constz
820+
821+
#[rustfmt::skip]
822+
fn test3() {
823+
let x = Some(0); // $ item=Some
824+
match x {
825+
Some(x) // $ item=Some
826+
=> x,
827+
_ => 0
828+
};
829+
match x {
830+
Some(z) => z, // $ item=Some item=constz
831+
_ => 0
832+
};
833+
}
816834
}
817835

818836
fn main() {

0 commit comments

Comments
 (0)