| 1 |
/* Classes for saving, deduplicating, and emitting analyzer diagnostics. |
| 2 |
Copyright (C) 2019-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by David Malcolm <[email protected]>. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it |
| 8 |
under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but |
| 13 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#define INCLUDE_VECTOR |
| 23 |
#include "system.h" |
| 24 |
#include "coretypes.h" |
| 25 |
#include "tree.h" |
| 26 |
#include "input.h" |
| 27 |
#include "diagnostic-core.h" |
| 28 |
#include "pretty-print.h" |
| 29 |
#include "gcc-rich-location.h" |
| 30 |
#include "gimple-pretty-print.h" |
| 31 |
#include "function.h" |
| 32 |
#include "diagnostic-event-id.h" |
| 33 |
#include "diagnostic-path.h" |
| 34 |
#include "bitmap.h" |
| 35 |
#include "ordered-hash-map.h" |
| 36 |
#include "analyzer/analyzer.h" |
| 37 |
#include "analyzer/analyzer-logging.h" |
| 38 |
#include "analyzer/sm.h" |
| 39 |
#include "analyzer/pending-diagnostic.h" |
| 40 |
#include "analyzer/diagnostic-manager.h" |
| 41 |
#include "analyzer/call-string.h" |
| 42 |
#include "analyzer/program-point.h" |
| 43 |
#include "analyzer/store.h" |
| 44 |
#include "analyzer/region-model.h" |
| 45 |
#include "analyzer/constraint-manager.h" |
| 46 |
#include "cfg.h" |
| 47 |
#include "basic-block.h" |
| 48 |
#include "gimple.h" |
| 49 |
#include "gimple-iterator.h" |
| 50 |
#include "inlining-iterator.h" |
| 51 |
#include "cgraph.h" |
| 52 |
#include "digraph.h" |
| 53 |
#include "analyzer/supergraph.h" |
| 54 |
#include "analyzer/program-state.h" |
| 55 |
#include "analyzer/exploded-graph.h" |
| 56 |
#include "analyzer/trimmed-graph.h" |
| 57 |
#include "analyzer/feasible-graph.h" |
| 58 |
#include "analyzer/checker-path.h" |
| 59 |
#include "analyzer/reachability.h" |
| 60 |
#include "make-unique.h" |
| 61 |
#include "diagnostic-format-sarif.h" |
| 62 |
|
| 63 |
#if ENABLE_ANALYZER |
| 64 |
|
| 65 |
namespace ana { |
| 66 |
|
| 67 |
class feasible_worklist; |
| 68 |
|
| 69 |
/* State for finding the shortest feasible exploded_path for a |
| 70 |
saved_diagnostic. |
| 71 |
This is shared between all diagnostics, so that we avoid repeating work. */ |
| 72 |
|
| 73 |
class epath_finder |
| 74 |
{ |
| 75 |
public: |
| 76 |
epath_finder (const exploded_graph &eg) |
| 77 |
: m_eg (eg), |
| 78 |
m_sep (NULL) |
| 79 |
{ |
| 80 |
/* This is shared by all diagnostics, but only needed if |
| 81 |
!flag_analyzer_feasibility. */ |
| 82 |
if (!flag_analyzer_feasibility) |
| 83 |
m_sep = new shortest_exploded_paths (eg, eg.get_origin (), |
| 84 |
SPS_FROM_GIVEN_ORIGIN); |
| 85 |
} |
| 86 |
|
| 87 |
~epath_finder () { delete m_sep; } |
| 88 |
|
| 89 |
logger *get_logger () const { return m_eg.get_logger (); } |
| 90 |
|
| 91 |
std::unique_ptr<exploded_path> |
| 92 |
get_best_epath (const exploded_node *target_enode, |
| 93 |
const gimple *target_stmt, |
| 94 |
const pending_diagnostic &pd, |
| 95 |
const char *desc, unsigned diag_idx, |
| 96 |
std::unique_ptr<feasibility_problem> *out_problem); |
| 97 |
|
| 98 |
private: |
| 99 |
DISABLE_COPY_AND_ASSIGN(epath_finder); |
| 100 |
|
| 101 |
std::unique_ptr<exploded_path> |
| 102 |
explore_feasible_paths (const exploded_node *target_enode, |
| 103 |
const gimple *target_stmt, |
| 104 |
const pending_diagnostic &pd, |
| 105 |
const char *desc, unsigned diag_idx); |
| 106 |
bool |
| 107 |
process_worklist_item (feasible_worklist *worklist, |
| 108 |
const trimmed_graph &tg, |
| 109 |
feasible_graph *fg, |
| 110 |
const exploded_node *target_enode, |
| 111 |
const gimple *target_stmt, |
| 112 |
const pending_diagnostic &pd, |
| 113 |
unsigned diag_idx, |
| 114 |
std::unique_ptr<exploded_path> *out_best_path) const; |
| 115 |
void dump_trimmed_graph (const exploded_node *target_enode, |
| 116 |
const char *desc, unsigned diag_idx, |
| 117 |
const trimmed_graph &tg, |
| 118 |
const shortest_paths<eg_traits, exploded_path> &sep); |
| 119 |
void dump_feasible_graph (const exploded_node *target_enode, |
| 120 |
const char *desc, unsigned diag_idx, |
| 121 |
const feasible_graph &fg); |
| 122 |
void dump_feasible_path (const exploded_node *target_enode, |
| 123 |
unsigned diag_idx, |
| 124 |
const feasible_graph &fg, |
| 125 |
const feasible_node &fnode) const; |
| 126 |
|
| 127 |
const exploded_graph &m_eg; |
| 128 |
shortest_exploded_paths *m_sep; |
| 129 |
}; |
| 130 |
|
| 131 |
/* class epath_finder. */ |
| 132 |
|
| 133 |
/* Get the "best" exploded_path for reaching ENODE from the origin, |
| 134 |
returning ownership of it to the caller. |
| 135 |
|
| 136 |
If TARGET_STMT is non-NULL, then check for reaching that stmt |
| 137 |
within ENODE. |
| 138 |
|
| 139 |
Ideally we want to report the shortest feasible path. |
| 140 |
Return NULL if we could not find a feasible path |
| 141 |
(when flag_analyzer_feasibility is true). |
| 142 |
|
| 143 |
If flag_analyzer_feasibility is false, then simply return the |
| 144 |
shortest path. |
| 145 |
|
| 146 |
Use DESC and DIAG_IDX when logging. |
| 147 |
|
| 148 |
Write any feasibility_problem to *OUT_PROBLEM. */ |
| 149 |
|
| 150 |
std::unique_ptr<exploded_path> |
| 151 |
epath_finder::get_best_epath (const exploded_node *enode, |
| 152 |
const gimple *target_stmt, |
| 153 |
const pending_diagnostic &pd, |
| 154 |
const char *desc, unsigned diag_idx, |
| 155 |
std::unique_ptr<feasibility_problem> *out_problem) |
| 156 |
{ |
| 157 |
logger *logger = get_logger (); |
| 158 |
LOG_SCOPE (logger); |
| 159 |
|
| 160 |
unsigned snode_idx = enode->get_supernode ()->m_index; |
| 161 |
if (logger) |
| 162 |
logger->log ("considering %qs at EN: %i, SN: %i (sd: %i)", |
| 163 |
desc, enode->m_index, snode_idx, diag_idx); |
| 164 |
|
| 165 |
/* State-merging means that not every path in the egraph corresponds |
| 166 |
to a feasible one w.r.t. states. |
| 167 |
|
| 168 |
We want to find the shortest feasible path from the origin to ENODE |
| 169 |
in the egraph. */ |
| 170 |
|
| 171 |
if (flag_analyzer_feasibility) |
| 172 |
{ |
| 173 |
/* Attempt to find the shortest feasible path using feasible_graph. */ |
| 174 |
if (logger) |
| 175 |
logger->log ("trying to find shortest feasible path"); |
| 176 |
if (std::unique_ptr<exploded_path> epath |
| 177 |
= explore_feasible_paths (enode, target_stmt, pd, desc, diag_idx)) |
| 178 |
{ |
| 179 |
if (logger) |
| 180 |
logger->log ("accepting %qs at EN: %i, SN: %i (sd: %i)" |
| 181 |
" with feasible path (length: %i)", |
| 182 |
desc, enode->m_index, snode_idx, diag_idx, |
| 183 |
epath->length ()); |
| 184 |
return epath; |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
if (logger) |
| 189 |
logger->log ("rejecting %qs at EN: %i, SN: %i (sd: %i)" |
| 190 |
" due to not finding feasible path", |
| 191 |
desc, enode->m_index, snode_idx, diag_idx); |
| 192 |
return NULL; |
| 193 |
} |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
/* As a crude approximation to shortest feasible path, simply find |
| 198 |
the shortest path, and note whether it is feasible. |
| 199 |
There could be longer feasible paths within the egraph, so this |
| 200 |
approach would lead to diagnostics being falsely rejected |
| 201 |
(PR analyzer/96374). */ |
| 202 |
if (logger) |
| 203 |
logger->log ("trying to find shortest path ignoring feasibility"); |
| 204 |
gcc_assert (m_sep); |
| 205 |
std::unique_ptr<exploded_path> epath |
| 206 |
= make_unique<exploded_path> (m_sep->get_shortest_path (enode)); |
| 207 |
if (epath->feasible_p (logger, out_problem, m_eg.get_engine (), &m_eg)) |
| 208 |
{ |
| 209 |
if (logger) |
| 210 |
logger->log ("accepting %qs at EN: %i, SN: %i (sn: %i)" |
| 211 |
" with feasible path (length: %i)", |
| 212 |
desc, enode->m_index, snode_idx, diag_idx, |
| 213 |
epath->length ()); |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
if (logger) |
| 218 |
logger->log ("accepting %qs at EN: %i, SN: %i (sn: %i) (length: %i)" |
| 219 |
" despite infeasible path (due to %qs)", |
| 220 |
desc, enode->m_index, snode_idx, diag_idx, |
| 221 |
epath->length (), |
| 222 |
"-fno-analyzer-feasibility"); |
| 223 |
} |
| 224 |
return epath; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/* A class for managing the worklist of feasible_nodes in |
| 229 |
epath_finder::explore_feasible_paths, prioritizing them |
| 230 |
so that shorter paths appear earlier in the queue. */ |
| 231 |
|
| 232 |
class feasible_worklist |
| 233 |
{ |
| 234 |
public: |
| 235 |
feasible_worklist (const shortest_paths<eg_traits, exploded_path> &sep) |
| 236 |
: m_queue (key_t (*this, NULL)), |
| 237 |
m_sep (sep) |
| 238 |
{ |
| 239 |
} |
| 240 |
|
| 241 |
feasible_node *take_next () { return m_queue.extract_min (); } |
| 242 |
|
| 243 |
void add_node (feasible_node *fnode) |
| 244 |
{ |
| 245 |
m_queue.insert (key_t (*this, fnode), fnode); |
| 246 |
} |
| 247 |
|
| 248 |
private: |
| 249 |
struct key_t |
| 250 |
{ |
| 251 |
key_t (const feasible_worklist &w, feasible_node *fnode) |
| 252 |
: m_worklist (w), m_fnode (fnode) |
| 253 |
{} |
| 254 |
|
| 255 |
bool operator< (const key_t &other) const |
| 256 |
{ |
| 257 |
return cmp (*this, other) < 0; |
| 258 |
} |
| 259 |
|
| 260 |
bool operator== (const key_t &other) const |
| 261 |
{ |
| 262 |
return cmp (*this, other) == 0; |
| 263 |
} |
| 264 |
|
| 265 |
bool operator> (const key_t &other) const |
| 266 |
{ |
| 267 |
return !(*this == other || *this < other); |
| 268 |
} |
| 269 |
|
| 270 |
private: |
| 271 |
static int cmp (const key_t &ka, const key_t &kb) |
| 272 |
{ |
| 273 |
/* Choose the node for which if the remaining path were feasible, |
| 274 |
it would be the shortest path (summing the length of the |
| 275 |
known-feasible path so far with that of the remaining |
| 276 |
possibly-feasible path). */ |
| 277 |
int ca = ka.m_worklist.get_estimated_cost (ka.m_fnode); |
| 278 |
int cb = kb.m_worklist.get_estimated_cost (kb.m_fnode); |
| 279 |
return ca - cb; |
| 280 |
} |
| 281 |
|
| 282 |
const feasible_worklist &m_worklist; |
| 283 |
feasible_node *m_fnode; |
| 284 |
}; |
| 285 |
|
| 286 |
/* Get the estimated length of a path involving FNODE from |
| 287 |
the origin to the target enode. |
| 288 |
Sum the length of the known-feasible path so far with |
| 289 |
that of the remaining possibly-feasible path. */ |
| 290 |
|
| 291 |
int get_estimated_cost (const feasible_node *fnode) const |
| 292 |
{ |
| 293 |
unsigned length_so_far = fnode->get_path_length (); |
| 294 |
int shortest_remaining_path |
| 295 |
= m_sep.get_shortest_distance (fnode->get_inner_node ()); |
| 296 |
|
| 297 |
gcc_assert (shortest_remaining_path >= 0); |
| 298 |
/* This should be true since we're only exploring nodes within |
| 299 |
the trimmed graph (and we anticipate it being much smaller |
| 300 |
than this, and thus not overflowing the sum). */ |
| 301 |
gcc_assert (shortest_remaining_path < INT_MAX); |
| 302 |
|
| 303 |
return length_so_far + shortest_remaining_path; |
| 304 |
} |
| 305 |
|
| 306 |
/* Priority queue, backed by a fibonacci_heap. */ |
| 307 |
typedef fibonacci_heap<key_t, feasible_node> queue_t; |
| 308 |
queue_t m_queue; |
| 309 |
const shortest_paths<eg_traits, exploded_path> &m_sep; |
| 310 |
}; |
| 311 |
|
| 312 |
/* When we're building the exploded graph we want to simplify |
| 313 |
overly-complicated symbolic values down to "UNKNOWN" to try to avoid |
| 314 |
state explosions and unbounded chains of exploration. |
| 315 |
|
| 316 |
However, when we're building the feasibility graph for a diagnostic |
| 317 |
(actually a tree), we don't want UNKNOWN values, as conditions on them |
| 318 |
are also unknown: we don't want to have a contradiction such as a path |
| 319 |
where (VAL != 0) and then (VAL == 0) along the same path. |
| 320 |
|
| 321 |
Hence this is an RAII class for temporarily disabling complexity-checking |
| 322 |
in the region_model_manager, for use within |
| 323 |
epath_finder::explore_feasible_paths. |
| 324 |
|
| 325 |
We also disable the creation of unknown_svalue instances during feasibility |
| 326 |
checking, instead creating unique svalues, to avoid paradoxes in paths. */ |
| 327 |
|
| 328 |
class auto_checking_feasibility |
| 329 |
{ |
| 330 |
public: |
| 331 |
auto_checking_feasibility (region_model_manager *mgr) : m_mgr (mgr) |
| 332 |
{ |
| 333 |
m_mgr->begin_checking_feasibility (); |
| 334 |
} |
| 335 |
~auto_checking_feasibility () |
| 336 |
{ |
| 337 |
m_mgr->end_checking_feasibility (); |
| 338 |
} |
| 339 |
private: |
| 340 |
region_model_manager *m_mgr; |
| 341 |
}; |
| 342 |
|
| 343 |
/* Attempt to find the shortest feasible path from the origin to |
| 344 |
TARGET_ENODE by iteratively building a feasible_graph, in which |
| 345 |
every path to a feasible_node is feasible by construction. |
| 346 |
|
| 347 |
If TARGET_STMT is non-NULL, then check for reaching that stmt |
| 348 |
within TARGET_ENODE. |
| 349 |
|
| 350 |
We effectively explore the tree of feasible paths in order of shortest |
| 351 |
path until we either find a feasible path to TARGET_ENODE, or hit |
| 352 |
a limit and give up. |
| 353 |
|
| 354 |
Preliminaries: |
| 355 |
- Find the shortest path from each node to the TARGET_ENODE (without |
| 356 |
checking feasibility), so that we can prioritize our worklist. |
| 357 |
- Construct a trimmed_graph: the subset of nodes/edges that |
| 358 |
are on a path that eventually reaches TARGET_ENODE. We will only need |
| 359 |
to consider these when considering the shortest feasible path. |
| 360 |
|
| 361 |
Build a feasible_graph, in which every path to a feasible_node |
| 362 |
is feasible by construction. |
| 363 |
We use a worklist to flatten the exploration into an iteration. |
| 364 |
Starting at the origin, find feasible out-edges within the trimmed graph. |
| 365 |
At each stage, choose the node for which if the remaining path were feasible, |
| 366 |
it would be the shortest path (summing the length of the known-feasible path |
| 367 |
so far with that of the remaining possibly-feasible path). |
| 368 |
This way, the first feasible path we find to TARGET_ENODE is the shortest. |
| 369 |
We start by trying the shortest possible path, but if that fails, |
| 370 |
we explore progressively longer paths, eventually trying iterations through |
| 371 |
loops. The exploration is captured in the feasible_graph, which can be |
| 372 |
dumped as a .dot file to visualize the exploration. The indices of the |
| 373 |
feasible_nodes show the order in which they were created. |
| 374 |
|
| 375 |
This is something of a brute-force approach, but the trimmed_graph |
| 376 |
hopefully keeps the complexity manageable. |
| 377 |
|
| 378 |
Terminate with failure when the number of infeasible edges exceeds |
| 379 |
a threshold (--param=analyzer-max-infeasible-edges=). |
| 380 |
This is guaranteed to eventually lead to terminatation, as |
| 381 |
we can't keep creating feasible nodes without eventually |
| 382 |
either reaching an infeasible edge, or reaching the |
| 383 |
TARGET_ENODE. Specifically, there can't be a cycle of |
| 384 |
feasible edges that doesn't reach the target_enode without |
| 385 |
an out-edge that either fails feasibility or gets closer |
| 386 |
to the TARGET_ENODE: on each iteration we are either: |
| 387 |
- effectively getting closer to the TARGET_ENODE (which can't |
| 388 |
continue forever without reaching the target), or |
| 389 |
- getting monotonically closer to the termination threshold. */ |
| 390 |
|
| 391 |
std::unique_ptr<exploded_path> |
| 392 |
epath_finder::explore_feasible_paths (const exploded_node *target_enode, |
| 393 |
const gimple *target_stmt, |
| 394 |
const pending_diagnostic &pd, |
| 395 |
const char *desc, unsigned diag_idx) |
| 396 |
{ |
| 397 |
logger *logger = get_logger (); |
| 398 |
LOG_SCOPE (logger); |
| 399 |
|
| 400 |
region_model_manager *mgr = m_eg.get_engine ()->get_model_manager (); |
| 401 |
|
| 402 |
/* Determine the shortest path to TARGET_ENODE from each node in |
| 403 |
the exploded graph. */ |
| 404 |
shortest_paths<eg_traits, exploded_path> sep |
| 405 |
(m_eg, target_enode, SPS_TO_GIVEN_TARGET); |
| 406 |
|
| 407 |
/* Construct a trimmed_graph: the subset of nodes/edges that |
| 408 |
are on a path that eventually reaches TARGET_ENODE. |
| 409 |
We only need to consider these when considering the shortest |
| 410 |
feasible path. */ |
| 411 |
trimmed_graph tg (m_eg, target_enode); |
| 412 |
|
| 413 |
if (flag_dump_analyzer_feasibility) |
| 414 |
dump_trimmed_graph (target_enode, desc, diag_idx, tg, sep); |
| 415 |
|
| 416 |
feasible_graph fg; |
| 417 |
feasible_worklist worklist (sep); |
| 418 |
|
| 419 |
/* Populate the worklist with the origin node. */ |
| 420 |
{ |
| 421 |
feasibility_state init_state (mgr, m_eg.get_supergraph ()); |
| 422 |
feasible_node *origin = fg.add_node (m_eg.get_origin (), init_state, 0); |
| 423 |
worklist.add_node (origin); |
| 424 |
} |
| 425 |
|
| 426 |
/* Iteratively explore the tree of feasible paths in order of shortest |
| 427 |
path until we either find a feasible path to TARGET_ENODE, or hit |
| 428 |
a limit. */ |
| 429 |
|
| 430 |
/* Set this if we find a feasible path to TARGET_ENODE. */ |
| 431 |
std::unique_ptr<exploded_path> best_path = NULL; |
| 432 |
|
| 433 |
{ |
| 434 |
auto_checking_feasibility sentinel (mgr); |
| 435 |
|
| 436 |
while (process_worklist_item (&worklist, tg, &fg, target_enode, target_stmt, |
| 437 |
pd, diag_idx, &best_path)) |
| 438 |
{ |
| 439 |
/* Empty; the work is done within process_worklist_item. */ |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
if (logger) |
| 444 |
{ |
| 445 |
logger->log ("tg for sd: %i:", diag_idx); |
| 446 |
logger->inc_indent (); |
| 447 |
tg.log_stats (logger); |
| 448 |
logger->dec_indent (); |
| 449 |
|
| 450 |
logger->log ("fg for sd: %i:", diag_idx); |
| 451 |
logger->inc_indent (); |
| 452 |
fg.log_stats (logger); |
| 453 |
logger->dec_indent (); |
| 454 |
} |
| 455 |
|
| 456 |
/* Dump the feasible_graph. */ |
| 457 |
if (flag_dump_analyzer_feasibility) |
| 458 |
dump_feasible_graph (target_enode, desc, diag_idx, fg); |
| 459 |
|
| 460 |
return best_path; |
| 461 |
} |
| 462 |
|
| 463 |
/* Process the next item in WORKLIST, potentially adding new items |
| 464 |
based on feasible out-edges, and extending FG accordingly. |
| 465 |
Use TG to ignore out-edges that don't lead to TARGET_ENODE. |
| 466 |
Return true if the worklist processing should continue. |
| 467 |
Return false if the processing of the worklist should stop |
| 468 |
(either due to reaching TARGET_ENODE, or hitting a limit). |
| 469 |
Write to *OUT_BEST_PATH if stopping due to finding a feasible path |
| 470 |
to TARGET_ENODE. |
| 471 |
Use PD to provide additional restrictions on feasibility of |
| 472 |
the final path in the feasible_graph before converting to |
| 473 |
an exploded_path. */ |
| 474 |
|
| 475 |
bool |
| 476 |
epath_finder:: |
| 477 |
process_worklist_item (feasible_worklist *worklist, |
| 478 |
const trimmed_graph &tg, |
| 479 |
feasible_graph *fg, |
| 480 |
const exploded_node *target_enode, |
| 481 |
const gimple *target_stmt, |
| 482 |
const pending_diagnostic &pd, |
| 483 |
unsigned diag_idx, |
| 484 |
std::unique_ptr<exploded_path> *out_best_path) const |
| 485 |
{ |
| 486 |
logger *logger = get_logger (); |
| 487 |
|
| 488 |
feasible_node *fnode = worklist->take_next (); |
| 489 |
if (!fnode) |
| 490 |
{ |
| 491 |
if (logger) |
| 492 |
logger->log ("drained worklist for sd: %i" |
| 493 |
" without finding feasible path", |
| 494 |
diag_idx); |
| 495 |
return false; |
| 496 |
} |
| 497 |
|
| 498 |
log_scope s (logger, "fg worklist item", |
| 499 |
"considering FN: %i (EN: %i) for sd: %i", |
| 500 |
fnode->get_index (), fnode->get_inner_node ()->m_index, |
| 501 |
diag_idx); |
| 502 |
|
| 503 |
/* Iterate through all out-edges from this item. */ |
| 504 |
unsigned i; |
| 505 |
exploded_edge *succ_eedge; |
| 506 |
FOR_EACH_VEC_ELT (fnode->get_inner_node ()->m_succs, i, succ_eedge) |
| 507 |
{ |
| 508 |
log_scope s (logger, "edge", "considering edge: EN:%i -> EN:%i", |
| 509 |
succ_eedge->m_src->m_index, |
| 510 |
succ_eedge->m_dest->m_index); |
| 511 |
/* Reject edges that aren't in the trimmed graph. */ |
| 512 |
if (!tg.contains_p (succ_eedge)) |
| 513 |
{ |
| 514 |
if (logger) |
| 515 |
logger->log ("rejecting: not in trimmed graph"); |
| 516 |
continue; |
| 517 |
} |
| 518 |
|
| 519 |
feasibility_state succ_state (fnode->get_state ()); |
| 520 |
std::unique_ptr<rejected_constraint> rc; |
| 521 |
if (succ_state.maybe_update_for_edge (logger, succ_eedge, nullptr, &rc)) |
| 522 |
{ |
| 523 |
gcc_assert (rc == NULL); |
| 524 |
feasible_node *succ_fnode |
| 525 |
= fg->add_node (succ_eedge->m_dest, |
| 526 |
succ_state, |
| 527 |
fnode->get_path_length () + 1); |
| 528 |
if (logger) |
| 529 |
logger->log ("accepting as FN: %i", succ_fnode->get_index ()); |
| 530 |
fg->add_edge (new feasible_edge (fnode, succ_fnode, succ_eedge)); |
| 531 |
|
| 532 |
/* Have we reached TARGET_ENODE? */ |
| 533 |
if (succ_fnode->get_inner_node () == target_enode) |
| 534 |
{ |
| 535 |
if (logger) |
| 536 |
logger->log ("success: got feasible path to EN: %i (sd: %i)" |
| 537 |
" (length: %i)", |
| 538 |
target_enode->m_index, diag_idx, |
| 539 |
succ_fnode->get_path_length ()); |
| 540 |
if (!pd.check_valid_fpath_p (*succ_fnode, target_stmt)) |
| 541 |
{ |
| 542 |
if (logger) |
| 543 |
logger->log ("rejecting feasible path due to" |
| 544 |
" pending_diagnostic"); |
| 545 |
return false; |
| 546 |
} |
| 547 |
*out_best_path = fg->make_epath (succ_fnode); |
| 548 |
if (flag_dump_analyzer_feasibility) |
| 549 |
dump_feasible_path (target_enode, diag_idx, *fg, *succ_fnode); |
| 550 |
|
| 551 |
/* Success: stop the worklist iteration. */ |
| 552 |
return false; |
| 553 |
} |
| 554 |
else |
| 555 |
worklist->add_node (succ_fnode); |
| 556 |
} |
| 557 |
else |
| 558 |
{ |
| 559 |
if (logger) |
| 560 |
logger->log ("infeasible"); |
| 561 |
gcc_assert (rc); |
| 562 |
fg->add_feasibility_problem (fnode, |
| 563 |
succ_eedge, |
| 564 |
std::move (rc)); |
| 565 |
|
| 566 |
/* Give up if there have been too many infeasible edges. */ |
| 567 |
if (fg->get_num_infeasible () |
| 568 |
> (unsigned)param_analyzer_max_infeasible_edges) |
| 569 |
{ |
| 570 |
if (logger) |
| 571 |
logger->log ("too many infeasible edges (%i); giving up", |
| 572 |
fg->get_num_infeasible ()); |
| 573 |
return false; |
| 574 |
} |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
/* Continue the worklist iteration. */ |
| 579 |
return true; |
| 580 |
} |
| 581 |
|
| 582 |
/* Helper class for epath_finder::dump_trimmed_graph |
| 583 |
to dump extra per-node information. |
| 584 |
Use SEP to add the length of the shortest path from each |
| 585 |
node to the target node to each node's dump. */ |
| 586 |
|
| 587 |
class dump_eg_with_shortest_path : public eg_traits::dump_args_t |
| 588 |
{ |
| 589 |
public: |
| 590 |
dump_eg_with_shortest_path |
| 591 |
(const exploded_graph &eg, |
| 592 |
const shortest_paths<eg_traits, exploded_path> &sep) |
| 593 |
: dump_args_t (eg), |
| 594 |
m_sep (sep) |
| 595 |
{ |
| 596 |
} |
| 597 |
|
| 598 |
void dump_extra_info (const exploded_node *enode, |
| 599 |
pretty_printer *pp) const final override |
| 600 |
{ |
| 601 |
pp_printf (pp, "sp: %i", m_sep.get_shortest_path (enode).length ()); |
| 602 |
pp_newline (pp); |
| 603 |
} |
| 604 |
|
| 605 |
private: |
| 606 |
const shortest_paths<eg_traits, exploded_path> &m_sep; |
| 607 |
}; |
| 608 |
|
| 609 |
/* Dump TG to "BASE_NAME.DESC.DIAG_IDX.to-enN.tg.dot", |
| 610 |
annotating each node with the length of the shortest path |
| 611 |
from that node to TARGET_ENODE (using SEP). */ |
| 612 |
|
| 613 |
void |
| 614 |
epath_finder:: |
| 615 |
dump_trimmed_graph (const exploded_node *target_enode, |
| 616 |
const char *desc, unsigned diag_idx, |
| 617 |
const trimmed_graph &tg, |
| 618 |
const shortest_paths<eg_traits, exploded_path> &sep) |
| 619 |
{ |
| 620 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 621 |
dump_eg_with_shortest_path inner_args (m_eg, sep); |
| 622 |
trimmed_graph::dump_args_t args (inner_args); |
| 623 |
pretty_printer pp; |
| 624 |
pp_printf (&pp, "%s.%s.%i.to-en%i.tg.dot", |
| 625 |
dump_base_name, desc, diag_idx, target_enode->m_index); |
| 626 |
char *filename = xstrdup (pp_formatted_text (&pp)); |
| 627 |
tg.dump_dot (filename, NULL, args); |
| 628 |
free (filename); |
| 629 |
} |
| 630 |
|
| 631 |
/* Dump FG to "BASE_NAME.DESC.DIAG_IDX.to-enN.fg.dot". */ |
| 632 |
|
| 633 |
void |
| 634 |
epath_finder::dump_feasible_graph (const exploded_node *target_enode, |
| 635 |
const char *desc, unsigned diag_idx, |
| 636 |
const feasible_graph &fg) |
| 637 |
{ |
| 638 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 639 |
exploded_graph::dump_args_t inner_args (m_eg); |
| 640 |
feasible_graph::dump_args_t args (inner_args); |
| 641 |
pretty_printer pp; |
| 642 |
pp_printf (&pp, "%s.%s.%i.to-en%i.fg.dot", |
| 643 |
dump_base_name, desc, diag_idx, target_enode->m_index); |
| 644 |
char *filename = xstrdup (pp_formatted_text (&pp)); |
| 645 |
fg.dump_dot (filename, NULL, args); |
| 646 |
free (filename); |
| 647 |
} |
| 648 |
|
| 649 |
/* Dump the path to FNODE to "BASE_NAME.DIAG_IDX.to-enN.fpath.txt". */ |
| 650 |
|
| 651 |
void |
| 652 |
epath_finder::dump_feasible_path (const exploded_node *target_enode, |
| 653 |
unsigned diag_idx, |
| 654 |
const feasible_graph &fg, |
| 655 |
const feasible_node &fnode) const |
| 656 |
{ |
| 657 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 658 |
pretty_printer pp; |
| 659 |
pp_printf (&pp, "%s.%i.to-en%i.fpath.txt", |
| 660 |
dump_base_name, diag_idx, target_enode->m_index); |
| 661 |
char *filename = xstrdup (pp_formatted_text (&pp)); |
| 662 |
fg.dump_feasible_path (fnode, filename); |
| 663 |
free (filename); |
| 664 |
} |
| 665 |
|
| 666 |
/* class saved_diagnostic. */ |
| 667 |
|
| 668 |
/* saved_diagnostic's ctor. */ |
| 669 |
|
| 670 |
saved_diagnostic::saved_diagnostic (const state_machine *sm, |
| 671 |
const pending_location &ploc, |
| 672 |
tree var, |
| 673 |
const svalue *sval, |
| 674 |
state_machine::state_t state, |
| 675 |
std::unique_ptr<pending_diagnostic> d, |
| 676 |
unsigned idx) |
| 677 |
: m_sm (sm), m_enode (ploc.m_enode), m_snode (ploc.m_snode), |
| 678 |
m_stmt (ploc.m_stmt), |
| 679 |
/* stmt_finder could be on-stack; we want our own copy that can |
| 680 |
outlive that. */ |
| 681 |
m_stmt_finder (ploc.m_finder ? ploc.m_finder->clone () : nullptr), |
| 682 |
m_loc (ploc.m_loc), |
| 683 |
m_var (var), m_sval (sval), m_state (state), |
| 684 |
m_d (std::move (d)), m_trailing_eedge (nullptr), |
| 685 |
m_idx (idx), |
| 686 |
m_best_epath (nullptr), m_problem (nullptr), |
| 687 |
m_notes () |
| 688 |
{ |
| 689 |
/* We must have an enode in order to be able to look for paths |
| 690 |
through the exploded_graph to this diagnostic. */ |
| 691 |
gcc_assert (m_enode); |
| 692 |
} |
| 693 |
|
| 694 |
bool |
| 695 |
saved_diagnostic::operator== (const saved_diagnostic &other) const |
| 696 |
{ |
| 697 |
if (m_notes.length () != other.m_notes.length ()) |
| 698 |
return false; |
| 699 |
for (unsigned i = 0; i < m_notes.length (); i++) |
| 700 |
if (!m_notes[i]->equal_p (*other.m_notes[i])) |
| 701 |
return false; |
| 702 |
return (m_sm == other.m_sm |
| 703 |
/* We don't compare m_enode. */ |
| 704 |
&& m_snode == other.m_snode |
| 705 |
&& m_stmt == other.m_stmt |
| 706 |
/* We don't compare m_stmt_finder. */ |
| 707 |
&& m_loc == other.m_loc |
| 708 |
&& pending_diagnostic::same_tree_p (m_var, other.m_var) |
| 709 |
&& m_state == other.m_state |
| 710 |
&& m_d->equal_p (*other.m_d) |
| 711 |
&& m_trailing_eedge == other.m_trailing_eedge); |
| 712 |
} |
| 713 |
|
| 714 |
/* Add PN to this diagnostic, taking ownership of it. */ |
| 715 |
|
| 716 |
void |
| 717 |
saved_diagnostic::add_note (std::unique_ptr<pending_note> pn) |
| 718 |
{ |
| 719 |
gcc_assert (pn); |
| 720 |
m_notes.safe_push (pn.release ()); |
| 721 |
} |
| 722 |
|
| 723 |
/* Add EVENT to this diagnostic. */ |
| 724 |
|
| 725 |
void |
| 726 |
saved_diagnostic::add_event (std::unique_ptr<checker_event> event) |
| 727 |
{ |
| 728 |
gcc_assert (event); |
| 729 |
m_saved_events.safe_push (event.release ()); |
| 730 |
} |
| 731 |
|
| 732 |
/* Return a new json::object of the form |
| 733 |
{"sm": optional str, |
| 734 |
"enode": int, |
| 735 |
"snode": int, |
| 736 |
"sval": optional str, |
| 737 |
"state": optional str, |
| 738 |
"path_length": optional int, |
| 739 |
"pending_diagnostic": str, |
| 740 |
"idx": int}. */ |
| 741 |
|
| 742 |
std::unique_ptr<json::object> |
| 743 |
saved_diagnostic::to_json () const |
| 744 |
{ |
| 745 |
auto sd_obj = ::make_unique<json::object> (); |
| 746 |
|
| 747 |
if (m_sm) |
| 748 |
sd_obj->set_string ("sm", m_sm->get_name ()); |
| 749 |
sd_obj->set_integer ("enode", m_enode->m_index); |
| 750 |
sd_obj->set_integer ("snode", m_snode->m_index); |
| 751 |
if (m_sval) |
| 752 |
sd_obj->set ("sval", m_sval->to_json ()); |
| 753 |
if (m_state) |
| 754 |
sd_obj->set ("state", m_state->to_json ()); |
| 755 |
if (m_best_epath) |
| 756 |
sd_obj->set_integer ("path_length", get_epath_length ()); |
| 757 |
sd_obj->set_string ("pending_diagnostic", m_d->get_kind ()); |
| 758 |
sd_obj->set_integer ("idx", m_idx); |
| 759 |
|
| 760 |
/* We're not yet JSONifying the following fields: |
| 761 |
const gimple *m_stmt; |
| 762 |
stmt_finder *m_stmt_finder; |
| 763 |
tree m_var; |
| 764 |
exploded_edge *m_trailing_eedge; |
| 765 |
enum status m_status; |
| 766 |
feasibility_problem *m_problem; |
| 767 |
auto_delete_vec <pending_note> m_notes; |
| 768 |
*/ |
| 769 |
|
| 770 |
return sd_obj; |
| 771 |
} |
| 772 |
|
| 773 |
/* Dump this to PP in a form suitable for use as an id in .dot output. */ |
| 774 |
|
| 775 |
void |
| 776 |
saved_diagnostic::dump_dot_id (pretty_printer *pp) const |
| 777 |
{ |
| 778 |
pp_printf (pp, "sd_%i", m_idx); |
| 779 |
} |
| 780 |
|
| 781 |
/* Dump this to PP in a form suitable for use as a node in .dot output. */ |
| 782 |
|
| 783 |
void |
| 784 |
saved_diagnostic::dump_as_dot_node (pretty_printer *pp) const |
| 785 |
{ |
| 786 |
dump_dot_id (pp); |
| 787 |
pp_printf (pp, |
| 788 |
" [shape=none,margin=0,style=filled,fillcolor=\"red\",label=\""); |
| 789 |
pp_write_text_to_stream (pp); |
| 790 |
|
| 791 |
/* Node label. */ |
| 792 |
pp_printf (pp, "DIAGNOSTIC: %s (sd: %i)\n", |
| 793 |
m_d->get_kind (), m_idx); |
| 794 |
if (m_sm) |
| 795 |
{ |
| 796 |
pp_printf (pp, "sm: %s", m_sm->get_name ()); |
| 797 |
if (m_state) |
| 798 |
{ |
| 799 |
pp_printf (pp, "; state: "); |
| 800 |
m_state->dump_to_pp (pp); |
| 801 |
} |
| 802 |
pp_newline (pp); |
| 803 |
} |
| 804 |
if (m_stmt) |
| 805 |
{ |
| 806 |
pp_string (pp, "stmt: "); |
| 807 |
pp_gimple_stmt_1 (pp, m_stmt, 0, (dump_flags_t)0); |
| 808 |
pp_newline (pp); |
| 809 |
} |
| 810 |
if (m_var) |
| 811 |
pp_printf (pp, "var: %qE\n", m_var); |
| 812 |
if (m_sval) |
| 813 |
{ |
| 814 |
pp_string (pp, "sval: "); |
| 815 |
m_sval->dump_to_pp (pp, true); |
| 816 |
pp_newline (pp); |
| 817 |
} |
| 818 |
if (m_best_epath) |
| 819 |
pp_printf (pp, "path length: %i\n", get_epath_length ()); |
| 820 |
|
| 821 |
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true); |
| 822 |
pp_string (pp, "\"];\n\n"); |
| 823 |
|
| 824 |
/* Show links to duplicates. */ |
| 825 |
for (auto iter : m_duplicates) |
| 826 |
{ |
| 827 |
dump_dot_id (pp); |
| 828 |
pp_string (pp, " -> "); |
| 829 |
iter->dump_dot_id (pp); |
| 830 |
pp_string (pp, " [style=\"dotted\" arrowhead=\"none\"];"); |
| 831 |
pp_newline (pp); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
/* Use PF to find the best exploded_path for this saved_diagnostic, |
| 836 |
and store it in m_best_epath. |
| 837 |
If we don't have a specific location in m_loc and m_stmt is still NULL, |
| 838 |
use m_stmt_finder on the epath to populate m_stmt. |
| 839 |
Return true if a best path was found. */ |
| 840 |
|
| 841 |
bool |
| 842 |
saved_diagnostic::calc_best_epath (epath_finder *pf) |
| 843 |
{ |
| 844 |
logger *logger = pf->get_logger (); |
| 845 |
LOG_SCOPE (logger); |
| 846 |
m_problem = NULL; |
| 847 |
|
| 848 |
m_best_epath = pf->get_best_epath (m_enode, m_stmt, |
| 849 |
*m_d, m_d->get_kind (), m_idx, |
| 850 |
&m_problem); |
| 851 |
|
| 852 |
/* Handle failure to find a feasible path. */ |
| 853 |
if (m_best_epath == NULL) |
| 854 |
return false; |
| 855 |
|
| 856 |
gcc_assert (m_best_epath); |
| 857 |
if (m_loc == UNKNOWN_LOCATION) |
| 858 |
{ |
| 859 |
if (m_stmt == NULL) |
| 860 |
{ |
| 861 |
gcc_assert (m_stmt_finder); |
| 862 |
m_stmt = m_stmt_finder->find_stmt (*m_best_epath); |
| 863 |
} |
| 864 |
gcc_assert (m_stmt); |
| 865 |
} |
| 866 |
|
| 867 |
return true; |
| 868 |
} |
| 869 |
|
| 870 |
unsigned |
| 871 |
saved_diagnostic::get_epath_length () const |
| 872 |
{ |
| 873 |
gcc_assert (m_best_epath); |
| 874 |
return m_best_epath->length (); |
| 875 |
} |
| 876 |
|
| 877 |
/* Record that OTHER (and its duplicates) are duplicates |
| 878 |
of this saved_diagnostic. */ |
| 879 |
|
| 880 |
void |
| 881 |
saved_diagnostic::add_duplicate (saved_diagnostic *other) |
| 882 |
{ |
| 883 |
gcc_assert (other); |
| 884 |
m_duplicates.reserve (m_duplicates.length () |
| 885 |
+ other->m_duplicates.length () |
| 886 |
+ 1); |
| 887 |
m_duplicates.splice (other->m_duplicates); |
| 888 |
other->m_duplicates.truncate (0); |
| 889 |
m_duplicates.safe_push (other); |
| 890 |
} |
| 891 |
|
| 892 |
/* Walk up the sedges of each of the two paths. |
| 893 |
If the two sequences of sedges do not perfectly correspond, |
| 894 |
then paths are incompatible. |
| 895 |
If there is at least one sedge that either cannot be paired up |
| 896 |
or its counterpart is not equal, then the paths are incompatible |
| 897 |
and this function returns FALSE. |
| 898 |
Otherwise return TRUE. |
| 899 |
|
| 900 |
Incompatible paths: |
| 901 |
|
| 902 |
<cond Y> |
| 903 |
/ \ |
| 904 |
/ \ |
| 905 |
true false |
| 906 |
| | |
| 907 |
... ... |
| 908 |
| | |
| 909 |
... stmt x |
| 910 |
| |
| 911 |
stmt x |
| 912 |
|
| 913 |
Both LHS_PATH and RHS_PATH final enodes should be |
| 914 |
over the same gimple statement. */ |
| 915 |
|
| 916 |
static bool |
| 917 |
compatible_epath_p (const exploded_path *lhs_path, |
| 918 |
const exploded_path *rhs_path) |
| 919 |
{ |
| 920 |
gcc_assert (lhs_path); |
| 921 |
gcc_assert (rhs_path); |
| 922 |
gcc_assert (rhs_path->length () > 0); |
| 923 |
gcc_assert (rhs_path->length () > 0); |
| 924 |
int lhs_eedge_idx = lhs_path->length () - 1; |
| 925 |
int rhs_eedge_idx = rhs_path->length () - 1; |
| 926 |
const exploded_edge *lhs_eedge; |
| 927 |
const exploded_edge *rhs_eedge; |
| 928 |
|
| 929 |
while (lhs_eedge_idx >= 0 && rhs_eedge_idx >= 0) |
| 930 |
{ |
| 931 |
while (lhs_eedge_idx >= 0) |
| 932 |
{ |
| 933 |
/* Find LHS_PATH's next superedge. */ |
| 934 |
lhs_eedge = lhs_path->m_edges[lhs_eedge_idx]; |
| 935 |
if (lhs_eedge->m_sedge) |
| 936 |
break; |
| 937 |
else |
| 938 |
lhs_eedge_idx--; |
| 939 |
} |
| 940 |
while (rhs_eedge_idx >= 0) |
| 941 |
{ |
| 942 |
/* Find RHS_PATH's next superedge. */ |
| 943 |
rhs_eedge = rhs_path->m_edges[rhs_eedge_idx]; |
| 944 |
if (rhs_eedge->m_sedge) |
| 945 |
break; |
| 946 |
else |
| 947 |
rhs_eedge_idx--; |
| 948 |
} |
| 949 |
|
| 950 |
if (lhs_eedge->m_sedge && rhs_eedge->m_sedge) |
| 951 |
{ |
| 952 |
if (lhs_eedge->m_sedge != rhs_eedge->m_sedge) |
| 953 |
/* Both superedges do not match. |
| 954 |
Superedges are not dependent on the exploded path, so even |
| 955 |
different epaths will have similar sedges if they follow |
| 956 |
the same outcome of a conditional node. */ |
| 957 |
return false; |
| 958 |
|
| 959 |
lhs_eedge_idx--; |
| 960 |
rhs_eedge_idx--; |
| 961 |
continue; |
| 962 |
} |
| 963 |
else if (lhs_eedge->m_sedge == nullptr && rhs_eedge->m_sedge == nullptr) |
| 964 |
/* Both paths were drained up entirely. |
| 965 |
No discriminant was found. */ |
| 966 |
return true; |
| 967 |
|
| 968 |
/* A superedge was found for only one of the two paths. */ |
| 969 |
return false; |
| 970 |
} |
| 971 |
|
| 972 |
/* A superedge was found for only one of the two paths. */ |
| 973 |
if (lhs_eedge_idx >= 0 || rhs_eedge_idx >= 0) |
| 974 |
return false; |
| 975 |
|
| 976 |
/* Both paths were drained up entirely. |
| 977 |
No discriminant was found. */ |
| 978 |
return true; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
/* Return true if this diagnostic supercedes OTHER, and that OTHER should |
| 983 |
therefore not be emitted. */ |
| 984 |
|
| 985 |
bool |
| 986 |
saved_diagnostic::supercedes_p (const saved_diagnostic &other) const |
| 987 |
{ |
| 988 |
/* They should be at the same stmt. */ |
| 989 |
if (m_stmt != other.m_stmt) |
| 990 |
return false; |
| 991 |
/* return early if OTHER won't be superseded anyway. */ |
| 992 |
if (!m_d->supercedes_p (*other.m_d)) |
| 993 |
return false; |
| 994 |
|
| 995 |
/* If the two saved_diagnostics' path are not compatible |
| 996 |
then they cannot supersede one another. */ |
| 997 |
return compatible_epath_p (m_best_epath.get (), other.m_best_epath.get ()); |
| 998 |
} |
| 999 |
|
| 1000 |
/* Move any saved checker_events from this saved_diagnostic to |
| 1001 |
the end of DST_PATH. */ |
| 1002 |
|
| 1003 |
void |
| 1004 |
saved_diagnostic::add_any_saved_events (checker_path &dst_path) |
| 1005 |
{ |
| 1006 |
for (auto &event : m_saved_events) |
| 1007 |
{ |
| 1008 |
dst_path.add_event (std::unique_ptr<checker_event> (event)); |
| 1009 |
event = nullptr; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* Emit any pending notes owned by this diagnostic. */ |
| 1014 |
|
| 1015 |
void |
| 1016 |
saved_diagnostic::emit_any_notes () const |
| 1017 |
{ |
| 1018 |
for (auto pn : m_notes) |
| 1019 |
pn->emit (); |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* For SARIF output, add additional properties to the "result" object |
| 1023 |
for this diagnostic. |
| 1024 |
This extra data is intended for use when debugging the analyzer. */ |
| 1025 |
|
| 1026 |
void |
| 1027 |
saved_diagnostic::maybe_add_sarif_properties (sarif_object &result_obj) const |
| 1028 |
{ |
| 1029 |
sarif_property_bag &props = result_obj.get_or_create_properties (); |
| 1030 |
#define PROPERTY_PREFIX "gcc/analyzer/saved_diagnostic/" |
| 1031 |
if (m_sm) |
| 1032 |
props.set_string (PROPERTY_PREFIX "sm", m_sm->get_name ()); |
| 1033 |
props.set_integer (PROPERTY_PREFIX "enode", m_enode->m_index); |
| 1034 |
props.set_integer (PROPERTY_PREFIX "snode", m_snode->m_index); |
| 1035 |
if (m_stmt) |
| 1036 |
{ |
| 1037 |
pretty_printer pp; |
| 1038 |
pp_gimple_stmt_1 (&pp, m_stmt, 0, (dump_flags_t)0); |
| 1039 |
props.set_string (PROPERTY_PREFIX "stmt", pp_formatted_text (&pp)); |
| 1040 |
} |
| 1041 |
if (m_var) |
| 1042 |
props.set (PROPERTY_PREFIX "var", tree_to_json (m_var)); |
| 1043 |
if (m_sval) |
| 1044 |
props.set (PROPERTY_PREFIX "sval", m_sval->to_json ()); |
| 1045 |
if (m_state) |
| 1046 |
props.set (PROPERTY_PREFIX "state", m_state->to_json ()); |
| 1047 |
// TODO: m_best_epath |
| 1048 |
props.set_integer (PROPERTY_PREFIX "idx", m_idx); |
| 1049 |
if (m_duplicates.length () > 0) |
| 1050 |
{ |
| 1051 |
auto duplicates_arr = ::make_unique<json::array> (); |
| 1052 |
for (auto iter : m_duplicates) |
| 1053 |
{ |
| 1054 |
auto sd_obj = ::make_unique<sarif_object> (); |
| 1055 |
iter->maybe_add_sarif_properties (*sd_obj); |
| 1056 |
duplicates_arr->append (std::move (sd_obj)); |
| 1057 |
} |
| 1058 |
props.set<json::array> (PROPERTY_PREFIX "duplicates", |
| 1059 |
std::move (duplicates_arr)); |
| 1060 |
} |
| 1061 |
#undef PROPERTY_PREFIX |
| 1062 |
|
| 1063 |
#define PROPERTY_PREFIX "gcc/analyzer/pending_diagnostic/" |
| 1064 |
props.set_string (PROPERTY_PREFIX "kind", m_d->get_kind ()); |
| 1065 |
#undef PROPERTY_PREFIX |
| 1066 |
|
| 1067 |
/* Potentially add pending_diagnostic-specific properties. */ |
| 1068 |
m_d->maybe_add_sarif_properties (result_obj); |
| 1069 |
} |
| 1070 |
|
| 1071 |
/* State for building a checker_path from a particular exploded_path. |
| 1072 |
In particular, this precomputes reachability information: the set of |
| 1073 |
source enodes for which a path be found to the diagnostic enode. */ |
| 1074 |
|
| 1075 |
class path_builder |
| 1076 |
{ |
| 1077 |
public: |
| 1078 |
path_builder (const exploded_graph &eg, |
| 1079 |
const exploded_path &epath, |
| 1080 |
const feasibility_problem *problem, |
| 1081 |
const saved_diagnostic &sd) |
| 1082 |
: m_eg (eg), |
| 1083 |
m_diag_enode (epath.get_final_enode ()), |
| 1084 |
m_sd (sd), |
| 1085 |
m_reachability (eg, m_diag_enode), |
| 1086 |
m_feasibility_problem (problem) |
| 1087 |
{} |
| 1088 |
|
| 1089 |
const exploded_node *get_diag_node () const { return m_diag_enode; } |
| 1090 |
|
| 1091 |
pending_diagnostic *get_pending_diagnostic () const |
| 1092 |
{ |
| 1093 |
return m_sd.m_d.get (); |
| 1094 |
} |
| 1095 |
|
| 1096 |
bool reachable_from_p (const exploded_node *src_enode) const |
| 1097 |
{ |
| 1098 |
return m_reachability.reachable_from_p (src_enode); |
| 1099 |
} |
| 1100 |
|
| 1101 |
const extrinsic_state &get_ext_state () const { return m_eg.get_ext_state (); } |
| 1102 |
|
| 1103 |
const feasibility_problem *get_feasibility_problem () const |
| 1104 |
{ |
| 1105 |
return m_feasibility_problem; |
| 1106 |
} |
| 1107 |
|
| 1108 |
const state_machine *get_sm () const { return m_sd.m_sm; } |
| 1109 |
|
| 1110 |
private: |
| 1111 |
typedef reachability<eg_traits> enode_reachability; |
| 1112 |
|
| 1113 |
const exploded_graph &m_eg; |
| 1114 |
|
| 1115 |
/* The enode where the diagnostic occurs. */ |
| 1116 |
const exploded_node *m_diag_enode; |
| 1117 |
|
| 1118 |
const saved_diagnostic &m_sd; |
| 1119 |
|
| 1120 |
/* Precompute all enodes from which the diagnostic is reachable. */ |
| 1121 |
enode_reachability m_reachability; |
| 1122 |
|
| 1123 |
const feasibility_problem *m_feasibility_problem; |
| 1124 |
}; |
| 1125 |
|
| 1126 |
/* Determine the emission location for PD at STMT in FUN. */ |
| 1127 |
|
| 1128 |
static location_t |
| 1129 |
get_emission_location (const gimple *stmt, function *fun, |
| 1130 |
const pending_diagnostic &pd) |
| 1131 |
{ |
| 1132 |
location_t loc = get_stmt_location (stmt, fun); |
| 1133 |
|
| 1134 |
/* Allow the pending_diagnostic to fix up the location. */ |
| 1135 |
loc = pd.fixup_location (loc, true); |
| 1136 |
|
| 1137 |
return loc; |
| 1138 |
} |
| 1139 |
|
| 1140 |
/* class diagnostic_manager. */ |
| 1141 |
|
| 1142 |
/* diagnostic_manager's ctor. */ |
| 1143 |
|
| 1144 |
diagnostic_manager::diagnostic_manager (logger *logger, engine *eng, |
| 1145 |
int verbosity) |
| 1146 |
: log_user (logger), m_eng (eng), m_verbosity (verbosity), |
| 1147 |
m_num_disabled_diagnostics (0) |
| 1148 |
{ |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* Queue pending_diagnostic D at ENODE for later emission. |
| 1152 |
Return true/false signifying if the diagnostic was actually added. */ |
| 1153 |
|
| 1154 |
bool |
| 1155 |
diagnostic_manager::add_diagnostic (const state_machine *sm, |
| 1156 |
const pending_location &ploc, |
| 1157 |
tree var, |
| 1158 |
const svalue *sval, |
| 1159 |
state_machine::state_t state, |
| 1160 |
std::unique_ptr<pending_diagnostic> d) |
| 1161 |
{ |
| 1162 |
LOG_FUNC (get_logger ()); |
| 1163 |
|
| 1164 |
/* We must have an enode in order to be able to look for paths |
| 1165 |
through the exploded_graph to the diagnostic. */ |
| 1166 |
gcc_assert (ploc.m_enode); |
| 1167 |
|
| 1168 |
/* If this warning is ultimately going to be rejected by a -Wno-analyzer-* |
| 1169 |
flag, reject it now. |
| 1170 |
We can only do this for diagnostics where we already know the stmt, |
| 1171 |
and thus can determine the emission location. */ |
| 1172 |
if (ploc.m_stmt) |
| 1173 |
{ |
| 1174 |
location_t loc |
| 1175 |
= get_emission_location (ploc.m_stmt, ploc.m_snode->m_fun, *d); |
| 1176 |
int option = d->get_controlling_option (); |
| 1177 |
if (!warning_enabled_at (loc, option)) |
| 1178 |
{ |
| 1179 |
if (get_logger ()) |
| 1180 |
get_logger ()->log ("rejecting disabled warning %qs", |
| 1181 |
d->get_kind ()); |
| 1182 |
m_num_disabled_diagnostics++; |
| 1183 |
return false; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
saved_diagnostic *sd |
| 1188 |
= new saved_diagnostic (sm, ploc, var, sval, state, std::move (d), |
| 1189 |
m_saved_diagnostics.length ()); |
| 1190 |
m_saved_diagnostics.safe_push (sd); |
| 1191 |
ploc.m_enode->add_diagnostic (sd); |
| 1192 |
if (get_logger ()) |
| 1193 |
log ("adding saved diagnostic %i at SN %i to EN %i: %qs", |
| 1194 |
sd->get_index (), |
| 1195 |
ploc.m_snode->m_index, ploc.m_enode->m_index, sd->m_d->get_kind ()); |
| 1196 |
return true; |
| 1197 |
} |
| 1198 |
|
| 1199 |
/* Queue pending_diagnostic D at ENODE for later emission. |
| 1200 |
Return true/false signifying if the diagnostic was actually added. |
| 1201 |
Take ownership of D (or delete it). */ |
| 1202 |
|
| 1203 |
bool |
| 1204 |
diagnostic_manager::add_diagnostic (const pending_location &ploc, |
| 1205 |
std::unique_ptr<pending_diagnostic> d) |
| 1206 |
{ |
| 1207 |
gcc_assert (ploc.m_enode); |
| 1208 |
return add_diagnostic (NULL, ploc, NULL_TREE, NULL, 0, std::move (d)); |
| 1209 |
} |
| 1210 |
|
| 1211 |
/* Add PN to the most recent saved_diagnostic. */ |
| 1212 |
|
| 1213 |
void |
| 1214 |
diagnostic_manager::add_note (std::unique_ptr<pending_note> pn) |
| 1215 |
{ |
| 1216 |
LOG_FUNC (get_logger ()); |
| 1217 |
gcc_assert (pn); |
| 1218 |
|
| 1219 |
/* Get most recent saved_diagnostic. */ |
| 1220 |
gcc_assert (m_saved_diagnostics.length () > 0); |
| 1221 |
saved_diagnostic *sd = m_saved_diagnostics[m_saved_diagnostics.length () - 1]; |
| 1222 |
sd->add_note (std::move (pn)); |
| 1223 |
} |
| 1224 |
|
| 1225 |
/* Add EVENT to the most recent saved_diagnostic. */ |
| 1226 |
|
| 1227 |
void |
| 1228 |
diagnostic_manager::add_event (std::unique_ptr<checker_event> event) |
| 1229 |
{ |
| 1230 |
LOG_FUNC (get_logger ()); |
| 1231 |
gcc_assert (event); |
| 1232 |
|
| 1233 |
/* Get most recent saved_diagnostic. */ |
| 1234 |
gcc_assert (m_saved_diagnostics.length () > 0); |
| 1235 |
saved_diagnostic *sd = m_saved_diagnostics[m_saved_diagnostics.length () - 1]; |
| 1236 |
sd->add_event (std::move (event)); |
| 1237 |
} |
| 1238 |
|
| 1239 |
/* Return a new json::object of the form |
| 1240 |
{"diagnostics" : [obj for saved_diagnostic]}. */ |
| 1241 |
|
| 1242 |
std::unique_ptr<json::object> |
| 1243 |
diagnostic_manager::to_json () const |
| 1244 |
{ |
| 1245 |
auto dm_obj = ::make_unique<json::object> (); |
| 1246 |
|
| 1247 |
{ |
| 1248 |
auto sd_arr = ::make_unique<json::array> (); |
| 1249 |
int i; |
| 1250 |
saved_diagnostic *sd; |
| 1251 |
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd) |
| 1252 |
sd_arr->append (sd->to_json ()); |
| 1253 |
dm_obj->set ("diagnostics", std::move (sd_arr)); |
| 1254 |
} |
| 1255 |
|
| 1256 |
return dm_obj; |
| 1257 |
} |
| 1258 |
|
| 1259 |
/* A class for identifying sets of duplicated pending_diagnostic. |
| 1260 |
|
| 1261 |
We want to find the simplest saved_diagnostic amongst those that share a |
| 1262 |
dedupe_key. */ |
| 1263 |
|
| 1264 |
class dedupe_key |
| 1265 |
{ |
| 1266 |
public: |
| 1267 |
dedupe_key (const saved_diagnostic &sd) |
| 1268 |
: m_sd (sd), m_stmt (sd.m_stmt), m_loc (sd.m_loc) |
| 1269 |
{ |
| 1270 |
gcc_assert (m_stmt || m_loc != UNKNOWN_LOCATION); |
| 1271 |
} |
| 1272 |
|
| 1273 |
hashval_t hash () const |
| 1274 |
{ |
| 1275 |
inchash::hash hstate; |
| 1276 |
hstate.add_ptr (m_stmt); |
| 1277 |
// TODO: m_sd |
| 1278 |
return hstate.end (); |
| 1279 |
} |
| 1280 |
bool operator== (const dedupe_key &other) const |
| 1281 |
{ |
| 1282 |
return (m_sd == other.m_sd |
| 1283 |
&& m_stmt == other.m_stmt |
| 1284 |
&& m_loc == other.m_loc); |
| 1285 |
} |
| 1286 |
|
| 1287 |
location_t get_location () const |
| 1288 |
{ |
| 1289 |
if (m_loc != UNKNOWN_LOCATION) |
| 1290 |
return m_loc; |
| 1291 |
gcc_assert (m_stmt); |
| 1292 |
return m_stmt->location; |
| 1293 |
} |
| 1294 |
|
| 1295 |
/* A qsort comparator for use by dedupe_winners::emit_best |
| 1296 |
to sort them into location_t order. */ |
| 1297 |
|
| 1298 |
static int |
| 1299 |
comparator (const void *p1, const void *p2) |
| 1300 |
{ |
| 1301 |
const dedupe_key *pk1 = *(const dedupe_key * const *)p1; |
| 1302 |
const dedupe_key *pk2 = *(const dedupe_key * const *)p2; |
| 1303 |
|
| 1304 |
location_t loc1 = pk1->get_location (); |
| 1305 |
location_t loc2 = pk2->get_location (); |
| 1306 |
|
| 1307 |
if (int cmp = linemap_compare_locations (line_table, loc2, loc1)) |
| 1308 |
return cmp; |
| 1309 |
if (int cmp = ((int)pk1->m_sd.get_epath_length () |
| 1310 |
- (int)pk2->m_sd.get_epath_length ())) |
| 1311 |
return cmp; |
| 1312 |
if (int cmp = strcmp (pk1->m_sd.m_d->get_kind (), |
| 1313 |
pk2->m_sd.m_d->get_kind ())) |
| 1314 |
return cmp; |
| 1315 |
return 0; |
| 1316 |
} |
| 1317 |
|
| 1318 |
const saved_diagnostic &m_sd; |
| 1319 |
const gimple *m_stmt; |
| 1320 |
location_t m_loc; |
| 1321 |
}; |
| 1322 |
|
| 1323 |
/* Traits for use by dedupe_winners. */ |
| 1324 |
|
| 1325 |
class dedupe_hash_map_traits |
| 1326 |
{ |
| 1327 |
public: |
| 1328 |
typedef const dedupe_key *key_type; |
| 1329 |
typedef saved_diagnostic *value_type; |
| 1330 |
typedef saved_diagnostic *compare_type; |
| 1331 |
|
| 1332 |
static inline hashval_t hash (const key_type &v) |
| 1333 |
{ |
| 1334 |
return v->hash (); |
| 1335 |
} |
| 1336 |
static inline bool equal_keys (const key_type &k1, const key_type &k2) |
| 1337 |
{ |
| 1338 |
return *k1 == *k2; |
| 1339 |
} |
| 1340 |
template <typename T> |
| 1341 |
static inline void remove (T &) |
| 1342 |
{ |
| 1343 |
// TODO |
| 1344 |
} |
| 1345 |
template <typename T> |
| 1346 |
static inline void mark_deleted (T &entry) |
| 1347 |
{ |
| 1348 |
entry.m_key = reinterpret_cast<key_type> (1); |
| 1349 |
} |
| 1350 |
template <typename T> |
| 1351 |
static inline void mark_empty (T &entry) |
| 1352 |
{ |
| 1353 |
entry.m_key = NULL; |
| 1354 |
} |
| 1355 |
template <typename T> |
| 1356 |
static inline bool is_deleted (const T &entry) |
| 1357 |
{ |
| 1358 |
return entry.m_key == reinterpret_cast<key_type> (1); |
| 1359 |
} |
| 1360 |
template <typename T> |
| 1361 |
static inline bool is_empty (const T &entry) |
| 1362 |
{ |
| 1363 |
return entry.m_key == NULL; |
| 1364 |
} |
| 1365 |
static const bool empty_zero_p = true; |
| 1366 |
}; |
| 1367 |
|
| 1368 |
/* A class for deduplicating diagnostics and finding (and emitting) the |
| 1369 |
best saved_diagnostic within each partition. */ |
| 1370 |
|
| 1371 |
class dedupe_winners |
| 1372 |
{ |
| 1373 |
public: |
| 1374 |
~dedupe_winners () |
| 1375 |
{ |
| 1376 |
/* Delete all keys, but not the saved_diagnostics. */ |
| 1377 |
for (map_t::iterator iter = m_map.begin (); |
| 1378 |
iter != m_map.end (); |
| 1379 |
++iter) |
| 1380 |
delete (*iter).first; |
| 1381 |
} |
| 1382 |
|
| 1383 |
/* Determine an exploded_path for SD using PF and, if it's feasible, |
| 1384 |
determine if SD is the best seen so far for its dedupe_key. |
| 1385 |
Record the winning SD for each dedupe_key. */ |
| 1386 |
|
| 1387 |
void add (logger *logger, |
| 1388 |
epath_finder *pf, |
| 1389 |
saved_diagnostic *sd) |
| 1390 |
{ |
| 1391 |
/* Determine best epath for SD. */ |
| 1392 |
if (!sd->calc_best_epath (pf)) |
| 1393 |
return; |
| 1394 |
|
| 1395 |
dedupe_key *key = new dedupe_key (*sd); |
| 1396 |
if (saved_diagnostic **slot = m_map.get (key)) |
| 1397 |
{ |
| 1398 |
if (logger) |
| 1399 |
logger->log ("already have this dedupe_key"); |
| 1400 |
|
| 1401 |
saved_diagnostic *cur_best_sd = *slot; |
| 1402 |
|
| 1403 |
if (sd->get_epath_length () < cur_best_sd->get_epath_length ()) |
| 1404 |
{ |
| 1405 |
/* We've got a shorter path for the key; replace |
| 1406 |
the current candidate, marking it as a duplicate of SD. */ |
| 1407 |
if (logger) |
| 1408 |
logger->log ("length %i is better than existing length %i;" |
| 1409 |
" taking over this dedupe_key", |
| 1410 |
sd->get_epath_length (), |
| 1411 |
cur_best_sd->get_epath_length ()); |
| 1412 |
sd->add_duplicate (cur_best_sd); |
| 1413 |
*slot = sd; |
| 1414 |
} |
| 1415 |
else |
| 1416 |
/* We haven't beaten the current best candidate; add SD |
| 1417 |
as a duplicate of it. */ |
| 1418 |
{ |
| 1419 |
if (logger) |
| 1420 |
logger->log ("length %i isn't better than existing length %i;" |
| 1421 |
" dropping this candidate", |
| 1422 |
sd->get_epath_length (), |
| 1423 |
cur_best_sd->get_epath_length ()); |
| 1424 |
cur_best_sd->add_duplicate (sd); |
| 1425 |
} |
| 1426 |
delete key; |
| 1427 |
} |
| 1428 |
else |
| 1429 |
{ |
| 1430 |
/* This is the first candidate for this key. */ |
| 1431 |
m_map.put (key, sd); |
| 1432 |
if (logger) |
| 1433 |
logger->log ("first candidate for this dedupe_key"); |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
/* Handle interactions between the dedupe winners, so that some |
| 1438 |
diagnostics can supercede others (of different kinds). |
| 1439 |
|
| 1440 |
We want use-after-free to supercede use-of-unitialized-value, |
| 1441 |
so that if we have these at the same stmt, we don't emit |
| 1442 |
a use-of-uninitialized, just the use-after-free. */ |
| 1443 |
|
| 1444 |
void handle_interactions (diagnostic_manager *dm) |
| 1445 |
{ |
| 1446 |
LOG_SCOPE (dm->get_logger ()); |
| 1447 |
auto_vec<const dedupe_key *> superceded; |
| 1448 |
for (auto outer : m_map) |
| 1449 |
{ |
| 1450 |
const saved_diagnostic *outer_sd = outer.second; |
| 1451 |
for (auto inner : m_map) |
| 1452 |
{ |
| 1453 |
const saved_diagnostic *inner_sd = inner.second; |
| 1454 |
if (inner_sd->supercedes_p (*outer_sd)) |
| 1455 |
{ |
| 1456 |
superceded.safe_push (outer.first); |
| 1457 |
if (dm->get_logger ()) |
| 1458 |
dm->log ("sd[%i] \"%s\" superceded by sd[%i] \"%s\"", |
| 1459 |
outer_sd->get_index (), outer_sd->m_d->get_kind (), |
| 1460 |
inner_sd->get_index (), inner_sd->m_d->get_kind ()); |
| 1461 |
break; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
} |
| 1465 |
for (auto iter : superceded) |
| 1466 |
m_map.remove (iter); |
| 1467 |
} |
| 1468 |
|
| 1469 |
/* Emit the simplest diagnostic within each set. */ |
| 1470 |
|
| 1471 |
void emit_best (diagnostic_manager *dm, |
| 1472 |
const exploded_graph &eg) |
| 1473 |
{ |
| 1474 |
LOG_SCOPE (dm->get_logger ()); |
| 1475 |
|
| 1476 |
/* Get keys into a vec for sorting. */ |
| 1477 |
auto_vec<const dedupe_key *> keys (m_map.elements ()); |
| 1478 |
for (map_t::iterator iter = m_map.begin (); |
| 1479 |
iter != m_map.end (); |
| 1480 |
++iter) |
| 1481 |
keys.quick_push ((*iter).first); |
| 1482 |
|
| 1483 |
dm->log ("# keys after de-duplication: %i", keys.length ()); |
| 1484 |
|
| 1485 |
/* Sort into a good emission order. */ |
| 1486 |
keys.qsort (dedupe_key::comparator); |
| 1487 |
|
| 1488 |
/* Emit the best saved_diagnostics for each key. */ |
| 1489 |
int i; |
| 1490 |
const dedupe_key *key; |
| 1491 |
FOR_EACH_VEC_ELT (keys, i, key) |
| 1492 |
{ |
| 1493 |
saved_diagnostic **slot = m_map.get (key); |
| 1494 |
gcc_assert (*slot); |
| 1495 |
saved_diagnostic *sd = *slot; |
| 1496 |
dm->emit_saved_diagnostic (eg, *sd); |
| 1497 |
} |
| 1498 |
} |
| 1499 |
|
| 1500 |
private: |
| 1501 |
/* This maps from each dedupe_key to a current best saved_diagnostic. */ |
| 1502 |
|
| 1503 |
typedef hash_map<const dedupe_key *, saved_diagnostic *, |
| 1504 |
dedupe_hash_map_traits> map_t; |
| 1505 |
map_t m_map; |
| 1506 |
}; |
| 1507 |
|
| 1508 |
/* Emit all saved diagnostics. */ |
| 1509 |
|
| 1510 |
void |
| 1511 |
diagnostic_manager::emit_saved_diagnostics (const exploded_graph &eg) |
| 1512 |
{ |
| 1513 |
LOG_SCOPE (get_logger ()); |
| 1514 |
auto_timevar tv (TV_ANALYZER_DIAGNOSTICS); |
| 1515 |
log ("# saved diagnostics: %i", m_saved_diagnostics.length ()); |
| 1516 |
log ("# disabled diagnostics: %i", m_num_disabled_diagnostics); |
| 1517 |
if (get_logger ()) |
| 1518 |
{ |
| 1519 |
unsigned i; |
| 1520 |
saved_diagnostic *sd; |
| 1521 |
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd) |
| 1522 |
log ("[%i] sd: %qs at EN: %i, SN: %i", |
| 1523 |
i, sd->m_d->get_kind (), sd->m_enode->m_index, |
| 1524 |
sd->m_snode->m_index); |
| 1525 |
} |
| 1526 |
|
| 1527 |
if (m_saved_diagnostics.length () == 0) |
| 1528 |
return; |
| 1529 |
|
| 1530 |
/* Compute the shortest_paths once, sharing it between all diagnostics. */ |
| 1531 |
epath_finder pf (eg); |
| 1532 |
|
| 1533 |
/* Iterate through all saved diagnostics, adding them to a dedupe_winners |
| 1534 |
instance. This partitions the saved diagnostics by dedupe_key, |
| 1535 |
generating exploded_paths for them, and retaining the best one in each |
| 1536 |
partition. */ |
| 1537 |
dedupe_winners best_candidates; |
| 1538 |
|
| 1539 |
int i; |
| 1540 |
saved_diagnostic *sd; |
| 1541 |
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd) |
| 1542 |
best_candidates.add (get_logger (), &pf, sd); |
| 1543 |
|
| 1544 |
best_candidates.handle_interactions (this); |
| 1545 |
|
| 1546 |
/* For each dedupe-key, call emit_saved_diagnostic on the "best" |
| 1547 |
saved_diagnostic. */ |
| 1548 |
best_candidates.emit_best (this, eg); |
| 1549 |
} |
| 1550 |
|
| 1551 |
/* Custom subclass of diagnostic_metadata which, for SARIF output, |
| 1552 |
populates the property bag of the diagnostic's "result" object |
| 1553 |
with information from the saved_diagnostic and the |
| 1554 |
pending_diagnostic. */ |
| 1555 |
|
| 1556 |
class pending_diagnostic_metadata : public diagnostic_metadata |
| 1557 |
{ |
| 1558 |
public: |
| 1559 |
pending_diagnostic_metadata (const saved_diagnostic &sd) |
| 1560 |
: m_sd (sd) |
| 1561 |
{ |
| 1562 |
} |
| 1563 |
|
| 1564 |
void |
| 1565 |
maybe_add_sarif_properties (sarif_object &result_obj) const override |
| 1566 |
{ |
| 1567 |
m_sd.maybe_add_sarif_properties (result_obj); |
| 1568 |
} |
| 1569 |
|
| 1570 |
private: |
| 1571 |
const saved_diagnostic &m_sd; |
| 1572 |
}; |
| 1573 |
|
| 1574 |
/* Given a saved_diagnostic SD with m_best_epath through EG, |
| 1575 |
create an checker_path of suitable events and use it to call |
| 1576 |
SD's underlying pending_diagnostic "emit" vfunc to emit a diagnostic. */ |
| 1577 |
|
| 1578 |
void |
| 1579 |
diagnostic_manager::emit_saved_diagnostic (const exploded_graph &eg, |
| 1580 |
saved_diagnostic &sd) |
| 1581 |
{ |
| 1582 |
LOG_SCOPE (get_logger ()); |
| 1583 |
log ("sd[%i]: %qs at SN: %i", |
| 1584 |
sd.get_index (), sd.m_d->get_kind (), sd.m_snode->m_index); |
| 1585 |
log ("num dupes: %i", sd.get_num_dupes ()); |
| 1586 |
|
| 1587 |
const exploded_path *epath = sd.get_best_epath (); |
| 1588 |
gcc_assert (epath); |
| 1589 |
|
| 1590 |
/* Precompute all enodes from which the diagnostic is reachable. */ |
| 1591 |
path_builder pb (eg, *epath, sd.get_feasibility_problem (), sd); |
| 1592 |
|
| 1593 |
/* This is the diagnostic_path subclass that will be built for |
| 1594 |
the diagnostic. */ |
| 1595 |
checker_path emission_path (get_logger ()); |
| 1596 |
|
| 1597 |
/* Populate emission_path with a full description of EPATH. */ |
| 1598 |
build_emission_path (pb, *epath, &emission_path); |
| 1599 |
|
| 1600 |
/* Now prune it to just cover the most pertinent events. */ |
| 1601 |
prune_path (&emission_path, sd.m_sm, sd.m_sval, sd.m_state); |
| 1602 |
|
| 1603 |
/* Add any saved events to the path, giving contextual information |
| 1604 |
about what the analyzer was simulating as the diagnostic was |
| 1605 |
generated. These don't get pruned, as they are probably pertinent. */ |
| 1606 |
sd.add_any_saved_events (emission_path); |
| 1607 |
|
| 1608 |
/* Add a final event to the path, covering the diagnostic itself. |
| 1609 |
We use the final enode from the epath, which might be different from |
| 1610 |
the sd.m_enode, as the dedupe code doesn't care about enodes, just |
| 1611 |
snodes. */ |
| 1612 |
{ |
| 1613 |
const exploded_node *const enode = epath->get_final_enode (); |
| 1614 |
const gimple *stmt = sd.m_stmt; |
| 1615 |
event_loc_info loc_info (get_stmt_location (stmt, enode->get_function ()), |
| 1616 |
enode->get_function ()->decl, |
| 1617 |
enode->get_stack_depth ()); |
| 1618 |
if (sd.m_stmt_finder) |
| 1619 |
sd.m_stmt_finder->update_event_loc_info (loc_info); |
| 1620 |
sd.m_d->add_final_event (sd.m_sm, enode, loc_info, |
| 1621 |
sd.m_var, sd.m_state, &emission_path); |
| 1622 |
} |
| 1623 |
|
| 1624 |
/* The "final" event might not be final; if the saved_diagnostic has a |
| 1625 |
trailing eedge stashed, add any events for it. This is for use |
| 1626 |
in handling longjmp, to show where a longjmp is rewinding to. */ |
| 1627 |
if (sd.m_trailing_eedge) |
| 1628 |
add_events_for_eedge (pb, *sd.m_trailing_eedge, &emission_path, NULL); |
| 1629 |
|
| 1630 |
emission_path.inject_any_inlined_call_events (get_logger ()); |
| 1631 |
|
| 1632 |
emission_path.prepare_for_emission (sd.m_d.get ()); |
| 1633 |
|
| 1634 |
location_t loc = sd.m_loc; |
| 1635 |
if (loc == UNKNOWN_LOCATION) |
| 1636 |
loc = get_emission_location (sd.m_stmt, sd.m_snode->m_fun, *sd.m_d); |
| 1637 |
|
| 1638 |
/* Allow the pending_diagnostic to fix up the locations of events. */ |
| 1639 |
emission_path.fixup_locations (sd.m_d.get ()); |
| 1640 |
|
| 1641 |
gcc_rich_location rich_loc (loc); |
| 1642 |
rich_loc.set_path (&emission_path); |
| 1643 |
|
| 1644 |
auto_diagnostic_group d; |
| 1645 |
auto_cfun sentinel (sd.m_snode->m_fun); |
| 1646 |
pending_diagnostic_metadata m (sd); |
| 1647 |
diagnostic_emission_context diag_ctxt (sd, rich_loc, m, get_logger ()); |
| 1648 |
if (sd.m_d->emit (diag_ctxt)) |
| 1649 |
{ |
| 1650 |
sd.emit_any_notes (); |
| 1651 |
|
| 1652 |
unsigned num_dupes = sd.get_num_dupes (); |
| 1653 |
if (flag_analyzer_show_duplicate_count && num_dupes > 0) |
| 1654 |
inform_n (loc, num_dupes, |
| 1655 |
"%i duplicate", "%i duplicates", |
| 1656 |
num_dupes); |
| 1657 |
if (flag_dump_analyzer_exploded_paths) |
| 1658 |
{ |
| 1659 |
auto_timevar tv (TV_ANALYZER_DUMP); |
| 1660 |
pretty_printer pp; |
| 1661 |
pp_printf (&pp, "%s.%i.%s.epath.txt", |
| 1662 |
dump_base_name, sd.get_index (), sd.m_d->get_kind ()); |
| 1663 |
char *filename = xstrdup (pp_formatted_text (&pp)); |
| 1664 |
epath->dump_to_file (filename, eg.get_ext_state ()); |
| 1665 |
inform (loc, "exploded path written to %qs", filename); |
| 1666 |
free (filename); |
| 1667 |
} |
| 1668 |
} |
| 1669 |
} |
| 1670 |
|
| 1671 |
/* Emit a "path" of events to EMISSION_PATH describing the exploded path |
| 1672 |
EPATH within EG. */ |
| 1673 |
|
| 1674 |
void |
| 1675 |
diagnostic_manager::build_emission_path (const path_builder &pb, |
| 1676 |
const exploded_path &epath, |
| 1677 |
checker_path *emission_path) const |
| 1678 |
{ |
| 1679 |
LOG_SCOPE (get_logger ()); |
| 1680 |
|
| 1681 |
interesting_t interest; |
| 1682 |
pb.get_pending_diagnostic ()->mark_interesting_stuff (&interest); |
| 1683 |
|
| 1684 |
/* Add region creation events for any globals of interest, at the |
| 1685 |
beginning of the path. */ |
| 1686 |
{ |
| 1687 |
for (auto reg : interest.m_region_creation) |
| 1688 |
switch (reg->get_memory_space ()) |
| 1689 |
{ |
| 1690 |
default: |
| 1691 |
continue; |
| 1692 |
case MEMSPACE_CODE: |
| 1693 |
case MEMSPACE_GLOBALS: |
| 1694 |
case MEMSPACE_READONLY_DATA: |
| 1695 |
{ |
| 1696 |
const region *base_reg = reg->get_base_region (); |
| 1697 |
if (tree decl = base_reg->maybe_get_decl ()) |
| 1698 |
if (DECL_P (decl) |
| 1699 |
&& DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION) |
| 1700 |
{ |
| 1701 |
emission_path->add_region_creation_events |
| 1702 |
(pb.get_pending_diagnostic (), |
| 1703 |
reg, NULL, |
| 1704 |
event_loc_info (DECL_SOURCE_LOCATION (decl), |
| 1705 |
NULL_TREE, |
| 1706 |
0), |
| 1707 |
m_verbosity > 3); |
| 1708 |
} |
| 1709 |
} |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
/* Walk EPATH, adding events as appropriate. */ |
| 1714 |
for (unsigned i = 0; i < epath.m_edges.length (); i++) |
| 1715 |
{ |
| 1716 |
const exploded_edge *eedge = epath.m_edges[i]; |
| 1717 |
add_events_for_eedge (pb, *eedge, emission_path, &interest); |
| 1718 |
} |
| 1719 |
add_event_on_final_node (pb, epath.get_final_enode (), |
| 1720 |
emission_path, &interest); |
| 1721 |
} |
| 1722 |
|
| 1723 |
/* Emit a region_creation_event when requested on the last statement in |
| 1724 |
the path. |
| 1725 |
|
| 1726 |
If a region_creation_event should be emitted on the last statement of the |
| 1727 |
path, we need to peek to the successors to get whether the final enode |
| 1728 |
created a region. |
| 1729 |
*/ |
| 1730 |
|
| 1731 |
void |
| 1732 |
diagnostic_manager::add_event_on_final_node (const path_builder &pb, |
| 1733 |
const exploded_node *final_enode, |
| 1734 |
checker_path *emission_path, |
| 1735 |
interesting_t *interest) const |
| 1736 |
{ |
| 1737 |
const program_point &src_point = final_enode->get_point (); |
| 1738 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 1739 |
const program_state &src_state = final_enode->get_state (); |
| 1740 |
const region_model *src_model = src_state.m_region_model; |
| 1741 |
|
| 1742 |
unsigned j; |
| 1743 |
exploded_edge *e; |
| 1744 |
FOR_EACH_VEC_ELT (final_enode->m_succs, j, e) |
| 1745 |
{ |
| 1746 |
exploded_node *dst = e->m_dest; |
| 1747 |
const program_state &dst_state = dst->get_state (); |
| 1748 |
const region_model *dst_model = dst_state.m_region_model; |
| 1749 |
if (src_model->get_dynamic_extents () |
| 1750 |
!= dst_model->get_dynamic_extents ()) |
| 1751 |
{ |
| 1752 |
unsigned i; |
| 1753 |
const region *reg; |
| 1754 |
bool emitted = false; |
| 1755 |
FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg) |
| 1756 |
{ |
| 1757 |
const region *base_reg = reg->get_base_region (); |
| 1758 |
const svalue *old_extents |
| 1759 |
= src_model->get_dynamic_extents (base_reg); |
| 1760 |
const svalue *new_extents |
| 1761 |
= dst_model->get_dynamic_extents (base_reg); |
| 1762 |
if (old_extents == NULL && new_extents != NULL) |
| 1763 |
switch (base_reg->get_kind ()) |
| 1764 |
{ |
| 1765 |
default: |
| 1766 |
break; |
| 1767 |
case RK_HEAP_ALLOCATED: |
| 1768 |
case RK_ALLOCA: |
| 1769 |
emission_path->add_region_creation_events |
| 1770 |
(pb.get_pending_diagnostic (), |
| 1771 |
reg, |
| 1772 |
dst_model, |
| 1773 |
event_loc_info (src_point.get_location (), |
| 1774 |
src_point.get_fndecl (), |
| 1775 |
src_stack_depth), |
| 1776 |
false); |
| 1777 |
emitted = true; |
| 1778 |
break; |
| 1779 |
} |
| 1780 |
} |
| 1781 |
if (emitted) |
| 1782 |
break; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
/* Subclass of state_change_visitor that creates state_change_event |
| 1788 |
instances. */ |
| 1789 |
|
| 1790 |
class state_change_event_creator : public state_change_visitor |
| 1791 |
{ |
| 1792 |
public: |
| 1793 |
state_change_event_creator (const path_builder &pb, |
| 1794 |
const exploded_edge &eedge, |
| 1795 |
checker_path *emission_path) |
| 1796 |
: m_pb (pb), |
| 1797 |
m_eedge (eedge), |
| 1798 |
m_emission_path (emission_path) |
| 1799 |
{} |
| 1800 |
|
| 1801 |
bool on_global_state_change (const state_machine &sm, |
| 1802 |
state_machine::state_t src_sm_val, |
| 1803 |
state_machine::state_t dst_sm_val) |
| 1804 |
final override |
| 1805 |
{ |
| 1806 |
if (&sm != m_pb.get_sm ()) |
| 1807 |
return false; |
| 1808 |
const exploded_node *src_node = m_eedge.m_src; |
| 1809 |
const program_point &src_point = src_node->get_point (); |
| 1810 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 1811 |
const exploded_node *dst_node = m_eedge.m_dest; |
| 1812 |
const gimple *stmt = src_point.get_stmt (); |
| 1813 |
const supernode *supernode = src_point.get_supernode (); |
| 1814 |
const program_state &dst_state = dst_node->get_state (); |
| 1815 |
|
| 1816 |
int stack_depth = src_stack_depth; |
| 1817 |
|
| 1818 |
m_emission_path->add_event |
| 1819 |
(make_unique<state_change_event> (supernode, |
| 1820 |
stmt, |
| 1821 |
stack_depth, |
| 1822 |
sm, |
| 1823 |
nullptr, |
| 1824 |
src_sm_val, |
| 1825 |
dst_sm_val, |
| 1826 |
nullptr, |
| 1827 |
dst_state, |
| 1828 |
src_node)); |
| 1829 |
return false; |
| 1830 |
} |
| 1831 |
|
| 1832 |
bool on_state_change (const state_machine &sm, |
| 1833 |
state_machine::state_t src_sm_val, |
| 1834 |
state_machine::state_t dst_sm_val, |
| 1835 |
const svalue *sval, |
| 1836 |
const svalue *dst_origin_sval) final override |
| 1837 |
{ |
| 1838 |
if (&sm != m_pb.get_sm ()) |
| 1839 |
return false; |
| 1840 |
const exploded_node *src_node = m_eedge.m_src; |
| 1841 |
const program_point &src_point = src_node->get_point (); |
| 1842 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 1843 |
const exploded_node *dst_node = m_eedge.m_dest; |
| 1844 |
const gimple *stmt = src_point.get_stmt (); |
| 1845 |
const supernode *supernode = src_point.get_supernode (); |
| 1846 |
const program_state &dst_state = dst_node->get_state (); |
| 1847 |
|
| 1848 |
int stack_depth = src_stack_depth; |
| 1849 |
|
| 1850 |
if (m_eedge.m_sedge |
| 1851 |
&& m_eedge.m_sedge->m_kind == SUPEREDGE_CFG_EDGE) |
| 1852 |
{ |
| 1853 |
supernode = src_point.get_supernode (); |
| 1854 |
stmt = supernode->get_last_stmt (); |
| 1855 |
stack_depth = src_stack_depth; |
| 1856 |
} |
| 1857 |
|
| 1858 |
/* Bulletproofing for state changes at calls/returns; |
| 1859 |
TODO: is there a better way? */ |
| 1860 |
if (!stmt) |
| 1861 |
return false; |
| 1862 |
|
| 1863 |
m_emission_path->add_event |
| 1864 |
(make_unique<state_change_event> (supernode, |
| 1865 |
stmt, |
| 1866 |
stack_depth, |
| 1867 |
sm, |
| 1868 |
sval, |
| 1869 |
src_sm_val, |
| 1870 |
dst_sm_val, |
| 1871 |
dst_origin_sval, |
| 1872 |
dst_state, |
| 1873 |
src_node)); |
| 1874 |
return false; |
| 1875 |
} |
| 1876 |
|
| 1877 |
const path_builder &m_pb; |
| 1878 |
const exploded_edge &m_eedge; |
| 1879 |
checker_path *m_emission_path; |
| 1880 |
}; |
| 1881 |
|
| 1882 |
/* Compare SRC_STATE and DST_STATE (which use EXT_STATE), and call |
| 1883 |
VISITOR's on_state_change for every sm-state change that occurs |
| 1884 |
to a tree, and on_global_state_change for every global state change |
| 1885 |
that occurs. |
| 1886 |
|
| 1887 |
This determines the state changes that ought to be reported to |
| 1888 |
the user: a combination of the effects of changes to sm_state_map |
| 1889 |
(which maps svalues to sm-states), and of region_model changes |
| 1890 |
(which map trees to svalues). |
| 1891 |
|
| 1892 |
Bail out early and return true if any call to on_global_state_change |
| 1893 |
or on_state_change returns true, otherwise return false. |
| 1894 |
|
| 1895 |
This is split out to make it easier to experiment with changes to |
| 1896 |
exploded_node granularity (so that we can observe what state changes |
| 1897 |
lead to state_change_events being emitted). */ |
| 1898 |
|
| 1899 |
bool |
| 1900 |
for_each_state_change (const program_state &src_state, |
| 1901 |
const program_state &dst_state, |
| 1902 |
const extrinsic_state &ext_state, |
| 1903 |
state_change_visitor *visitor) |
| 1904 |
{ |
| 1905 |
gcc_assert (src_state.m_checker_states.length () |
| 1906 |
== ext_state.get_num_checkers ()); |
| 1907 |
gcc_assert (dst_state.m_checker_states.length () |
| 1908 |
== ext_state.get_num_checkers ()); |
| 1909 |
for (unsigned i = 0; i < ext_state.get_num_checkers (); i++) |
| 1910 |
{ |
| 1911 |
const state_machine &sm = ext_state.get_sm (i); |
| 1912 |
const sm_state_map &src_smap = *src_state.m_checker_states[i]; |
| 1913 |
const sm_state_map &dst_smap = *dst_state.m_checker_states[i]; |
| 1914 |
|
| 1915 |
/* Add events for any global state changes. */ |
| 1916 |
if (src_smap.get_global_state () != dst_smap.get_global_state ()) |
| 1917 |
if (visitor->on_global_state_change (sm, |
| 1918 |
src_smap.get_global_state (), |
| 1919 |
dst_smap.get_global_state ())) |
| 1920 |
return true; |
| 1921 |
|
| 1922 |
/* Add events for per-svalue state changes. */ |
| 1923 |
for (sm_state_map::iterator_t iter = dst_smap.begin (); |
| 1924 |
iter != dst_smap.end (); |
| 1925 |
++iter) |
| 1926 |
{ |
| 1927 |
const svalue *sval = (*iter).first; |
| 1928 |
state_machine::state_t dst_sm_val = (*iter).second.m_state; |
| 1929 |
state_machine::state_t src_sm_val |
| 1930 |
= src_smap.get_state (sval, ext_state); |
| 1931 |
if (dst_sm_val != src_sm_val) |
| 1932 |
{ |
| 1933 |
const svalue *origin_sval = (*iter).second.m_origin; |
| 1934 |
if (visitor->on_state_change (sm, src_sm_val, dst_sm_val, |
| 1935 |
sval, origin_sval)) |
| 1936 |
return true; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
} |
| 1940 |
return false; |
| 1941 |
} |
| 1942 |
|
| 1943 |
/* An sm_context for adding state_change_event on assignments to NULL, |
| 1944 |
where the default state isn't m_start. Storing such state in the |
| 1945 |
sm_state_map would lead to bloat of the exploded_graph, so we want |
| 1946 |
to leave it as a default state, and inject state change events here |
| 1947 |
when we have a diagnostic. |
| 1948 |
Find transitions of constants, for handling on_zero_assignment. */ |
| 1949 |
|
| 1950 |
struct null_assignment_sm_context : public sm_context |
| 1951 |
{ |
| 1952 |
null_assignment_sm_context (int sm_idx, |
| 1953 |
const state_machine &sm, |
| 1954 |
const program_state *old_state, |
| 1955 |
const program_state *new_state, |
| 1956 |
const gimple *stmt, |
| 1957 |
const program_point *point, |
| 1958 |
checker_path *emission_path, |
| 1959 |
const extrinsic_state &ext_state) |
| 1960 |
: sm_context (sm_idx, sm), m_old_state (old_state), m_new_state (new_state), |
| 1961 |
m_stmt (stmt), m_point (point), m_emission_path (emission_path), |
| 1962 |
m_ext_state (ext_state) |
| 1963 |
{ |
| 1964 |
} |
| 1965 |
|
| 1966 |
tree get_fndecl_for_call (const gcall */*call*/) final override |
| 1967 |
{ |
| 1968 |
return NULL_TREE; |
| 1969 |
} |
| 1970 |
|
| 1971 |
state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED, |
| 1972 |
tree var) final override |
| 1973 |
{ |
| 1974 |
const svalue *var_old_sval |
| 1975 |
= m_old_state->m_region_model->get_rvalue (var, NULL); |
| 1976 |
const sm_state_map *old_smap = m_old_state->m_checker_states[m_sm_idx]; |
| 1977 |
|
| 1978 |
state_machine::state_t current |
| 1979 |
= old_smap->get_state (var_old_sval, m_ext_state); |
| 1980 |
|
| 1981 |
return current; |
| 1982 |
} |
| 1983 |
|
| 1984 |
state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED, |
| 1985 |
const svalue *sval) final override |
| 1986 |
{ |
| 1987 |
const sm_state_map *old_smap = m_old_state->m_checker_states[m_sm_idx]; |
| 1988 |
state_machine::state_t current = old_smap->get_state (sval, m_ext_state); |
| 1989 |
return current; |
| 1990 |
} |
| 1991 |
|
| 1992 |
void set_next_state (const gimple *stmt, |
| 1993 |
tree var, |
| 1994 |
state_machine::state_t to, |
| 1995 |
tree origin ATTRIBUTE_UNUSED) final override |
| 1996 |
{ |
| 1997 |
state_machine::state_t from = get_state (stmt, var); |
| 1998 |
if (from != m_sm.get_start_state ()) |
| 1999 |
return; |
| 2000 |
if (!is_transition_to_null (to)) |
| 2001 |
return; |
| 2002 |
|
| 2003 |
const svalue *var_new_sval |
| 2004 |
= m_new_state->m_region_model->get_rvalue (var, NULL); |
| 2005 |
|
| 2006 |
const supernode *supernode = m_point->get_supernode (); |
| 2007 |
int stack_depth = m_point->get_stack_depth (); |
| 2008 |
|
| 2009 |
m_emission_path->add_event |
| 2010 |
(make_unique<state_change_event> (supernode, |
| 2011 |
m_stmt, |
| 2012 |
stack_depth, |
| 2013 |
m_sm, |
| 2014 |
var_new_sval, |
| 2015 |
from, to, |
| 2016 |
nullptr, |
| 2017 |
*m_new_state, |
| 2018 |
nullptr)); |
| 2019 |
} |
| 2020 |
|
| 2021 |
void set_next_state (const gimple *stmt, |
| 2022 |
const svalue *sval, |
| 2023 |
state_machine::state_t to, |
| 2024 |
tree origin ATTRIBUTE_UNUSED) final override |
| 2025 |
{ |
| 2026 |
state_machine::state_t from = get_state (stmt, sval); |
| 2027 |
if (from != m_sm.get_start_state ()) |
| 2028 |
return; |
| 2029 |
if (!is_transition_to_null (to)) |
| 2030 |
return; |
| 2031 |
|
| 2032 |
const supernode *supernode = m_point->get_supernode (); |
| 2033 |
int stack_depth = m_point->get_stack_depth (); |
| 2034 |
|
| 2035 |
m_emission_path->add_event |
| 2036 |
(make_unique<state_change_event> (supernode, |
| 2037 |
m_stmt, |
| 2038 |
stack_depth, |
| 2039 |
m_sm, |
| 2040 |
sval, |
| 2041 |
from, to, |
| 2042 |
nullptr, |
| 2043 |
*m_new_state, |
| 2044 |
nullptr)); |
| 2045 |
} |
| 2046 |
|
| 2047 |
void warn (const supernode *, const gimple *, |
| 2048 |
tree, std::unique_ptr<pending_diagnostic>) final override |
| 2049 |
{ |
| 2050 |
} |
| 2051 |
void warn (const supernode *, const gimple *, |
| 2052 |
const svalue *, std::unique_ptr<pending_diagnostic>) final override |
| 2053 |
{ |
| 2054 |
} |
| 2055 |
|
| 2056 |
tree get_diagnostic_tree (tree expr) final override |
| 2057 |
{ |
| 2058 |
return expr; |
| 2059 |
} |
| 2060 |
|
| 2061 |
tree get_diagnostic_tree (const svalue *sval) final override |
| 2062 |
{ |
| 2063 |
return m_new_state->m_region_model->get_representative_tree (sval); |
| 2064 |
} |
| 2065 |
|
| 2066 |
state_machine::state_t get_global_state () const final override |
| 2067 |
{ |
| 2068 |
return 0; |
| 2069 |
} |
| 2070 |
|
| 2071 |
void set_global_state (state_machine::state_t) final override |
| 2072 |
{ |
| 2073 |
/* No-op. */ |
| 2074 |
} |
| 2075 |
|
| 2076 |
void clear_all_per_svalue_state () final override |
| 2077 |
{ |
| 2078 |
/* No-op. */ |
| 2079 |
} |
| 2080 |
|
| 2081 |
void on_custom_transition (custom_transition *) final override |
| 2082 |
{ |
| 2083 |
} |
| 2084 |
|
| 2085 |
tree is_zero_assignment (const gimple *stmt) final override |
| 2086 |
{ |
| 2087 |
const gassign *assign_stmt = dyn_cast <const gassign *> (stmt); |
| 2088 |
if (!assign_stmt) |
| 2089 |
return NULL_TREE; |
| 2090 |
if (const svalue *sval |
| 2091 |
= m_new_state->m_region_model->get_gassign_result (assign_stmt, NULL)) |
| 2092 |
if (tree cst = sval->maybe_get_constant ()) |
| 2093 |
if (::zerop(cst)) |
| 2094 |
return gimple_assign_lhs (assign_stmt); |
| 2095 |
return NULL_TREE; |
| 2096 |
} |
| 2097 |
|
| 2098 |
const program_state *get_old_program_state () const final override |
| 2099 |
{ |
| 2100 |
return m_old_state; |
| 2101 |
} |
| 2102 |
const program_state *get_new_program_state () const final override |
| 2103 |
{ |
| 2104 |
return m_new_state; |
| 2105 |
} |
| 2106 |
|
| 2107 |
/* We only care about transitions to the "null" state |
| 2108 |
within sm-malloc. Special-case this. */ |
| 2109 |
static bool is_transition_to_null (state_machine::state_t s) |
| 2110 |
{ |
| 2111 |
return !strcmp (s->get_name (), "null"); |
| 2112 |
} |
| 2113 |
|
| 2114 |
const program_state *m_old_state; |
| 2115 |
const program_state *m_new_state; |
| 2116 |
const gimple *m_stmt; |
| 2117 |
const program_point *m_point; |
| 2118 |
checker_path *m_emission_path; |
| 2119 |
const extrinsic_state &m_ext_state; |
| 2120 |
}; |
| 2121 |
|
| 2122 |
/* Subroutine of diagnostic_manager::build_emission_path. |
| 2123 |
Add any events for EEDGE to EMISSION_PATH. */ |
| 2124 |
|
| 2125 |
void |
| 2126 |
diagnostic_manager::add_events_for_eedge (const path_builder &pb, |
| 2127 |
const exploded_edge &eedge, |
| 2128 |
checker_path *emission_path, |
| 2129 |
interesting_t *interest) const |
| 2130 |
{ |
| 2131 |
const exploded_node *src_node = eedge.m_src; |
| 2132 |
const program_point &src_point = src_node->get_point (); |
| 2133 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 2134 |
const exploded_node *dst_node = eedge.m_dest; |
| 2135 |
const program_point &dst_point = dst_node->get_point (); |
| 2136 |
const int dst_stack_depth = dst_point.get_stack_depth (); |
| 2137 |
if (get_logger ()) |
| 2138 |
{ |
| 2139 |
get_logger ()->start_log_line (); |
| 2140 |
pretty_printer *pp = get_logger ()->get_printer (); |
| 2141 |
pp_printf (pp, "EN %i -> EN %i: ", |
| 2142 |
eedge.m_src->m_index, |
| 2143 |
eedge.m_dest->m_index); |
| 2144 |
src_point.print (pp, format (false)); |
| 2145 |
pp_string (pp, "-> "); |
| 2146 |
dst_point.print (pp, format (false)); |
| 2147 |
get_logger ()->end_log_line (); |
| 2148 |
} |
| 2149 |
const program_state &src_state = src_node->get_state (); |
| 2150 |
const program_state &dst_state = dst_node->get_state (); |
| 2151 |
|
| 2152 |
/* Add state change events for the states that have changed. |
| 2153 |
We add these before events for superedges, so that if we have a |
| 2154 |
state_change_event due to following an edge, we'll get this sequence |
| 2155 |
of events: |
| 2156 |
|
| 2157 |
| if (!ptr) |
| 2158 |
| ~ |
| 2159 |
| | |
| 2160 |
| (1) assuming 'ptr' is non-NULL (state_change_event) |
| 2161 |
| (2) following 'false' branch... (start_cfg_edge_event) |
| 2162 |
... |
| 2163 |
| do_something (ptr); |
| 2164 |
| ~~~~~~~~~~~~~^~~~~ |
| 2165 |
| | |
| 2166 |
| (3) ...to here (end_cfg_edge_event). */ |
| 2167 |
state_change_event_creator visitor (pb, eedge, emission_path); |
| 2168 |
for_each_state_change (src_state, dst_state, pb.get_ext_state (), |
| 2169 |
&visitor); |
| 2170 |
|
| 2171 |
/* Allow non-standard edges to add events, e.g. when rewinding from |
| 2172 |
longjmp to a setjmp. */ |
| 2173 |
if (eedge.m_custom_info) |
| 2174 |
eedge.m_custom_info->add_events_to_path (emission_path, eedge); |
| 2175 |
|
| 2176 |
/* Add events for superedges, function entries, and for statements. */ |
| 2177 |
switch (dst_point.get_kind ()) |
| 2178 |
{ |
| 2179 |
default: |
| 2180 |
break; |
| 2181 |
case PK_BEFORE_SUPERNODE: |
| 2182 |
if (src_point.get_kind () == PK_AFTER_SUPERNODE) |
| 2183 |
{ |
| 2184 |
if (eedge.m_sedge) |
| 2185 |
add_events_for_superedge (pb, eedge, emission_path); |
| 2186 |
} |
| 2187 |
/* Add function entry events. */ |
| 2188 |
if (dst_point.get_supernode ()->entry_p ()) |
| 2189 |
{ |
| 2190 |
pb.get_pending_diagnostic ()->add_function_entry_event |
| 2191 |
(eedge, emission_path); |
| 2192 |
/* Create region_creation_events for on-stack regions within |
| 2193 |
this frame. */ |
| 2194 |
if (interest) |
| 2195 |
{ |
| 2196 |
unsigned i; |
| 2197 |
const region *reg; |
| 2198 |
FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg) |
| 2199 |
if (const frame_region *frame = reg->maybe_get_frame_region ()) |
| 2200 |
if (frame->get_fndecl () == dst_point.get_fndecl ()) |
| 2201 |
{ |
| 2202 |
const region *base_reg = reg->get_base_region (); |
| 2203 |
if (tree decl = base_reg->maybe_get_decl ()) |
| 2204 |
if (DECL_P (decl) |
| 2205 |
&& DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION) |
| 2206 |
{ |
| 2207 |
emission_path->add_region_creation_events |
| 2208 |
(pb.get_pending_diagnostic (), |
| 2209 |
reg, dst_state.m_region_model, |
| 2210 |
event_loc_info (DECL_SOURCE_LOCATION (decl), |
| 2211 |
dst_point.get_fndecl (), |
| 2212 |
dst_stack_depth), |
| 2213 |
m_verbosity > 3); |
| 2214 |
} |
| 2215 |
} |
| 2216 |
} |
| 2217 |
} |
| 2218 |
break; |
| 2219 |
case PK_BEFORE_STMT: |
| 2220 |
{ |
| 2221 |
const gimple *stmt = dst_point.get_stmt (); |
| 2222 |
const gcall *call = dyn_cast <const gcall *> (stmt); |
| 2223 |
if (call && is_setjmp_call_p (call)) |
| 2224 |
emission_path->add_event |
| 2225 |
(make_unique<setjmp_event> (event_loc_info (stmt->location, |
| 2226 |
dst_point.get_fndecl (), |
| 2227 |
dst_stack_depth), |
| 2228 |
dst_node, |
| 2229 |
call)); |
| 2230 |
else |
| 2231 |
emission_path->add_event |
| 2232 |
(make_unique<statement_event> (stmt, |
| 2233 |
dst_point.get_fndecl (), |
| 2234 |
dst_stack_depth, dst_state)); |
| 2235 |
|
| 2236 |
/* Create state change events for assignment to NULL. |
| 2237 |
Iterate through the stmts in dst_enode, adding state change |
| 2238 |
events for them. */ |
| 2239 |
if (dst_state.m_region_model) |
| 2240 |
{ |
| 2241 |
log_scope s (get_logger (), "processing run of stmts"); |
| 2242 |
program_state iter_state (dst_state); |
| 2243 |
program_point iter_point (dst_point); |
| 2244 |
while (1) |
| 2245 |
{ |
| 2246 |
const gimple *stmt = iter_point.get_stmt (); |
| 2247 |
if (const gassign *assign = dyn_cast<const gassign *> (stmt)) |
| 2248 |
{ |
| 2249 |
const extrinsic_state &ext_state = pb.get_ext_state (); |
| 2250 |
program_state old_state (iter_state); |
| 2251 |
iter_state.m_region_model->on_assignment (assign, NULL); |
| 2252 |
for (unsigned i = 0; i < ext_state.get_num_checkers (); i++) |
| 2253 |
{ |
| 2254 |
const state_machine &sm = ext_state.get_sm (i); |
| 2255 |
null_assignment_sm_context sm_ctxt (i, sm, |
| 2256 |
&old_state, |
| 2257 |
&iter_state, |
| 2258 |
stmt, |
| 2259 |
&iter_point, |
| 2260 |
emission_path, |
| 2261 |
pb.get_ext_state ()); |
| 2262 |
sm.on_stmt (sm_ctxt, dst_point.get_supernode (), stmt); |
| 2263 |
// TODO: what about phi nodes? |
| 2264 |
} |
| 2265 |
} |
| 2266 |
iter_point.next_stmt (); |
| 2267 |
if (iter_point.get_kind () == PK_AFTER_SUPERNODE |
| 2268 |
|| (dst_node->m_succs.length () > 1 |
| 2269 |
&& (iter_point |
| 2270 |
== dst_node->m_succs[0]->m_dest->get_point ()))) |
| 2271 |
break; |
| 2272 |
} |
| 2273 |
|
| 2274 |
} |
| 2275 |
} |
| 2276 |
break; |
| 2277 |
} |
| 2278 |
|
| 2279 |
/* Look for changes in dynamic extents, which will identify |
| 2280 |
the creation of heap-based regions and alloca regions. */ |
| 2281 |
if (interest) |
| 2282 |
{ |
| 2283 |
const region_model *src_model = src_state.m_region_model; |
| 2284 |
const region_model *dst_model = dst_state.m_region_model; |
| 2285 |
if (src_model->get_dynamic_extents () |
| 2286 |
!= dst_model->get_dynamic_extents ()) |
| 2287 |
{ |
| 2288 |
unsigned i; |
| 2289 |
const region *reg; |
| 2290 |
FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg) |
| 2291 |
{ |
| 2292 |
const region *base_reg = reg->get_base_region (); |
| 2293 |
const svalue *old_extents |
| 2294 |
= src_model->get_dynamic_extents (base_reg); |
| 2295 |
const svalue *new_extents |
| 2296 |
= dst_model->get_dynamic_extents (base_reg); |
| 2297 |
if (old_extents == NULL && new_extents != NULL) |
| 2298 |
switch (base_reg->get_kind ()) |
| 2299 |
{ |
| 2300 |
default: |
| 2301 |
break; |
| 2302 |
case RK_HEAP_ALLOCATED: |
| 2303 |
case RK_ALLOCA: |
| 2304 |
emission_path->add_region_creation_events |
| 2305 |
(pb.get_pending_diagnostic (), |
| 2306 |
reg, dst_model, |
| 2307 |
event_loc_info (src_point.get_location (), |
| 2308 |
src_point.get_fndecl (), |
| 2309 |
src_stack_depth), |
| 2310 |
m_verbosity > 3); |
| 2311 |
break; |
| 2312 |
} |
| 2313 |
} |
| 2314 |
} |
| 2315 |
} |
| 2316 |
|
| 2317 |
if (pb.get_feasibility_problem () |
| 2318 |
&& &pb.get_feasibility_problem ()->m_eedge == &eedge) |
| 2319 |
{ |
| 2320 |
pretty_printer pp; |
| 2321 |
pp_format_decoder (&pp) = default_tree_printer; |
| 2322 |
pp_string (&pp, |
| 2323 |
"this path would have been rejected as infeasible" |
| 2324 |
" at this edge: "); |
| 2325 |
pb.get_feasibility_problem ()->dump_to_pp (&pp); |
| 2326 |
emission_path->add_event |
| 2327 |
(make_unique<precanned_custom_event> |
| 2328 |
(event_loc_info (dst_point.get_location (), |
| 2329 |
dst_point.get_fndecl (), |
| 2330 |
dst_stack_depth), |
| 2331 |
pp_formatted_text (&pp))); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
/* Return true if EEDGE is a significant edge in the path to the diagnostic |
| 2336 |
for PB. |
| 2337 |
|
| 2338 |
Consider all of the sibling out-eedges from the same source enode |
| 2339 |
as EEDGE. |
| 2340 |
If there's no path from the destinations of those eedges to the |
| 2341 |
diagnostic enode, then we have to take this eedge and thus it's |
| 2342 |
significant. |
| 2343 |
|
| 2344 |
Conversely if there is a path from the destination of any other sibling |
| 2345 |
eedge to the diagnostic enode, then this edge is insignificant. |
| 2346 |
|
| 2347 |
Example 1: redundant if-else: |
| 2348 |
|
| 2349 |
(A) if (...) A |
| 2350 |
(B) ... / \ |
| 2351 |
else B C |
| 2352 |
(C) ... \ / |
| 2353 |
(D) [DIAGNOSTIC] D |
| 2354 |
|
| 2355 |
D is reachable by either B or C, so neither of these edges |
| 2356 |
are significant. |
| 2357 |
|
| 2358 |
Example 2: pertinent if-else: |
| 2359 |
|
| 2360 |
(A) if (...) A |
| 2361 |
(B) ... / \ |
| 2362 |
else B C |
| 2363 |
(C) [NECESSARY CONDITION] | | |
| 2364 |
(D) [POSSIBLE DIAGNOSTIC] D1 D2 |
| 2365 |
|
| 2366 |
D becomes D1 and D2 in the exploded graph, where the diagnostic occurs |
| 2367 |
at D2. D2 is only reachable via C, so the A -> C edge is significant. |
| 2368 |
|
| 2369 |
Example 3: redundant loop: |
| 2370 |
|
| 2371 |
(A) while (...) +-->A |
| 2372 |
(B) ... | / \ |
| 2373 |
(C) ... +-B C |
| 2374 |
(D) [DIAGNOSTIC] | |
| 2375 |
D |
| 2376 |
|
| 2377 |
D is reachable from both B and C, so the A->C edge is not significant. */ |
| 2378 |
|
| 2379 |
bool |
| 2380 |
diagnostic_manager::significant_edge_p (const path_builder &pb, |
| 2381 |
const exploded_edge &eedge) const |
| 2382 |
{ |
| 2383 |
int i; |
| 2384 |
exploded_edge *sibling; |
| 2385 |
FOR_EACH_VEC_ELT (eedge.m_src->m_succs, i, sibling) |
| 2386 |
{ |
| 2387 |
if (sibling == &eedge) |
| 2388 |
continue; |
| 2389 |
if (pb.reachable_from_p (sibling->m_dest)) |
| 2390 |
{ |
| 2391 |
if (get_logger ()) |
| 2392 |
get_logger ()->log (" edge EN: %i -> EN: %i is insignificant as" |
| 2393 |
" EN: %i is also reachable via" |
| 2394 |
" EN: %i -> EN: %i", |
| 2395 |
eedge.m_src->m_index, eedge.m_dest->m_index, |
| 2396 |
pb.get_diag_node ()->m_index, |
| 2397 |
sibling->m_src->m_index, |
| 2398 |
sibling->m_dest->m_index); |
| 2399 |
return false; |
| 2400 |
} |
| 2401 |
} |
| 2402 |
|
| 2403 |
return true; |
| 2404 |
} |
| 2405 |
|
| 2406 |
/* Subroutine of diagnostic_manager::add_events_for_eedge |
| 2407 |
where EEDGE has an underlying superedge i.e. a CFG edge, |
| 2408 |
or an interprocedural call/return. |
| 2409 |
Add any events for the superedge to EMISSION_PATH. */ |
| 2410 |
|
| 2411 |
void |
| 2412 |
diagnostic_manager::add_events_for_superedge (const path_builder &pb, |
| 2413 |
const exploded_edge &eedge, |
| 2414 |
checker_path *emission_path) |
| 2415 |
const |
| 2416 |
{ |
| 2417 |
gcc_assert (eedge.m_sedge); |
| 2418 |
|
| 2419 |
/* Give diagnostics an opportunity to override this function. */ |
| 2420 |
pending_diagnostic *pd = pb.get_pending_diagnostic (); |
| 2421 |
if (pd->maybe_add_custom_events_for_superedge (eedge, emission_path)) |
| 2422 |
return; |
| 2423 |
|
| 2424 |
/* Don't add events for insignificant edges at verbosity levels below 3. */ |
| 2425 |
if (m_verbosity < 3) |
| 2426 |
if (!significant_edge_p (pb, eedge)) |
| 2427 |
return; |
| 2428 |
|
| 2429 |
const exploded_node *src_node = eedge.m_src; |
| 2430 |
const program_point &src_point = src_node->get_point (); |
| 2431 |
const exploded_node *dst_node = eedge.m_dest; |
| 2432 |
const program_point &dst_point = dst_node->get_point (); |
| 2433 |
const int src_stack_depth = src_point.get_stack_depth (); |
| 2434 |
const int dst_stack_depth = dst_point.get_stack_depth (); |
| 2435 |
const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt (); |
| 2436 |
|
| 2437 |
switch (eedge.m_sedge->m_kind) |
| 2438 |
{ |
| 2439 |
case SUPEREDGE_CFG_EDGE: |
| 2440 |
{ |
| 2441 |
emission_path->add_event |
| 2442 |
(make_unique<start_cfg_edge_event> |
| 2443 |
(eedge, |
| 2444 |
event_loc_info (last_stmt ? last_stmt->location : UNKNOWN_LOCATION, |
| 2445 |
src_point.get_fndecl (), |
| 2446 |
src_stack_depth))); |
| 2447 |
emission_path->add_event |
| 2448 |
(make_unique<end_cfg_edge_event> |
| 2449 |
(eedge, |
| 2450 |
event_loc_info (dst_point.get_supernode ()->get_start_location (), |
| 2451 |
dst_point.get_fndecl (), |
| 2452 |
dst_stack_depth))); |
| 2453 |
} |
| 2454 |
break; |
| 2455 |
|
| 2456 |
case SUPEREDGE_CALL: |
| 2457 |
pd->add_call_event (eedge, emission_path); |
| 2458 |
break; |
| 2459 |
|
| 2460 |
case SUPEREDGE_INTRAPROCEDURAL_CALL: |
| 2461 |
{ |
| 2462 |
/* TODO: add a subclass for this, or generate events for the |
| 2463 |
summary. */ |
| 2464 |
emission_path->add_event |
| 2465 |
(make_unique<debug_event> (event_loc_info (last_stmt |
| 2466 |
? last_stmt->location |
| 2467 |
: UNKNOWN_LOCATION, |
| 2468 |
src_point.get_fndecl (), |
| 2469 |
src_stack_depth), |
| 2470 |
"call summary")); |
| 2471 |
} |
| 2472 |
break; |
| 2473 |
|
| 2474 |
case SUPEREDGE_RETURN: |
| 2475 |
{ |
| 2476 |
const return_superedge *return_edge |
| 2477 |
= as_a <const return_superedge *> (eedge.m_sedge); |
| 2478 |
|
| 2479 |
const gcall *call_stmt = return_edge->get_call_stmt (); |
| 2480 |
emission_path->add_event |
| 2481 |
(make_unique<return_event> (eedge, |
| 2482 |
event_loc_info (call_stmt |
| 2483 |
? call_stmt->location |
| 2484 |
: UNKNOWN_LOCATION, |
| 2485 |
dst_point.get_fndecl (), |
| 2486 |
dst_stack_depth))); |
| 2487 |
} |
| 2488 |
break; |
| 2489 |
} |
| 2490 |
} |
| 2491 |
|
| 2492 |
/* Prune PATH, based on the verbosity level, to the most pertinent |
| 2493 |
events for a diagnostic that involves VAR ending in state STATE |
| 2494 |
(for state machine SM). |
| 2495 |
|
| 2496 |
PATH is updated in place, and the redundant checker_events are deleted. |
| 2497 |
|
| 2498 |
As well as deleting events, call record_critical_state on events in |
| 2499 |
which state critical to the pending_diagnostic is being handled; see |
| 2500 |
the comment for diagnostic_manager::prune_for_sm_diagnostic. */ |
| 2501 |
|
| 2502 |
void |
| 2503 |
diagnostic_manager::prune_path (checker_path *path, |
| 2504 |
const state_machine *sm, |
| 2505 |
const svalue *sval, |
| 2506 |
state_machine::state_t state) const |
| 2507 |
{ |
| 2508 |
LOG_FUNC (get_logger ()); |
| 2509 |
path->maybe_log (get_logger (), "path"); |
| 2510 |
prune_for_sm_diagnostic (path, sm, sval, state); |
| 2511 |
prune_interproc_events (path); |
| 2512 |
if (! flag_analyzer_show_events_in_system_headers) |
| 2513 |
prune_system_headers (path); |
| 2514 |
consolidate_conditions (path); |
| 2515 |
finish_pruning (path); |
| 2516 |
path->maybe_log (get_logger (), "pruned"); |
| 2517 |
} |
| 2518 |
|
| 2519 |
/* A cheap test to determine if EXPR can be the expression of interest in |
| 2520 |
an sm-diagnostic, so that we can reject cases where we have a non-lvalue. |
| 2521 |
We don't have always have a model when calling this, so we can't use |
| 2522 |
tentative_region_model_context, so there can be false positives. */ |
| 2523 |
|
| 2524 |
static bool |
| 2525 |
can_be_expr_of_interest_p (tree expr) |
| 2526 |
{ |
| 2527 |
if (!expr) |
| 2528 |
return false; |
| 2529 |
|
| 2530 |
/* Reject constants. */ |
| 2531 |
if (CONSTANT_CLASS_P (expr)) |
| 2532 |
return false; |
| 2533 |
|
| 2534 |
/* Otherwise assume that it can be an lvalue. */ |
| 2535 |
return true; |
| 2536 |
} |
| 2537 |
|
| 2538 |
/* First pass of diagnostic_manager::prune_path: apply verbosity level, |
| 2539 |
pruning unrelated state change events. |
| 2540 |
|
| 2541 |
Iterate backwards through PATH, skipping state change events that aren't |
| 2542 |
VAR but update the pertinent VAR when state-copying occurs. |
| 2543 |
|
| 2544 |
As well as deleting events, call record_critical_state on events in |
| 2545 |
which state critical to the pending_diagnostic is being handled, so |
| 2546 |
that the event's get_desc vfunc can potentially supply a more precise |
| 2547 |
description of the event to the user. |
| 2548 |
e.g. improving |
| 2549 |
"calling 'foo' from 'bar'" |
| 2550 |
to |
| 2551 |
"passing possibly-NULL pointer 'ptr' to 'foo' from 'bar' as param 1" |
| 2552 |
when the diagnostic relates to later dereferencing 'ptr'. */ |
| 2553 |
|
| 2554 |
void |
| 2555 |
diagnostic_manager::prune_for_sm_diagnostic (checker_path *path, |
| 2556 |
const state_machine *sm, |
| 2557 |
const svalue *sval, |
| 2558 |
state_machine::state_t state) const |
| 2559 |
{ |
| 2560 |
int idx = path->num_events () - 1; |
| 2561 |
while (idx >= 0 && idx < (signed)path->num_events ()) |
| 2562 |
{ |
| 2563 |
checker_event *base_event = path->get_checker_event (idx); |
| 2564 |
if (get_logger ()) |
| 2565 |
{ |
| 2566 |
if (sm) |
| 2567 |
{ |
| 2568 |
if (sval) |
| 2569 |
{ |
| 2570 |
label_text sval_desc = sval->get_desc (); |
| 2571 |
log ("considering event %i (%s), with sval: %qs, state: %qs", |
| 2572 |
idx, event_kind_to_string (base_event->m_kind), |
| 2573 |
sval_desc.get (), state->get_name ()); |
| 2574 |
} |
| 2575 |
else |
| 2576 |
log ("considering event %i (%s), with global state: %qs", |
| 2577 |
idx, event_kind_to_string (base_event->m_kind), |
| 2578 |
state->get_name ()); |
| 2579 |
} |
| 2580 |
else |
| 2581 |
log ("considering event %i", idx); |
| 2582 |
} |
| 2583 |
|
| 2584 |
switch (base_event->m_kind) |
| 2585 |
{ |
| 2586 |
default: |
| 2587 |
gcc_unreachable (); |
| 2588 |
|
| 2589 |
case EK_DEBUG: |
| 2590 |
if (m_verbosity < 4) |
| 2591 |
{ |
| 2592 |
log ("filtering event %i: debug event", idx); |
| 2593 |
path->delete_event (idx); |
| 2594 |
} |
| 2595 |
break; |
| 2596 |
|
| 2597 |
case EK_CUSTOM: |
| 2598 |
/* Don't filter custom events. */ |
| 2599 |
break; |
| 2600 |
|
| 2601 |
case EK_STMT: |
| 2602 |
{ |
| 2603 |
if (m_verbosity < 4) |
| 2604 |
{ |
| 2605 |
log ("filtering event %i: statement event", idx); |
| 2606 |
path->delete_event (idx); |
| 2607 |
} |
| 2608 |
} |
| 2609 |
break; |
| 2610 |
|
| 2611 |
case EK_REGION_CREATION: |
| 2612 |
/* Don't filter these. */ |
| 2613 |
break; |
| 2614 |
|
| 2615 |
case EK_FUNCTION_ENTRY: |
| 2616 |
if (m_verbosity < 1) |
| 2617 |
{ |
| 2618 |
log ("filtering event %i: function entry", idx); |
| 2619 |
path->delete_event (idx); |
| 2620 |
} |
| 2621 |
break; |
| 2622 |
|
| 2623 |
case EK_STATE_CHANGE: |
| 2624 |
{ |
| 2625 |
state_change_event *state_change = (state_change_event *)base_event; |
| 2626 |
gcc_assert (state_change->m_dst_state.m_region_model); |
| 2627 |
|
| 2628 |
if (state_change->m_sval == sval) |
| 2629 |
{ |
| 2630 |
if (state_change->m_origin) |
| 2631 |
{ |
| 2632 |
if (get_logger ()) |
| 2633 |
{ |
| 2634 |
label_text sval_desc = sval->get_desc (); |
| 2635 |
label_text origin_sval_desc |
| 2636 |
= state_change->m_origin->get_desc (); |
| 2637 |
log ("event %i:" |
| 2638 |
" switching var of interest from %qs to %qs", |
| 2639 |
idx, sval_desc.get (), |
| 2640 |
origin_sval_desc.get ()); |
| 2641 |
} |
| 2642 |
sval = state_change->m_origin; |
| 2643 |
} |
| 2644 |
log ("event %i: switching state of interest from %qs to %qs", |
| 2645 |
idx, state_change->m_to->get_name (), |
| 2646 |
state_change->m_from->get_name ()); |
| 2647 |
state = state_change->m_from; |
| 2648 |
} |
| 2649 |
else if (m_verbosity < 4) |
| 2650 |
{ |
| 2651 |
if (get_logger ()) |
| 2652 |
{ |
| 2653 |
if (state_change->m_sval) |
| 2654 |
{ |
| 2655 |
label_text change_sval_desc |
| 2656 |
= state_change->m_sval->get_desc (); |
| 2657 |
if (sval) |
| 2658 |
{ |
| 2659 |
label_text sval_desc = sval->get_desc (); |
| 2660 |
log ("filtering event %i:" |
| 2661 |
" state change to %qs unrelated to %qs", |
| 2662 |
idx, change_sval_desc.get (), |
| 2663 |
sval_desc.get ()); |
| 2664 |
} |
| 2665 |
else |
| 2666 |
log ("filtering event %i: state change to %qs", |
| 2667 |
idx, change_sval_desc.get ()); |
| 2668 |
} |
| 2669 |
else |
| 2670 |
log ("filtering event %i: global state change", idx); |
| 2671 |
} |
| 2672 |
path->delete_event (idx); |
| 2673 |
} |
| 2674 |
} |
| 2675 |
break; |
| 2676 |
|
| 2677 |
case EK_START_CFG_EDGE: |
| 2678 |
{ |
| 2679 |
cfg_edge_event *event = (cfg_edge_event *)base_event; |
| 2680 |
|
| 2681 |
/* TODO: is this edge significant to var? |
| 2682 |
See if var can be in other states in the dest, but not |
| 2683 |
in other states in the src? |
| 2684 |
Must have multiple sibling edges. */ |
| 2685 |
|
| 2686 |
if (event->should_filter_p (m_verbosity)) |
| 2687 |
{ |
| 2688 |
log ("filtering events %i and %i: CFG edge", idx, idx + 1); |
| 2689 |
path->delete_event (idx); |
| 2690 |
/* Also delete the corresponding EK_END_CFG_EDGE. */ |
| 2691 |
gcc_assert (path->get_checker_event (idx)->m_kind |
| 2692 |
== EK_END_CFG_EDGE); |
| 2693 |
path->delete_event (idx); |
| 2694 |
} |
| 2695 |
} |
| 2696 |
break; |
| 2697 |
|
| 2698 |
case EK_END_CFG_EDGE: |
| 2699 |
/* These come in pairs with EK_START_CFG_EDGE events and are |
| 2700 |
filtered when their start event is filtered. */ |
| 2701 |
break; |
| 2702 |
|
| 2703 |
case EK_CALL_EDGE: |
| 2704 |
{ |
| 2705 |
call_event *event = (call_event *)base_event; |
| 2706 |
const region_model *callee_model |
| 2707 |
= event->m_eedge.m_dest->get_state ().m_region_model; |
| 2708 |
const region_model *caller_model |
| 2709 |
= event->m_eedge.m_src->get_state ().m_region_model; |
| 2710 |
tree callee_var = callee_model->get_representative_tree (sval); |
| 2711 |
callsite_expr expr; |
| 2712 |
|
| 2713 |
tree caller_var; |
| 2714 |
if(event->m_sedge) |
| 2715 |
{ |
| 2716 |
const callgraph_superedge& cg_superedge |
| 2717 |
= event->get_callgraph_superedge (); |
| 2718 |
if (cg_superedge.m_cedge) |
| 2719 |
caller_var |
| 2720 |
= cg_superedge.map_expr_from_callee_to_caller (callee_var, |
| 2721 |
&expr); |
| 2722 |
else |
| 2723 |
caller_var = caller_model->get_representative_tree (sval); |
| 2724 |
} |
| 2725 |
else |
| 2726 |
caller_var = caller_model->get_representative_tree (sval); |
| 2727 |
|
| 2728 |
if (caller_var) |
| 2729 |
{ |
| 2730 |
if (get_logger ()) |
| 2731 |
{ |
| 2732 |
label_text sval_desc = sval->get_desc (); |
| 2733 |
log ("event %i:" |
| 2734 |
" recording critical state for %qs at call" |
| 2735 |
" from %qE in callee to %qE in caller", |
| 2736 |
idx, sval_desc.get (), callee_var, caller_var); |
| 2737 |
} |
| 2738 |
if (expr.param_p ()) |
| 2739 |
event->record_critical_state (caller_var, state); |
| 2740 |
} |
| 2741 |
} |
| 2742 |
break; |
| 2743 |
|
| 2744 |
case EK_RETURN_EDGE: |
| 2745 |
{ |
| 2746 |
if (sval) |
| 2747 |
{ |
| 2748 |
return_event *event = (return_event *)base_event; |
| 2749 |
const region_model *caller_model |
| 2750 |
= event->m_eedge.m_dest->get_state ().m_region_model; |
| 2751 |
tree caller_var = caller_model->get_representative_tree (sval); |
| 2752 |
const region_model *callee_model |
| 2753 |
= event->m_eedge.m_src->get_state ().m_region_model; |
| 2754 |
callsite_expr expr; |
| 2755 |
|
| 2756 |
tree callee_var; |
| 2757 |
if (event->m_sedge) |
| 2758 |
{ |
| 2759 |
const callgraph_superedge& cg_superedge |
| 2760 |
= event->get_callgraph_superedge (); |
| 2761 |
if (cg_superedge.m_cedge) |
| 2762 |
callee_var |
| 2763 |
= cg_superedge.map_expr_from_caller_to_callee (caller_var, |
| 2764 |
&expr); |
| 2765 |
else |
| 2766 |
callee_var = callee_model->get_representative_tree (sval); |
| 2767 |
} |
| 2768 |
else |
| 2769 |
callee_var = callee_model->get_representative_tree (sval); |
| 2770 |
|
| 2771 |
if (callee_var) |
| 2772 |
{ |
| 2773 |
if (get_logger ()) |
| 2774 |
{ |
| 2775 |
label_text sval_desc = sval->get_desc (); |
| 2776 |
log ("event %i:" |
| 2777 |
" recording critical state for %qs at return" |
| 2778 |
" from %qE in caller to %qE in callee", |
| 2779 |
idx, sval_desc.get (), callee_var, callee_var); |
| 2780 |
} |
| 2781 |
if (expr.return_value_p ()) |
| 2782 |
event->record_critical_state (callee_var, state); |
| 2783 |
} |
| 2784 |
} |
| 2785 |
} |
| 2786 |
break; |
| 2787 |
|
| 2788 |
case EK_INLINED_CALL: |
| 2789 |
/* We don't expect to see these yet, as they're added later. |
| 2790 |
We'd want to keep them around. */ |
| 2791 |
break; |
| 2792 |
|
| 2793 |
case EK_SETJMP: |
| 2794 |
/* TODO: only show setjmp_events that matter i.e. those for which |
| 2795 |
there is a later rewind event using them. */ |
| 2796 |
case EK_REWIND_FROM_LONGJMP: |
| 2797 |
case EK_REWIND_TO_SETJMP: |
| 2798 |
break; |
| 2799 |
|
| 2800 |
case EK_WARNING: |
| 2801 |
/* Always show the final "warning" event in the path. */ |
| 2802 |
break; |
| 2803 |
} |
| 2804 |
idx--; |
| 2805 |
} |
| 2806 |
} |
| 2807 |
|
| 2808 |
/* Subroutine of diagnostic_manager::prune_for_sm_diagnostic. |
| 2809 |
If *EXPR is not suitable to be the expression of interest in |
| 2810 |
an sm-diagnostic, set *EXPR to NULL and log. */ |
| 2811 |
|
| 2812 |
void |
| 2813 |
diagnostic_manager::update_for_unsuitable_sm_exprs (tree *expr) const |
| 2814 |
{ |
| 2815 |
gcc_assert (expr); |
| 2816 |
if (*expr && !can_be_expr_of_interest_p (*expr)) |
| 2817 |
{ |
| 2818 |
log ("new var %qE is unsuitable; setting var to NULL", *expr); |
| 2819 |
*expr = NULL_TREE; |
| 2820 |
} |
| 2821 |
} |
| 2822 |
|
| 2823 |
/* Second pass of diagnostic_manager::prune_path: remove redundant |
| 2824 |
interprocedural information. |
| 2825 |
|
| 2826 |
For example, given: |
| 2827 |
(1)- calling "f2" from "f1" |
| 2828 |
(2)--- entry to "f2" |
| 2829 |
(3)--- calling "f3" from "f2" |
| 2830 |
(4)----- entry to "f3" |
| 2831 |
(5)--- returning to "f2" to "f3" |
| 2832 |
(6)- returning to "f1" to "f2" |
| 2833 |
with no other intervening events, then none of these events are |
| 2834 |
likely to be interesting to the user. |
| 2835 |
|
| 2836 |
Prune [..., call, function-entry, return, ...] triples repeatedly |
| 2837 |
until nothing has changed. For the example above, this would |
| 2838 |
remove events (3, 4, 5), and then remove events (1, 2, 6). */ |
| 2839 |
|
| 2840 |
void |
| 2841 |
diagnostic_manager::prune_interproc_events (checker_path *path) const |
| 2842 |
{ |
| 2843 |
bool changed = false; |
| 2844 |
do |
| 2845 |
{ |
| 2846 |
changed = false; |
| 2847 |
int idx = (signed)path->num_events () - 1; |
| 2848 |
while (idx >= 0) |
| 2849 |
{ |
| 2850 |
/* Prune [..., call, function-entry, return, ...] triples. */ |
| 2851 |
if (idx + 2 < (signed)path->num_events () |
| 2852 |
&& path->get_checker_event (idx)->is_call_p () |
| 2853 |
&& path->get_checker_event (idx + 1)->is_function_entry_p () |
| 2854 |
&& path->get_checker_event (idx + 2)->is_return_p ()) |
| 2855 |
{ |
| 2856 |
if (get_logger ()) |
| 2857 |
{ |
| 2858 |
label_text desc |
| 2859 |
(path->get_checker_event (idx)->get_desc |
| 2860 |
(*global_dc->get_reference_printer ())); |
| 2861 |
log ("filtering events %i-%i:" |
| 2862 |
" irrelevant call/entry/return: %s", |
| 2863 |
idx, idx + 2, desc.get ()); |
| 2864 |
} |
| 2865 |
path->delete_event (idx + 2); |
| 2866 |
path->delete_event (idx + 1); |
| 2867 |
path->delete_event (idx); |
| 2868 |
changed = true; |
| 2869 |
idx--; |
| 2870 |
continue; |
| 2871 |
} |
| 2872 |
|
| 2873 |
/* Prune [..., call, return, ...] pairs |
| 2874 |
(for -fanalyzer-verbosity=0). */ |
| 2875 |
if (idx + 1 < (signed)path->num_events () |
| 2876 |
&& path->get_checker_event (idx)->is_call_p () |
| 2877 |
&& path->get_checker_event (idx + 1)->is_return_p ()) |
| 2878 |
{ |
| 2879 |
if (get_logger ()) |
| 2880 |
{ |
| 2881 |
label_text desc |
| 2882 |
(path->get_checker_event (idx)->get_desc |
| 2883 |
(*global_dc->get_reference_printer ())); |
| 2884 |
log ("filtering events %i-%i:" |
| 2885 |
" irrelevant call/return: %s", |
| 2886 |
idx, idx + 1, desc.get ()); |
| 2887 |
} |
| 2888 |
path->delete_event (idx + 1); |
| 2889 |
path->delete_event (idx); |
| 2890 |
changed = true; |
| 2891 |
idx--; |
| 2892 |
continue; |
| 2893 |
} |
| 2894 |
|
| 2895 |
idx--; |
| 2896 |
} |
| 2897 |
|
| 2898 |
} |
| 2899 |
while (changed); |
| 2900 |
} |
| 2901 |
|
| 2902 |
/* Remove everything within [call point, IDX]. For consistency, |
| 2903 |
IDX should represent the return event of the frame to delete, |
| 2904 |
or if there is none it should be the last event of the frame. |
| 2905 |
After this function, IDX designates the event prior to calling |
| 2906 |
this frame. */ |
| 2907 |
|
| 2908 |
static void |
| 2909 |
prune_frame (checker_path *path, int &idx) |
| 2910 |
{ |
| 2911 |
gcc_assert (idx >= 0); |
| 2912 |
int nesting = 1; |
| 2913 |
if (path->get_checker_event (idx)->is_return_p ()) |
| 2914 |
nesting = 0; |
| 2915 |
do |
| 2916 |
{ |
| 2917 |
if (path->get_checker_event (idx)->is_call_p ()) |
| 2918 |
nesting--; |
| 2919 |
else if (path->get_checker_event (idx)->is_return_p ()) |
| 2920 |
nesting++; |
| 2921 |
|
| 2922 |
path->delete_event (idx--); |
| 2923 |
} while (idx >= 0 && nesting != 0); |
| 2924 |
} |
| 2925 |
|
| 2926 |
/* This function is called when fanalyzer-show-events-in-system-headers |
| 2927 |
is disabled and will prune the diagnostic of all events within a |
| 2928 |
system header, only keeping the entry and exit events to the header. |
| 2929 |
This should be called after diagnostic_manager::prune_interproc_events |
| 2930 |
so that sucessive events [system header call, system header return] |
| 2931 |
are preserved thereafter. |
| 2932 |
|
| 2933 |
Given a diagnostics path diving into a system header in the form |
| 2934 |
[ |
| 2935 |
prefix events..., |
| 2936 |
system header call, |
| 2937 |
system header entry, |
| 2938 |
events within system headers..., |
| 2939 |
system header return, |
| 2940 |
suffix events... |
| 2941 |
] |
| 2942 |
|
| 2943 |
then transforms it into |
| 2944 |
[ |
| 2945 |
prefix events..., |
| 2946 |
system header call, |
| 2947 |
system header return, |
| 2948 |
suffix events... |
| 2949 |
]. */ |
| 2950 |
|
| 2951 |
void |
| 2952 |
diagnostic_manager::prune_system_headers (checker_path *path) const |
| 2953 |
{ |
| 2954 |
int idx = (signed)path->num_events () - 1; |
| 2955 |
while (idx >= 0) |
| 2956 |
{ |
| 2957 |
const checker_event *event = path->get_checker_event (idx); |
| 2958 |
/* Prune everything between |
| 2959 |
[..., system entry, (...), system return, ...]. */ |
| 2960 |
if (event->is_return_p () |
| 2961 |
&& in_system_header_at (event->get_location ())) |
| 2962 |
{ |
| 2963 |
int ret_idx = idx; |
| 2964 |
prune_frame (path, idx); |
| 2965 |
|
| 2966 |
if (get_logger ()) |
| 2967 |
{ |
| 2968 |
log ("filtering system headers events %i-%i:", |
| 2969 |
idx, ret_idx); |
| 2970 |
} |
| 2971 |
// Delete function entry within system headers. |
| 2972 |
if (idx >= 0) |
| 2973 |
{ |
| 2974 |
event = path->get_checker_event (idx); |
| 2975 |
if (event->is_function_entry_p () |
| 2976 |
&& in_system_header_at (event->get_location ())) |
| 2977 |
{ |
| 2978 |
if (get_logger ()) |
| 2979 |
{ |
| 2980 |
label_text desc |
| 2981 |
(event->get_desc (*global_dc->get_reference_printer ())); |
| 2982 |
log ("filtering event %i:" |
| 2983 |
"system header entry event: %s", |
| 2984 |
idx, desc.get ()); |
| 2985 |
} |
| 2986 |
|
| 2987 |
path->delete_event (idx); |
| 2988 |
} |
| 2989 |
} |
| 2990 |
} |
| 2991 |
|
| 2992 |
idx--; |
| 2993 |
} |
| 2994 |
} |
| 2995 |
|
| 2996 |
/* Return true iff event IDX within PATH is on the same line as REF_EXP_LOC. */ |
| 2997 |
|
| 2998 |
static bool |
| 2999 |
same_line_as_p (const expanded_location &ref_exp_loc, |
| 3000 |
checker_path *path, unsigned idx) |
| 3001 |
{ |
| 3002 |
const checker_event *ev = path->get_checker_event (idx); |
| 3003 |
expanded_location idx_exp_loc = expand_location (ev->get_location ()); |
| 3004 |
gcc_assert (ref_exp_loc.file); |
| 3005 |
if (idx_exp_loc.file == NULL) |
| 3006 |
return false; |
| 3007 |
if (strcmp (ref_exp_loc.file, idx_exp_loc.file)) |
| 3008 |
return false; |
| 3009 |
return ref_exp_loc.line == idx_exp_loc.line; |
| 3010 |
} |
| 3011 |
|
| 3012 |
/* This path-readability optimization reduces the verbosity of compound |
| 3013 |
conditional statements (without needing to reconstruct the AST, which |
| 3014 |
has already been lost). |
| 3015 |
|
| 3016 |
For example, it converts: |
| 3017 |
|
| 3018 |
| 61 | if (cp[0] != '\0' && cp[0] != '#') |
| 3019 |
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 3020 |
| | | | | |
| 3021 |
| | | | (6) ...to here |
| 3022 |
| | | (7) following ‘true’ branch... |
| 3023 |
| | (5) following ‘true’ branch... |
| 3024 |
| 62 | { |
| 3025 |
| 63 | alias = cp++; |
| 3026 |
| | ~~~~ |
| 3027 |
| | | |
| 3028 |
| | (8) ...to here |
| 3029 |
|
| 3030 |
into: |
| 3031 |
|
| 3032 |
| 61 | if (cp[0] != '\0' && cp[0] != '#') |
| 3033 |
| | ~ |
| 3034 |
| | | |
| 3035 |
| | (5) following ‘true’ branch... |
| 3036 |
| 62 | { |
| 3037 |
| 63 | alias = cp++; |
| 3038 |
| | ~~~~ |
| 3039 |
| | | |
| 3040 |
| | (6) ...to here |
| 3041 |
|
| 3042 |
by combining events 5-8 into new events 5-6. |
| 3043 |
|
| 3044 |
Find runs of consecutive (start_cfg_edge_event, end_cfg_edge_event) pairs |
| 3045 |
in which all events apart from the final end_cfg_edge_event are on the same |
| 3046 |
line, and for which either all the CFG edges are TRUE edges, or all are |
| 3047 |
FALSE edges. |
| 3048 |
|
| 3049 |
Consolidate each such run into a |
| 3050 |
(start_consolidated_cfg_edges_event, end_consolidated_cfg_edges_event) |
| 3051 |
pair. */ |
| 3052 |
|
| 3053 |
void |
| 3054 |
diagnostic_manager::consolidate_conditions (checker_path *path) const |
| 3055 |
{ |
| 3056 |
/* Don't simplify edges if we're debugging them. */ |
| 3057 |
if (flag_analyzer_verbose_edges) |
| 3058 |
return; |
| 3059 |
|
| 3060 |
for (int start_idx = 0; |
| 3061 |
start_idx < (signed)path->num_events () - 1; |
| 3062 |
start_idx++) |
| 3063 |
{ |
| 3064 |
if (path->cfg_edge_pair_at_p (start_idx)) |
| 3065 |
{ |
| 3066 |
const checker_event *old_start_ev |
| 3067 |
= path->get_checker_event (start_idx); |
| 3068 |
expanded_location start_exp_loc |
| 3069 |
= expand_location (old_start_ev->get_location ()); |
| 3070 |
if (start_exp_loc.file == NULL) |
| 3071 |
continue; |
| 3072 |
if (!same_line_as_p (start_exp_loc, path, start_idx + 1)) |
| 3073 |
continue; |
| 3074 |
|
| 3075 |
/* Are we looking for a run of all TRUE edges, or all FALSE edges? */ |
| 3076 |
gcc_assert (old_start_ev->m_kind == EK_START_CFG_EDGE); |
| 3077 |
const start_cfg_edge_event *old_start_cfg_ev |
| 3078 |
= (const start_cfg_edge_event *)old_start_ev; |
| 3079 |
const cfg_superedge& first_cfg_sedge |
| 3080 |
= old_start_cfg_ev->get_cfg_superedge (); |
| 3081 |
bool edge_sense; |
| 3082 |
if (first_cfg_sedge.true_value_p ()) |
| 3083 |
edge_sense = true; |
| 3084 |
else if (first_cfg_sedge.false_value_p ()) |
| 3085 |
edge_sense = false; |
| 3086 |
else |
| 3087 |
continue; |
| 3088 |
|
| 3089 |
/* Find a run of CFG start/end event pairs from |
| 3090 |
[start_idx, next_idx) |
| 3091 |
where all apart from the final event are on the same line, |
| 3092 |
and all are either TRUE or FALSE edges, matching the initial. */ |
| 3093 |
int next_idx = start_idx + 2; |
| 3094 |
while (path->cfg_edge_pair_at_p (next_idx) |
| 3095 |
&& same_line_as_p (start_exp_loc, path, next_idx)) |
| 3096 |
{ |
| 3097 |
const checker_event *iter_ev |
| 3098 |
= path->get_checker_event (next_idx); |
| 3099 |
gcc_assert (iter_ev->m_kind == EK_START_CFG_EDGE); |
| 3100 |
const start_cfg_edge_event *iter_cfg_ev |
| 3101 |
= (const start_cfg_edge_event *)iter_ev; |
| 3102 |
const cfg_superedge& iter_cfg_sedge |
| 3103 |
= iter_cfg_ev->get_cfg_superedge (); |
| 3104 |
if (edge_sense) |
| 3105 |
{ |
| 3106 |
if (!iter_cfg_sedge.true_value_p ()) |
| 3107 |
break; |
| 3108 |
} |
| 3109 |
else |
| 3110 |
{ |
| 3111 |
if (!iter_cfg_sedge.false_value_p ()) |
| 3112 |
break; |
| 3113 |
} |
| 3114 |
next_idx += 2; |
| 3115 |
} |
| 3116 |
|
| 3117 |
/* If we have more than one pair in the run, consolidate. */ |
| 3118 |
if (next_idx > start_idx + 2) |
| 3119 |
{ |
| 3120 |
const checker_event *old_end_ev |
| 3121 |
= path->get_checker_event (next_idx - 1); |
| 3122 |
log ("consolidating CFG edge events %i-%i into %i-%i", |
| 3123 |
start_idx, next_idx - 1, start_idx, start_idx +1); |
| 3124 |
start_consolidated_cfg_edges_event *new_start_ev |
| 3125 |
= new start_consolidated_cfg_edges_event |
| 3126 |
(event_loc_info (old_start_ev->get_location (), |
| 3127 |
old_start_ev->get_fndecl (), |
| 3128 |
old_start_ev->get_stack_depth ()), |
| 3129 |
edge_sense); |
| 3130 |
checker_event *new_end_ev |
| 3131 |
= new end_consolidated_cfg_edges_event |
| 3132 |
(event_loc_info (old_end_ev->get_location (), |
| 3133 |
old_end_ev->get_fndecl (), |
| 3134 |
old_end_ev->get_stack_depth ())); |
| 3135 |
path->replace_event (start_idx, new_start_ev); |
| 3136 |
path->replace_event (start_idx + 1, new_end_ev); |
| 3137 |
path->delete_events (start_idx + 2, next_idx - (start_idx + 2)); |
| 3138 |
} |
| 3139 |
} |
| 3140 |
} |
| 3141 |
} |
| 3142 |
|
| 3143 |
/* Final pass of diagnostic_manager::prune_path. |
| 3144 |
|
| 3145 |
If all we're left with is in one function, then filter function entry |
| 3146 |
events. */ |
| 3147 |
|
| 3148 |
void |
| 3149 |
diagnostic_manager::finish_pruning (checker_path *path) const |
| 3150 |
{ |
| 3151 |
if (!path->interprocedural_p ()) |
| 3152 |
{ |
| 3153 |
int idx = path->num_events () - 1; |
| 3154 |
while (idx >= 0 && idx < (signed)path->num_events ()) |
| 3155 |
{ |
| 3156 |
checker_event *base_event = path->get_checker_event (idx); |
| 3157 |
if (base_event->m_kind == EK_FUNCTION_ENTRY) |
| 3158 |
{ |
| 3159 |
log ("filtering event %i:" |
| 3160 |
" function entry for purely intraprocedural path", idx); |
| 3161 |
path->delete_event (idx); |
| 3162 |
} |
| 3163 |
idx--; |
| 3164 |
} |
| 3165 |
} |
| 3166 |
} |
| 3167 |
|
| 3168 |
} // namespace ana |
| 3169 |
|
| 3170 |
#endif /* #if ENABLE_ANALYZER */ |