Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Add unresolved macro calls diagnostic #17940

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions rust/ql/src/queries/diagnostics/UnextractedElements.ql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @name Unextracted Elements
* @description List all elements that weren't extracted due to unimplemented features or parse errors.
* @kind diagnostic
* @id rust/diagnostics/unextracted-elements
*/

Expand Down
12 changes: 12 additions & 0 deletions rust/ql/src/queries/diagnostics/UnresolvedMacroCalls.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @name Unresolved Macro Calls
* @description List all macro calls that were not resolved to a target.
* @kind diagnostic
* @id rust/diagnostics/unresolved-macro-calls
*/

import rust

from MacroCall mc
where not mc.hasExpanded()
select mc, "Macro call was not resolved to a target."
8 changes: 7 additions & 1 deletion rust/ql/src/queries/summary/SummaryStats.ql
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ where
key = "Inconsistencies - CFG" and value = getTotalCfgInconsistencies()
or
key = "Inconsistencies - data flow" and value = getTotalDataFlowInconsistencies()
select key, value
or
key = "Macro calls - total" and value = count(MacroCall mc)
or
key = "Macro calls - resolved" and value = count(MacroCall mc | mc.hasExpanded())
or
key = "Macro calls - unresolved" and value = count(MacroCall mc | not mc.hasExpanded())
select key, value order by key
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ extractionWarning
| does_not_compile.rs:2:26:2:25 | expected SEMICOLON |
| does_not_compile.rs:2:32:2:31 | expected field name or number |
| error.rs:2:5:2:17 | An error! |
| my_macro.rs:17:9:17:27 | macro expansion failed: could not resolve macro 'myUndefinedMacro' |
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
| does_not_compile.rs:2:26:2:25 | expected SEMICOLON | Extraction warning in does_not_compile.rs with message expected SEMICOLON | 1 |
| does_not_compile.rs:2:32:2:31 | expected field name or number | Extraction warning in does_not_compile.rs with message expected field name or number | 1 |
| error.rs:2:5:2:17 | An error! | Extraction warning in error.rs with message An error! | 1 |
| my_macro.rs:17:9:17:27 | macro expansion failed: could not resolve macro 'myUndefinedMacro' | Extraction warning in my_macro.rs with message macro expansion failed: could not resolve macro 'myUndefinedMacro' | 1 |
2 changes: 1 addition & 1 deletion rust/ql/test/query-tests/diagnostics/LinesOfCode.expected
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| 59 |
| 60 |
Original file line number Diff line number Diff line change
@@ -1 +1 @@
| 59 |
| 60 |
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| my_struct.rs:0:0:0:0 | my_struct.rs | 20 |
| comments.rs:0:0:0:0 | comments.rs | 13 |
| main.rs:0:0:0:0 | main.rs | 8 |
| my_macro.rs:0:0:0:0 | my_macro.rs | 7 |
| my_macro.rs:0:0:0:0 | my_macro.rs | 8 |
| lib.rs:0:0:0:0 | lib.rs | 5 |
| does_not_compile.rs:0:0:0:0 | does_not_compile.rs | 3 |
| error.rs:0:0:0:0 | error.rs | 3 |
Expand Down
15 changes: 9 additions & 6 deletions rust/ql/test/query-tests/diagnostics/SummaryStats.expected
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
| Elements extracted | 376 |
| Elements extracted | 383 |
| Elements unextracted | 0 |
| Extraction errors | 0 |
| Extraction warnings | 7 |
| Extraction warnings | 8 |
| Files extracted - total | 8 |
| Files extracted - with errors | 2 |
| Files extracted - without errors | 6 |
| Files extracted - with errors | 3 |
| Files extracted - without errors | 5 |
| Inconsistencies - AST | 0 |
| Inconsistencies - CFG | 0 |
| Inconsistencies - data flow | 0 |
| Lines of code extracted | 59 |
| Lines of user code extracted | 59 |
| Lines of code extracted | 60 |
| Lines of user code extracted | 60 |
| Macro calls - resolved | 8 |
| Macro calls - total | 9 |
| Macro calls - unresolved | 1 |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| my_macro.rs:17:9:17:27 | MacroCall | Macro call was not resolved to a target. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
queries/diagnostics/UnresolvedMacroCalls.ql
5 changes: 3 additions & 2 deletions rust/ql/test/query-tests/diagnostics/my_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* total lines in this file: 18
* of which code: 10
* total lines in this file: 19
* of which code: 11
* of which only comments: 6
* of which blank: 2
*/
Expand All @@ -14,5 +14,6 @@ macro_rules! myMacro {
pub fn my_func() {
if true {
myMacro!();
myUndefinedMacro!();
}
}