XPathでソースコード検索

 XPathでソースコード検索

第七回闇PHP勉強会

003f59d1b2e3b864c744f8eb21f68e82?s=128

Atsushi Takayama

December 12, 2016
Tweet

Transcript

  1. XPathͰιʔείʔυݕࡧ ҋPHPษڧձ (2016/12/11)

  2. ࣗݾ঺հ ߴࢁԹ @edvakf ϐΫγϒגࣜձࣾ ࣥߦ໾һ CTO ISUCON6ຊબग़୊͠·ͨ͠ JavaScript, PHP, Ruby,

    Go, Scala ͋ͨΓ͕कඋൣғ
  3. ྫ͑͹͜͏͍͏έʔε ϓϩδΣΫτͷίʔσΟϯάن໿Ͱ
 in_arrayͷୈ3Ҿ਺ʹtrueΛ౉͢͜ͱ͕ඞਢʹͳͬͨ ୈ3Ҿ਺͕trueͰͳ͍ՕॴΛ͢΂ͯݕग़͍ͨ͠

  4. grep git grep in_array | grep -v true in_array(1, [1,2]);

    ←ݕग़Ͱ͖Δ in_array(1, [
 1,
 2,
 ], true); ←͜ͷํ๏Ͱ͸ݕग़Ͱ͖ͳ͍
  5. खܰʹݕग़Ͱ͖Δ πʔϧ͸ແ͍

  6. token_get_all % php -r 'var_export(token_get_all("<?php in_array(1, [1, 2]);"));' array (

    0 => array ( 0 => 379, 1 => '<?php ', 2 => 1, ), 1 => array ( 0 => 319, 1 => 'in_array', 2 => 1, ), 2 => '(', 3 => array ( 0 => 317, 1 => '1', 2 => 1, ), 4 => ',', 5 => array ( 0 => 382, 1 => ' ', 2 => 1, ), 6 => '[', 7 => array ( 0 => 317, 1 => '1', 2 => 1, ), 8 => ',', 9 => array ( 0 => 382, 1 => ' ', 2 => 1, ), 10 => array ( 0 => 317, 1 => '2', 2 => 1, ), 11 => ']', 12 => ')', 13 => ';', )
  7. τʔΫϯྻ͔Β ݕࡧ͢Δͷ͸ҙ֎ͱ໘౗ εςʔτϚγϯΛ࡞Δ ಛఆͷจ຺ʢεςʔτʣͰ ಛఆͷτʔΫϯΛൃݟͨ͠ Βܯࠂ จ຺ͷωετ͕͋ΓಘΔͷ Ͱɺ࣮ࡍʹ͸ӈͷྫΑΓԿ ഒ΋ෳࡶʹͳΔ

  8. ast\Node::__set_state(array( 'kind' => 133, 'flags' => 0, 'lineno' => 1,

    'children' => array ( 0 => ast\Node::__set_state(array( 'kind' => 515, 'flags' => 0, 'lineno' => 1, 'children' => array ( 'expr' => ast\Node::__set_state(array( 'kind' => 2048, 'flags' => 1, 'lineno' => 1, 'children' => array ( 'name' => 'in_array', ), )), 'args' => ast\Node::__set_state(array( 'kind' => 128, 'flags' => 0, 'lineno' => 1, 'children' => array ( 0 => 1, 1 => ast\Node::__set_state(array( 'kind' => 130, 'flags' => 0, 'lineno' => 1, 'children' => array ( 0 => ast\Node::__set_state(array( 'kind' => 525, 'flags' => 0, $code = "<?php in_array(1,[1,2]);"; $ast = ast\parse_code($code, 35); var_export($ast); php-ast
  9. ASTʹର͢Δݕࡧ τʔΫϯྻʹର͢Δݕࡧʹ ൺ΂Ε͹·ͩ؆୯͕ͩɺ࠶ ؼͳͲ͕ग़͖ͯͯ໘౗ ΍ͬͺΓπϦʔʹର͢Δݕ ࡧΫΤϦͱ͍͑͹…

  10. XPath ASTΛXMLʹม׵͢Ε͹XPath͕࢖͑Δʂ XPath͸XMLʹ͓͚Δਖ਼نදݱ πϦʔʹର͢Δݕࡧͱ͍͑͹XPath ࢓༷͕͔ͬ͠Γ͍࣮ͯͯ͠૷΋ଟ͘ɺղઆจॻ΋ଟ͍ DSLΛ֮͑Δͷ͸ਏ͍ͷͰɺطʹ஌ͬͯΔDSL͕࢖͑Δͷ͕خ͍͠ ؆୯ͳΫΤϦ͸؆୯ʹॻ͚ɺͪΐͬͱෳࡶͳΫΤϦ΋ॻ͚Δ

  11. php-ast-to-xml https://github.com/edvakf/php-ast-to-xml

  12. php-ast-xpath ୈࡾҾ਺ͷ༩͑ΒΕ͍ͯͳ͍in_arrayΛݕࡧ $ php-ast-xpath some.php '//AST_CALL[./expr/ AST_NAME/name/scalar[@value="in_array"] and not(./args/AST_ARG_LIST/*[3])]'
 some.php:10


    some.php:12
  13. σϞ

  14. ·ͱΊ ϊʔυ୳ࡧϓϩάϥϜΛॻ͘ͷ͸େม php-astͷग़ྗΛػցతʹXMLʹม׵Ͱ͖Δ XPathΛ࢖͑͹؆୯ͳΫΤϦͰgrepΑΓਖ਼֬ʹ
 ιʔείʔυΛݕࡧͰ͖Δ

  15. ͔͜͜Β͸ໝ૝

  16. php-astͰASTʹͯ͠͠·͏ͱɺAST͔Βݩͷιʔε ίʔυʹ໭͢͜ͱ͕Ͱ͖ͳ͍ token_get_allͰऔಘͨ͠τʔΫϯྻͳΒ࿈݁͢Ε͹ݩ ͷιʔείʔυʹ໭ͤΔ token_get_allͷ৘ใΛอͬͨ··XMLʹม׵Ͱ͖Ε ͹ɺXSLTͰπϦʔม׵ͯ͠PHPʹ໭͢ϦϑΝΫλϦϯ άπʔϧ͕Ͱ͖ͦ͏ →LISPͷϚΫϩͩ