Skip to content

Commit bd79fc1

Browse files
committed
experiment with -x option
1 parent 1cb510e commit bd79fc1

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

ppremake/config_msvc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,5 @@
8686
** Also be sure to change the version number **
8787
** at the beginning of configure.in. **
8888
**************** ****************/
89-
#define VERSION "1.14"
89+
#define VERSION "1.15"
9090
/**************** UPDATE VERSION NUMBER HERE ****************/

ppremake/configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dnl **************** UPDATE VERSION NUMBER HERE ****************
55
dnl ** Also be sure to change the version number **
66
dnl ** at the end of config_msvc.h. **
77
dnl **************** ****************
8-
AM_INIT_AUTOMAKE(ppremake, 1.14)
8+
AM_INIT_AUTOMAKE(ppremake, 1.15)
99
dnl **************** UPDATE VERSION NUMBER HERE ****************
1010

1111
AM_CONFIG_HEADER(config.h)

ppremake/ppScope.cxx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//
44
////////////////////////////////////////////////////////////////////
55

6+
#include "ppremake.h"
67
#include "ppScope.h"
78
#include "ppNamedScopes.h"
89
#include "ppFilenamePattern.h"
@@ -493,7 +494,29 @@ set_directory(PPDirectory *directory) {
493494
////////////////////////////////////////////////////////////////////
494495
string PPScope::
495496
expand_string(const string &str) {
496-
return r_expand_string(str, (ExpandedVariable *)NULL);
497+
string result = r_expand_string(str, (ExpandedVariable *)NULL);
498+
499+
if (debug_expansions > 0 && str != result) {
500+
// Look for the str in our table--how many times has this
501+
// particular string been expanded?
502+
ExpandResultCount &result_count = debug_expand[str];
503+
504+
// Then, how many times has it expanded to this same result?
505+
// First, assuming this is the first time it has expanded to this
506+
// result, try to insert the result string with an initial count
507+
// of 1.
508+
pair<ExpandResultCount::iterator, bool> r =
509+
result_count.insert(ExpandResultCount::value_type(result, 1));
510+
511+
if (!r.second) {
512+
// If the result string was not successfully inserted into the
513+
// map, it was already there--so increment the count.
514+
ExpandResultCount::iterator rci = r.first;
515+
(*rci).second++;
516+
}
517+
}
518+
519+
return result;
497520
}
498521

499522
////////////////////////////////////////////////////////////////////

ppremake/ppremake.cxx

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ bool windows_platform = false;
3333
bool dry_run = false;
3434
bool verbose_dry_run = false;
3535
int verbose = 0;
36+
int debug_expansions = 0;
37+
38+
DebugExpand debug_expand;
39+
40+
class DebugExpandReport {
41+
public:
42+
DebugExpandReport(DebugExpand::const_iterator source,
43+
ExpandResultCount::const_iterator result) :
44+
_source(source),
45+
_result(result)
46+
{ }
47+
48+
const string &get_source() const {
49+
return (*_source).first;
50+
}
51+
const string &get_result() const {
52+
return (*_result).first;
53+
}
54+
int get_count() const {
55+
return (*_result).second;
56+
}
57+
58+
bool operator < (const DebugExpandReport &other) const {
59+
return get_count() > other.get_count();
60+
}
61+
62+
DebugExpand::const_iterator _source;
63+
ExpandResultCount::const_iterator _result;
64+
};
65+
3666

3767
static void
3868
usage() {
@@ -74,9 +104,15 @@ usage() {
74104
" -I Report the compiled-in default for INSTALL_DIR, and exit.\n"
75105
" -v Turn on verbose output (may help in debugging .pp files).\n"
76106
" -vv Be very verbose (if you're getting desperate).\n"
107+
" -x count Print a histogram of the count most-frequently expanded strings\n"
108+
" and their results. Useful to optimize .pp scripts so that\n"
109+
" variables are not needlessly repeatedly expanded.\n\n"
110+
77111
" -P Report the current platform name, and exit.\n\n"
112+
78113
" -D pp.dep Examine the given dependency file, and re-run ppremake\n"
79114
" only if the dependency file is stale.\n\n"
115+
80116
" -d Instead of generating makefiles, report the set of\n"
81117
" subdirectories that the named subdirectory depends on.\n"
82118
" Directories are named by their local name, not by the\n"
@@ -229,7 +265,7 @@ main(int argc, char *argv[]) {
229265
string progname = argv[0];
230266
extern char *optarg;
231267
extern int optind;
232-
const char *optstr = "hVIvPD:drnNp:c:s:";
268+
const char *optstr = "hVIvx:PD:drnNp:c:s:";
233269

234270
bool any_d = false;
235271
bool dependencies_stale = false;
@@ -268,6 +304,10 @@ main(int argc, char *argv[]) {
268304
++verbose;
269305
break;
270306

307+
case 'x':
308+
debug_expansions = atoi(optarg);
309+
break;
310+
271311
case 'P':
272312
report_platform();
273313
exit(0);
@@ -408,6 +448,32 @@ main(int argc, char *argv[]) {
408448
}
409449
}
410450

451+
if (debug_expansions > 0) {
452+
// Now report the worst expansion offenders. These are the
453+
// strings that were most often expanded to the same thing.
454+
cerr << "\nExpansion report:\n";
455+
vector<DebugExpandReport> report;
456+
457+
DebugExpand::const_iterator dei;
458+
for (dei = debug_expand.begin(); dei != debug_expand.end(); ++dei) {
459+
const ExpandResultCount &result_count = (*dei).second;
460+
ExpandResultCount::const_iterator rci;
461+
for (rci = result_count.begin(); rci != result_count.end(); ++rci) {
462+
report.push_back(DebugExpandReport(dei, rci));
463+
}
464+
}
465+
466+
sort(report.begin(), report.end());
467+
468+
int num_reports = min((int)report.size(), debug_expansions);
469+
for (int i = 0; i < num_reports; i++) {
470+
cerr << "\"" << report[i].get_source() << "\" -> \""
471+
<< report[i].get_result()
472+
<< "\" (" << report[i].get_count() << ")\n";
473+
}
474+
cerr << "\n";
475+
}
476+
411477
cerr << "No errors.\n";
412478
return (0);
413479
}

ppremake/ppremake.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ typedef ios::seek_dir ios_seekdir;
5050
#endif
5151

5252
#include <string>
53+
#include <map>
5354

5455
#ifdef HAVE_NAMESPACE
5556
using namespace std;
@@ -92,6 +93,16 @@ extern bool windows_platform;
9293
extern bool dry_run;
9394
extern bool verbose_dry_run;
9495
extern int verbose; // 0..9 to set verbose level. 0 == off.
96+
extern int debug_expansions;
97+
98+
/* This structure tracks the number of expansions that are performed
99+
on a particular string, and the different values it produces, only
100+
if debug_expansions (above) is set true by command-line parameter
101+
-x. */
102+
typedef map<string, int> ExpandResultCount;
103+
typedef map<string, ExpandResultCount> DebugExpand;
104+
extern DebugExpand debug_expand;
105+
95106
#endif
96107

97108
/* These are defined so that we may build Filename, DSearchPath, and

0 commit comments

Comments
 (0)