From 7292c24ba3e4c0016e40466239437fe5819c47de Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 23 Jul 2021 10:49:37 +0200 Subject: Reserve variable names/components that start with underscore to build2 core --- libbuild2/parser.cxx | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'libbuild2/parser.cxx') 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:: -- cgit v1.1