Skip to content

Commit

Permalink
unit test nullcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Mar 7, 2024
1 parent 0cca63d commit e545b4f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
18 changes: 13 additions & 5 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
enum { A, B };
void free(void* _Owner _Opt p);
struct X
{
int i;
void* _Owner p;
};
void f(struct X* p);
int main()
{

if (B > A) {}

}
struct X x = { 0 };
static_state(x.p, "null");
f(&x);
static_state(x.p, "maybe-null");
free(x.p);
}
8 changes: 7 additions & 1 deletion src/flow_visit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,13 @@ static int compare_function_arguments2(struct parser_ctx* ctx,
we are passing a pointer to an non const object
everything can happen with this object
*/
object_set_unknown(&p_current_argument->expression->type, p_argument_object);
struct type argument_type =
type_remove_pointer(&p_current_argument->expression->type);

struct object * pointed = object_get_pointed_object(p_argument_object);
object_set_unknown(&argument_type, pointed);
type_destroy(&argument_type);
//object_set_unknown(&p_current_argument->expression->type, p_argument_object);
}
}

Expand Down
40 changes: 33 additions & 7 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -33723,7 +33723,13 @@ static int compare_function_arguments2(struct parser_ctx* ctx,
we are passing a pointer to an non const object
everything can happen with this object
*/
object_set_unknown(&p_current_argument->expression->type, p_argument_object);
struct type argument_type =
type_remove_pointer(&p_current_argument->expression->type);

struct object * pointed = object_get_pointed_object(p_argument_object);
object_set_unknown(&argument_type, pointed);
type_destroy(&argument_type);
//object_set_unknown(&p_current_argument->expression->type, p_argument_object);
}
}

Expand Down Expand Up @@ -39745,7 +39751,7 @@ void object_to_non_const()
{
const char* source
=
"void free(void* _Owner p);\n"
"void free(void* _Owner _Opt p);\n"
"struct X\n"
"{\n"
" int i;\n"
Expand All @@ -39755,11 +39761,15 @@ void object_to_non_const()
"int main()\n"
"{\n"
" struct X x = { 0 };\n"
" f(x);\n"
" static_state(x.p, \"null\");\n"
" f(&x); \n"
" static_state(x.p, \"maybe-null\");\n"
" free(x.p);\n"
"}";
assert(compile_without_errors_warnings(true, false /*nullcheck disabled*/, source));
"}\n"
"";


assert(compile_without_errors_warnings(true, true /*nullcheck disabled*/, source));

}
void object_to_const()
Expand Down Expand Up @@ -39814,8 +39824,24 @@ void sizeof_union_test()
"static_assert(sizeof(union X) == 16);";
assert(compile_without_errors_warnings(true, false /*nullcheck disabled*/, source));
}


void not_null_does_not_change()
{
const char* source
=
"struct X { int i; };\n"
"void f(struct X* p);\n"
"void f2(struct X* p);\n"
"\n"
"void delete(struct X* p)\n"
"{\n"
" static_state(p, \"not-null\");\n"
" f(p);\n"
" static_state(p, \"not-null\");\n"
" f2(p);\n"
"}\n"
"";
assert(compile_without_errors_warnings(true, true, source));
}
#endif


32 changes: 26 additions & 6 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3577,7 +3577,7 @@ void object_to_non_const()
{
const char* source
=
"void free(void* _Owner p);\n"
"void free(void* _Owner _Opt p);\n"
"struct X\n"
"{\n"
" int i;\n"
Expand All @@ -3587,11 +3587,15 @@ void object_to_non_const()
"int main()\n"
"{\n"
" struct X x = { 0 };\n"
" f(x);\n"
" static_state(x.p, \"null\");\n"
" f(&x); \n"
" static_state(x.p, \"maybe-null\");\n"
" free(x.p);\n"
"}";
assert(compile_without_errors_warnings(true, false /*nullcheck disabled*/, source));
"}\n"
"";


assert(compile_without_errors_warnings(true, true /*nullcheck disabled*/, source));

}
void object_to_const()
Expand Down Expand Up @@ -3646,7 +3650,23 @@ void sizeof_union_test()
"static_assert(sizeof(union X) == 16);";
assert(compile_without_errors_warnings(true, false /*nullcheck disabled*/, source));
}


void not_null_does_not_change()
{
const char* source
=
"struct X { int i; };\n"
"void f(struct X* p);\n"
"void f2(struct X* p);\n"
"\n"
"void delete(struct X* p)\n"
"{\n"
" static_state(p, \"not-null\");\n"
" f(p);\n"
" static_state(p, \"not-null\");\n"
" f2(p);\n"
"}\n"
"";
assert(compile_without_errors_warnings(true, true, source));
}
#endif

2 changes: 2 additions & 0 deletions src/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ void object_to_non_const(void);
void object_to_const(void);
void union_size(void);
void sizeof_union_test(void);
void not_null_does_not_change(void);

/*end of forward declarations*/

Expand Down Expand Up @@ -473,6 +474,7 @@ g_unit_test_success_count = 0;
object_to_const();
union_size();
sizeof_union_test();
not_null_does_not_change();
return g_unit_test_error_count;

}
Expand Down
2 changes: 1 addition & 1 deletion vc/cprimev3.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>file.c -fanalyzer -Wno-conditional-constant</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>file.c -fanalyzer -nullchecks</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(ProjectDir)/../src/</LocalDebuggerWorkingDirectory>
</PropertyGroup>
Expand Down

0 comments on commit e545b4f

Please sign in to comment.