forked from pbiggar/phc
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Everything required to run the script which replaces concatinations o…
…f constants and literals in includes.
- Loading branch information
Showing
5 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} |