From 86290713925c9e24e1f10aa7b40e1d38f9225aca Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 21 Mar 2014 10:22:59 +0100 Subject: [PATCH] Improve line number information. Fixes #5825 --- lib/config/config_parser.yy | 93 +++++++++++++++++++++---------------- lib/config/debuginfo.h | 11 +++++ 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy index 87ed3a378..37b1284cf 100644 --- a/lib/config/config_parser.yy +++ b/lib/config/config_parser.yy @@ -1,4 +1,6 @@ %{ + #define YYDEBUG 1 + /****************************************************************************** * Icinga 2 * * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) * @@ -39,6 +41,15 @@ #include #define YYLTYPE icinga::DebugInfo +#define YYERROR_VERBOSE + +#define YY_LOCATION_PRINT(file, loc) \ +do { \ + std::ostringstream msgbuf; \ + msgbuf << loc; \ + std::string str = msgbuf.str(); \ + fputs(str.c_str(), file); \ +} while (0) using namespace icinga; @@ -179,24 +190,24 @@ statement: object | type | include | include_recursive | library | variable | ap include: T_INCLUDE value { - context->HandleInclude(*$2, false, yylloc); + context->HandleInclude(*$2, false, DebugInfoRange(@1, @2)); delete $2; } | T_INCLUDE T_STRING_ANGLE { - context->HandleInclude($2, true, yylloc); + context->HandleInclude($2, true, DebugInfoRange(@1, @2)); free($2); } ; include_recursive: T_INCLUDE_RECURSIVE value { - context->HandleIncludeRecursive(*$2, "*.conf", yylloc); + context->HandleIncludeRecursive(*$2, "*.conf", DebugInfoRange(@1, @2)); delete $2; } | T_INCLUDE_RECURSIVE value value { - context->HandleIncludeRecursive(*$2, *$3, yylloc); + context->HandleIncludeRecursive(*$2, *$3, DebugInfoRange(@1, @3)); delete $2; delete $3; } @@ -256,7 +267,7 @@ type: partial_specifier T_TYPE identifier if ($1) BOOST_THROW_EXCEPTION(std::invalid_argument("Partial type definition for unknown type '" + name + "'")); - m_Type = make_shared(name, yylloc); + m_Type = make_shared(name, DebugInfoRange(@1, @3)); m_Type->Register(); } } @@ -316,14 +327,14 @@ typerule: T_REQUIRE T_STRING } | T_ATTRIBUTE type T_STRING { - TypeRule rule($2, String(), $3, TypeRuleList::Ptr(), yylloc); + TypeRule rule($2, String(), $3, TypeRuleList::Ptr(), DebugInfoRange(@1, @3)); free($3); m_RuleLists.top()->AddRule(rule); } | T_ATTRIBUTE T_TYPE_NAME '(' identifier ')' T_STRING { - TypeRule rule($2, $4, $6, TypeRuleList::Ptr(), yylloc); + TypeRule rule($2, $4, $6, TypeRuleList::Ptr(), DebugInfoRange(@1, @6)); free($4); free($6); @@ -331,7 +342,7 @@ typerule: T_REQUIRE T_STRING } | T_ATTRIBUTE type T_STRING typerulelist { - TypeRule rule($2, String(), $3, *$4, yylloc); + TypeRule rule($2, String(), $3, *$4, DebugInfoRange(@1, @4)); free($3); delete $4; m_RuleLists.top()->AddRule(rule); @@ -362,9 +373,9 @@ object: { m_Abstract = false; } -object_declaration identifier T_STRING object_inherits_specifier expressionlist + object_declaration identifier T_STRING object_inherits_specifier expressionlist { - ConfigItemBuilder::Ptr item = make_shared(yylloc); + ConfigItemBuilder::Ptr item = make_shared(DebugInfoRange(@2, @6)); ConfigItem::Ptr oldItem = ConfigItem::GetObject($3, $4); @@ -491,20 +502,20 @@ expressions_inner: /* empty */ expression: identifier operator value { - $$ = new Expression($1, $2, *$3, yylloc); + $$ = new Expression($1, $2, *$3, DebugInfoRange(@1, @3)); free($1); delete $3; } | identifier '[' T_STRING ']' operator value { - Expression subexpr($3, $5, *$6, yylloc); + Expression subexpr($3, $5, *$6, DebugInfoRange(@1, @6)); free($3); delete $6; ExpressionList::Ptr subexprl = make_shared(); subexprl->AddExpression(subexpr); - $$ = new Expression($1, OperatorPlus, subexprl, yylloc); + $$ = new Expression($1, OperatorPlus, subexprl, DebugInfoRange(@1, @6)); free($1); } ; @@ -552,41 +563,41 @@ array_items_inner: /* empty */ aexpression: T_STRING { - $$ = new Value(make_shared(AEReturn, AValue(ATSimple, $1), yylloc)); + $$ = new Value(make_shared(AEReturn, AValue(ATSimple, $1), @1)); free($1); } | T_NUMBER { - $$ = new Value(make_shared(AEReturn, AValue(ATSimple, $1), yylloc)); + $$ = new Value(make_shared(AEReturn, AValue(ATSimple, $1), @1)); } | T_NULL { - $$ = new Value(make_shared(AEReturn, AValue(ATSimple, Empty), yylloc)); + $$ = new Value(make_shared(AEReturn, AValue(ATSimple, Empty), @1)); } | T_IDENTIFIER '(' array_items ')' { Array::Ptr arguments = Array::Ptr($3); - $$ = new Value(make_shared(AEFunctionCall, AValue(ATSimple, $1), AValue(ATSimple, arguments), yylloc)); + $$ = new Value(make_shared(AEFunctionCall, AValue(ATSimple, $1), AValue(ATSimple, arguments), DebugInfoRange(@1, @4))); free($1); } | T_IDENTIFIER { - $$ = new Value(make_shared(AEReturn, AValue(ATVariable, $1), yylloc)); + $$ = new Value(make_shared(AEReturn, AValue(ATVariable, $1), @1)); free($1); } | '!' aexpression { - $$ = new Value(make_shared(AENegate, static_cast(*$2), yylloc)); + $$ = new Value(make_shared(AENegate, static_cast(*$2), DebugInfoRange(@1, @2))); delete $2; } | '~' aexpression { - $$ = new Value(make_shared(AENegate, static_cast(*$2), yylloc)); + $$ = new Value(make_shared(AENegate, static_cast(*$2), DebugInfoRange(@1, @2))); delete $2; } | '[' array_items ']' { - $$ = new Value(make_shared(AEArray, AValue(ATSimple, Array::Ptr($2)), yylloc)); + $$ = new Value(make_shared(AEArray, AValue(ATSimple, Array::Ptr($2)), DebugInfoRange(@1, @3))); } | '(' aexpression ')' { @@ -594,109 +605,109 @@ aexpression: T_STRING } | aexpression '+' aexpression { - $$ = new Value(make_shared(AEAdd, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEAdd, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '-' aexpression { - $$ = new Value(make_shared(AESubtract, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AESubtract, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '*' aexpression { - $$ = new Value(make_shared(AEMultiply, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEMultiply, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '/' aexpression { - $$ = new Value(make_shared(AEDivide, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEDivide, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '&' aexpression { - $$ = new Value(make_shared(AEBinaryAnd, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEBinaryAnd, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '|' aexpression { - $$ = new Value(make_shared(AEBinaryOr, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEBinaryOr, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @2))); delete $1; delete $3; } | aexpression T_IN aexpression { - $$ = new Value(make_shared(AEIn, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEIn, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_NOT_IN aexpression { - $$ = new Value(make_shared(AENotIn, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AENotIn, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_LESS_THAN_OR_EQUAL aexpression { - $$ = new Value(make_shared(AELessThanOrEqual, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AELessThanOrEqual, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_GREATER_THAN_OR_EQUAL aexpression { - $$ = new Value(make_shared(AEGreaterThanOrEqual, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEGreaterThanOrEqual, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '<' aexpression { - $$ = new Value(make_shared(AELessThan, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AELessThan, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression '>' aexpression { - $$ = new Value(make_shared(AEGreaterThan, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEGreaterThan, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_EQUAL aexpression { - $$ = new Value(make_shared(AEEqual, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEEqual, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_NOT_EQUAL aexpression { - $$ = new Value(make_shared(AENotEqual, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AENotEqual, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_SHIFT_LEFT aexpression { - $$ = new Value(make_shared(AEShiftLeft, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEShiftLeft, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_SHIFT_RIGHT aexpression { - $$ = new Value(make_shared(AEShiftRight, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AEShiftRight, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_LOGICAL_AND aexpression { - $$ = new Value(make_shared(AELogicalAnd, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AELogicalAnd, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } | aexpression T_LOGICAL_OR aexpression { - $$ = new Value(make_shared(AELogicalOr, static_cast(*$1), static_cast(*$3), yylloc)); + $$ = new Value(make_shared(AELogicalOr, static_cast(*$1), static_cast(*$3), DebugInfoRange(@1, @3))); delete $1; delete $3; } @@ -729,8 +740,8 @@ apply: T_APPLY optional_template identifier identifier T_TO identifier T_WHERE a arguments->Add(*$8); delete $8; - AExpression::Ptr aexpr = make_shared(AEFunctionCall, AValue(ATSimple, "bool"), AValue(ATSimple, arguments), yylloc); + AExpression::Ptr aexpr = make_shared(AEFunctionCall, AValue(ATSimple, "bool"), AValue(ATSimple, arguments), @8); - ApplyRule::AddRule($3, $4, $6, aexpr, yylloc); + ApplyRule::AddRule($3, $4, $6, aexpr, DebugInfoRange(@1, @8)); } %% diff --git a/lib/config/debuginfo.h b/lib/config/debuginfo.h index 3ea8d96b6..393e39524 100644 --- a/lib/config/debuginfo.h +++ b/lib/config/debuginfo.h @@ -76,6 +76,17 @@ inline std::ostream& operator<<(std::ostream& out, const DebugInfo& val) return out; } +inline DebugInfo DebugInfoRange(const DebugInfo& start, const DebugInfo& end) +{ + DebugInfo result; + result.Path = start.Path; + result.FirstLine = start.FirstLine; + result.FirstColumn = start.FirstColumn; + result.LastLine = end.LastLine; + result.LastColumn = end.LastColumn; + return result; +} + } #endif /* DEBUGINFO_H */