Skip to content

Commit

Permalink
Everything required to run the script which replaces concatinations o…
Browse files Browse the repository at this point in the history
…f constants and literals in includes.
  • Loading branch information
jimmehc committed Sep 2, 2009
1 parent cd4a2e3 commit f4ec9df
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Makefile.am
Expand Up @@ -591,6 +591,10 @@ nobase_pkglib_LTLIBRARIES += plugins/tools/remove_all.la
plugins_tools_remove_all_la_SOURCES = plugins/tools/remove_all.cpp
plugins_tools_remove_all_la_LDFLAGS = ${PLUGIN_LDFLAGS}

nobase_pkglib_LTLIBRARIES += plugins/tools/replace_include_constants.la
plugins_tools_replace_include_constants_la_SOURCES = plugins/tools/replace_include_constants.cpp
plugins_tools_replace_include_constants_la_LDFLAGS = ${PLUGIN_LDFLAGS}

nobase_pkglib_LTLIBRARIES += plugins/tutorials/Comment_ifs.la
plugins_tutorials_Comment_ifs_la_SOURCES = plugins/tutorials/Comment_ifs.cpp
plugins_tutorials_Comment_ifs_la_LDFLAGS = ${PLUGIN_LDFLAGS}
Expand Down
25 changes: 25 additions & 0 deletions Makefile.in
Expand Up @@ -289,6 +289,16 @@ plugins_tools_remove_all_la_LINK = $(LIBTOOL) --tag=CXX \
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \
$(AM_CXXFLAGS) $(CXXFLAGS) \
$(plugins_tools_remove_all_la_LDFLAGS) $(LDFLAGS) -o $@
plugins_tools_replace_include_constants_la_LIBADD =
am_plugins_tools_replace_include_constants_la_OBJECTS = \
replace_include_constants.lo
plugins_tools_replace_include_constants_la_OBJECTS = \
$(am_plugins_tools_replace_include_constants_la_OBJECTS)
plugins_tools_replace_include_constants_la_LINK = $(LIBTOOL) --tag=CXX \
$(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CXXLD) \
$(AM_CXXFLAGS) $(CXXFLAGS) \
$(plugins_tools_replace_include_constants_la_LDFLAGS) \
$(LDFLAGS) -o $@
plugins_tutorials_Comment_ifs_la_LIBADD =
am_plugins_tutorials_Comment_ifs_la_OBJECTS = Comment_ifs.lo
plugins_tutorials_Comment_ifs_la_OBJECTS = \
Expand Down Expand Up @@ -413,6 +423,7 @@ SOURCES = $(libphc_la_SOURCES) \
$(plugins_tools_purity_test_la_SOURCES) \
$(plugins_tools_reduce_statements_la_SOURCES) \
$(plugins_tools_remove_all_la_SOURCES) \
$(plugins_tools_replace_include_constants_la_SOURCES) \
$(plugins_tutorials_Comment_ifs_la_SOURCES) \
$(plugins_tutorials_Expand_includes_la_SOURCES) \
$(plugins_tutorials_InsertDB_la_SOURCES) \
Expand Down Expand Up @@ -442,6 +453,7 @@ DIST_SOURCES = $(libphc_la_SOURCES) \
$(plugins_tools_purity_test_la_SOURCES) \
$(plugins_tools_reduce_statements_la_SOURCES) \
$(plugins_tools_remove_all_la_SOURCES) \
$(plugins_tools_replace_include_constants_la_SOURCES) \
$(plugins_tutorials_Comment_ifs_la_SOURCES) \
$(plugins_tutorials_Expand_includes_la_SOURCES) \
$(plugins_tutorials_InsertDB_la_SOURCES) \
Expand Down Expand Up @@ -1141,6 +1153,7 @@ nobase_pkglib_LTLIBRARIES = plugins/tests/canonical_unparser.la \
plugins/tools/function_finder.la \
plugins/tools/dynamic_things.la plugins/tools/get_defines.la \
plugins/tools/reduce_statements.la plugins/tools/remove_all.la \
plugins/tools/replace_include_constants.la \
plugins/tutorials/Comment_ifs.la \
plugins/tutorials/count_statements_difficult.la \
plugins/tutorials/count_statements_easy.la \
Expand Down Expand Up @@ -1187,6 +1200,8 @@ plugins_tools_reduce_statements_la_SOURCES = plugins/tools/reduce_statements.cpp
plugins_tools_reduce_statements_la_LDFLAGS = ${PLUGIN_LDFLAGS}
plugins_tools_remove_all_la_SOURCES = plugins/tools/remove_all.cpp
plugins_tools_remove_all_la_LDFLAGS = ${PLUGIN_LDFLAGS}
plugins_tools_replace_include_constants_la_SOURCES = plugins/tools/replace_include_constants.cpp
plugins_tools_replace_include_constants_la_LDFLAGS = ${PLUGIN_LDFLAGS}
plugins_tutorials_Comment_ifs_la_SOURCES = plugins/tutorials/Comment_ifs.cpp
plugins_tutorials_Comment_ifs_la_LDFLAGS = ${PLUGIN_LDFLAGS}
plugins_tutorials_count_statements_difficult_la_SOURCES = plugins/tutorials/count_statements_difficult.cpp
Expand Down Expand Up @@ -1379,6 +1394,8 @@ plugins/tools/reduce_statements.la: $(plugins_tools_reduce_statements_la_OBJECTS
$(plugins_tools_reduce_statements_la_LINK) -rpath $(pkglibdir) $(plugins_tools_reduce_statements_la_OBJECTS) $(plugins_tools_reduce_statements_la_LIBADD) $(LIBS)
plugins/tools/remove_all.la: $(plugins_tools_remove_all_la_OBJECTS) $(plugins_tools_remove_all_la_DEPENDENCIES) plugins/tools/$(am__dirstamp)
$(plugins_tools_remove_all_la_LINK) -rpath $(pkglibdir) $(plugins_tools_remove_all_la_OBJECTS) $(plugins_tools_remove_all_la_LIBADD) $(LIBS)
plugins/tools/replace_include_constants.la: $(plugins_tools_replace_include_constants_la_OBJECTS) $(plugins_tools_replace_include_constants_la_DEPENDENCIES) plugins/tools/$(am__dirstamp)
$(plugins_tools_replace_include_constants_la_LINK) -rpath $(pkglibdir) $(plugins_tools_replace_include_constants_la_OBJECTS) $(plugins_tools_replace_include_constants_la_LIBADD) $(LIBS)
plugins/tutorials/$(am__dirstamp):
@$(MKDIR_P) plugins/tutorials
@: > plugins/tutorials/$(am__dirstamp)
Expand Down Expand Up @@ -1635,6 +1652,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/raise_globals.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/reduce_statements.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/remove_all.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/replace_include_constants.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/show_traversal_order.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/source_vs_semantic_values.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/stats.Plo@am__quote@
Expand Down Expand Up @@ -2761,6 +2779,13 @@ remove_all.lo: plugins/tools/remove_all.cpp
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o remove_all.lo `test -f 'plugins/tools/remove_all.cpp' || echo '$(srcdir)/'`plugins/tools/remove_all.cpp

replace_include_constants.lo: plugins/tools/replace_include_constants.cpp
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT replace_include_constants.lo -MD -MP -MF $(DEPDIR)/replace_include_constants.Tpo -c -o replace_include_constants.lo `test -f 'plugins/tools/replace_include_constants.cpp' || echo '$(srcdir)/'`plugins/tools/replace_include_constants.cpp
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/replace_include_constants.Tpo $(DEPDIR)/replace_include_constants.Plo
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='plugins/tools/replace_include_constants.cpp' object='replace_include_constants.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o replace_include_constants.lo `test -f 'plugins/tools/replace_include_constants.cpp' || echo '$(srcdir)/'`plugins/tools/replace_include_constants.cpp

Comment_ifs.lo: plugins/tutorials/Comment_ifs.cpp
@am__fastdepCXX_TRUE@ $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT Comment_ifs.lo -MD -MP -MF $(DEPDIR)/Comment_ifs.Tpo -c -o Comment_ifs.lo `test -f 'plugins/tutorials/Comment_ifs.cpp' || echo '$(srcdir)/'`plugins/tutorials/Comment_ifs.cpp
@am__fastdepCXX_TRUE@ mv -f $(DEPDIR)/Comment_ifs.Tpo $(DEPDIR)/Comment_ifs.Plo
Expand Down
89 changes: 89 additions & 0 deletions misc/replace_concat_includes.php
@@ -0,0 +1,89 @@
#!/usr/bin/env php
<?php

include ("test/framework/lib/header.php");

function eh ($errno, $errstr)
{
if ($errno == E_NOTICE)
return;

print "$errno, $errstr";
}

set_error_handler ("eh", E_ALL);



$directory_name = "downloaded_php/downloads/";

$dirs = get_directories ($directory_name);
foreach ($dirs as $dir)
{
$defines = "";
$i = 0;
$files = get_all_files ($dir);
foreach ($files as $file)
{
if ($file != NULL)
{
$defines = $defines.get_defines ($file);
echo "Analysing $file\n";
}
}
foreach ($files as $file)
{
echo "Applying changes to $file\n";
apply_changes ($file, $defines);

}

}



function get_directories ($dirname)
{
$result = array ();

foreach (new DirectoryIterator($dirname) as $fileInfo)
{
if ($fileInfo->isDot())
continue;

$result[] = $fileInfo->getPathname ();
}

return $result;
}

function get_all_files ($dir)
{
$results = split ("\n", _exec ("find $dir/ -type f -name \"*.php\""));
array_pop ($results);
return $results;
}


function _exec ($command)
{
// 20 seconds, max
list ($output) = complete_exec ($command, NULL, 20);
return $output;
}

function get_defines ($file)
{
$command = "src/phc $file --run plugins/tools/get_defines.la 2>/dev/null";
return _exec ($command);
}

function apply_changes ($file, $defines)
{
$command = "src/phc $file --run plugins/tools/replace_include_constants.la --r-option='$defines' --dump=decomment 2>/dev/null";
$newcode = _exec ($command);
$fh = fopen ($file, "w");
fwrite ($fh, $newcode);
fclose ($fh);
}
?>
12 changes: 11 additions & 1 deletion plugins/tools/get_defines.cpp
Expand Up @@ -44,7 +44,17 @@ class Get_defines: virtual public GC_obj, public Visitor
Literal* value = dynamic_cast<Literal*> (params->back ()->expr);

if (key && value)
{
foreach (char c, *key->get_value_as_string ())
if (c == '\n' || c == '"' || c == '\'')
return;

foreach (char c, *value->get_value_as_string ())
if (c == '\n' || c == '"' || c == '\'')
return;

definitions [*key->get_value_as_string ()] = *value->get_value_as_string ();
}
}
}
}
Expand All @@ -53,7 +63,7 @@ class Get_defines: virtual public GC_obj, public Visitor
{
string s,t;
foreach (tie (s,t), definitions)
cout << s << "," << t << endl;
cout << s << "\\," << t << "\\|";

exit (0);
}
Expand Down
68 changes: 68 additions & 0 deletions plugins/tools/replace_include_constants.cpp
@@ -0,0 +1,68 @@
/*
* phc -- the open source PHP compiler
* See doc/license/README.license for licensing information
*
*
*/

