Skip to content

Commit 42e1431

Browse files
committed
Handle MSVC's __noop in type checker
The documentation states that it does not generate any code other than `0`. Hence, don't maintain a library function, but instead discard the expression in the type checker.
1 parent 4c3a0a6 commit 42e1431

File tree

14 files changed

+48
-45
lines changed

14 files changed

+48
-45
lines changed

regression/ansi-c/noop1/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// this is permitted - __noop yields a compile-time constant 0
2+
int array[__noop() + 1];
3+
4+
struct S
5+
{
6+
int x;
7+
};
8+
9+
int main(int argc, char *argv)
10+
{
11+
struct S s;
12+
// the arguments to __noop are type checked, and thus the following is not
13+
// permitted
14+
__noop((char *)s);
15+
}

regression/ansi-c/noop1/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--i386-win32
4+
type cast from 'struct S' not permitted
5+
^EXIT=(1|64)$
6+
^SIGNAL=0$
7+
--
8+
^warning: ignoring

regression/cbmc-library/__noop-01/main.c

Lines changed: 0 additions & 9 deletions
This file was deleted.

regression/cbmc/noop1/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#include <assert.h>
2-
31
int some_function()
42
{
53
// this will not be executed
6-
assert(0);
4+
__CPROVER_assert(0, "not executed");
75
}
86

97
int main()

regression/cbmc/noop1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
main.c
3-
3+
-winx64
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

regression/cbmc/noop2/main.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int main(int argc, char *argv)
2+
{
3+
int x = 0;
4+
__noop(++x);
5+
__CPROVER_assert(x == 0, "__noop ignores side effects");
6+
}

regression/cbmc-library/__noop-01/test.desc renamed to regression/cbmc/noop2/test.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
KNOWNBUG
1+
CORE
22
main.c
3-
--pointer-check --bounds-check
3+
-win32
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,7 @@ void ansi_c_internal_additions(std::string &code)
295295

296296
// this is Visual C/C++ only
297297
if(config.ansi_c.os==configt::ansi_ct::ost::OS_WIN)
298-
code+="int __noop();\n"
299-
"int __assume(int);\n";
298+
code += "int __assume(int);\n";
300299

301300
// ARM stuff
302301
if(config.ansi_c.mode==configt::ansi_ct::flavourt::ARM)

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1884,6 +1884,20 @@ void c_typecheck_baset::typecheck_side_effect_function_call(
18841884
{
18851885
// yes, it's a builtin
18861886
}
1887+
else if(
1888+
identifier == "__noop" &&
1889+
config.ansi_c.mode == configt::ansi_ct::flavourt::VISUAL_STUDIO)
1890+
{
1891+
// https://docs.microsoft.com/en-us/cpp/intrinsics/noop
1892+
// typecheck and discard, just generating 0 instead
1893+
for(auto &op : expr.arguments())
1894+
typecheck_expr(op);
1895+
1896+
exprt result = from_integer(0, signed_int_type());
1897+
expr.swap(result);
1898+
1899+
return;
1900+
}
18871901
else if(
18881902
auto gcc_polymorphic = typecheck_gcc_polymorphic_builtin(
18891903
identifier, expr.arguments(), f_op.source_location()))

src/ansi-c/library/noop.c

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)