Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
888ae4e3
Commit
888ae4e3
authored
3 years ago
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Check that natural integer cannot become negative on decrement
parent
fcbca642
No related branches found
No related tags found
1 merge request
!145
git subrepo clone git@gitlab.com:OlMon/org-themes.git packages/org-themes
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/language/node_processor/IncDecExpressionProcessor.hpp
+16
-1
16 additions, 1 deletion
src/language/node_processor/IncDecExpressionProcessor.hpp
tests/test_IncDecExpressionProcessor.cpp
+27
-0
27 additions, 0 deletions
tests/test_IncDecExpressionProcessor.cpp
with
43 additions
and
1 deletion
src/language/node_processor/IncDecExpressionProcessor.hpp
+
16
−
1
View file @
888ae4e3
...
@@ -16,6 +16,11 @@ struct IncDecOp<language::unary_minusminus>
...
@@ -16,6 +16,11 @@ struct IncDecOp<language::unary_minusminus>
PUGS_INLINE
A
PUGS_INLINE
A
eval
(
A
&
a
)
eval
(
A
&
a
)
{
{
if
constexpr
(
std
::
is_same_v
<
A
,
uint64_t
>
)
{
if
(
a
==
0
)
{
throw
std
::
domain_error
(
"decrement would produce negative value"
);
}
}
return
--
a
;
return
--
a
;
}
}
};
};
...
@@ -38,6 +43,11 @@ struct IncDecOp<language::post_minusminus>
...
@@ -38,6 +43,11 @@ struct IncDecOp<language::post_minusminus>
PUGS_INLINE
A
PUGS_INLINE
A
eval
(
A
&
a
)
eval
(
A
&
a
)
{
{
if
constexpr
(
std
::
is_same_v
<
A
,
uint64_t
>
)
{
if
(
a
==
0
)
{
throw
std
::
domain_error
(
"decrement would produce negative value"
);
}
}
return
a
--
;
return
a
--
;
}
}
};
};
...
@@ -64,8 +74,13 @@ class IncDecExpressionProcessor final : public INodeProcessor
...
@@ -64,8 +74,13 @@ class IncDecExpressionProcessor final : public INodeProcessor
DataVariant
DataVariant
execute
(
ExecutionPolicy
&
)
execute
(
ExecutionPolicy
&
)
{
{
try
{
return
IncDecOp
<
IncDecOpT
>
().
eval
(
std
::
get
<
DataT
>
(
*
p_value
));
return
IncDecOp
<
IncDecOpT
>
().
eval
(
std
::
get
<
DataT
>
(
*
p_value
));
}
}
catch
(
std
::
domain_error
&
e
)
{
throw
ParseError
(
e
.
what
(),
m_node
.
children
[
0
]
->
begin
());
}
}
IncDecExpressionProcessor
(
ASTNode
&
node
)
:
m_node
{
node
}
IncDecExpressionProcessor
(
ASTNode
&
node
)
:
m_node
{
node
}
{
{
...
...
This diff is collapsed.
Click to expand it.
tests/test_IncDecExpressionProcessor.cpp
+
27
−
0
View file @
888ae4e3
...
@@ -58,6 +58,23 @@
...
@@ -58,6 +58,23 @@
REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast}, error_message); \
REQUIRE_THROWS_WITH(ASTNodeDataTypeBuilder{*ast}, error_message); \
}
}
#define CHECK_INCDEC_THROWS_WITH(data, error_message) \
{ \
TAO_PEGTL_NAMESPACE::string_input input{data, "test.pgs"}; \
auto ast = ASTBuilder::build(input); \
\
ASTSymbolTableBuilder{*ast}; \
ASTNodeDataTypeBuilder{*ast}; \
\
ASTNodeDeclarationToAffectationConverter{*ast}; \
ASTNodeTypeCleaner<language::var_declaration>{*ast}; \
\
ASTNodeExpressionBuilder{*ast}; \
ExecutionPolicy exec_policy; \
\
REQUIRE_THROWS_WITH(ast->execute(exec_policy), error_message); \
}
// clazy:excludeall=non-pod-global-static
// clazy:excludeall=non-pod-global-static
TEST_CASE
(
"IncDecExpressionProcessor"
,
"[language]"
)
TEST_CASE
(
"IncDecExpressionProcessor"
,
"[language]"
)
...
@@ -124,6 +141,16 @@ TEST_CASE("IncDecExpressionProcessor", "[language]")
...
@@ -124,6 +141,16 @@ TEST_CASE("IncDecExpressionProcessor", "[language]")
SECTION
(
"errors"
)
SECTION
(
"errors"
)
{
{
SECTION
(
"negative pre -- operator for N"
)
{
CHECK_INCDEC_THROWS_WITH
(
R"(let n:N, n=0;--n;)"
,
"decrement would produce negative value"
);
}
SECTION
(
"negative post -- operator for N"
)
{
CHECK_INCDEC_THROWS_WITH
(
R"(let n:N, n=0;n--;)"
,
"decrement would produce negative value"
);
}
SECTION
(
"undefined pre -- operator"
)
SECTION
(
"undefined pre -- operator"
)
{
{
auto
error_message
=
[](
std
::
string
type_name
)
{
auto
error_message
=
[](
std
::
string
type_name
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment