Skip to content

Commit 8376055

Browse files
committed
debug
1 parent fa48969 commit 8376055

File tree

11 files changed

+496
-270
lines changed

11 files changed

+496
-270
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: 59 additions & 24 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

@@ -457,7 +461,12 @@ module Impl {
457461
VariableAccessCand cand, VariableScope scope, string name, int nestLevel, int ord
458462
) {
459463
name = cand.getName() and
460-
scope = [cand.(VariableScope), getEnclosingScope(cand)] and
464+
(
465+
scope = cand.(VariableScope)
466+
or
467+
not cand instanceof VariableScope and
468+
scope = getEnclosingScope(cand)
469+
) and
461470
ord = getPreOrderNumbering(scope, cand) and
462471
nestLevel = 0
463472
or
@@ -470,6 +479,19 @@ module Impl {
470479
)
471480
}
472481

482+
private predicate testvariableAccessCandInScope(
483+
VariableAccessCand cand, VariableScope scope, string name, int nestLevel, int ord
484+
) {
485+
// variableAccessCandInScope(cand, scope, name, nestLevel, ord) and
486+
cand.getLocation().getStartLine() = 770 and
487+
(
488+
name = cand.getName() and
489+
scope = [cand.(VariableScope), getEnclosingScope(cand)] and
490+
ord = getPreOrderNumbering(scope, cand) and
491+
nestLevel = 0
492+
)
493+
}
494+
473495
private newtype TDefOrAccessCand =
474496
TDefOrAccessCandNestedFunction(Function f, BlockExprScope scope) {
475497
f = scope.getStmtList().getAStatement()
@@ -647,6 +669,14 @@ module Impl {
647669
)
648670
}
649671

672+
private predicate testvariableReachesCand(
673+
VariableScope scope, string name, NestedFunctionOrVariable v, VariableAccessCand cand,
674+
int nestLevel
675+
) {
676+
variableReachesCand(scope, name, v, cand, nestLevel) and
677+
v.getLocation().getStartLine() = [767, 769]
678+
}
679+
650680
pragma[nomagic]
651681
predicate access(string name, NestedFunctionOrVariable v, VariableAccessCand cand) {
652682
v =
@@ -657,6 +687,11 @@ module Impl {
657687
)
658688
}
659689

690+
private predicate testaccess(string name, NestedFunctionOrVariable v, VariableAccessCand cand) {
691+
access(name, v, cand) and
692+
cand.getLocation().getStartLine() = 770
693+
}
694+
660695
/** A variable access. */
661696
class VariableAccess extends PathExprBaseImpl::PathExprBase {
662697
private string name;

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

0 commit comments

Comments
 (0)