@@ -605,6 +605,49 @@ TEST_F(TransformerTest, RewriteDescendantsReferToParentBinding) {
605605 Input, Expected);
606606}
607607
608+ TEST_F (TransformerTest, RewriteDescendantsApplyFirstOrderedRuleUnrelated) {
609+ std::string Input = " int f(int x) { int y = x; return x; }" ;
610+ std::string Expected = " int f(int x) { char y = 3; return 3; }" ;
611+ auto IntToChar = makeRule (typeLoc (loc (qualType (isInteger (), builtinType ()))),
612+ changeTo (cat (" char" )));
613+ auto InlineX =
614+ makeRule (declRefExpr (to (varDecl (hasName (" x" )))), changeTo (cat (" 3" )));
615+ testRule (
616+ makeRule (functionDecl (hasName (" f" ), hasBody (stmt ().bind (" body" ))),
617+ rewriteDescendants (" body" , applyFirst ({InlineX, IntToChar}))),
618+ Input, Expected);
619+ }
620+
621+ TEST_F (TransformerTest, RewriteDescendantsApplyFirstOrderedRuleRelated) {
622+ std::string Input = " int f(int x) { int y = x; return x; }" ;
623+ std::string Expected = " int f(int x) { int y = 3; return y; }" ;
624+ auto ReturnY = makeRule (
625+ traverse (TK_IgnoreUnlessSpelledInSource,
626+ declRefExpr (to (varDecl (hasName (" x" ))), hasParent (returnStmt ()))),
627+ changeTo (cat (" y" )));
628+ auto InlineX = makeRule (traverse (TK_IgnoreUnlessSpelledInSource,
629+ declRefExpr (to (varDecl (hasName (" x" ))))),
630+ changeTo (cat (" 3" )));
631+ testRule (makeRule (functionDecl (hasName (" f" ), hasBody (stmt ().bind (" body" ))),
632+ rewriteDescendants (" body" , applyFirst ({ReturnY, InlineX}))),
633+ Input, Expected);
634+ }
635+
636+ TEST_F (TransformerTest, RewriteDescendantsApplyFirstOrderedRuleRelatedSwapped) {
637+ std::string Input = " int f(int x) { int y = x; return x; }" ;
638+ std::string Expected = " int f(int x) { int y = 3; return 3; }" ;
639+ auto ReturnY = makeRule (
640+ traverse (TK_IgnoreUnlessSpelledInSource,
641+ declRefExpr (to (varDecl (hasName (" x" ))), hasParent (returnStmt ()))),
642+ changeTo (cat (" y" )));
643+ auto InlineX = makeRule (traverse (TK_IgnoreUnlessSpelledInSource,
644+ declRefExpr (to (varDecl (hasName (" x" ))))),
645+ changeTo (cat (" 3" )));
646+ testRule (makeRule (functionDecl (hasName (" f" ), hasBody (stmt ().bind (" body" ))),
647+ rewriteDescendants (" body" , applyFirst ({InlineX, ReturnY}))),
648+ Input, Expected);
649+ }
650+
608651TEST_F (TransformerTest, RewriteDescendantsUnboundNode) {
609652 std::string Input =
610653 " int f(int x) { int y = x; { int z = x * x; } return x; }" ;
0 commit comments