Skip to content

Commit c89b49f

Browse files
committed
Rust: Remove elements superseded by attribute macro expansions
1 parent 069170f commit c89b49f

File tree

16 files changed

+76
-252
lines changed

16 files changed

+76
-252
lines changed

rust/ql/.generated.list

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/.gitattributes

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,50 @@ private import codeql.rust.elements.internal.generated.Element
1111
* be referenced directly.
1212
*/
1313
module Impl {
14+
private import rust
15+
private import codeql.rust.elements.internal.generated.Synth
16+
private import codeql.rust.elements.internal.generated.Raw
17+
18+
/**
19+
* Holds if `e` is superseded by an attribute macro expansion. That is, `e` is
20+
* a transitive child of an item with an attribute macro expansion.
21+
*/
22+
private predicate supersededByAttributeMacroExpansion(Raw::Element e) {
23+
exists(Raw::Item item |
24+
exists(item.getAttributeMacroExpansion()) and
25+
e = Raw::getImmediateChild(item, _) and
26+
not e = item.getAttributeMacroExpansion() and
27+
// Don't consider attributes themselves to be superseded. E.g., in `#[a] fn
28+
// f() {}` the macro expansion supersedes `fn f() {}` but not `#[a]`.
29+
not e instanceof Raw::Attr
30+
)
31+
or
32+
exists(Raw::Element parent |
33+
e = Raw::getImmediateChild(parent, _) and
34+
supersededByAttributeMacroExpansion(parent)
35+
)
36+
}
37+
1438
class Element extends Generated::Element {
39+
cached
40+
Element() {
41+
exists(Raw::Element raw |
42+
raw = Synth::convertElementToRaw(this) and
43+
not supersededByAttributeMacroExpansion(raw)
44+
)
45+
or
46+
// Handle all synthetic elements below
47+
exists(FormatArgsExpr parent, Raw::Element raw |
48+
raw = Synth::convertFormatArgsExprToRaw(parent)
49+
|
50+
this = Synth::TFormat(raw, _, _, _)
51+
or
52+
this = Synth::TFormatArgument(raw, _, _, _, _, _)
53+
or
54+
this = Synth::TFormatTemplateVariableAccess(raw, _, _)
55+
)
56+
}
57+
1558
override string toStringImpl() { result = this.getAPrimaryQlClass() }
1659

1760
/**

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// generated by codegen, remove this comment if you wish to edit this file
21
/**
32
* This module provides a hand-modifiable wrapper around the generated class `Item`.
43
*
@@ -12,6 +11,7 @@ private import codeql.rust.elements.internal.generated.Item
1211
* be referenced directly.
1312
*/
1413
module Impl {
14+
// the following QLdoc is generated: if you need to edit it, do it in the schema file
1515
/**
1616
* An item such as a function, struct, enum, etc.
1717
*
@@ -23,4 +23,10 @@ module Impl {
2323
* ```
2424
*/
2525
class Item extends Generated::Item { }
26+
27+
private class ItemWithAttributeMacroExpansion extends Item {
28+
ItemWithAttributeMacroExpansion() { this.hasAttributeMacroExpansion() }
29+
30+
override string toStringImpl() { result = "(item with attribute macro expansion)" }
31+
}
2632
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ module Impl {
2020
or
2121
n = root.(Adt).getDeriveMacroExpansion(_)
2222
or
23-
n = root.(Item).getAttributeMacroExpansion()
24-
or
2523
isInMacroExpansion(root, n.getParentNode())
2624
}
2725

@@ -35,9 +33,7 @@ module Impl {
3533
* ```
3634
*/
3735
class MacroCall extends Generated::MacroCall {
38-
override string toStringImpl() {
39-
if this.hasPath() then result = this.getPath().toAbbreviatedString() + "!..." else result = ""
40-
}
36+
override string toStringImpl() { result = this.getPath().toAbbreviatedString() + "!..." }
4137

4238
/** Gets an AST node whose location is inside the token tree belonging to this macro call. */
4339
pragma[nomagic]

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,6 @@ private module UseOption = Option<Use>;
9090

9191
private class UseOption = UseOption::Option;
9292

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-
11193
/**
11294
* An item that may be referred to by a path, and which is a node in
11395
* the _item graph_.
@@ -186,10 +168,7 @@ predicate supersededByAttributeMacroExpansion(AstNode n) {
186168
* - https://doc.rust-lang.org/reference/names/namespaces.html
187169
*/
188170
abstract class ItemNode extends Locatable {
189-
ItemNode() {
190-
// Exclude items that are superseded by the expansion of an attribute macro.
191-
not supersededByAttributeMacroExpansion(this)
192-
}
171+
ItemNode() { not this.(Item).hasAttributeMacroExpansion() }
193172

194173
/** Gets the (original) name of this item. */
195174
abstract string getName();

rust/ql/test/extractor-tests/macro-expansion/PrintAst.expected

Lines changed: 8 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ macro_expansion.rs:
174174
# 1| getPath(): [Path] MyTrait
175175
# 1| getSegment(): [PathSegment] MyTrait
176176
# 1| getIdentifier(): [NameRef] MyTrait
177-
# 3| getItem(1): [Function] fn foo
177+
# 3| getItem(1): [Function] (item with attribute macro expansion)
178178
# 4| getAttributeMacroExpansion(): [MacroItems] MacroItems
179179
# 4| getItem(0): [Function] fn foo
180180
# 4| getParamList(): [ParamList] ParamList
@@ -190,7 +190,7 @@ macro_expansion.rs:
190190
# 5| getIdentifier(): [NameRef] concat
191191
# 5| getTokenTree(): [TokenTree] TokenTree
192192
# 5| getMacroCallExpansion(): [StringLiteralExpr] "Hello world!"
193-
# 7| getStatement(1): [Function] fn inner
193+
# 7| getStatement(1): [Function] (item with attribute macro expansion)
194194
# 8| getAttributeMacroExpansion(): [MacroItems] MacroItems
195195
# 8| getItem(0): [Function] fn inner_0
196196
# 8| getParamList(): [ParamList] ParamList
@@ -202,16 +202,12 @@ macro_expansion.rs:
202202
# 8| getFunctionBody(): [BlockExpr] { ... }
203203
# 8| getStmtList(): [StmtList] StmtList
204204
# 8| getName(): [Name] inner_1
205-
# 8| getParamList(): [ParamList] ParamList
206205
# 7| getAttr(0): [Attr] Attr
207206
# 7| getMeta(): [Meta] Meta
208207
# 7| getPath(): [Path] repeat
209208
# 7| getSegment(): [PathSegment] repeat
210209
# 7| getIdentifier(): [NameRef] repeat
211210
# 7| getTokenTree(): [TokenTree] TokenTree
212-
# 8| getFunctionBody(): [BlockExpr] { ... }
213-
# 8| getStmtList(): [StmtList] StmtList
214-
# 8| getName(): [Name] inner
215211
# 10| getStatement(2): [ExprStmt] ExprStmt
216212
# 10| getExpr(): [CallExpr] inner_0(...)
217213
# 10| getArgList(): [ArgList] ArgList
@@ -242,7 +238,7 @@ macro_expansion.rs:
242238
# 5| getIdentifier(): [NameRef] concat
243239
# 5| getTokenTree(): [TokenTree] TokenTree
244240
# 5| getMacroCallExpansion(): [StringLiteralExpr] "Hello world!"
245-
# 7| getStatement(1): [Function] fn inner
241+
# 7| getStatement(1): [Function] (item with attribute macro expansion)
246242
# 8| getAttributeMacroExpansion(): [MacroItems] MacroItems
247243
# 8| getItem(0): [Function] fn inner_0
248244
# 8| getParamList(): [ParamList] ParamList
@@ -254,16 +250,12 @@ macro_expansion.rs:
254250
# 8| getFunctionBody(): [BlockExpr] { ... }
255251
# 8| getStmtList(): [StmtList] StmtList
256252
# 8| getName(): [Name] inner_1
257-
# 8| getParamList(): [ParamList] ParamList
258253
# 7| getAttr(0): [Attr] Attr
259254
# 7| getMeta(): [Meta] Meta
260255
# 7| getPath(): [Path] repeat
261256
# 7| getSegment(): [PathSegment] repeat
262257
# 7| getIdentifier(): [NameRef] repeat
263258
# 7| getTokenTree(): [TokenTree] TokenTree
264-
# 8| getFunctionBody(): [BlockExpr] { ... }
265-
# 8| getStmtList(): [StmtList] StmtList
266-
# 8| getName(): [Name] inner
267259
# 10| getStatement(2): [ExprStmt] ExprStmt
268260
# 10| getExpr(): [CallExpr] inner_0(...)
269261
# 10| getArgList(): [ArgList] ArgList
@@ -280,53 +272,14 @@ macro_expansion.rs:
280272
# 11| getIdentifier(): [NameRef] inner_1
281273
# 4| getName(): [Name] foo_new
282274
# 4| getVisibility(): [Visibility] Visibility
283-
# 4| getParamList(): [ParamList] ParamList
284275
# 3| getAttr(0): [Attr] Attr
285276
# 3| getMeta(): [Meta] Meta
286277
# 3| getPath(): [Path] add_one
287278
# 3| getSegment(): [PathSegment] add_one
288279
# 3| getIdentifier(): [NameRef] add_one
289-
# 4| getFunctionBody(): [BlockExpr] { ... }
290-
# 4| getStmtList(): [StmtList] StmtList
291-
# 5| getStatement(0): [ExprStmt] ExprStmt
292-
# 5| getExpr(): [AssignmentExpr] ... = ...
293-
# 5| getLhs(): [UnderscoreExpr] _
294-
# 5| getRhs(): [MacroExpr] MacroExpr
295-
# 5| getMacroCall(): [MacroCall] concat!...
296-
# 5| getPath(): [Path] concat
297-
# 5| getSegment(): [PathSegment] concat
298-
# 5| getIdentifier(): [NameRef] concat
299-
# 5| getTokenTree(): [TokenTree] TokenTree
300-
# 7| getStatement(1): [Function] fn inner
301-
# 8| getParamList(): [ParamList] ParamList
302-
# 7| getAttr(0): [Attr] Attr
303-
# 7| getMeta(): [Meta] Meta
304-
# 7| getPath(): [Path] repeat
305-
# 7| getSegment(): [PathSegment] repeat
306-
# 7| getIdentifier(): [NameRef] repeat
307-
# 7| getTokenTree(): [TokenTree] TokenTree
308-
# 8| getFunctionBody(): [BlockExpr] { ... }
309-
# 8| getStmtList(): [StmtList] StmtList
310-
# 8| getName(): [Name] inner
311-
# 10| getStatement(2): [ExprStmt] ExprStmt
312-
# 10| getExpr(): [CallExpr] inner_0(...)
313-
# 10| getArgList(): [ArgList] ArgList
314-
# 10| getFunction(): [PathExpr] inner_0
315-
# 10| getPath(): [Path] inner_0
316-
# 10| getSegment(): [PathSegment] inner_0
317-
# 10| getIdentifier(): [NameRef] inner_0
318-
# 11| getStatement(3): [ExprStmt] ExprStmt
319-
# 11| getExpr(): [CallExpr] inner_1(...)
320-
# 11| getArgList(): [ArgList] ArgList
321-
# 11| getFunction(): [PathExpr] inner_1
322-
# 11| getPath(): [Path] inner_1
323-
# 11| getSegment(): [PathSegment] inner_1
324-
# 11| getIdentifier(): [NameRef] inner_1
325-
# 4| getName(): [Name] foo
326-
# 4| getVisibility(): [Visibility] Visibility
327-
# 14| getItem(2): [Function] fn bar
280+
# 14| getItem(2): [Function] (item with attribute macro expansion)
328281
# 15| getAttributeMacroExpansion(): [MacroItems] MacroItems
329-
# 15| getItem(0): [Function] fn bar_0
282+
# 15| getItem(0): [Function] (item with attribute macro expansion)
330283
# 16| getAttributeMacroExpansion(): [MacroItems] MacroItems
331284
# 16| getItem(0): [Function] fn bar_0
332285
# 16| getParamList(): [ParamList] ParamList
@@ -340,17 +293,12 @@ macro_expansion.rs:
340293
# 16| getStmtList(): [StmtList] StmtList
341294
# 16| getName(): [Name] bar_0_new
342295
# 16| getVisibility(): [Visibility] Visibility
343-
# 16| getParamList(): [ParamList] ParamList
344296
# 15| getAttr(0): [Attr] Attr
345297
# 15| getMeta(): [Meta] Meta
346298
# 15| getPath(): [Path] add_one
347299
# 15| getSegment(): [PathSegment] add_one
348300
# 15| getIdentifier(): [NameRef] add_one
349-
# 16| getFunctionBody(): [BlockExpr] { ... }
350-
# 16| getStmtList(): [StmtList] StmtList
351-
# 16| getName(): [Name] bar_0
352-
# 16| getVisibility(): [Visibility] Visibility
353-
# 15| getItem(1): [Function] fn bar_1
301+
# 15| getItem(1): [Function] (item with attribute macro expansion)
354302
# 16| getAttributeMacroExpansion(): [MacroItems] MacroItems
355303
# 16| getItem(0): [Function] fn bar_1
356304
# 16| getParamList(): [ParamList] ParamList
@@ -364,17 +312,11 @@ macro_expansion.rs:
364312
# 16| getStmtList(): [StmtList] StmtList
365313
# 16| getName(): [Name] bar_1_new
366314
# 16| getVisibility(): [Visibility] Visibility
367-
# 16| getParamList(): [ParamList] ParamList
368315
# 15| getAttr(0): [Attr] Attr
369316
# 15| getMeta(): [Meta] Meta
370317
# 15| getPath(): [Path] add_one
371318
# 15| getSegment(): [PathSegment] add_one
372319
# 15| getIdentifier(): [NameRef] add_one
373-
# 16| getFunctionBody(): [BlockExpr] { ... }
374-
# 16| getStmtList(): [StmtList] StmtList
375-
# 16| getName(): [Name] bar_1
376-
# 16| getVisibility(): [Visibility] Visibility
377-
# 16| getParamList(): [ParamList] ParamList
378320
# 14| getAttr(0): [Attr] Attr
379321
# 14| getMeta(): [Meta] Meta
380322
# 14| getPath(): [Path] repeat
@@ -386,22 +328,13 @@ macro_expansion.rs:
386328
# 15| getPath(): [Path] add_one
387329
# 15| getSegment(): [PathSegment] add_one
388330
# 15| getIdentifier(): [NameRef] add_one
389-
# 16| getFunctionBody(): [BlockExpr] { ... }
390-
# 16| getStmtList(): [StmtList] StmtList
391-
# 16| getName(): [Name] bar
392-
# 16| getVisibility(): [Visibility] Visibility
393-
# 18| getItem(3): [Function] fn baz
331+
# 18| getItem(3): [Function] (item with attribute macro expansion)
394332
# 18| getAttributeMacroExpansion(): [MacroItems] MacroItems
395-
# 19| getParamList(): [ParamList] ParamList
396333
# 18| getAttr(0): [Attr] Attr
397334
# 18| getMeta(): [Meta] Meta
398335
# 18| getPath(): [Path] erase
399336
# 18| getSegment(): [PathSegment] erase
400337
# 18| getIdentifier(): [NameRef] erase
401-
# 19| getFunctionBody(): [BlockExpr] { ... }
402-
# 19| getStmtList(): [StmtList] StmtList
403-
# 19| getName(): [Name] baz
404-
# 19| getVisibility(): [Visibility] Visibility
405338
# 22| getItem(4): [MacroRules] MacroRules
406339
# 22| getName(): [Name] hello
407340
# 22| getTokenTree(): [TokenTree] TokenTree
@@ -410,7 +343,7 @@ macro_expansion.rs:
410343
# 28| getVisibility(): [Visibility] Visibility
411344
# 30| getItem(6): [Impl] impl S { ... }
412345
# 30| getAssocItemList(): [AssocItemList] AssocItemList
413-
# 31| getAssocItem(0): [Function] fn bzz
346+
# 31| getAssocItem(0): [Function] (item with attribute macro expansion)
414347
# 32| getAttributeMacroExpansion(): [MacroItems] MacroItems
415348
# 32| getItem(0): [Function] fn bzz_0
416349
# 32| getParamList(): [ParamList] ParamList
@@ -556,24 +489,12 @@ macro_expansion.rs:
556489
# 31| getIdentifier(): [NameRef] _print
557490
# 32| getName(): [Name] bzz_2
558491
# 32| getVisibility(): [Visibility] Visibility
559-
# 32| getParamList(): [ParamList] ParamList
560492
# 31| getAttr(0): [Attr] Attr
561493
# 31| getMeta(): [Meta] Meta
562494
# 31| getPath(): [Path] repeat
563495
# 31| getSegment(): [PathSegment] repeat
564496
# 31| getIdentifier(): [NameRef] repeat
565497
# 31| getTokenTree(): [TokenTree] TokenTree
566-
# 32| getFunctionBody(): [BlockExpr] { ... }
567-
# 32| getStmtList(): [StmtList] StmtList
568-
# 33| getStatement(0): [ExprStmt] ExprStmt
569-
# 33| getExpr(): [MacroExpr] MacroExpr
570-
# 33| getMacroCall(): [MacroCall] hello!...
571-
# 33| getPath(): [Path] hello
572-
# 33| getSegment(): [PathSegment] hello
573-
# 33| getIdentifier(): [NameRef] hello
574-
# 33| getTokenTree(): [TokenTree] TokenTree
575-
# 32| getName(): [Name] bzz
576-
# 32| getVisibility(): [Visibility] Visibility
577498
# 30| getSelfTy(): [PathTypeRepr] S
578499
# 30| getPath(): [Path] S
579500
# 30| getSegment(): [PathSegment] S

0 commit comments

Comments
 (0)