analyzer: getchar has no side-effects
authorDavid Malcolm <dmalcolm@redhat.com>
Wed, 16 Sep 2020 17:12:39 +0000 (13:12 -0400)
committerDavid Malcolm <dmalcolm@redhat.com>
Wed, 16 Sep 2020 22:56:50 +0000 (18:56 -0400)
Seen whilst debugging another issue, where the analyzer was assuming
conservatively that a call to getchar could clobber a global.

This is handled for most of the other stdio functions by the list
in sm-file.cc

gcc/analyzer/ChangeLog:
* region-model.cc (region_model::on_call_pre): Treat getchar as
having no side-effects.

gcc/testsuite/ChangeLog:
* gcc.dg/analyzer/getchar-1.c: New test.

gcc/analyzer/region-model.cc
gcc/testsuite/gcc.dg/analyzer/getchar-1.c [new file with mode: 0644]

index d53272e4332c283e5f978556c63ef0b0289518ce..1312391557db57a9da891aa7d950100f9876b7e5 100644 (file)
@@ -732,6 +732,11 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt)
        return impl_call_calloc (cd);
       else if (is_named_call_p (callee_fndecl, "alloca", call, 1))
        return impl_call_alloca (cd);
+      else if (is_named_call_p (callee_fndecl, "getchar", call, 0))
+       {
+         /* No side-effects (tracking stream state is out-of-scope
+            for the analyzer).  */
+       }
       else if (is_named_call_p (callee_fndecl, "memset", call, 3))
        {
          impl_call_memset (cd);
diff --git a/gcc/testsuite/gcc.dg/analyzer/getchar-1.c b/gcc/testsuite/gcc.dg/analyzer/getchar-1.c
new file mode 100644 (file)
index 0000000..25595e0
--- /dev/null
@@ -0,0 +1,19 @@
+#include <stdio.h>
+#include "analyzer-decls.h"
+
+int test_1 (void)
+{
+  int c = getchar ();
+  return c;
+}
+
+int glob_2;
+int test_2 (void)
+{
+  int c;
+  glob_2 = 42;
+  __analyzer_eval (glob_2 == 42); /* { dg-warning "TRUE" } */
+  c = getchar ();
+  __analyzer_eval (glob_2 == 42); /* { dg-warning "TRUE" } */
+  return c;
+}