@@ -29,6 +29,57 @@ module Impl {
2929 result = getImmediateParent ( e )
3030 }
3131
32+ cached
33+ private module MacroExpansion {
34+ pragma [ nomagic]
35+ private predicate isInMacroExpansion ( AstNode root , AstNode n ) {
36+ n = root .( MacroCall ) .getMacroCallExpansion ( )
37+ or
38+ n = root .( Adt ) .getDeriveMacroExpansion ( _)
39+ or
40+ n = root .( Item ) .getAttributeMacroExpansion ( )
41+ or
42+ isInMacroExpansion ( root , n .getParentNode ( ) )
43+ }
44+
45+ pragma [ nomagic]
46+ private predicate isInAttributeMacroExpansionSource ( Item i , AstNode n ) {
47+ i .hasAttributeMacroExpansion ( ) and
48+ n .getParentNode ( ) = i and
49+ not n = i .getAttributeMacroExpansion ( ) and
50+ not n instanceof Attr
51+ or
52+ isInAttributeMacroExpansionSource ( i , n .getParentNode ( ) )
53+ }
54+
55+ pragma [ nomagic]
56+ private predicate isAttributeMacroExpansionSourceLocation ( Item i , Location l ) {
57+ exists ( AstNode n |
58+ isInAttributeMacroExpansionSource ( i , n ) and
59+ n .getLocation ( ) = l
60+ )
61+ }
62+
63+ /** Gets an AST node whose location is inside the token tree belonging to `mc`. */
64+ pragma [ nomagic]
65+ private AstNode getATokenTreeNode ( MacroCall mc ) {
66+ isInMacroExpansion ( mc , result ) and
67+ mc .getTokenTree ( ) .getLocation ( ) .contains ( result .getLocation ( ) )
68+ }
69+
70+ cached
71+ predicate isInMacroExpansion ( AstNode n ) { isInMacroExpansion ( _, n ) }
72+
73+ cached
74+ predicate isFromMacroExpansion ( AstNode n ) {
75+ exists ( AstNode root |
76+ isInMacroExpansion ( root , n ) and
77+ not n = getATokenTreeNode ( root ) and
78+ not isAttributeMacroExpansionSourceLocation ( root , n .getLocation ( ) )
79+ )
80+ }
81+ }
82+
3283 class AstNode extends Generated:: AstNode {
3384 /**
3485 * Gets the nearest enclosing parent of this node, which is also an `AstNode`,
@@ -71,21 +122,15 @@ module Impl {
71122 }
72123
73124 /** Holds if this node is inside a macro expansion. */
74- predicate isInMacroExpansion ( ) { MacroCallImpl :: isInMacroExpansion ( _ , this ) }
125+ predicate isInMacroExpansion ( ) { MacroExpansion :: isInMacroExpansion ( this ) }
75126
76127 /**
77128 * Holds if this node exists only as the result of a macro expansion.
78129 *
79130 * This is the same as `isInMacroExpansion()`, but excludes AST nodes corresponding
80- * to macro arguments.
131+ * to macro arguments, including attribute macro targets .
81132 */
82- pragma [ nomagic]
83- predicate isFromMacroExpansion ( ) {
84- exists ( AstNode root |
85- MacroCallImpl:: isInMacroExpansion ( root , this ) and
86- not this = root .( MacroCall ) .getATokenTreeNode ( )
87- )
88- }
133+ predicate isFromMacroExpansion ( ) { MacroExpansion:: isFromMacroExpansion ( this ) }
89134
90135 /**
91136 * Gets a control flow node for this AST node, if any.
0 commit comments