Merge pull request #7449 from Icinga/feature/warn-set-global-var

DSL: warn on x=y if x is a global variable
This commit is contained in:
Michael Friedrich 2019-08-27 14:53:29 +02:00 committed by GitHub
commit b75e1585d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 1 deletions

View File

@ -1,7 +1,7 @@
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
if (!globals.contains("NscpPath")) {
NscpPath = dirname(msi_get_component_path("{5C45463A-4AE9-4325-96DB-6E239C034F93}"))
globals.NscpPath = dirname(msi_get_component_path("{5C45463A-4AE9-4325-96DB-6E239C034F93}"))
}
object CheckCommand "nscp-local" {

View File

@ -552,6 +552,58 @@ ExpressionResult GetScopeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *d
VERIFY(!"Invalid scope.");
}
static inline
void WarnOnImplicitlySetGlobalVar(const std::unique_ptr<Expression>& setLhs, const Value& setLhsParent, CombinedSetOp setOp, const DebugInfo& debug)
{
auto var (dynamic_cast<VariableExpression*>(setLhs.get()));
if (var && setLhsParent.IsObject()) {
auto ns (dynamic_pointer_cast<Namespace>(setLhsParent.Get<Object::Ptr>()));
if (ns && ns == ScriptGlobal::GetGlobals()) {
const char *opStr = nullptr;
switch (setOp) {
case OpSetLiteral:
opStr = "=";
break;
case OpSetAdd:
opStr = "+=";
break;
case OpSetSubtract:
opStr = "-=";
break;
case OpSetMultiply:
opStr = "*=";
break;
case OpSetDivide:
opStr = "/=";
break;
case OpSetModulo:
opStr = "%=";
break;
case OpSetXor:
opStr = "^=";
break;
case OpSetBinaryAnd:
opStr = "&=";
break;
case OpSetBinaryOr:
opStr = "|=";
break;
default:
VERIFY(!"Invalid opcode.");
}
auto varName (var->GetVariable());
Log(LogWarning, "config")
<< "Global variable '" << varName << "' has been set implicitly via '" << varName << ' ' << opStr << " ...' " << debug << "."
" Please set it explicitly via 'globals." << varName << ' ' << opStr << " ...' instead.";
}
}
}
ExpressionResult SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const
{
if (frame.Sandboxed)
@ -610,6 +662,8 @@ ExpressionResult SetExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint)
delete psdhint;
}
WarnOnImplicitlySetGlobalVar(m_Operand1, parent, m_Op, m_DebugInfo);
return Empty;
}