experiment with -x option

This commit is contained in:
David Rose 2004-06-14 23:25:55 +00:00
parent 1cb510e7c1
commit bd79fc1738
5 changed files with 104 additions and 4 deletions

View File

@ -86,5 +86,5 @@
** Also be sure to change the version number **
** at the beginning of configure.in. **
**************** ****************/
#define VERSION "1.14"
#define VERSION "1.15"
/**************** UPDATE VERSION NUMBER HERE ****************/

View File

@ -5,7 +5,7 @@ dnl **************** UPDATE VERSION NUMBER HERE ****************
dnl ** Also be sure to change the version number **
dnl ** at the end of config_msvc.h. **
dnl **************** ****************
AM_INIT_AUTOMAKE(ppremake, 1.14)
AM_INIT_AUTOMAKE(ppremake, 1.15)
dnl **************** UPDATE VERSION NUMBER HERE ****************
AM_CONFIG_HEADER(config.h)

View File

@ -3,6 +3,7 @@
//
////////////////////////////////////////////////////////////////////
#include "ppremake.h"
#include "ppScope.h"
#include "ppNamedScopes.h"
#include "ppFilenamePattern.h"
@ -493,7 +494,29 @@ set_directory(PPDirectory *directory) {
////////////////////////////////////////////////////////////////////
string PPScope::
expand_string(const string &str) {
return r_expand_string(str, (ExpandedVariable *)NULL);
string result = r_expand_string(str, (ExpandedVariable *)NULL);
if (debug_expansions > 0 && str != result) {
// Look for the str in our table--how many times has this
// particular string been expanded?
ExpandResultCount &result_count = debug_expand[str];
// Then, how many times has it expanded to this same result?
// First, assuming this is the first time it has expanded to this
// result, try to insert the result string with an initial count
// of 1.
pair<ExpandResultCount::iterator, bool> r =
result_count.insert(ExpandResultCount::value_type(result, 1));
if (!r.second) {
// If the result string was not successfully inserted into the
// map, it was already there--so increment the count.
ExpandResultCount::iterator rci = r.first;
(*rci).second++;
}
}
return result;
}
////////////////////////////////////////////////////////////////////

View File

@ -33,6 +33,36 @@ bool windows_platform = false;
bool dry_run = false;
bool verbose_dry_run = false;
int verbose = 0;
int debug_expansions = 0;
DebugExpand debug_expand;
class DebugExpandReport {
public:
DebugExpandReport(DebugExpand::const_iterator source,
ExpandResultCount::const_iterator result) :
_source(source),
_result(result)
{ }
const string &get_source() const {
return (*_source).first;
}
const string &get_result() const {
return (*_result).first;
}
int get_count() const {
return (*_result).second;
}
bool operator < (const DebugExpandReport &other) const {
return get_count() > other.get_count();
}
DebugExpand::const_iterator _source;
ExpandResultCount::const_iterator _result;
};
static void
usage() {
@ -74,9 +104,15 @@ usage() {
" -I Report the compiled-in default for INSTALL_DIR, and exit.\n"
" -v Turn on verbose output (may help in debugging .pp files).\n"
" -vv Be very verbose (if you're getting desperate).\n"
" -x count Print a histogram of the count most-frequently expanded strings\n"
" and their results. Useful to optimize .pp scripts so that\n"
" variables are not needlessly repeatedly expanded.\n\n"
" -P Report the current platform name, and exit.\n\n"
" -D pp.dep Examine the given dependency file, and re-run ppremake\n"
" only if the dependency file is stale.\n\n"
" -d Instead of generating makefiles, report the set of\n"
" subdirectories that the named subdirectory depends on.\n"
" Directories are named by their local name, not by the\n"
@ -229,7 +265,7 @@ main(int argc, char *argv[]) {
string progname = argv[0];
extern char *optarg;
extern int optind;
const char *optstr = "hVIvPD:drnNp:c:s:";
const char *optstr = "hVIvx:PD:drnNp:c:s:";
bool any_d = false;
bool dependencies_stale = false;
@ -268,6 +304,10 @@ main(int argc, char *argv[]) {
++verbose;
break;
case 'x':
debug_expansions = atoi(optarg);
break;
case 'P':
report_platform();
exit(0);
@ -408,6 +448,32 @@ main(int argc, char *argv[]) {
}
}
if (debug_expansions > 0) {
// Now report the worst expansion offenders. These are the
// strings that were most often expanded to the same thing.
cerr << "\nExpansion report:\n";
vector<DebugExpandReport> report;
DebugExpand::const_iterator dei;
for (dei = debug_expand.begin(); dei != debug_expand.end(); ++dei) {
const ExpandResultCount &result_count = (*dei).second;
ExpandResultCount::const_iterator rci;
for (rci = result_count.begin(); rci != result_count.end(); ++rci) {
report.push_back(DebugExpandReport(dei, rci));
}
}
sort(report.begin(), report.end());
int num_reports = min((int)report.size(), debug_expansions);
for (int i = 0; i < num_reports; i++) {
cerr << "\"" << report[i].get_source() << "\" -> \""
<< report[i].get_result()
<< "\" (" << report[i].get_count() << ")\n";
}
cerr << "\n";
}
cerr << "No errors.\n";
return (0);
}

View File

@ -50,6 +50,7 @@ typedef ios::seek_dir ios_seekdir;
#endif
#include <string>
#include <map>
#ifdef HAVE_NAMESPACE
using namespace std;
@ -92,6 +93,16 @@ extern bool windows_platform;
extern bool dry_run;
extern bool verbose_dry_run;
extern int verbose; // 0..9 to set verbose level. 0 == off.
extern int debug_expansions;
/* This structure tracks the number of expansions that are performed
on a particular string, and the different values it produces, only
if debug_expansions (above) is set true by command-line parameter
-x. */
typedef map<string, int> ExpandResultCount;
typedef map<string, ExpandResultCount> DebugExpand;
extern DebugExpand debug_expand;
#endif
/* These are defined so that we may build Filename, DSearchPath, and