diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2021-07-23 10:49:37 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2021-07-23 10:49:37 +0200 |
commit | 7292c24ba3e4c0016e40466239437fe5819c47de (patch) | |
tree | fa066913ee9d5a8aea7d26bfd02ffa691dd4e1a4 | |
parent | 4c628d939413bb722a6819d8e3798d79051619ce (diff) |
Reserve variable names/components that start with underscore to build2 core
-rw-r--r-- | libbuild2/parser.cxx | 30 | ||||
-rw-r--r-- | libbuild2/variable.cxx | 6 | ||||
-rw-r--r-- | libbuild2/variable.hxx | 23 |
3 files changed, 44 insertions, 15 deletions
diff --git a/libbuild2/parser.cxx b/libbuild2/parser.cxx index d0661a9..a6f6ae6 100644 --- a/libbuild2/parser.cxx +++ b/libbuild2/parser.cxx @@ -4216,8 +4216,34 @@ namespace build2 // Note that the overridability can still be restricted (e.g., by a module // that enters this variable or by a pattern). // - return scope_->var_pool ().insert ( - move (ns[0].value), true /* overridable */); + bool ovr (true); + auto r (scope_->var_pool ().insert ( + move (ns[0].value), nullptr, nullptr, &ovr)); + + if (!r.second) + return r.first; + + // If it's newly entered, verify it's not reserved for the build2 core. + // We reserve: + // + // - Variable components that start with underscore (_x, x._y). + // + // - Variables in the `build`, `import`, and `export` namespaces. + // + const string& n (r.first.name); + + const char* w ( + n[0] == '_' ? "name starts with underscore" : + n.find ("._") != string::npos ? "component starts with underscore" : + n.compare (0, 6, "build.") == 0 ? "is in 'build' namespace" : + n.compare (0, 7, "import.") == 0 ? "is in 'import' namespace" : + n.compare (0, 7, "export.") == 0 ? "is in 'export' namespace" : nullptr); + + if (w != nullptr) + fail (l) << "variable name '" << n << "' is reserved" << + info << "variable " << w; + + return r.first; } void parser:: diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx index 1855f3e..d84945f 100644 --- a/libbuild2/variable.cxx +++ b/libbuild2/variable.cxx @@ -1553,7 +1553,7 @@ namespace build2 } } - variable& variable_pool:: + pair<variable&, bool> variable_pool:: insert (string n, const build2::value_type* t, const variable_visibility* v, @@ -1619,7 +1619,7 @@ namespace build2 update (var, pt, pv, po); // Not changing the key. } - return var; + return pair<variable&, bool> (var, r.second); } const variable& variable_pool:: @@ -1631,7 +1631,7 @@ namespace build2 var.type, &var.visibility, nullptr /* override */, - false /* pattern */)); + false /* pattern */).first); assert (a.overrides == nullptr); diff --git a/libbuild2/variable.hxx b/libbuild2/variable.hxx index 53e393d..c0f0fd9 100644 --- a/libbuild2/variable.hxx +++ b/libbuild2/variable.hxx @@ -1222,25 +1222,25 @@ namespace build2 const variable& insert (string name) { - return insert (move (name), nullptr, nullptr, nullptr); + return insert (move (name), nullptr, nullptr, nullptr).first; } const variable& insert (string name, variable_visibility v) { - return insert (move (name), nullptr, &v, nullptr); + return insert (move (name), nullptr, &v, nullptr).first; } const variable& insert (string name, bool overridable) { - return insert (move (name), nullptr, nullptr, &overridable); + return insert (move (name), nullptr, nullptr, &overridable).first; } const variable& insert (string name, bool overridable, variable_visibility v) { - return insert (move (name), nullptr, &v, &overridable); + return insert (move (name), nullptr, &v, &overridable). first; } template <typename T> @@ -1248,14 +1248,15 @@ namespace build2 insert (string name) { return insert ( - move (name), &value_traits<T>::value_type, nullptr, nullptr); + move (name), &value_traits<T>::value_type, nullptr, nullptr).first; } template <typename T> const variable& insert (string name, variable_visibility v) { - return insert (move (name), &value_traits<T>::value_type, &v, nullptr); + return insert ( + move (name), &value_traits<T>::value_type, &v, nullptr).first; } template <typename T> @@ -1263,7 +1264,7 @@ namespace build2 insert (string name, bool overridable) { return insert ( - move (name), &value_traits<T>::value_type, nullptr, &overridable); + move (name), &value_traits<T>::value_type, nullptr, &overridable).first; } template <typename T> @@ -1271,7 +1272,7 @@ namespace build2 insert (string name, bool overridable, variable_visibility v) { return insert ( - move (name), &value_traits<T>::value_type, &v, &overridable); + move (name), &value_traits<T>::value_type, &v, &overridable).first; } const variable& @@ -1280,7 +1281,7 @@ namespace build2 bool overridable, variable_visibility v) { - return insert (move (name), type, &v, &overridable); + return insert (move (name), type, &v, &overridable).first; } // Alias an existing variable with a new name. @@ -1371,10 +1372,12 @@ namespace build2 friend class scope; private: + friend class parser; + // Note that in insert() NULL overridable is interpreted as false unless // overridden by a pattern while in update() NULL overridable is ignored. // - LIBBUILD2_SYMEXPORT variable& + LIBBUILD2_SYMEXPORT pair<variable&, bool> insert (string name, const value_type*, const variable_visibility*, |