Skip to content

Commit

Permalink
Merge pull request #17940 from geoffw0/resolvable
Browse files Browse the repository at this point in the history
Rust: Add unresolved macro calls diagnostic
  • Loading branch information
geoffw0 authored Nov 25, 2024
2 parents 74aa47a + f92e855 commit 93e7202
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 12 deletions.
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 @@ -5,3 +5,4 @@ extractionWarning
| does_not_compile.rs:2:21:2:20 | expected SEMICOLON |
| does_not_compile.rs:2:26:2:25 | expected SEMICOLON |
| 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 @@ -4,3 +4,4 @@
| does_not_compile.rs:2:21:2:20 | expected SEMICOLON | Extraction warning in does_not_compile.rs with message expected SEMICOLON | 1 |
| does_not_compile.rs:2:26:2:25 | expected SEMICOLON | Extraction warning in does_not_compile.rs with message expected SEMICOLON | 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 | 375 |
| Elements extracted | 382 |
| Elements unextracted | 0 |
| Extraction errors | 0 |
| Extraction warnings | 6 |
| Extraction warnings | 7 |
| 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 | myUndefinedMacro!... | 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!();
}
}

0 comments on commit 93e7202

Please sign in to comment.