Skip to content

C front-end: Do not lose comments in type checking #4622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions regression/cbmc/pragma_cprover2/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
int foo(int x)
{
return x;
}

int main()
{
int n;

#pragma CPROVER check push
#pragma CPROVER check disable "signed-overflow"
// do not generate assertions for the following statements
int x = n + n;
++n;
n++;
n += 1;
foo(x + n);
// pop all annotations
#pragma CPROVER check pop
// but do generate assertions for these
x = n + n;
foo(x + n);
return x;
}
11 changes: 11 additions & 0 deletions regression/cbmc/pragma_cprover2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
--signed-overflow-check
^\[main.overflow\.1\] line 21 arithmetic overflow on signed \+ in n \+ n: FAILURE$
^\[main.overflow\.2\] line 22 arithmetic overflow on signed \+ in x \+ n: FAILURE$
^\*\* 2 of 2 failed
^VERIFICATION FAILED$
^EXIT=10$
^SIGNAL=0$
--
^warning: ignoring
9 changes: 9 additions & 0 deletions src/ansi-c/c_typecheck_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,16 @@ void c_typecheck_baset::typecheck_decl(codet &code)
}

ansi_c_declarationt declaration;
irep_idt comment = code.source_location().get_comment();
declaration.swap(code.op0());
if(!comment.empty())
{
for(auto &d : declaration.declarators())
{
if(d.source_location().get_comment().empty())
d.add_source_location().set_comment(comment);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comment is non empty, should we merge instead of ignoring?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new implementation in #4666 also resolves this problem (in parts by just removing the above code :-) ).

}
}

if(declaration.get_is_static_assert())
{
Expand Down
4 changes: 4 additions & 0 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,10 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
throw 0;
}

irep_idt comment = expr.source_location().get_comment();
if(!comment.empty() && f_op.source_location().get_comment().empty())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comment is non empty, should we merge instead of ignoring?

f_op.add_source_location().set_comment(comment);

const code_typet &code_type=to_code_type(f_op.type());

expr.type()=code_type.return_type();
Expand Down