From b679b1452d6513c0769928a2238ccd367c2b78f7 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 31 Mar 2020 08:07:02 +0200 Subject: Handle duplicate config directives for same variable --- libbuild2/config/utility.hxx | 3 ++- libbuild2/parser.cxx | 19 ++++++++++++++++++- tests/directive/config.testscript | 10 +++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/libbuild2/config/utility.hxx b/libbuild2/config/utility.hxx index f2bddc2..38a3ae1 100644 --- a/libbuild2/config/utility.hxx +++ b/libbuild2/config/utility.hxx @@ -99,7 +99,8 @@ namespace build2 // then, if there is no value, call the version with the default value and // end up with the same result if we called the default value version // straight away. This is useful when computing the default value is - // expensive. + // expensive. It is also ok to call both versions multiple times provided + // the flags are the same. // // @@ Should we pass flags and interpret save_null_omitted to treat null // as undefined? Sounds logical. diff --git a/libbuild2/parser.cxx b/libbuild2/parser.cxx index 532b357..797a484 100644 --- a/libbuild2/parser.cxx +++ b/libbuild2/parser.cxx @@ -1885,7 +1885,24 @@ namespace build2 if (l.var != nullptr) { - config_report.push_back (make_pair (l, move (*report))); + auto r (make_pair (l, move (*report))); + + // If we have a duplicate, update it (it could be useful to have + // multiple config directives to "probe" the value before calculating + // the default; see lookup_config() for details). + // + auto i (find_if (config_report.begin (), + config_report.end (), + [&l] (const pair& p) + { + return p.first.var == l.var; + })); + + if (i == config_report.end ()) + config_report.push_back (move (r)); + else + *i = move (r); + config_report_new = config_report_new || new_val; } } diff --git a/tests/directive/config.testscript b/tests/directive/config.testscript index f77a098..e84ca0a 100644 --- a/tests/directive/config.testscript +++ b/tests/directive/config.testscript @@ -180,6 +180,8 @@ test.arguments = config [string, config.report.variable=e] config.test.e ?= abc config [ config.report] f config [bool] config.test.n ?= [null] + config [bool] config.test.p + config [bool] config.test.p ?= true e = "'$config.test.e'" f = ($config.test.b || $config.test.c) @@ -206,11 +208,16 @@ test.arguments = e 'abc' f true n [null] + p true EOO # Configured. # - $* configure config.test.a=true config.test.e=xyz config.test.n=true; + $* configure \ + config.test.a=true \ + config.test.e=xyz \ + config.test.n=true \ + config.test.p=false; $* noop -v; $* noop -V 2>>~/EOO/; /config test@.+/ @@ -223,6 +230,7 @@ test.arguments = e 'xyz' f true n true + p false EOO $* disfigure } -- cgit v1.1