#include <iostream>
#include "AST_visitor.h"
#include "process_ast/AST_unparser.h"
#include "pass_manager/Plugin_pass.h"
#include "lib/List.h"
#include "process_ir/General.h"
#include <boost/algorithm/string.hpp>

using namespace AST;
using namespace std;
using namespace boost;

class Replace_include_constants : virtual public GC_obj, public Visitor
{
public:

Map<string, string> def_dictionary;

Replace_include_constants (String* option)
{
list<string> dict; // List (capital 'L') doesn't support the necessary constructors for boost split
string delim("|");
split (dict, *option, is_any_of(delim));
foreach (string entry, dict)
{
list<string> parsed_entry;
split (parsed_entry, entry, is_any_of(","));
def_dictionary[parsed_entry.front ()] = parsed_entry.back ();
}
}


void post_bin_op (Bin_op* bop)
{
if (true || bop->op->value == s("."))
{
Constant* con;
if ((con = dynamic_cast<Constant*> (bop->left)))
{
if (def_dictionary.has (*con->constant_name->value))
bop->left = new STRING (s(def_dictionary[*con->constant_name->value]));
}

if ((con = dynamic_cast<Constant*> (bop->right)))
{
if (def_dictionary.has (*con->constant_name->value))
bop->right = new STRING (s(def_dictionary[*con->constant_name->value]));
}
}
}
};

extern "C" void load (Pass_manager* pm, Plugin_pass* pass)
{
pm->add_after_named_pass (pass, s("incl1"));
}

extern "C" void run_ast (PHP_script* in, Pass_manager* pm, String* option)
{
in->visit (new Replace_include_constants (option));
}

0 comments on commit f4ec9df

Please sign in to comment.