Skip to content

Commit 3b384ed

Browse files
authored
Merge pull request #4622 from tautschnig/maintain-comments
C front-end: Do not lose comments in type checking
2 parents f2468ef + 575d386 commit 3b384ed

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
int foo(int x)
2+
{
3+
return x;
4+
}
5+
6+
int main()
7+
{
8+
int n;
9+
10+
#pragma CPROVER check push
11+
#pragma CPROVER check disable "signed-overflow"
12+
// do not generate assertions for the following statements
13+
int x = n + n;
14+
++n;
15+
n++;
16+
n += 1;
17+
foo(x + n);
18+
// pop all annotations
19+
#pragma CPROVER check pop
20+
// but do generate assertions for these
21+
x = n + n;
22+
foo(x + n);
23+
return x;
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--signed-overflow-check
4+
^\[main.overflow\.1\] line 21 arithmetic overflow on signed \+ in n \+ n: FAILURE$
5+
^\[main.overflow\.2\] line 22 arithmetic overflow on signed \+ in x \+ n: FAILURE$
6+
^\*\* 2 of 2 failed
7+
^VERIFICATION FAILED$
8+
^EXIT=10$
9+
^SIGNAL=0$
10+
--
11+
^warning: ignoring

src/ansi-c/c_typecheck_code.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,16 @@ void c_typecheck_baset::typecheck_decl(codet &code)
243243
}
244244

245245
ansi_c_declarationt declaration;
246+
irep_idt comment = code.source_location().get_comment();
246247
declaration.swap(code.op0());
248+
if(!comment.empty())
249+
{
250+
for(auto &d : declaration.declarators())
251+
{
252+
if(d.source_location().get_comment().empty())
253+
d.add_source_location().set_comment(comment);
254+
}
255+
}
247256

248257
if(declaration.get_is_static_assert())
249258
{

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,10 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
20192019
throw 0;
20202020
}
20212021

2022+
irep_idt comment = expr.source_location().get_comment();
2023+
if(!comment.empty() && f_op.source_location().get_comment().empty())
2024+
f_op.add_source_location().set_comment(comment);
2025+
20222026
const code_typet &code_type=to_code_type(f_op.type());
20232027

20242028
expr.type()=code_type.return_type();

0 commit comments

Comments
 (0)