Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

sgrep: support for regexp when matching strings

Summary:
A few people asked for this. This will also make it easy
to add the lint check against the use of $GLOBALS['<package_name>_CONSTANTS']

Task ID: #674013

Blame Rev:

Reviewers: julienv

CC: arice, erling, dsanduleac, mkjones

Test Plan:
unit test included
$ ./sgrep -test
3 tests OK
$ ./sgrep -verbose -f tests/php/sgrep/regexp.sgrep tests/php/sgrep/regexp.php
processing: tests/php/sgrep/regexp.php
tests/php/sgrep/regexp.php:3
 foo('FOO_CONSTANT');

Revert Plan:

Tags:

- begin *PUBLIC* platform impact section -
Bugzilla: #
- end platform impact -

Differential Revision: 310351
  • Loading branch information...
commit 6024490323fee28aa5d29d0fa31d7852a664693e 1 parent bc94dc7
@aryx aryx authored
View
5 Makefile
@@ -399,10 +399,11 @@ clean::
#------------------------------------------------------------------------------
sgrep: $(LIBS) main_sgrep.cmo
- $(OCAMLC) $(CUSTOM) -o $@ $(SYSLIBS) $^
+ $(OCAMLC) $(CUSTOM) -o $@ $(SYSLIBS) $(REGEXPCMA) $^
sgrep.opt: $(BASICLIBS:.cma=.cmxa) main_sgrep.cmx
- $(OCAMLOPT) $(STATIC) -o $@ $(BASICSYSLIBS:.cma=.cmxa) $^
+ $(OCAMLOPT) $(STATIC) -o $@ $(BASICSYSLIBS:.cma=.cmxa) \
+ $(REGEXPCMA:.cma=.cmxa) $^
clean::
rm -f sgrep
View
1  lang_php/matcher/Makefile
@@ -22,6 +22,7 @@ INCLUDEDIRS= $(TOP)/commons $(TOP)/commons/ocollection \
$(TOP)/commons/ocamlextra \
$(TOP)/h_program-lang $(TOP)/h_version-control \
$(TOP)/globals \
+ $(TOP)/external/ocamlpcre/lib \
../parsing
##############################################################################
View
23 lang_php/matcher/php_vs_php.ml
@@ -29,8 +29,10 @@ open Common
* pfff/meta/gen_code -matcher_gen_all ast_php.ml
*
* using ocaml pad-style reflection (see commons/ocaml.ml) on
- * parsing_php/ast_php.ml. An alternative could have been to transform
- * ast_php.ml in a very simple term language and do the 1-vs-1 match
+ * parsing_php/ast_php.ml.
+ *
+ * An alternative could have been to transform ast_php.ml
+ * in a very simple term language and do the 1-vs-1 match
* on this term language, but depending on the construct (a PHP variable,
* a string) we may want to do special things so it's better to work
* on the full AST. Working on a term language would be like working
@@ -44,7 +46,7 @@ open Common
*)
(* A is the pattern, and B the concrete source code. For now
- * we both use the same type, Ast_php, but they may differ later
+ * we both use the same module, Ast_php, but they may differ later
* as the expressivity of the pattern language grows.
*)
module A = Ast_php
@@ -1879,6 +1881,21 @@ and m_constant a b =
B.String (s, b)
))
+ (* todo: handle spatch too! one could want to bind things to \1 \2
+ * and reference those \xxx in the + side of a semantic patch.
+ *)
+ | A.String(name, info_name), B.String(sb, info_sb)
+ when name =~ "^=~/\\(.*\\)/$" ->
+ let s = Common.matched1 name in
+ let rex = Pcre.regexp s in
+ if Pcre.pmatch ~rex sb
+ then
+ return (
+ A.String(name, info_name),
+ B.String(sb, info_sb)
+ )
+ else fail ()
+
| A.String(a1), B.String(b1) ->
m_wrap m_string a1 b1 >>= (fun (a1, b1) ->
return (
View
3  lang_php/matcher/unit_matcher_php.ml
@@ -74,6 +74,9 @@ let sgrep_unittest = [
"return <x:frag foo=\"3\" border=\"1\" ></x:frag>;",
false;
+ (* regexp, pcre syntax *)
+ "foo('=~/.*CONSTANT/');", "foo('MY_CONSTANT');", true;
+ "foo('=~/.*CONSTANT/');", "foo('MY_CONSTAN');", false;
] in
triples +> List.iter (fun (spattern, scode, should_match) ->
View
4 tests/php/sgrep/regexp.php
@@ -0,0 +1,4 @@
+<?php
+
+foo('FOO_CONSTANT');
+foo('FOO2');
View
1  tests/php/sgrep/regexp.sgrep
@@ -0,0 +1 @@
+foo('=~/.*_CONSTANT/')
Please sign in to comment.
Something went wrong with that request. Please try again.