From: Gunnar Beutner Date: Fri, 15 Dec 2017 04:34:46 +0000 (+0100) Subject: Use std::unique_ptr for Expression objects X-Git-Tag: v2.9.0~282^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ab8386cf5c8832879658a65abb2b8468a74bedc9;p=icinga2 Use std::unique_ptr for Expression objects --- diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index 95df3714b..f5e9f91ca 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -176,20 +176,16 @@ static int Main(void) String initconfig = Application::GetSysconfDir() + "/icinga2/init.conf"; if (Utility::PathExists(initconfig)) { - Expression *expression; + std::unique_ptr expression; try { expression = ConfigCompiler::CompileFile(initconfig); ScriptFrame frame; expression->Evaluate(frame); } catch (const std::exception& ex) { - delete expression; - Log(LogCritical, "config", DiagnosticInformation(ex)); return EXIT_FAILURE; } - - delete expression; } if (!autocomplete) diff --git a/lib/cli/consolecommand.cpp b/lib/cli/consolecommand.cpp index 0ace2fc69..98956e535 100644 --- a/lib/cli/consolecommand.cpp +++ b/lib/cli/consolecommand.cpp @@ -68,7 +68,7 @@ extern "C" void dbg_inspect_object(Object *obj) extern "C" void dbg_eval(const char *text) { - Expression *expr = nullptr; + std::unique_ptr expr; try { ScriptFrame frame; @@ -78,13 +78,11 @@ extern "C" void dbg_eval(const char *text) } catch (const std::exception& ex) { std::cout << "Error: " << DiagnosticInformation(ex) << "\n"; } - - delete expr; } extern "C" void dbg_eval_with_value(const Value& value, const char *text) { - Expression *expr = nullptr; + std::unique_ptr expr; try { ScriptFrame frame; @@ -96,13 +94,11 @@ extern "C" void dbg_eval_with_value(const Value& value, const char *text) } catch (const std::exception& ex) { std::cout << "Error: " << DiagnosticInformation(ex) << "\n"; } - - delete expr; } extern "C" void dbg_eval_with_object(Object *object, const char *text) { - Expression *expr = nullptr; + std::unique_ptr expr; try { ScriptFrame frame; @@ -114,8 +110,6 @@ extern "C" void dbg_eval_with_object(Object *object, const char *text) } catch (const std::exception& ex) { std::cout << "Error: " << DiagnosticInformation(ex) << "\n"; } - - delete expr; } void ConsoleCommand::BreakpointHandler(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di) @@ -400,7 +394,7 @@ incomplete: command += line; - Expression *expr = nullptr; + std::unique_ptr expr; try { lines[fileName] = command; @@ -412,7 +406,7 @@ incomplete: /* This relies on the fact that - for syntax errors - CompileText() * returns an AST where the top-level expression is a 'throw'. */ - if (!syntaxOnly || dynamic_cast(expr)) { + if (!syntaxOnly || dynamic_cast(expr.get())) { if (syntaxOnly) std::cerr << " => " << command << std::endl; result = Serialize(expr->Evaluate(scriptFrame), 0); @@ -502,8 +496,6 @@ incomplete: if (!commandOnce.IsEmpty()) return EXIT_FAILURE; } - - delete expr; } return EXIT_SUCCESS; diff --git a/lib/cli/daemonutility.cpp b/lib/cli/daemonutility.cpp index d9c9ff67d..19d180e5f 100644 --- a/lib/cli/daemonutility.cpp +++ b/lib/cli/daemonutility.cpp @@ -51,9 +51,9 @@ static void IncludeZoneDirRecursive(const String& path, const String& package, b /* register this zone path for cluster config sync */ ConfigCompiler::RegisterZoneDir("_etc", path, zoneName); - std::vector expressions; + std::vector > expressions; Utility::GlobRecursive(path, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile); - DictExpression expr(expressions); + DictExpression expr(std::move(expressions)); if (!ExecuteExpression(&expr)) success = false; } @@ -74,9 +74,9 @@ static void IncludeNonLocalZone(const String& zonePath, const String& package, b return; } - std::vector expressions; + std::vector > expressions; Utility::GlobRecursive(zonePath, "*.conf", std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zoneName, package), GlobFile); - DictExpression expr(expressions); + DictExpression expr(std::move(expressions)); if (!ExecuteExpression(&expr)) success = false; } @@ -88,13 +88,11 @@ static void IncludePackage(const String& packagePath, bool& success) String packageName = Utility::BaseName(packagePath); if (Utility::PathExists(packagePath + "/include.conf")) { - Expression *expr = ConfigCompiler::CompileFile(packagePath + "/include.conf", + std::unique_ptr expr = ConfigCompiler::CompileFile(packagePath + "/include.conf", String(), packageName); - if (!ExecuteExpression(expr)) + if (!ExecuteExpression(&*expr)) success = false; - - delete expr; } } @@ -107,9 +105,8 @@ bool DaemonUtility::ValidateConfigFiles(const std::vector& configs, if (!configs.empty()) { for (const String& configPath : configs) { try { - Expression *expression = ConfigCompiler::CompileFile(configPath, String(), "_etc"); - success = ExecuteExpression(expression); - delete expression; + std::unique_ptr expression = ConfigCompiler::CompileFile(configPath, String(), "_etc"); + success = ExecuteExpression(&*expression); if (!success) return false; } catch (const std::exception& ex) { diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy index af9b2a019..45fed54f1 100644 --- a/lib/config/config_parser.yy +++ b/lib/config/config_parser.yy @@ -68,7 +68,7 @@ using namespace icinga; template static void MakeRBinaryOp(Expression** result, Expression *left, Expression *right, const DebugInfo& diLeft, const DebugInfo& diRight) { - *result = new T(left, right, DebugInfoRange(diLeft, diRight)); + *result = new T(std::unique_ptr(left), std::unique_ptr(right), DebugInfoRange(diLeft, diRight)); } %} @@ -80,7 +80,7 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig %error-verbose %glr-parser -%parse-param { std::vector > *llist } +%parse-param { std::vector, EItemInfo> > *llist } %parse-param { ConfigCompiler *context } %lex-param { void *scanner } @@ -92,12 +92,12 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig icinga::DictExpression *dexpr; CombinedSetOp csop; std::vector *slist; - std::vector > *llist; - std::vector *elist; - std::vector > *ebranchlist; - std::pair *ebranch; - std::pair *cvitem; - std::map *cvlist; + std::vector, EItemInfo> > *llist; + std::vector > *elist; + std::vector, std::unique_ptr > > *ebranchlist; + std::pair, std::unique_ptr > *ebranch; + std::pair > *cvitem; + std::map > *cvlist; icinga::ScopeSpecifier scope; } @@ -238,13 +238,13 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner); extern int yydebug; -void yyerror(const YYLTYPE *locp, std::vector > *, ConfigCompiler *context, const char *err) +void yyerror(const YYLTYPE *locp, std::vector, EItemInfo> > *, ConfigCompiler *context, const char *err) { bool incomplete = context && context->m_Eof && (context->m_OpenBraces > 0); BOOST_THROW_EXCEPTION(ScriptError(err, *locp, incomplete)); } -int yyparse(std::vector > *llist, ConfigCompiler *context); +int yyparse(std::vector, EItemInfo> > *llist, ConfigCompiler *context); static void BeginFlowControlBlock(ConfigCompiler *compiler, int allowedTypes, bool inherit) { @@ -267,9 +267,9 @@ static void UseFlowControl(ConfigCompiler *compiler, FlowControlType type, const BOOST_THROW_EXCEPTION(ScriptError("Invalid flow control statement.", location)); } -Expression *ConfigCompiler::Compile(void) +std::unique_ptr ConfigCompiler::Compile(void) { - std::vector > llist; + std::vector, EItemInfo> > llist; //yydebug = 1; @@ -282,19 +282,19 @@ Expression *ConfigCompiler::Compile(void) EndFlowControlBlock(this); m_IgnoreNewlines.pop(); - std::vector dlist; - std::vector >::size_type num = 0; - for (const auto& litem : llist) { + std::vector > dlist; + decltype(llist.size()) num = 0; + for (auto& litem : llist) { if (!litem.second.SideEffect && num != llist.size() - 1) { yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); } - dlist.push_back(litem.first); + dlist.push_back(std::move(litem.first)); num++; } - DictExpression *expr = new DictExpression(dlist); + std::unique_ptr expr{new DictExpression(std::move(dlist))}; expr->MakeInline(); - return expr; + return std::move(expr); } #define scanner (context->GetScanner()) @@ -318,7 +318,7 @@ statements: newlines lterm_items lterm_items: /* empty */ { - $$ = new std::vector >(); + $$ = new std::vector, EItemInfo> >(); } | lterm_items_inner | lterm_items_inner sep @@ -326,26 +326,26 @@ lterm_items: /* empty */ lterm_items_inner: lterm %dprec 2 { - $$ = new std::vector >(); + $$ = new std::vector, EItemInfo> >(); EItemInfo info = { true, @1 }; - $$->emplace_back($1, info); + $$->emplace_back(std::unique_ptr($1), info); } | rterm_no_side_effect { - $$ = new std::vector >(); + $$ = new std::vector, EItemInfo> >(); EItemInfo info = { false, @1 }; - $$->emplace_back($1, info); + $$->emplace_back(std::unique_ptr($1), info); } | lterm_items_inner sep lterm %dprec 1 { if ($1) $$ = $1; else - $$ = new std::vector >(); + $$ = new std::vector, EItemInfo> >(); if ($3) { EItemInfo info = { true, @3 }; - $$->emplace_back($3, info); + $$->emplace_back(std::unique_ptr($3), info); } } | lterm_items_inner sep rterm_no_side_effect %dprec 1 @@ -353,11 +353,11 @@ lterm_items_inner: lterm %dprec 2 if ($1) $$ = $1; else - $$ = new std::vector >(); + $$ = new std::vector, EItemInfo> >(); if ($3) { EItemInfo info = { false, @3 }; - $$->emplace_back($3, info); + $$->emplace_back(std::unique_ptr($3), info); } } ; @@ -396,26 +396,28 @@ object: bool seen_ignore = context->m_SeenIgnore.top(); context->m_SeenIgnore.pop(); - Expression *ignore = context->m_Ignore.top(); + std::unique_ptr ignore{std::move(context->m_Ignore.top())}; context->m_Ignore.pop(); - Expression *assign = context->m_Assign.top(); + std::unique_ptr assign{std::move(context->m_Assign.top())}; context->m_Assign.pop(); - Expression *filter = NULL; + std::unique_ptr filter; if (seen_assign) { if (ignore) { - Expression *rex = new LogicalNegateExpression(ignore, DebugInfoRange(@2, @5)); + std::unique_ptr rex{new LogicalNegateExpression(std::move(ignore), DebugInfoRange(@2, @5))}; - filter = new LogicalAndExpression(assign, rex, DebugInfoRange(@2, @5)); + filter.reset(new LogicalAndExpression(std::move(assign), std::move(rex), DebugInfoRange(@2, @5))); } else - filter = assign; + filter.swap(assign); } else if (seen_ignore) { BOOST_THROW_EXCEPTION(ScriptError("object rule 'ignore where' cannot be used without 'assign where'", DebugInfoRange(@2, @4))); } - $$ = new ObjectExpression(abstract, $3, $4, filter, context->GetZone(), context->GetPackage(), $5, $6, $7, $9, DebugInfoRange(@2, @7)); + $$ = new ObjectExpression(abstract, std::unique_ptr($3), std::unique_ptr($4), + std::move(filter), context->GetZone(), context->GetPackage(), std::move(*$5), $6, $7, std::unique_ptr($9), DebugInfoRange(@2, @7)); + delete $5; } ; @@ -468,15 +470,15 @@ combined_set_op: T_SET lterm: T_LIBRARY rterm { - $$ = new LibraryExpression($2, @$); + $$ = new LibraryExpression(std::unique_ptr($2), @$); } | rterm combined_set_op rterm { - $$ = new SetExpression($1, $2, $3, @$); + $$ = new SetExpression(std::unique_ptr($1), $2, std::unique_ptr($3), @$); } | T_INCLUDE rterm { - $$ = new IncludeExpression(Utility::DirName(context->GetPath()), $2, NULL, NULL, IncludeRegular, false, context->GetZone(), context->GetPackage(), @$); + $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr($2), NULL, NULL, IncludeRegular, false, context->GetZone(), context->GetPackage(), @$); } | T_INCLUDE T_STRING_ANGLE { @@ -485,23 +487,23 @@ lterm: T_LIBRARY rterm } | T_INCLUDE_RECURSIVE rterm { - $$ = new IncludeExpression(Utility::DirName(context->GetPath()), $2, MakeLiteral("*.conf"), NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$); + $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr($2), MakeLiteral("*.conf"), NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$); } | T_INCLUDE_RECURSIVE rterm ',' rterm { - $$ = new IncludeExpression(Utility::DirName(context->GetPath()), $2, $4, NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$); + $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr($2), std::unique_ptr($4), NULL, IncludeRecursive, false, context->GetZone(), context->GetPackage(), @$); } | T_INCLUDE_ZONES rterm ',' rterm { - $$ = new IncludeExpression(Utility::DirName(context->GetPath()), $4, MakeLiteral("*.conf"), $2, IncludeZones, false, context->GetZone(), context->GetPackage(), @$); + $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr($4), MakeLiteral("*.conf"), std::unique_ptr($2), IncludeZones, false, context->GetZone(), context->GetPackage(), @$); } | T_INCLUDE_ZONES rterm ',' rterm ',' rterm { - $$ = new IncludeExpression(Utility::DirName(context->GetPath()), $4, $6, $2, IncludeZones, false, context->GetZone(), context->GetPackage(), @$); + $$ = new IncludeExpression(Utility::DirName(context->GetPath()), std::unique_ptr($4), std::unique_ptr($6), std::unique_ptr($2), IncludeZones, false, context->GetZone(), context->GetPackage(), @$); } | T_IMPORT rterm { - $$ = new ImportExpression($2, @$); + $$ = new ImportExpression(std::unique_ptr($2), @$); } | T_ASSIGN T_WHERE { @@ -517,11 +519,11 @@ lterm: T_LIBRARY rterm context->m_SeenAssign.top() = true; if (context->m_Assign.top()) - context->m_Assign.top() = new LogicalOrExpression(context->m_Assign.top(), $4, @$); + context->m_Assign.top() = new LogicalOrExpression(std::unique_ptr(context->m_Assign.top()), std::unique_ptr($4), @$); else context->m_Assign.top() = $4; - $$ = MakeLiteral(); + $$ = MakeLiteralRaw(); } | T_ASSIGN T_WHERE rterm %dprec 1 { @@ -533,11 +535,11 @@ lterm: T_LIBRARY rterm context->m_SeenAssign.top() = true; if (context->m_Assign.top()) - context->m_Assign.top() = new LogicalOrExpression(context->m_Assign.top(), $3, @$); + context->m_Assign.top() = new LogicalOrExpression(std::unique_ptr(context->m_Assign.top()), std::unique_ptr($3), @$); else context->m_Assign.top() = $3; - $$ = MakeLiteral(); + $$ = MakeLiteralRaw(); } | T_IGNORE T_WHERE { @@ -553,11 +555,11 @@ lterm: T_LIBRARY rterm context->m_SeenIgnore.top() = true; if (context->m_Ignore.top()) - context->m_Ignore.top() = new LogicalOrExpression(context->m_Ignore.top(), $4, @$); + context->m_Ignore.top() = new LogicalOrExpression(std::unique_ptr(context->m_Ignore.top()), std::unique_ptr($4), @$); else context->m_Ignore.top() = $4; - $$ = MakeLiteral(); + $$ = MakeLiteralRaw(); } | T_IGNORE T_WHERE rterm %dprec 1 { @@ -569,16 +571,16 @@ lterm: T_LIBRARY rterm context->m_SeenIgnore.top() = true; if (context->m_Ignore.top()) - context->m_Ignore.top() = new LogicalOrExpression(context->m_Ignore.top(), $3, @$); + context->m_Ignore.top() = new LogicalOrExpression(std::unique_ptr(context->m_Ignore.top()), std::unique_ptr($3), @$); else context->m_Ignore.top() = $3; - $$ = MakeLiteral(); + $$ = MakeLiteralRaw(); } | T_RETURN optional_rterm { UseFlowControl(context, FlowControlReturn, @$); - $$ = new ReturnExpression($2, @$); + $$ = new ReturnExpression(std::unique_ptr($2), @$); } | T_BREAK { @@ -596,7 +598,7 @@ lterm: T_LIBRARY rterm } | T_USING rterm { - $$ = new UsingExpression($2, @$); + $$ = new UsingExpression(std::unique_ptr($2), @$); } | apply | object @@ -608,7 +610,7 @@ lterm: T_LIBRARY rterm { EndFlowControlBlock(context); - $$ = new ForExpression(*$3, *$5, $7, $10, @$); + $$ = new ForExpression(*$3, *$5, std::unique_ptr($7), std::unique_ptr($10), @$); delete $3; delete $5; } @@ -620,7 +622,7 @@ lterm: T_LIBRARY rterm { EndFlowControlBlock(context); - $$ = new ForExpression(*$3, "", $5, $8, @$); + $$ = new ForExpression(*$3, "", std::unique_ptr($5), std::unique_ptr($8), @$); delete $3; } | T_FUNCTION identifier '(' identifier_items ')' use_specifier @@ -631,28 +633,29 @@ lterm: T_LIBRARY rterm { EndFlowControlBlock(context); - FunctionExpression *fexpr = new FunctionExpression(*$2, *$4, $6, $8, @$); + std::unique_ptr fexpr{new FunctionExpression(*$2, *$4, std::move(*$6), std::unique_ptr($8), @$)}; delete $4; + delete $6; - $$ = new SetExpression(MakeIndexer(ScopeThis, *$2), OpSetLiteral, fexpr, @$); + $$ = new SetExpression(MakeIndexer(ScopeThis, *$2), OpSetLiteral, std::move(fexpr), @$); delete $2; } | T_CONST T_IDENTIFIER T_SET rterm { - $$ = new SetExpression(MakeIndexer(ScopeGlobal, *$2), OpSetLiteral, $4); + $$ = new SetExpression(MakeIndexer(ScopeGlobal, *$2), OpSetLiteral, std::unique_ptr($4)); delete $2; } | T_VAR rterm { - Expression *expr = $2; + std::unique_ptr expr{$2}; BindToScope(expr, ScopeLocal); - $$ = new SetExpression(expr, OpSetLiteral, MakeLiteral(), @$); + $$ = new SetExpression(std::move(expr), OpSetLiteral, MakeLiteral(), @$); } | T_VAR rterm combined_set_op rterm { - Expression *expr = $2; + std::unique_ptr expr{$2}; BindToScope(expr, ScopeLocal); - $$ = new SetExpression(expr, $3, $4, @$); + $$ = new SetExpression(std::move(expr), $3, std::unique_ptr($4), @$); } | T_WHILE '(' rterm ')' { @@ -662,22 +665,22 @@ lterm: T_LIBRARY rterm { EndFlowControlBlock(context); - $$ = new WhileExpression($3, $6, @$); + $$ = new WhileExpression(std::unique_ptr($3), std::unique_ptr($6), @$); } | T_THROW rterm { - $$ = new ThrowExpression($2, false, @$); + $$ = new ThrowExpression(std::unique_ptr($2), false, @$); } | T_TRY rterm_scope T_EXCEPT rterm_scope { - $$ = new TryExceptExpression($2, $4, @$); + $$ = new TryExceptExpression(std::unique_ptr($2), std::unique_ptr($4), @$); } | rterm_side_effect ; rterm_items: /* empty */ { - $$ = new std::vector(); + $$ = new std::vector >(); } | rterm_items_inner | rterm_items_inner ',' @@ -687,13 +690,13 @@ rterm_items: /* empty */ rterm_items_inner: rterm { - $$ = new std::vector(); - $$->push_back($1); + $$ = new std::vector >(); + $$->emplace_back($1); } | rterm_items_inner arraysep rterm { $$ = $1; - $$->push_back($3); + $$->emplace_back($3); } ; @@ -704,7 +707,7 @@ rterm_array: '[' newlines rterm_items ']' { context->m_OpenBraces--; - $$ = new ArrayExpression(*$4, @$); + $$ = new ArrayExpression(std::move(*$4), @$); delete $4; } | '[' @@ -714,7 +717,7 @@ rterm_array: '[' rterm_items ']' { context->m_OpenBraces--; - $$ = new ArrayExpression(*$3, @$); + $$ = new ArrayExpression(std::move(*$3), @$); delete $3; } ; @@ -730,17 +733,14 @@ rterm_dict: '{' EndFlowControlBlock(context); context->m_OpenBraces--; context->m_IgnoreNewlines.pop(); - std::vector dlist; - typedef std::pair EListItem; - int num = 0; - for (const EListItem& litem : *$3) { + std::vector > dlist; + for (auto& litem : *$3) { if (!litem.second.SideEffect) yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); - dlist.push_back(litem.first); - num++; + dlist.push_back(std::move(litem.first)); } delete $3; - $$ = new DictExpression(dlist, @$); + $$ = new DictExpression(std::move(dlist), @$); } ; @@ -753,17 +753,14 @@ rterm_scope_require_side_effect: '{' { context->m_OpenBraces--; context->m_IgnoreNewlines.pop(); - std::vector dlist; - typedef std::pair EListItem; - int num = 0; - for (const EListItem& litem : *$3) { + std::vector > dlist; + for (auto& litem : *$3) { if (!litem.second.SideEffect) yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); - dlist.push_back(litem.first); - num++; + dlist.push_back(std::move(litem.first)); } delete $3; - $$ = new DictExpression(dlist, @$); + $$ = new DictExpression(std::move(dlist), @$); $$->MakeInline(); } ; @@ -777,101 +774,102 @@ rterm_scope: '{' { context->m_OpenBraces--; context->m_IgnoreNewlines.pop(); - std::vector dlist; - typedef std::pair EListItem; - std::vector >::size_type num = 0; - for (const EListItem& litem : *$3) { + std::vector > dlist; + decltype($3->size()) num = 0; + for (auto& litem : *$3) { if (!litem.second.SideEffect && num != $3->size() - 1) yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); - dlist.push_back(litem.first); + dlist.push_back(std::move(litem.first)); num++; } delete $3; - $$ = new DictExpression(dlist, @$); + $$ = new DictExpression(std::move(dlist), @$); $$->MakeInline(); } ; else_if_branch: T_ELSE T_IF '(' rterm ')' rterm_scope { - $$ = new std::pair($4, $6); + $$ = new std::pair, std::unique_ptr >(std::unique_ptr($4), std::unique_ptr($6)); } ; else_if_branches: /* empty */ { - $$ = new std::vector >(); + $$ = new std::vector, std::unique_ptr > >(); } | else_if_branches else_if_branch { $$ = $1; - $$->push_back(*$2); + $$->push_back(std::move(*$2)); delete $2; } ; rterm_side_effect: rterm '(' rterm_items ')' { - $$ = new FunctionCallExpression($1, *$3, @$); + $$ = new FunctionCallExpression(std::unique_ptr($1), std::move(*$3), @$); delete $3; } | T_IF '(' rterm ')' rterm_scope else_if_branches { - std::vector > ebranches = *$6; + std::vector, std::unique_ptr > > ebranches; + $6->swap(ebranches); delete $6; - Expression *afalse = NULL; + std::unique_ptr afalse; for (int i = ebranches.size() - 1; i >= 0; i--) { - const std::pair& ebranch = ebranches[i]; - afalse = new ConditionalExpression(ebranch.first, ebranch.second, afalse, @6); + auto& ebranch = ebranches[i]; + afalse.reset(new ConditionalExpression(std::move(ebranch.first), std::move(ebranch.second), std::move(afalse), @6)); } - $$ = new ConditionalExpression($3, $5, afalse, @$); + $$ = new ConditionalExpression(std::unique_ptr($3), std::unique_ptr($5), std::move(afalse), @$); } | T_IF '(' rterm ')' rterm_scope else_if_branches T_ELSE rterm_scope { - std::vector > ebranches = *$6; + std::vector, std::unique_ptr > > ebranches; + $6->swap(ebranches); delete $6; $8->MakeInline(); - Expression *afalse = $8; + std::unique_ptr afalse{$8}; for (int i = ebranches.size() - 1; i >= 0; i--) { - const std::pair& ebranch = ebranches[i]; - afalse = new ConditionalExpression(ebranch.first, ebranch.second, afalse, @6); + auto& ebranch = ebranches[i]; + afalse.reset(new ConditionalExpression(std::move(ebranch.first), std::move(ebranch.second), std::move(afalse), @6)); } - $$ = new ConditionalExpression($3, $5, afalse, @$); + $$ = new ConditionalExpression(std::unique_ptr($3), std::unique_ptr($5), std::move(afalse), @$); } ; rterm_no_side_effect_no_dict: T_STRING { - $$ = MakeLiteral(*$1); + $$ = MakeLiteralRaw(*$1); delete $1; } | T_NUMBER { - $$ = MakeLiteral($1); + $$ = MakeLiteralRaw($1); } | T_BOOLEAN { - $$ = MakeLiteral($1); + $$ = MakeLiteralRaw($1); } | T_NULL { - $$ = MakeLiteral(); + $$ = MakeLiteralRaw(); } | rterm '.' T_IDENTIFIER %dprec 2 { - $$ = new IndexerExpression($1, MakeLiteral(*$3), @$); + $$ = new IndexerExpression(std::unique_ptr($1), MakeLiteral(*$3), @$); delete $3; } | rterm '[' rterm ']' { - $$ = new IndexerExpression($1, $3, @$); + $$ = new IndexerExpression(std::unique_ptr($1), std::unique_ptr($3), @$); } | T_IDENTIFIER { @@ -880,11 +878,11 @@ rterm_no_side_effect_no_dict: T_STRING } | '!' rterm { - $$ = new LogicalNegateExpression($2, @$); + $$ = new LogicalNegateExpression(std::unique_ptr($2), @$); } | '~' rterm { - $$ = new NegateExpression($2, @$); + $$ = new NegateExpression(std::unique_ptr($2), @$); } | T_PLUS rterm %prec UNARY_PLUS { @@ -892,7 +890,7 @@ rterm_no_side_effect_no_dict: T_STRING } | T_MINUS rterm %prec UNARY_MINUS { - $$ = new SubtractExpression(MakeLiteral(0), $2, @$); + $$ = new SubtractExpression(MakeLiteral(0), std::unique_ptr($2), @$); } | T_THIS { @@ -908,11 +906,11 @@ rterm_no_side_effect_no_dict: T_STRING } | T_CURRENT_FILENAME { - $$ = MakeLiteral(@$.Path); + $$ = MakeLiteralRaw(@$.Path); } | T_CURRENT_LINE { - $$ = MakeLiteral(@$.FirstLine); + $$ = MakeLiteralRaw(@$.FirstLine); } | identifier T_FOLLOWS { @@ -926,7 +924,7 @@ rterm_no_side_effect_no_dict: T_STRING args.push_back(*$1); delete $1; - $$ = new FunctionExpression("", args, new std::map(), $4, @$); + $$ = new FunctionExpression("", args, {}, std::unique_ptr($4), @$); } | identifier T_FOLLOWS rterm %dprec 1 { @@ -936,7 +934,7 @@ rterm_no_side_effect_no_dict: T_STRING args.push_back(*$1); delete $1; - $$ = new FunctionExpression("", args, new std::map(), $3, @$); + $$ = new FunctionExpression("", args, {}, std::unique_ptr($3), @$); } | '(' identifier_items ')' T_FOLLOWS { @@ -946,14 +944,14 @@ rterm_no_side_effect_no_dict: T_STRING { EndFlowControlBlock(context); - $$ = new FunctionExpression("", *$2, new std::map(), $6, @$); + $$ = new FunctionExpression("", *$2, {}, std::unique_ptr($6), @$); delete $2; } | '(' identifier_items ')' T_FOLLOWS rterm %dprec 1 { ASSERT(!dynamic_cast($5)); - $$ = new FunctionExpression("", *$2, new std::map(), $5, @$); + $$ = new FunctionExpression("", *$2, {}, std::unique_ptr($5), @$); delete $2; } | rterm_array @@ -994,8 +992,9 @@ rterm_no_side_effect_no_dict: T_STRING { EndFlowControlBlock(context); - $$ = new FunctionExpression("", *$3, $5, $7, @$); + $$ = new FunctionExpression("", *$3, std::move(*$5), std::unique_ptr($7), @$); delete $3; + delete $5; } | T_NULLARY_LAMBDA_BEGIN { @@ -1005,20 +1004,19 @@ rterm_no_side_effect_no_dict: T_STRING { EndFlowControlBlock(context); - std::vector dlist; - typedef std::pair EListItem; - std::vector >::size_type num = 0; - for (const EListItem& litem : *$3) { + std::vector > dlist; + decltype(dlist.size()) num = 0; + for (auto& litem : *$3) { if (!litem.second.SideEffect && num != $3->size() - 1) yyerror(&litem.second.DebugInfo, NULL, NULL, "Value computed is not used."); - dlist.push_back(litem.first); + dlist.push_back(std::move(litem.first)); num++; } delete $3; - DictExpression *aexpr = new DictExpression(dlist, @$); + std::unique_ptr aexpr{new DictExpression(std::move(dlist), @$)}; aexpr->MakeInline(); - $$ = new FunctionExpression("", std::vector(), NULL, aexpr, @$); + $$ = new FunctionExpression("", {}, {}, std::move(aexpr), @$); } ; @@ -1026,9 +1024,9 @@ rterm_no_side_effect: rterm_no_side_effect_no_dict %dprec 1 | rterm_dict %dprec 2 { - Expression *expr = $1; + std::unique_ptr expr{$1}; BindToScope(expr, ScopeThis); - $$ = expr; + $$ = expr.release(); } ; @@ -1069,7 +1067,7 @@ ignore_specifier: /* empty */ use_specifier: /* empty */ { - $$ = NULL; + $$ = new std::map >(); } | T_USE '(' use_specifier_items ')' { @@ -1079,26 +1077,26 @@ use_specifier: /* empty */ use_specifier_items: use_specifier_item { - $$ = new std::map(); - $$->insert(*$1); + $$ = new std::map >(); + $$->insert(std::move(*$1)); delete $1; } | use_specifier_items ',' use_specifier_item { $$ = $1; - $$->insert(*$3); + $$->insert(std::move(*$3)); delete $3; } ; use_specifier_item: identifier { - $$ = new std::pair(*$1, new VariableExpression(*$1, @1)); + $$ = new std::pair >(*$1, std::unique_ptr(new VariableExpression(*$1, @1))); delete $1; } | identifier T_SET rterm { - $$ = new std::pair(*$1, $3); + $$ = new std::pair >(*$1, std::unique_ptr($3)); delete $1; } ; @@ -1127,7 +1125,7 @@ apply_for_specifier: /* empty */ optional_rterm: /* empty */ { - $$ = MakeLiteral(); + $$ = MakeLiteralRaw(); } | rterm ; @@ -1189,26 +1187,26 @@ apply: if (!seen_assign && !context->m_FTerm.top()) BOOST_THROW_EXCEPTION(ScriptError("'apply' is missing 'assign'/'for'", DebugInfoRange(@2, @3))); - Expression *ignore = context->m_Ignore.top(); + std::unique_ptr ignore{context->m_Ignore.top()}; context->m_Ignore.pop(); - Expression *assign; + std::unique_ptr assign; if (!seen_assign) assign = MakeLiteral(true); else - assign = context->m_Assign.top(); + assign.reset(context->m_Assign.top()); context->m_Assign.pop(); - Expression *filter; + std::unique_ptr filter; if (ignore) { - Expression *rex = new LogicalNegateExpression(ignore, DebugInfoRange(@2, @5)); + std::unique_ptrrex{new LogicalNegateExpression(std::move(ignore), DebugInfoRange(@2, @5))}; - filter = new LogicalAndExpression(assign, rex, DebugInfoRange(@2, @5)); + filter.reset(new LogicalAndExpression(std::move(assign), std::move(rex), DebugInfoRange(@2, @5))); } else - filter = assign; + filter.swap(assign); String fkvar = context->m_FKVar.top(); context->m_FKVar.pop(); @@ -1216,10 +1214,11 @@ apply: String fvvar = context->m_FVVar.top(); context->m_FVVar.pop(); - Expression *fterm = context->m_FTerm.top(); + std::unique_ptr fterm{context->m_FTerm.top()}; context->m_FTerm.pop(); - $$ = new ApplyExpression(type, target, $4, filter, context->GetPackage(), fkvar, fvvar, fterm, $7, $8, $10, DebugInfoRange(@2, @8)); + $$ = new ApplyExpression(type, target, std::unique_ptr($4), std::move(filter), context->GetPackage(), fkvar, fvvar, std::move(fterm), std::move(*$7), $8, std::unique_ptr($10), DebugInfoRange(@2, @8)); + delete $7; } ; diff --git a/lib/config/configcompiler.cpp b/lib/config/configcompiler.cpp index dbb6c0c01..9b76046e3 100644 --- a/lib/config/configcompiler.cpp +++ b/lib/config/configcompiler.cpp @@ -109,12 +109,11 @@ String ConfigCompiler::GetPackage(void) const return m_Package; } -void ConfigCompiler::CollectIncludes(std::vector& expressions, +void ConfigCompiler::CollectIncludes(std::vector >& expressions, const String& file, const String& zone, const String& package) { try { - Expression *expr = CompileFile(file, zone, package); - expressions.push_back(expr); + expressions.emplace_back(CompileFile(file, zone, package)); } catch (const std::exception& ex) { Log(LogWarning, "ConfigCompiler") << "Cannot compile file '" @@ -130,7 +129,7 @@ void ConfigCompiler::CollectIncludes(std::vector& expressions, * @param search Whether to search global include dirs. * @param debuginfo Debug information. */ -Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const String& path, +std::unique_ptr ConfigCompiler::HandleInclude(const String& relativeBase, const String& path, bool search, const String& zone, const String& package, const DebugInfo& debuginfo) { String upath; @@ -153,7 +152,7 @@ Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const Stri } } - std::vector expressions; + std::vector > expressions; if (!Utility::Glob(includePath, std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zone, package), GlobFile) && includePath.FindFirstOf("*?") == String::NPos) { std::ostringstream msgbuf; @@ -161,9 +160,9 @@ Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const Stri BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), debuginfo)); } - DictExpression *expr = new DictExpression(expressions); + std::unique_ptr expr{new DictExpression(std::move(expressions))}; expr->MakeInline(); - return expr; + return std::move(expr); } /** @@ -174,7 +173,7 @@ Expression *ConfigCompiler::HandleInclude(const String& relativeBase, const Stri * @param pattern The file pattern. * @param debuginfo Debug information. */ -Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path, +std::unique_ptr ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, const String& path, const String& pattern, const String& zone, const String& package, const DebugInfo&) { String ppath; @@ -184,15 +183,15 @@ Expression *ConfigCompiler::HandleIncludeRecursive(const String& relativeBase, c else ppath = relativeBase + "/" + path; - std::vector expressions; + std::vector > expressions; Utility::GlobRecursive(ppath, pattern, std::bind(&ConfigCompiler::CollectIncludes, std::ref(expressions), _1, zone, package), GlobFile); - DictExpression *dict = new DictExpression(expressions); + std::unique_ptr dict{new DictExpression(std::move(expressions))}; dict->MakeInline(); - return dict; + return std::move(dict); } -void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector& expressions) +void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector >& expressions) { String zoneName = Utility::BaseName(path); @@ -217,7 +216,7 @@ void ConfigCompiler::HandleIncludeZone(const String& relativeBase, const String& * @param pattern The file pattern. * @param debuginfo Debug information. */ -Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag, +std::unique_ptr ConfigCompiler::HandleIncludeZones(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, const DebugInfo&) { String ppath; @@ -230,9 +229,9 @@ Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const newRelativeBase = "."; } - std::vector expressions; + std::vector > expressions; Utility::Glob(ppath + "/*", std::bind(&ConfigCompiler::HandleIncludeZone, newRelativeBase, tag, _1, pattern, package, std::ref(expressions)), GlobDirectory); - return new DictExpression(expressions); + return std::unique_ptr(new DictExpression(std::move(expressions))); } /** @@ -242,7 +241,7 @@ Expression *ConfigCompiler::HandleIncludeZones(const String& relativeBase, const * @param stream The input stream. * @returns Configuration items. */ -Expression *ConfigCompiler::CompileStream(const String& path, +std::unique_ptr ConfigCompiler::CompileStream(const String& path, std::istream *stream, const String& zone, const String& package) { CONTEXT("Compiling configuration stream with name '" + path + "'"); @@ -254,9 +253,9 @@ Expression *ConfigCompiler::CompileStream(const String& path, try { return ctx.Compile(); } catch (const ScriptError& ex) { - return new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo()); + return std::unique_ptr(new ThrowExpression(MakeLiteral(ex.what()), ex.IsIncompleteExpression(), ex.GetDebugInfo())); } catch (const std::exception& ex) { - return new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false); + return std::unique_ptr(new ThrowExpression(MakeLiteral(DiagnosticInformation(ex)), false)); } } @@ -266,7 +265,7 @@ Expression *ConfigCompiler::CompileStream(const String& path, * @param path The path. * @returns Configuration items. */ -Expression *ConfigCompiler::CompileFile(const String& path, const String& zone, +std::unique_ptr ConfigCompiler::CompileFile(const String& path, const String& zone, const String& package) { CONTEXT("Compiling configuration file '" + path + "'"); @@ -292,7 +291,7 @@ Expression *ConfigCompiler::CompileFile(const String& path, const String& zone, * @param text The text. * @returns Configuration items. */ -Expression *ConfigCompiler::CompileText(const String& path, const String& text, +std::unique_ptr ConfigCompiler::CompileText(const String& path, const String& text, const String& zone, const String& package) { std::stringstream stream(text); diff --git a/lib/config/configcompiler.hpp b/lib/config/configcompiler.hpp index 5820562cc..16052445d 100644 --- a/lib/config/configcompiler.hpp +++ b/lib/config/configcompiler.hpp @@ -90,13 +90,13 @@ public: const String& zone = String(), const String& package = String()); virtual ~ConfigCompiler(void); - Expression *Compile(void); + std::unique_ptr Compile(void); - static Expression *CompileStream(const String& path, std::istream *stream, + static std::unique_ptrCompileStream(const String& path, std::istream *stream, const String& zone = String(), const String& package = String()); - static Expression *CompileFile(const String& path, const String& zone = String(), + static std::unique_ptrCompileFile(const String& path, const String& zone = String(), const String& package = String()); - static Expression *CompileText(const String& path, const String& text, + static std::unique_ptrCompileText(const String& path, const String& text, const String& zone = String(), const String& package = String()); static void AddIncludeSearchDir(const String& dir); @@ -109,14 +109,14 @@ public: void SetPackage(const String& package); String GetPackage(void) const; - static void CollectIncludes(std::vector& expressions, + static void CollectIncludes(std::vector >& expressions, const String& file, const String& zone, const String& package); - static Expression *HandleInclude(const String& relativeBase, const String& path, bool search, + static std::unique_ptr HandleInclude(const String& relativeBase, const String& path, bool search, const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo()); - static Expression *HandleIncludeRecursive(const String& relativeBase, const String& path, + static std::unique_ptr HandleIncludeRecursive(const String& relativeBase, const String& path, const String& pattern, const String& zone, const String& package, const DebugInfo& debuginfo = DebugInfo()); - static Expression *HandleIncludeZones(const String& relativeBase, const String& tag, + static std::unique_ptr HandleIncludeZones(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, const DebugInfo& debuginfo = DebugInfo()); size_t ReadInput(char *buffer, size_t max_bytes); @@ -144,7 +144,7 @@ private: void InitializeScanner(void); void DestroyScanner(void); - static void HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector& expressions); + static void HandleIncludeZone(const String& relativeBase, const String& tag, const String& path, const String& pattern, const String& package, std::vector >& expressions); static bool IsAbsolutePath(const String& path); diff --git a/lib/config/configfragment.hpp b/lib/config/configfragment.hpp index 049f30b36..ee7a122cd 100644 --- a/lib/config/configfragment.hpp +++ b/lib/config/configfragment.hpp @@ -28,7 +28,7 @@ #define REGISTER_CONFIG_FRAGMENT(name, fragment) \ INITIALIZE_ONCE_WITH_PRIORITY([]() { \ - icinga::Expression *expression = icinga::ConfigCompiler::CompileText(name, fragment); \ + std::unique_ptr expression = icinga::ConfigCompiler::CompileText(name, fragment); \ VERIFY(expression); \ try { \ icinga::ScriptFrame frame; \ @@ -37,7 +37,6 @@ std::cerr << icinga::DiagnosticInformation(ex) << std::endl; \ icinga::Application::Exit(1); \ } \ - delete expression; \ }, 5) #endif /* CONFIGFRAGMENT_H */ diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index 9227db3e6..41d0e9f63 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -580,7 +580,7 @@ bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector expression = ConfigCompiler::CompileFile(Application::GetModAttrPath()); if (expression) { try { @@ -590,8 +590,6 @@ bool ConfigItem::ActivateItems(WorkQueue& upq, const std::vector& filter) @@ -110,24 +110,20 @@ ConfigItem::Ptr ConfigItemBuilder::Compile(void) BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo)); } - std::vector exprs; + std::vector > exprs; Array::Ptr templateArray = new Array(); templateArray->Add(m_Name); - exprs.push_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd, - new LiteralExpression(templateArray), m_DebugInfo)); - - DictExpression *dexpr = new DictExpression(m_Expressions, m_DebugInfo); - dexpr->MakeInline(); - exprs.push_back(dexpr); + exprs.emplace_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd, + std::unique_ptr(new LiteralExpression(templateArray)), m_DebugInfo)); #ifdef I2_DEBUG if (!m_Abstract) { bool foundDefaultImport = false; - for (Expression *expr : m_Expressions) { - if (dynamic_cast(expr)) { + for (const std::unique_ptr& expr : m_Expressions) { + if (dynamic_cast(expr.get())) { foundDefaultImport = true; break; } @@ -137,7 +133,11 @@ ConfigItem::Ptr ConfigItemBuilder::Compile(void) } #endif /* I2_DEBUG */ - std::shared_ptr exprl = std::make_shared(exprs, m_DebugInfo); + DictExpression *dexpr = new DictExpression(std::move(m_Expressions), m_DebugInfo); + dexpr->MakeInline(); + exprs.emplace_back(dexpr); + + std::shared_ptr exprl = std::make_shared(std::move(exprs), m_DebugInfo); exprl->MakeInline(); return new ConfigItem(m_Type, m_Name, m_Abstract, exprl, m_Filter, diff --git a/lib/config/configitembuilder.hpp b/lib/config/configitembuilder.hpp index c804ca377..d670f2453 100644 --- a/lib/config/configitembuilder.hpp +++ b/lib/config/configitembuilder.hpp @@ -60,7 +60,7 @@ private: Type::Ptr m_Type; /**< The object type. */ String m_Name; /**< The name. */ bool m_Abstract; /**< Whether the item is abstract. */ - std::vector m_Expressions; /**< Expressions for this item. */ + std::vector > m_Expressions; /**< Expressions for this item. */ std::shared_ptr m_Filter; /**< Filter expression. */ DebugInfo m_DebugInfo; /**< Debug information. */ Dictionary::Ptr m_Scope; /**< variable scope. */ diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp index af3eb883d..6e9ef816a 100644 --- a/lib/config/expression.cpp +++ b/lib/config/expression.cpp @@ -90,10 +90,10 @@ const DebugInfo& Expression::GetDebugInfo(void) const return debugInfo; } -Expression *icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index) +std::unique_ptr icinga::MakeIndexer(ScopeSpecifier scopeSpec, const String& index) { - Expression *scope = new GetScopeExpression(scopeSpec); - return new IndexerExpression(scope, MakeLiteral(index)); + std::unique_ptr scope{new GetScopeExpression(scopeSpec)}; + return std::unique_ptr(new IndexerExpression(std::move(scope), MakeLiteral(index))); } void DictExpression::MakeInline(void) @@ -429,7 +429,7 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin if (vfunc.IsObjectType()) { std::vector arguments; arguments.reserve(m_Args.size()); - for (Expression *arg : m_Args) { + for (const auto& arg : m_Args) { ExpressionResult argres = arg->Evaluate(frame); CHECK_RESULT(argres); @@ -449,7 +449,7 @@ ExpressionResult FunctionCallExpression::DoEvaluate(ScriptFrame& frame, DebugHin std::vector arguments; arguments.reserve(m_Args.size()); - for (Expression *arg : m_Args) { + for (const auto& arg : m_Args) { ExpressionResult argres = arg->Evaluate(frame); CHECK_RESULT(argres); @@ -464,7 +464,7 @@ ExpressionResult ArrayExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhin Array::Ptr result = new Array(); result->Reserve(m_Expressions.size()); - for (Expression *aexpr : m_Expressions) { + for (const auto& aexpr : m_Expressions) { ExpressionResult element = aexpr->Evaluate(frame); CHECK_RESULT(element); @@ -486,7 +486,7 @@ ExpressionResult DictExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dhint Value result; try { - for (Expression *aexpr : m_Expressions) { + for (const auto& aexpr : m_Expressions) { ExpressionResult element = aexpr->Evaluate(frame, m_Inline ? dhint : nullptr); CHECK_RESULT(element); result = element.GetValue(); @@ -683,18 +683,18 @@ bool IndexerExpression::GetReference(ScriptFrame& frame, bool init_dict, Value * return true; } -void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec) +void icinga::BindToScope(std::unique_ptr& expr, ScopeSpecifier scopeSpec) { - DictExpression *dexpr = dynamic_cast(expr); + DictExpression *dexpr = dynamic_cast(expr.get()); if (dexpr) { - for (Expression *& expr : dexpr->m_Expressions) + for (auto& expr : dexpr->m_Expressions) BindToScope(expr, scopeSpec); return; } - SetExpression *aexpr = dynamic_cast(expr); + SetExpression *aexpr = dynamic_cast(expr.get()); if (aexpr) { BindToScope(aexpr->m_Operand1, scopeSpec); @@ -702,27 +702,25 @@ void icinga::BindToScope(Expression *& expr, ScopeSpecifier scopeSpec) return; } - IndexerExpression *iexpr = dynamic_cast(expr); + IndexerExpression *iexpr = dynamic_cast(expr.get()); if (iexpr) { BindToScope(iexpr->m_Operand1, scopeSpec); return; } - LiteralExpression *lexpr = dynamic_cast(expr); + LiteralExpression *lexpr = dynamic_cast(expr.get()); if (lexpr && lexpr->GetValue().IsString()) { - Expression *scope = new GetScopeExpression(scopeSpec); - expr = new IndexerExpression(scope, lexpr, lexpr->GetDebugInfo()); + std::unique_ptr scope{new GetScopeExpression(scopeSpec)}; + expr.reset(new IndexerExpression(std::move(scope), std::move(expr), lexpr->GetDebugInfo())); } - VariableExpression *vexpr = dynamic_cast(expr); + VariableExpression *vexpr = dynamic_cast(expr.get()); if (vexpr) { - Expression *scope = new GetScopeExpression(scopeSpec); - Expression *new_expr = new IndexerExpression(scope, MakeLiteral(vexpr->GetVariable()), vexpr->GetDebugInfo()); - delete expr; - expr = new_expr; + std::unique_ptr scope{new GetScopeExpression(scopeSpec)}; + expr.reset(new IndexerExpression(std::move(scope), MakeLiteral(vexpr->GetVariable()), vexpr->GetDebugInfo())); } } @@ -852,7 +850,7 @@ ExpressionResult IncludeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dh if (frame.Sandboxed) BOOST_THROW_EXCEPTION(ScriptError("Includes are not allowed in sandbox mode.", m_DebugInfo)); - Expression *expr; + std::unique_ptr expr; String name, path, pattern; switch (m_Type) { @@ -910,12 +908,9 @@ ExpressionResult IncludeExpression::DoEvaluate(ScriptFrame& frame, DebugHint *dh try { res = expr->Evaluate(frame, dhint); } catch (const std::exception&) { - delete expr; throw; } - delete expr; - return res; } diff --git a/lib/config/expression.hpp b/lib/config/expression.hpp index e8046448f..bd6639ba8 100644 --- a/lib/config/expression.hpp +++ b/lib/config/expression.hpp @@ -211,7 +211,7 @@ public: static void ScriptBreakpoint(ScriptFrame& frame, ScriptError *ex, const DebugInfo& di); }; -I2_CONFIG_API Expression *MakeIndexer(ScopeSpecifier scopeSpec, const String& index); +I2_CONFIG_API std::unique_ptr MakeIndexer(ScopeSpecifier scopeSpec, const String& index); class I2_CONFIG_API OwnedExpression : public Expression { @@ -252,11 +252,16 @@ private: Value m_Value; }; -inline LiteralExpression *MakeLiteral(const Value& literal = Value()) +inline LiteralExpression *MakeLiteralRaw(const Value& literal = Value()) { return new LiteralExpression(literal); } +inline std::unique_ptr MakeLiteral(const Value& literal = Value()) +{ + return std::unique_ptr(MakeLiteralRaw(literal)); +} + class I2_CONFIG_API DebuggableExpression : public Expression { public: @@ -273,35 +278,24 @@ protected: class I2_CONFIG_API UnaryExpression : public DebuggableExpression { public: - UnaryExpression(Expression *operand, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Operand(operand) + UnaryExpression(std::unique_ptr operand, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Operand(std::move(operand)) { } - ~UnaryExpression(void) - { - delete m_Operand; - } - protected: - Expression *m_Operand; + std::unique_ptr m_Operand; }; class I2_CONFIG_API BinaryExpression : public DebuggableExpression { public: - BinaryExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Operand1(operand1), m_Operand2(operand2) + BinaryExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Operand1(std::move(operand1)), m_Operand2(std::move(operand2)) { } - ~BinaryExpression(void) - { - delete m_Operand1; - delete m_Operand2; - } - protected: - Expression *m_Operand1; - Expression *m_Operand2; + std::unique_ptr m_Operand1; + std::unique_ptr m_Operand2; }; class I2_CONFIG_API VariableExpression : public DebuggableExpression @@ -323,14 +317,14 @@ protected: private: String m_Variable; - friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); + friend I2_CONFIG_API void BindToScope(std::unique_ptr& expr, ScopeSpecifier scopeSpec); }; class I2_CONFIG_API NegateExpression : public UnaryExpression { public: - NegateExpression(Expression *operand, const DebugInfo& debugInfo = DebugInfo()) - : UnaryExpression(operand, debugInfo) + NegateExpression(std::unique_ptr operand, const DebugInfo& debugInfo = DebugInfo()) + : UnaryExpression(std::move(operand), debugInfo) { } protected: @@ -340,8 +334,8 @@ protected: class I2_CONFIG_API LogicalNegateExpression : public UnaryExpression { public: - LogicalNegateExpression(Expression *operand, const DebugInfo& debugInfo = DebugInfo()) - : UnaryExpression(operand, debugInfo) + LogicalNegateExpression(std::unique_ptr operand, const DebugInfo& debugInfo = DebugInfo()) + : UnaryExpression(std::move(operand), debugInfo) { } protected: @@ -351,8 +345,8 @@ protected: class I2_CONFIG_API AddExpression : public BinaryExpression { public: - AddExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + AddExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -362,8 +356,8 @@ protected: class I2_CONFIG_API SubtractExpression : public BinaryExpression { public: - SubtractExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + SubtractExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -373,8 +367,8 @@ protected: class I2_CONFIG_API MultiplyExpression : public BinaryExpression { public: - MultiplyExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + MultiplyExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -384,8 +378,8 @@ protected: class I2_CONFIG_API DivideExpression : public BinaryExpression { public: - DivideExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + DivideExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -395,8 +389,8 @@ protected: class I2_CONFIG_API ModuloExpression : public BinaryExpression { public: - ModuloExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + ModuloExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -406,8 +400,8 @@ protected: class I2_CONFIG_API XorExpression : public BinaryExpression { public: - XorExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + XorExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -417,8 +411,8 @@ protected: class I2_CONFIG_API BinaryAndExpression : public BinaryExpression { public: - BinaryAndExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + BinaryAndExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -428,8 +422,8 @@ protected: class I2_CONFIG_API BinaryOrExpression : public BinaryExpression { public: - BinaryOrExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + BinaryOrExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -439,8 +433,8 @@ protected: class I2_CONFIG_API ShiftLeftExpression : public BinaryExpression { public: - ShiftLeftExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + ShiftLeftExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -450,8 +444,8 @@ protected: class I2_CONFIG_API ShiftRightExpression : public BinaryExpression { public: - ShiftRightExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + ShiftRightExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -461,8 +455,8 @@ protected: class I2_CONFIG_API EqualExpression : public BinaryExpression { public: - EqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + EqualExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -472,8 +466,8 @@ protected: class I2_CONFIG_API NotEqualExpression : public BinaryExpression { public: - NotEqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + NotEqualExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -483,8 +477,8 @@ protected: class I2_CONFIG_API LessThanExpression : public BinaryExpression { public: - LessThanExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + LessThanExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -494,8 +488,8 @@ protected: class I2_CONFIG_API GreaterThanExpression : public BinaryExpression { public: - GreaterThanExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + GreaterThanExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -505,8 +499,8 @@ protected: class I2_CONFIG_API LessThanOrEqualExpression : public BinaryExpression { public: - LessThanOrEqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + LessThanOrEqualExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -516,8 +510,8 @@ protected: class I2_CONFIG_API GreaterThanOrEqualExpression : public BinaryExpression { public: - GreaterThanOrEqualExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + GreaterThanOrEqualExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -527,8 +521,8 @@ protected: class I2_CONFIG_API InExpression : public BinaryExpression { public: - InExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + InExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -538,8 +532,8 @@ protected: class I2_CONFIG_API NotInExpression : public BinaryExpression { public: - NotInExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + NotInExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -549,8 +543,8 @@ protected: class I2_CONFIG_API LogicalAndExpression : public BinaryExpression { public: - LogicalAndExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + LogicalAndExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -560,8 +554,8 @@ protected: class I2_CONFIG_API LogicalOrExpression : public BinaryExpression { public: - LogicalOrExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + LogicalOrExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: @@ -571,76 +565,56 @@ protected: class I2_CONFIG_API FunctionCallExpression : public DebuggableExpression { public: - FunctionCallExpression(Expression *fname, const std::vector& args, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_FName(fname), m_Args(args) + FunctionCallExpression(std::unique_ptr fname, std::vector >&& args, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_FName(std::move(fname)), m_Args(std::move(args)) { } - ~FunctionCallExpression(void) - { - delete m_FName; - - for (Expression *expr : m_Args) - delete expr; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; public: - Expression *m_FName; - std::vector m_Args; + std::unique_ptr m_FName; + std::vector > m_Args; }; class I2_CONFIG_API ArrayExpression : public DebuggableExpression { public: - ArrayExpression(const std::vector& expressions, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Expressions(expressions) + ArrayExpression(std::vector >&& expressions, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Expressions(std::move(expressions)) { } - ~ArrayExpression(void) - { - for (Expression *expr : m_Expressions) - delete expr; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - std::vector m_Expressions; + std::vector > m_Expressions; }; class I2_CONFIG_API DictExpression : public DebuggableExpression { public: - DictExpression(const std::vector& expressions = std::vector(), const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Expressions(expressions), m_Inline(false) + DictExpression(std::vector >&& expressions = {}, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Expressions(std::move(expressions)), m_Inline(false) { } - ~DictExpression(void) - { - for (Expression *expr : m_Expressions) - delete expr; - } - void MakeInline(void); protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - std::vector m_Expressions; + std::vector > m_Expressions; bool m_Inline; - friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); + friend I2_CONFIG_API void BindToScope(std::unique_ptr& expr, ScopeSpecifier scopeSpec); }; class I2_CONFIG_API SetExpression : public BinaryExpression { public: - SetExpression(Expression *operand1, CombinedSetOp op, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo), m_Op(op) + SetExpression(std::unique_ptr operand1, CombinedSetOp op, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo), m_Op(op) { } protected: @@ -649,59 +623,46 @@ protected: private: CombinedSetOp m_Op; - friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); + friend I2_CONFIG_API void BindToScope(std::unique_ptr& expr, ScopeSpecifier scopeSpec); }; class I2_CONFIG_API ConditionalExpression : public DebuggableExpression { public: - ConditionalExpression(Expression *condition, Expression *true_branch, Expression *false_branch, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Condition(condition), m_TrueBranch(true_branch), m_FalseBranch(false_branch) + ConditionalExpression(std::unique_ptr condition, std::unique_ptr true_branch, std::unique_ptr false_branch, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Condition(std::move(condition)), m_TrueBranch(std::move(true_branch)), m_FalseBranch(std::move(false_branch)) { } - ~ConditionalExpression(void) - { - delete m_Condition; - delete m_TrueBranch; - delete m_FalseBranch; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - Expression *m_Condition; - Expression *m_TrueBranch; - Expression *m_FalseBranch; + std::unique_ptr m_Condition; + std::unique_ptr m_TrueBranch; + std::unique_ptr m_FalseBranch; }; class I2_CONFIG_API WhileExpression : public DebuggableExpression { public: - WhileExpression(Expression *condition, Expression *loop_body, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Condition(condition), m_LoopBody(loop_body) + WhileExpression(std::unique_ptr condition, std::unique_ptr loop_body, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Condition(std::move(condition)), m_LoopBody(std::move(loop_body)) { } - ~WhileExpression(void) - { - delete m_Condition; - delete m_LoopBody; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - Expression *m_Condition; - Expression *m_LoopBody; + std::unique_ptr m_Condition; + std::unique_ptr m_LoopBody; }; class I2_CONFIG_API ReturnExpression : public UnaryExpression { public: - ReturnExpression(Expression *expression, const DebugInfo& debugInfo = DebugInfo()) - : UnaryExpression(expression, debugInfo) + ReturnExpression(std::unique_ptr expression, const DebugInfo& debugInfo = DebugInfo()) + : UnaryExpression(std::move(expression), debugInfo) { } protected: @@ -747,56 +708,46 @@ private: class I2_CONFIG_API IndexerExpression : public BinaryExpression { public: - IndexerExpression(Expression *operand1, Expression *operand2, const DebugInfo& debugInfo = DebugInfo()) - : BinaryExpression(operand1, operand2, debugInfo) + IndexerExpression(std::unique_ptr operand1, std::unique_ptr operand2, const DebugInfo& debugInfo = DebugInfo()) + : BinaryExpression(std::move(operand1), std::move(operand2), debugInfo) { } protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; virtual bool GetReference(ScriptFrame& frame, bool init_dict, Value *parent, String *index, DebugHint **dhint) const override; - friend I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); + friend I2_CONFIG_API void BindToScope(std::unique_ptr& expr, ScopeSpecifier scopeSpec); }; -I2_CONFIG_API void BindToScope(Expression *& expr, ScopeSpecifier scopeSpec); +I2_CONFIG_API void BindToScope(std::unique_ptr& expr, ScopeSpecifier scopeSpec); class I2_CONFIG_API ThrowExpression : public DebuggableExpression { public: - ThrowExpression(Expression *message, bool incompleteExpr, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Message(message), m_IncompleteExpr(incompleteExpr) + ThrowExpression(std::unique_ptr message, bool incompleteExpr, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Message(std::move(message)), m_IncompleteExpr(incompleteExpr) { } - ~ThrowExpression(void) - { - delete m_Message; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - Expression *m_Message; + std::unique_ptr m_Message; bool m_IncompleteExpr; }; class I2_CONFIG_API ImportExpression : public DebuggableExpression { public: - ImportExpression(Expression *name, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Name(name) + ImportExpression(std::unique_ptr name, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Name(std::move(name)) { } - ~ImportExpression(void) - { - delete m_Name; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - Expression *m_Name; + std::unique_ptr m_Name; }; class I2_CONFIG_API ImportDefaultTemplatesExpression : public DebuggableExpression @@ -814,145 +765,99 @@ class I2_CONFIG_API FunctionExpression : public DebuggableExpression { public: FunctionExpression(const String& name, const std::vector& args, - std::map *closedVars, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Name(name), m_Args(args), m_ClosedVars(closedVars), m_Expression(expression) + std::map >&& closedVars, std::unique_ptr expression, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Name(name), m_Args(args), m_ClosedVars(std::move(closedVars)), m_Expression(std::move(expression)) { } - ~FunctionExpression(void) - { - if (m_ClosedVars) { - typedef std::pair kv_pair; - for (const kv_pair& kv : *m_ClosedVars) { - delete kv.second; - } - } - - delete m_ClosedVars; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: String m_Name; std::vector m_Args; - std::map *m_ClosedVars; + std::map > m_ClosedVars; std::shared_ptr m_Expression; }; class I2_CONFIG_API ApplyExpression : public DebuggableExpression { public: - ApplyExpression(const String& type, const String& target, Expression *name, - Expression *filter, const String& package, const String& fkvar, const String& fvvar, - Expression *fterm, std::map *closedVars, bool ignoreOnError, - Expression *expression, const DebugInfo& debugInfo = DebugInfo()) + ApplyExpression(const String& type, const String& target, std::unique_ptr name, + std::unique_ptr filter, const String& package, const String& fkvar, const String& fvvar, + std::unique_ptr fterm, std::map >&& closedVars, bool ignoreOnError, + std::unique_ptr expression, const DebugInfo& debugInfo = DebugInfo()) : DebuggableExpression(debugInfo), m_Type(type), m_Target(target), - m_Name(name), m_Filter(filter), m_Package(package), m_FKVar(fkvar), m_FVVar(fvvar), - m_FTerm(fterm), m_IgnoreOnError(ignoreOnError), m_ClosedVars(closedVars), - m_Expression(expression) + m_Name(std::move(name)), m_Filter(std::move(filter)), m_Package(package), m_FKVar(fkvar), m_FVVar(fvvar), + m_FTerm(std::move(fterm)), m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)), + m_Expression(std::move(expression)) { } - ~ApplyExpression(void) - { - delete m_Name; - - if (m_ClosedVars) { - typedef std::pair kv_pair; - for (const kv_pair& kv : *m_ClosedVars) { - delete kv.second; - } - } - - delete m_ClosedVars; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: String m_Type; String m_Target; - Expression *m_Name; + std::unique_ptr m_Name; std::shared_ptr m_Filter; String m_Package; String m_FKVar; String m_FVVar; std::shared_ptr m_FTerm; bool m_IgnoreOnError; - std::map *m_ClosedVars; + std::map > m_ClosedVars; std::shared_ptr m_Expression; }; class I2_CONFIG_API ObjectExpression : public DebuggableExpression { public: - ObjectExpression(bool abstract, Expression *type, Expression *name, Expression *filter, - const String& zone, const String& package, std::map *closedVars, - bool defaultTmpl, bool ignoreOnError, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Abstract(abstract), m_Type(type), - m_Name(name), m_Filter(filter), m_Zone(zone), m_Package(package), m_DefaultTmpl(defaultTmpl), - m_IgnoreOnError(ignoreOnError), m_ClosedVars(closedVars), m_Expression(expression) + ObjectExpression(bool abstract, std::unique_ptr type, std::unique_ptr name, std::unique_ptr filter, + const String& zone, const String& package, std::map >&& closedVars, + bool defaultTmpl, bool ignoreOnError, std::unique_ptr expression, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Abstract(abstract), m_Type(std::move(type)), + m_Name(std::move(name)), m_Filter(std::move(filter)), m_Zone(zone), m_Package(package), m_DefaultTmpl(defaultTmpl), + m_IgnoreOnError(ignoreOnError), m_ClosedVars(std::move(closedVars)), m_Expression(std::move(expression)) { } - ~ObjectExpression(void) - { - delete m_Name; - - if (m_ClosedVars) { - typedef std::pair kv_pair; - for (const kv_pair& kv : *m_ClosedVars) { - delete kv.second; - } - } - - delete m_ClosedVars; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: bool m_Abstract; - Expression *m_Type; - Expression *m_Name; + std::unique_ptr m_Type; + std::unique_ptr m_Name; std::shared_ptr m_Filter; String m_Zone; String m_Package; bool m_DefaultTmpl; bool m_IgnoreOnError; - std::map *m_ClosedVars; + std::map > m_ClosedVars; std::shared_ptr m_Expression; }; class I2_CONFIG_API ForExpression : public DebuggableExpression { public: - ForExpression(const String& fkvar, const String& fvvar, Expression *value, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_FKVar(fkvar), m_FVVar(fvvar), m_Value(value), m_Expression(expression) + ForExpression(const String& fkvar, const String& fvvar, std::unique_ptr value, std::unique_ptr expression, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_FKVar(fkvar), m_FVVar(fvvar), m_Value(std::move(value)), m_Expression(std::move(expression)) { } - ~ForExpression(void) - { - delete m_Value; - delete m_Expression; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: String m_FKVar; String m_FVVar; - Expression *m_Value; - Expression *m_Expression; + std::unique_ptr m_Value; + std::unique_ptr m_Expression; }; class I2_CONFIG_API LibraryExpression : public UnaryExpression { public: - LibraryExpression(Expression *expression, const DebugInfo& debugInfo = DebugInfo()) - : UnaryExpression(expression, debugInfo) + LibraryExpression(std::unique_ptr expression, const DebugInfo& debugInfo = DebugInfo()) + : UnaryExpression(std::move(expression), debugInfo) { } protected: @@ -969,27 +874,20 @@ enum IncludeType class I2_CONFIG_API IncludeExpression : public DebuggableExpression { public: - IncludeExpression(const String& relativeBase, Expression *path, Expression *pattern, Expression *name, + IncludeExpression(const String& relativeBase, std::unique_ptr path, std::unique_ptr pattern, std::unique_ptr name, IncludeType type, bool searchIncludes, const String& zone, const String& package, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_RelativeBase(relativeBase), m_Path(path), m_Pattern(pattern), - m_Name(name), m_Type(type), m_SearchIncludes(searchIncludes), m_Zone(zone), m_Package(package) + : DebuggableExpression(debugInfo), m_RelativeBase(relativeBase), m_Path(std::move(path)), m_Pattern(std::move(pattern)), + m_Name(std::move(name)), m_Type(type), m_SearchIncludes(searchIncludes), m_Zone(zone), m_Package(package) { } - ~IncludeExpression(void) - { - delete m_Path; - delete m_Pattern; - delete m_Name; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: String m_RelativeBase; - Expression *m_Path; - Expression *m_Pattern; - Expression *m_Name; + std::unique_ptr m_Path; + std::unique_ptr m_Pattern; + std::unique_ptr m_Name; IncludeType m_Type; bool m_SearchIncludes; String m_Zone; @@ -1010,41 +908,30 @@ protected: class I2_CONFIG_API UsingExpression : public DebuggableExpression { public: - UsingExpression(Expression *name, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_Name(name) + UsingExpression(std::unique_ptr name, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_Name(std::move(name)) { } - ~UsingExpression(void) - { - delete m_Name; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - Expression *m_Name; + std::unique_ptr m_Name; }; class I2_CONFIG_API TryExceptExpression : public DebuggableExpression { public: - TryExceptExpression(Expression *tryBody, Expression *exceptBody, const DebugInfo& debugInfo = DebugInfo()) - : DebuggableExpression(debugInfo), m_TryBody(tryBody), m_ExceptBody(exceptBody) + TryExceptExpression(std::unique_ptr tryBody, std::unique_ptr exceptBody, const DebugInfo& debugInfo = DebugInfo()) + : DebuggableExpression(debugInfo), m_TryBody(std::move(tryBody)), m_ExceptBody(std::move(exceptBody)) { } - ~TryExceptExpression(void) - { - delete m_TryBody; - delete m_ExceptBody; - } - protected: virtual ExpressionResult DoEvaluate(ScriptFrame& frame, DebugHint *dhint) const override; private: - Expression *m_TryBody; - Expression *m_ExceptBody; + std::unique_ptr m_TryBody; + std::unique_ptr m_ExceptBody; }; } diff --git a/lib/config/vmops.hpp b/lib/config/vmops.hpp index 0c56fee71..5f0c27eac 100644 --- a/lib/config/vmops.hpp +++ b/lib/config/vmops.hpp @@ -109,7 +109,7 @@ public: } static inline Value NewFunction(ScriptFrame& frame, const String& name, const std::vector& argNames, - std::map *closedVars, const std::shared_ptr& expression) + const std::map >& closedVars, const std::shared_ptr& expression) { auto evaluatedClosedVars = EvaluateClosedVars(frame, closedVars); @@ -132,7 +132,7 @@ public: } static inline Value NewApply(ScriptFrame& frame, const String& type, const String& target, const String& name, const std::shared_ptr& filter, - const String& package, const String& fkvar, const String& fvvar, const std::shared_ptr& fterm, std::map *closedVars, + const String& package, const String& fkvar, const String& fvvar, const std::shared_ptr& fterm, const std::map >& closedVars, bool ignoreOnError, const std::shared_ptr& expression, const DebugInfo& debugInfo = DebugInfo()) { ApplyRule::AddRule(type, target, name, expression, filter, package, fkvar, @@ -142,7 +142,7 @@ public: } static inline Value NewObject(ScriptFrame& frame, bool abstract, const Type::Ptr& type, const String& name, const std::shared_ptr& filter, - const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, std::map *closedVars, const std::shared_ptr& expression, const DebugInfo& debugInfo = DebugInfo()) + const String& zone, const String& package, bool defaultTmpl, bool ignoreOnError, const std::map >& closedVars, const std::shared_ptr& expression, const DebugInfo& debugInfo = DebugInfo()) { ConfigItemBuilder::Ptr item = new ConfigItemBuilder(debugInfo); @@ -190,7 +190,7 @@ public: return Empty; } - static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, Expression *expression, const DebugInfo& debugInfo = DebugInfo()) + static inline ExpressionResult For(ScriptFrame& frame, const String& fkvar, const String& fvvar, const Value& value, const std::unique_ptr& expression, const DebugInfo& debugInfo = DebugInfo()) { if (value.IsObjectType()) { if (!fvvar.IsEmpty()) @@ -251,15 +251,14 @@ public: } private: - static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, std::map *closedVars) + static inline Dictionary::Ptr EvaluateClosedVars(ScriptFrame& frame, const std::map >& closedVars) { Dictionary::Ptr locals; - if (closedVars) { + if (!closedVars.empty()) { locals = new Dictionary(); - typedef std::pair ClosedVar; - for (const ClosedVar& cvar : *closedVars) { + for (const auto& cvar : closedVars) { locals->Set(cvar.first, cvar.second->Evaluate(frame)); } } diff --git a/lib/remote/configobjectutility.cpp b/lib/remote/configobjectutility.cpp index fd1afc1e8..8cdbb800e 100644 --- a/lib/remote/configobjectutility.cpp +++ b/lib/remote/configobjectutility.cpp @@ -124,15 +124,14 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full fp << config; fp.close(); - Expression *expr = ConfigCompiler::CompileFile(path, String(), "_api"); + std::unique_ptr expr = ConfigCompiler::CompileFile(path, String(), "_api"); try { ActivationScope ascope; ScriptFrame frame; expr->Evaluate(frame); - delete expr; - expr = nullptr; + expr.reset(); WorkQueue upq; std::vector newItems; @@ -156,8 +155,6 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full ApiListener::UpdateObjectAuthority(); } catch (const std::exception& ex) { - delete expr; - if (unlink(path.CStr()) < 0 && errno != ENOENT) { BOOST_THROW_EXCEPTION(posix_error() << boost::errinfo_api_function("unlink") diff --git a/lib/remote/consolehandler.cpp b/lib/remote/consolehandler.cpp index ed2748c3a..825c361a5 100644 --- a/lib/remote/consolehandler.cpp +++ b/lib/remote/consolehandler.cpp @@ -123,7 +123,7 @@ bool ConsoleHandler::ExecuteScriptHelper(HttpRequest& request, HttpResponse& res Array::Ptr results = new Array(); Dictionary::Ptr resultInfo = new Dictionary(); - Expression *expr = nullptr; + std::unique_ptr expr; Value exprResult; try { @@ -160,11 +160,7 @@ bool ConsoleHandler::ExecuteScriptHelper(HttpRequest& request, HttpResponse& res debugInfo->Set("last_line", di.LastLine); debugInfo->Set("last_column", di.LastColumn); resultInfo->Set("debug_info", debugInfo); - } catch (...) { - delete expr; - throw; } - delete expr; results->Add(resultInfo); @@ -301,13 +297,12 @@ std::vector ConsoleHandler::GetAutocompletionSuggestions(const String& w Value value; try { - Expression *expr = ConfigCompiler::CompileText("temp", pword); + std::unique_ptr expr = ConfigCompiler::CompileText("temp", pword); if (expr) value = expr->Evaluate(frame); AddSuggestions(matches, word, pword, true, value); - } catch (...) { /* Ignore the exception */ } } diff --git a/lib/remote/eventqueue.cpp b/lib/remote/eventqueue.cpp index e7b0b4bf9..a9b43f42a 100644 --- a/lib/remote/eventqueue.cpp +++ b/lib/remote/eventqueue.cpp @@ -25,14 +25,9 @@ using namespace icinga; EventQueue::EventQueue(const String& name) - : m_Name(name), m_Filter(nullptr) + : m_Name(name) { } -EventQueue::~EventQueue(void) -{ - delete m_Filter; -} - bool EventQueue::CanProcessEvent(const String& type) const { boost::mutex::scoped_lock lock(m_Mutex); @@ -46,7 +41,7 @@ void EventQueue::ProcessEvent(const Dictionary::Ptr& event) frame.Sandboxed = true; try { - if (!FilterUtility::EvaluateFilter(frame, m_Filter, event, "event")) + if (!FilterUtility::EvaluateFilter(frame, &*m_Filter, event, "event")) return; } catch (const std::exception& ex) { Log(LogWarning, "EventQueue") @@ -93,11 +88,10 @@ void EventQueue::SetTypes(const std::set& types) m_Types = types; } -void EventQueue::SetFilter(Expression *filter) +void EventQueue::SetFilter(std::unique_ptr filter) { boost::mutex::scoped_lock lock(m_Mutex); - delete m_Filter; - m_Filter = filter; + m_Filter.swap(filter); } Dictionary::Ptr EventQueue::WaitForEvent(void *client, double timeout) diff --git a/lib/remote/eventqueue.hpp b/lib/remote/eventqueue.hpp index 893fd8efe..e27f42020 100644 --- a/lib/remote/eventqueue.hpp +++ b/lib/remote/eventqueue.hpp @@ -38,7 +38,6 @@ public: DECLARE_PTR_TYPEDEFS(EventQueue); EventQueue(const String& name); - ~EventQueue(void); bool CanProcessEvent(const String& type) const; void ProcessEvent(const Dictionary::Ptr& event); @@ -46,7 +45,7 @@ public: void RemoveClient(void *client); void SetTypes(const std::set& types); - void SetFilter(Expression *filter); + void SetFilter(std::unique_ptr filter); Dictionary::Ptr WaitForEvent(void *client, double timeout = 5); @@ -64,7 +63,7 @@ private: boost::condition_variable m_CV; std::set m_Types; - Expression *m_Filter; + std::unique_ptr m_Filter; std::map > m_Events; }; diff --git a/lib/remote/eventshandler.cpp b/lib/remote/eventshandler.cpp index c8c57c6b5..673515d53 100644 --- a/lib/remote/eventshandler.cpp +++ b/lib/remote/eventshandler.cpp @@ -66,7 +66,7 @@ bool EventsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request String filter = HttpUtility::GetLastParameter(params, "filter"); - Expression *ufilter = nullptr; + std::unique_ptr ufilter; if (!filter.IsEmpty()) ufilter = ConfigCompiler::CompileText("", filter); @@ -80,7 +80,7 @@ bool EventsHandler::HandleRequest(const ApiUser::Ptr& user, HttpRequest& request } queue->SetTypes(types->ToSet()); - queue->SetFilter(ufilter); + queue->SetFilter(std::move(ufilter)); queue->AddClient(&request); diff --git a/lib/remote/filterutility.cpp b/lib/remote/filterutility.cpp index 672d3afe5..0c0bee07a 100644 --- a/lib/remote/filterutility.cpp +++ b/lib/remote/filterutility.cpp @@ -162,14 +162,15 @@ void FilterUtility::CheckPermission(const ApiUser::Ptr& user, const String& perm foundPermission = true; if (filter && permissionFilter) { - std::vector args; - args.push_back(new GetScopeExpression(ScopeLocal)); - FunctionCallExpression *fexpr = new FunctionCallExpression(new IndexerExpression(MakeLiteral(filter), MakeLiteral("call")), args); + std::vector > args; + args.emplace_back(new GetScopeExpression(ScopeLocal)); + std::unique_ptr indexer{new IndexerExpression(std::unique_ptr(MakeLiteral(filter)), std::unique_ptr(MakeLiteral("call")))}; + FunctionCallExpression *fexpr = new FunctionCallExpression(std::move(indexer), std::move(args)); if (!*permissionFilter) *permissionFilter = fexpr; else - *permissionFilter = new LogicalOrExpression(*permissionFilter, fexpr); + *permissionFilter = new LogicalOrExpression(std::unique_ptr(*permissionFilter), std::unique_ptr(fexpr)); } } } @@ -250,7 +251,7 @@ std::vector FilterUtility::GetFilterTargets(const QueryDescription& qd, c frame.Sandboxed = true; Dictionary::Ptr uvars = new Dictionary(); - Expression *ufilter = nullptr; + std::unique_ptr ufilter; if (query->Contains("filter")) { String filter = HttpUtility::GetLastParameter(query, "filter"); @@ -267,16 +268,9 @@ std::vector FilterUtility::GetFilterTargets(const QueryDescription& qd, c frame.Self = uvars; - try { - provider->FindTargets(type, std::bind(&FilteredAddTarget, - std::ref(permissionFrame), permissionFilter, - std::ref(frame), ufilter, std::ref(result), variableName, _1)); - } catch (const std::exception&) { - delete ufilter; - throw; - } - - delete ufilter; + provider->FindTargets(type, std::bind(&FilteredAddTarget, + std::ref(permissionFrame), permissionFilter, + std::ref(frame), &*ufilter, std::ref(result), variableName, _1)); } return result; diff --git a/test/config-ops.cpp b/test/config-ops.cpp index 7ca39e92f..8ab386dd2 100644 --- a/test/config-ops.cpp +++ b/test/config-ops.cpp @@ -28,308 +28,236 @@ BOOST_AUTO_TEST_SUITE(config_ops) BOOST_AUTO_TEST_CASE(simple) { ScriptFrame frame; - Expression *expr; + std::unique_ptr expr; Dictionary::Ptr dict; expr = ConfigCompiler::CompileText("", ""); BOOST_CHECK(expr->Evaluate(frame).GetValue() == Empty); - delete expr; expr = ConfigCompiler::CompileText("", "\n3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "{ 3\n\n5 }"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "1 + 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 4); - delete expr; expr = ConfigCompiler::CompileText("", "3 - 1"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 2); - delete expr; expr = ConfigCompiler::CompileText("", "5m * 10"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3000); - delete expr; expr = ConfigCompiler::CompileText("", "5m / 5"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 60); - delete expr; expr = ConfigCompiler::CompileText("", "7 & 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "2 | 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "true && false"); BOOST_CHECK(!expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "true || false"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "3 < 5"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "3 > 5"); BOOST_CHECK(!expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "3 <= 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "3 >= 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "2 + 3 * 4"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 14); - delete expr; expr = ConfigCompiler::CompileText("", "(2 + 3) * 4"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 20); - delete expr; expr = ConfigCompiler::CompileText("", "2 * - 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == -6); - delete expr; expr = ConfigCompiler::CompileText("", "-(2 + 3)"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == -5); - delete expr; expr = ConfigCompiler::CompileText("", "- 2 * 2 - 2 * 3 - 4 * - 5"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 10); - delete expr; expr = ConfigCompiler::CompileText("", "!0 == true"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "~0"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == (double)~(long)0); - delete expr; expr = ConfigCompiler::CompileText("", "4 << 8"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 1024); - delete expr; expr = ConfigCompiler::CompileText("", "1024 >> 4"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 64); - delete expr; expr = ConfigCompiler::CompileText("", "2 << 3 << 4"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 256); - delete expr; expr = ConfigCompiler::CompileText("", "256 >> 4 >> 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 2); - delete expr; expr = ConfigCompiler::CompileText("", "\"hello\" == \"hello\""); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"hello\" != \"hello\""); BOOST_CHECK(!expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" in [ \"foo\", \"bar\" ]"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" in [ \"bar\", \"baz\" ]"); BOOST_CHECK(!expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" in null"); BOOST_CHECK(!expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" in \"bar\""); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" !in [ \"bar\", \"baz\" ]"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" !in [ \"foo\", \"bar\" ]"); BOOST_CHECK(!expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" !in null"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"foo\" !in \"bar\""); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "{ a += 3 }"); dict = expr->Evaluate(frame).GetValue(); - delete expr; BOOST_CHECK(dict->GetLength() == 1); BOOST_CHECK(dict->Get("a") == 3); expr = ConfigCompiler::CompileText("", "test"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "null + 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "3 + null"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "\"test\" + 3"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == "test3"); - delete expr; expr = ConfigCompiler::CompileText("", "\"\\\"te\\\\st\""); BOOST_CHECK(expr->Evaluate(frame).GetValue() == "\"te\\st"); - delete expr; expr = ConfigCompiler::CompileText("", "\"\\'test\""); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "({ a = 3\nb = 3 })"); BOOST_CHECK(expr->Evaluate(frame).GetValue().IsObjectType()); - delete expr; } BOOST_AUTO_TEST_CASE(advanced) { ScriptFrame frame; - Expression *expr; + std::unique_ptr expr; Function::Ptr func; expr = ConfigCompiler::CompileText("", "regex(\"^Hello\", \"Hello World\")"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "__boost_test()"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; Object::Ptr self = new Object(); ScriptFrame frame2(self); expr = ConfigCompiler::CompileText("", "this"); BOOST_CHECK(expr->Evaluate(frame2).GetValue() == Value(self)); - delete expr; expr = ConfigCompiler::CompileText("", "var v = 7; v"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "{ a = 3 }.a"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "[ 2, 3 ][1]"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "var v = { a = 3}; v.a"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "a = 3 b = 3"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "function() { 3 }()"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "function() { return 3, 5 }()"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "typeof([]) == Array"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "typeof({}) == Dictionary"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "typeof(3) == Number"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "typeof(\"test\") == String"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "(7 | 8) == 15"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "(7 ^ 8) == 15"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "(7 & 15) == 7"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "7 in [7] == true"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "7 !in [7] == false"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "(7 | 8) > 14"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "(7 ^ 8) > 14"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "(7 & 15) > 6"); BOOST_CHECK(expr->Evaluate(frame).GetValue()); - delete expr; expr = ConfigCompiler::CompileText("", "\"a\" = 3"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "3 = 3"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "var e; e"); BOOST_CHECK(expr->Evaluate(frame).GetValue().IsEmpty()); - delete expr; expr = ConfigCompiler::CompileText("", "var e = 3; e"); BOOST_CHECK(expr->Evaluate(frame).GetValue() == 3); - delete expr; expr = ConfigCompiler::CompileText("", "Array.x"); BOOST_CHECK_THROW(expr->Evaluate(frame).GetValue(), ScriptError); - delete expr; expr = ConfigCompiler::CompileText("", "{{ 3 }}"); func = expr->Evaluate(frame).GetValue(); BOOST_CHECK(func->Invoke() == 3); - delete expr; } BOOST_AUTO_TEST_SUITE_END() diff --git a/test/livestatus-fixture.cpp b/test/livestatus-fixture.cpp index 054d05827..20d3f13b0 100644 --- a/test/livestatus-fixture.cpp +++ b/test/livestatus-fixture.cpp @@ -58,7 +58,7 @@ apply Service "livestatus" { } )CONFIG"; - Expression *expr = ConfigCompiler::CompileText("", config); + std::unique_ptr expr = ConfigCompiler::CompileText("", config); expr->Evaluate(*ScriptFrame::GetCurrentFrame()); } };