From bd79fc1738936afee221008471d2dbfbdbfb34ec Mon Sep 17 00:00:00 2001 From: David Rose Date: Mon, 14 Jun 2004 23:25:55 +0000 Subject: [PATCH] experiment with -x option --- ppremake/config_msvc.h | 2 +- ppremake/configure.in | 2 +- ppremake/ppScope.cxx | 25 +++++++++++++++- ppremake/ppremake.cxx | 68 +++++++++++++++++++++++++++++++++++++++++- ppremake/ppremake.h | 11 +++++++ 5 files changed, 104 insertions(+), 4 deletions(-) diff --git a/ppremake/config_msvc.h b/ppremake/config_msvc.h index 76083e6370..8895935534 100644 --- a/ppremake/config_msvc.h +++ b/ppremake/config_msvc.h @@ -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 ****************/ diff --git a/ppremake/configure.in b/ppremake/configure.in index 86d055824c..059c6f4876 100644 --- a/ppremake/configure.in +++ b/ppremake/configure.in @@ -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) diff --git a/ppremake/ppScope.cxx b/ppremake/ppScope.cxx index 3ed925a938..0b506bcae9 100644 --- a/ppremake/ppScope.cxx +++ b/ppremake/ppScope.cxx @@ -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 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; } //////////////////////////////////////////////////////////////////// diff --git a/ppremake/ppremake.cxx b/ppremake/ppremake.cxx index 0b38a47c33..fed53954da 100644 --- a/ppremake/ppremake.cxx +++ b/ppremake/ppremake.cxx @@ -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 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); } diff --git a/ppremake/ppremake.h b/ppremake/ppremake.h index 6d88ca8363..a870e9386d 100644 --- a/ppremake/ppremake.h +++ b/ppremake/ppremake.h @@ -50,6 +50,7 @@ typedef ios::seek_dir ios_seekdir; #endif #include +#include #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 ExpandResultCount; +typedef map DebugExpand; +extern DebugExpand debug_expand; + #endif /* These are defined so that we may build Filename, DSearchPath, and