Skip to content

Commit 49bf48e

Browse files
committed
Rust: fix duplicate asm! expressions
1 parent d38459a commit 49bf48e

File tree

30 files changed

+162
-139
lines changed

30 files changed

+162
-139
lines changed

rust/ast-generator/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ fn should_enum_be_skipped(name: &str) -> bool {
8282

8383
fn should_node_be_skipped(name: &str) -> bool {
8484
name == "TypeAnchor" // we flatten TypeAnchor into PathSegment in the extractor
85+
|| name == "MacroStmts" // we workaround a getter bug in the extractor
8586
}
8687

8788
fn should_node_be_skipped_in_extractor(name: &str) -> bool {

rust/extractor/src/generated/.generated.list

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

rust/extractor/src/generated/top.rs

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

rust/extractor/src/translate/base.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,38 @@ impl<'a> Translator<'a> {
834834
}
835835
}
836836

837+
pub(crate) fn emit_macro_stmts(
838+
&mut self,
839+
node: &ast::MacroStmts,
840+
) -> Option<Label<generated::MacroBlockExpr>> {
841+
// not generated to work around a bug in rust-analyzer AST generation machinery.
842+
// Because an Expr can also be a Stmt (AsmExpr: Expr and AsmExpr: Item: Stmt)
843+
// then such an element will be returned by both `expr()` and `statements()`
844+
let mut statements = node.statements().collect::<Vec<_>>();
845+
let tail_expr = node.expr();
846+
if tail_expr
847+
.as_ref()
848+
.is_some_and(|e| statements.last().is_some_and(|s| s.syntax() == e.syntax()))
849+
{
850+
// if the expression matched as both the tail_expr and the last of the statements,
851+
// only take it as tail_expr
852+
statements.pop();
853+
}
854+
let tail_expr = tail_expr.and_then(|e| self.emit_expr(&e));
855+
let statements = statements
856+
.iter()
857+
.filter_map(|x| self.emit_stmt(x))
858+
.collect();
859+
let label = self.trap.emit(generated::MacroBlockExpr {
860+
id: TrapId::Star,
861+
tail_expr,
862+
statements,
863+
});
864+
self.emit_location(label, node);
865+
self.emit_tokens(node, label.into(), node.syntax().children_with_tokens());
866+
Some(label)
867+
}
868+
837869
fn is_attribute_macro_target(&self, node: &ast::Item) -> bool {
838870
// rust-analyzer considers as an `attr_macro_call` also a plain macro call, but we want to
839871
// process that differently (in `extract_macro_call_expanded`)

rust/extractor/src/translate/generated.rs

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

rust/ql/.generated.list

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

rust/ql/lib/codeql/rust/controlflow/internal/generated/CfgNodes.qll

Lines changed: 20 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/ql/lib/codeql/rust/elements/MacroBlockExpr.qll

Lines changed: 8 additions & 2 deletions
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/MacroBlockExprImpl.qll

Lines changed: 8 additions & 2 deletions
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/generated/MacroBlockExpr.qll

Lines changed: 23 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)