state->dump (eg.get_ext_state (), true);
return;
}
+ else if (is_special_named_call_p (call, "__analyzer_dump_state", 2))
+ state->impl_call_analyzer_dump_state (call, eg.get_ext_state (),
+ ctxt);
else if (is_setjmp_call_p (call))
{
state->m_region_model->on_setjmp (call, this, ctxt);
return NULL; /* for selftests. */
}
+/* Try to find a state machine named NAME.
+ If found, return true and write its index to *OUT.
+ Otherwise return false. */
+
+bool
+extrinsic_state::get_sm_idx_by_name (const char *name, unsigned *out) const
+{
+ unsigned i;
+ state_machine *sm;
+ FOR_EACH_VEC_ELT (m_checkers, i, sm)
+ if (0 == strcmp (name, sm->get_name ()))
+ {
+ /* Found NAME. */
+ *out = i;
+ return true;
+ }
+
+ /* NAME not found. */
+ return false;
+}
+
/* struct sm_state_map::entry_t. */
int
dest_state.m_region_model->unset_dynamic_extents (reg);
}
+/* Handle calls to "__analyzer_dump_state". */
+
+void
+program_state::impl_call_analyzer_dump_state (const gcall *call,
+ const extrinsic_state &ext_state,
+ region_model_context *ctxt)
+{
+ call_details cd (call, m_region_model, ctxt);
+ const char *sm_name = cd.get_arg_string_literal (0);
+ if (!sm_name)
+ {
+ error_at (call->location, "cannot determine state machine");
+ return;
+ }
+ unsigned sm_idx;
+ if (!ext_state.get_sm_idx_by_name (sm_name, &sm_idx))
+ {
+ error_at (call->location, "unrecognized state machine %qs", sm_name);
+ return;
+ }
+ const sm_state_map *smap = m_checker_states[sm_idx];
+
+ const svalue *sval = cd.get_arg_svalue (1);
+
+ state_machine::state_t state = smap->get_state (sval, ext_state);
+ warning_at (call->location, 0, "state: %qs", state->get_name ());
+}
+
#if CHECKING_P
namespace selftest {
engine *get_engine () const { return m_engine; }
region_model_manager *get_model_manager () const;
+ bool get_sm_idx_by_name (const char *name, unsigned *out) const;
+
private:
/* The state machines. */
auto_delete_vec <state_machine> &m_checkers;
const extrinsic_state &ext_state,
region_model_context *ctxt);
+ void impl_call_analyzer_dump_state (const gcall *call,
+ const extrinsic_state &ext_state,
+ region_model_context *ctxt);
+
/* TODO: lose the pointer here (const-correctness issues?). */
region_model *m_region_model;
auto_delete_vec<sm_state_map> m_checker_states;
return m_model->get_rvalue (arg, m_ctxt);
}
+/* Attempt to get the string literal for argument IDX, or return NULL
+ otherwise.
+ For use when implementing "__analyzer_*" functions that take
+ string literals. */
+
+const char *
+call_details::get_arg_string_literal (unsigned idx) const
+{
+ const svalue *str_arg = get_arg_svalue (idx);
+ if (const region *pointee = str_arg->maybe_get_region ())
+ if (const string_region *string_reg = pointee->dyn_cast_string_region ())
+ {
+ tree string_cst = string_reg->get_string_cst ();
+ return TREE_STRING_POINTER (string_cst);
+ }
+ return NULL;
+}
+
/* Dump a multiline representation of this call to PP. */
void
tree get_arg_tree (unsigned idx) const;
tree get_arg_type (unsigned idx) const;
const svalue *get_arg_svalue (unsigned idx) const;
+ const char *get_arg_string_literal (unsigned idx) const;
void dump_to_pp (pretty_printer *pp, bool simple) const;
void dump (bool simple) const;
@end smallexample
will dump the region_model's state to stderr.
+@smallexample
+__analyzer_dump_state ("malloc", ptr);
+@end smallexample
+
+will emit a warning describing the state of the 2nd argument
+(which can be of any type) with respect to the state machine with
+a name matching the 1st argument (which must be a string literal).
+This is for use when debugging, and may be of use in DejaGnu tests.
+
@smallexample
__analyzer_eval (expr);
@end smallexample
/* Dump the region_model's state to stderr. */
extern void __analyzer_dump_region_model (void);
+/* Emit a warning describing the state of the 2nd argument
+ (which can be of any type) with respect to NAME.
+ This is for use when debugging, and may be of use in DejaGnu tests. */
+extern void __analyzer_dump_state (const char *name, ...);
+
/* Emit a warning with text "TRUE", FALSE" or "UNKNOWN" based on the
truthfulness of the argument. */
extern void __analyzer_eval (int);
--- /dev/null
+/* Verify that __analyzer_dump_state works as expected. */
+
+#include <stdlib.h>
+#include "analyzer-decls.h"
+
+void test_1 (void)
+{
+ void *p = malloc (1024);
+ __analyzer_dump_state ("malloc", p); /* { dg-warning "state: 'unchecked'" } */
+ free (p);
+ __analyzer_dump_state ("malloc", p); /* { dg-warning "state: 'freed'" } */
+ __analyzer_dump_state (NULL, p); /* { dg-error "cannot determine state machine" } */
+ __analyzer_dump_state ("not a state machine", p); /* { dg-error "unrecognized state machine 'not a state machine'" } */
+}