Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1644,8 +1644,9 @@ trait RustVisitor(implicit withSchemaValidation: ValidationMode) { this: AstCrea
// MacroCall
private def visitMacroExpr(macroExpr: MacroExpr): Ast = {
visitMacroCall(macroExpr.macroCall) match {
case Nil => macroNotExpanded(macroExpr.macroCall)
case exprAst :: Nil => exprAst
case asts => blockAst(blockNode(macroExpr), asts.toList)
case asts => blockAst(blockNode(macroExpr, code(macroExpr), typeFullNameForExpr(macroExpr)), asts.toList)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class MacroTests extends Rust2CpgSuite(noSysRoot = true) {

"have correct children" in {
inside(cpg.method.nameExact("main").block.assignment.source.isBlock.astChildren.l) { case (three: Block) :: Nil =>
three.code shouldBe "three!()"
three.typeFullName shouldBe "i32"
inside(three.astChildren.l) {
case (local: Local) :: (assign: Call) :: (assignPlus: Call) :: (ident: Identifier) :: Nil =>
local.name shouldBe "s"
Expand Down Expand Up @@ -196,6 +198,60 @@ class MacroTests extends Rust2CpgSuite(noSysRoot = true) {
}
}
}

"identity proc-macro call" should {
val cpg = code("""
|use proc_macro::TokenStream;
|
|#[proc_macro]
|pub fn foo(item: TokenStream) -> TokenStream { item }
|
|pub fn bar() {
| let baz = foo![Qux];
| foo![Qux]
|}
|""".stripMargin)
.moreCode(
"""
|[package]
|name = "rust2cpgtest"
|version = "0.1.0"
|edition = "2021"
|
|[lib]
|proc-macro = true
|""".stripMargin,
fileName = "Cargo.toml"
)

"have correct assignment" in {
cpg.method.fullNameExact("rust2cpgtest::bar").block.assignment.code.l shouldBe List("let baz = foo![Qux];")

// TODO(rust_ast_gen): expand proc-macros.
inside(cpg.method.fullNameExact("rust2cpgtest::bar").block.assignment.source.l) { case (qux: Unknown) :: Nil =>
qux.code shouldBe "foo![Qux]"
qux.lineNumber shouldBe Some(8)
}
pendingUntilFixed {
inside(cpg.method.fullNameExact("rust2cpgtest::bar").block.assignment.source.l) {
case (qux: Identifier) :: Nil =>
qux.name shouldBe "Qux"
qux.code shouldBe "Qux"
}
}
}

// TODO: update once proc-macros are supported.
"have correct return" in {
inside(cpg.method.nameExact("bar").block.astChildren.isReturn.l) { case ret :: Nil =>
ret.code shouldBe "foo![Qux]"
inside(ret.astChildren.l) { case (qux: Unknown) :: Nil =>
qux.code shouldBe "foo![Qux]"
qux.lineNumber shouldBe Some(9)
}
}
}
}
}

class MacroTestsWithSysroot extends Rust2CpgSuite(noSysRoot = false) {
Expand Down