From 6a242a8050edd0a33ea0e770a6eca4823a98b8d1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 6 Jan 2022 06:59:31 +0200 Subject: Factor reusable code to target_key::effective_name() --- libbuild2/adhoc-rule-regex-pattern.cxx | 19 ++++----------- libbuild2/target-key.hxx | 6 +++++ libbuild2/target.ixx | 43 ++++++++++++++++++++++++++++++++++ libbuild2/variable.cxx | 35 ++------------------------- 4 files changed, 56 insertions(+), 47 deletions(-) diff --git a/libbuild2/adhoc-rule-regex-pattern.cxx b/libbuild2/adhoc-rule-regex-pattern.cxx index 89a4766..b0de827 100644 --- a/libbuild2/adhoc-rule-regex-pattern.cxx +++ b/libbuild2/adhoc-rule-regex-pattern.cxx @@ -196,26 +196,17 @@ namespace build2 // string& ns (me.buffer); - auto append_name = [&ns, first = true] (const target_key& tk, - const element& e) mutable + auto append_name = [&ns, + first = true, + storage = string ()] (const target_key& tk, + const element& e) mutable { if (!first) ns += '/'; else first = false; - ns += *tk.name; - - // The same semantics as in variable_type_map::find(). - // - if (tk.ext && !tk.ext->empty () && - (e.match_ext || - tk.type->fixed_extension == &target_extension_none || - tk.type->fixed_extension == &target_extension_must)) - { - ns += '.'; - ns += *tk.ext; - } + ns += tk.effective_name (storage, e.match_ext); }; // Primary target (always a pattern). diff --git a/libbuild2/target-key.hxx b/libbuild2/target-key.hxx index cd1ba83..c5690a9 100644 --- a/libbuild2/target-key.hxx +++ b/libbuild2/target-key.hxx @@ -31,6 +31,12 @@ namespace build2 bool is_a () const {return type->is_a ();} bool is_a (const target_type& tt) const {return type->is_a (tt);} + // Return an "effective" name, for example, for pattern matching, that + // includes the extension where appropriate. + // + const string& + effective_name (string& storage, bool force_ext = false) const; + // Append/return the target name or a pair of names if out-qualified. // // See also target::as_name() for the returned name stability guarantees. diff --git a/libbuild2/target.ixx b/libbuild2/target.ixx index 540c718..cfc3847 100644 --- a/libbuild2/target.ixx +++ b/libbuild2/target.ixx @@ -9,6 +9,49 @@ namespace build2 { + // target_key + // + inline const string& target_key:: + effective_name (string& r, bool force_ext) const + { + const target_type& tt (*type); + + // Note that if the name is not empty, then we always use that, even + // if the type is dir/fsdir. + // + if (name->empty () && (tt.is_a () || tt.is_a ())) + { + r = dir->leaf ().string (); + } + // If we have the extension and the type expects the extension to be + // always specified explicitly by the user, then add it to the name. + // + // Overall, we have the following cases: + // + // 1. Extension is fixed: man1{}. + // + // 2. Extension is always specified by the user: file{}. + // + // 3. Default extension that may be overridden by the user: hxx{}. + // + // 4. Extension assigned by the rule but may be overridden by the + // user: obje{}. + // + // By default we only include the extension for (2). + // + else if (ext && !ext->empty () && + (force_ext || + tt.fixed_extension == &target_extension_none || + tt.fixed_extension == &target_extension_must)) + { + r = *name + '.' + *ext; + } + else + return *name; // Use name as is. + + return r; + } + // match_extra // inline void match_extra:: diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx index 6f2812c..8ed9605 100644 --- a/libbuild2/variable.cxx +++ b/libbuild2/variable.cxx @@ -1857,39 +1857,8 @@ namespace build2 { if (!oname) { - const target_type& tt (*tk.type); - - // Note that if the name is not empty, then we always use that, even - // if the type is dir/fsdir. - // - if (tk.name->empty () && (tt.is_a () || tt.is_a ())) - { - oname = tk.dir->leaf ().string (); - } - // If we have the extension and the type expects the extension to be - // always specified explicitly by the user, then add it to the name. - // - // Overall, we have the following cases: - // - // 1. Extension is fixed: man1{}. - // - // 2. Extension is always specified by the user: file{}. - // - // 3. Default extension that may be overridden by the user: hxx{}. - // - // 4. Extension assigned by the rule but may be overridden by the - // user: obje{}. - // - // By default we only match the extension for (2). - // - else if (tk.ext && !tk.ext->empty () && - (tt.fixed_extension == &target_extension_none || - tt.fixed_extension == &target_extension_must)) - { - oname = *tk.name + '.' + *tk.ext; - } - else - oname = string (); // Use tk.name as is. + oname = string (); + tk.effective_name (*oname); } return oname->empty () ? *tk.name : *oname; -- cgit v1.1