PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/gcc/analyzer/analyzer.cc

https://gitlab.com/adotout/gcc
C++ | 449 lines | 279 code | 72 blank | 98 comment | 63 complexity | 86097836c7e35fcf638d49cfab39a89c MD5 | raw file
  1. /* Utility functions for the analyzer.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tree.h"
  20. #include "function.h"
  21. #include "basic-block.h"
  22. #include "gimple.h"
  23. #include "diagnostic.h"
  24. #include "intl.h"
  25. #include "function.h"
  26. #include "analyzer/analyzer.h"
  27. #if ENABLE_ANALYZER
  28. namespace ana {
  29. /* Workaround for missing location information for some stmts,
  30. which ultimately should be solved by fixing the frontends
  31. to provide the locations (TODO). */
  32. location_t
  33. get_stmt_location (const gimple *stmt, function *fun)
  34. {
  35. if (get_pure_location (stmt->location) == UNKNOWN_LOCATION)
  36. {
  37. /* Workaround for missing location information for clobber
  38. stmts, which seem to lack location information in the C frontend
  39. at least. Created by gimplify_bind_expr, which uses the
  40. BLOCK_SOURCE_END_LOCATION (BIND_EXPR_BLOCK (bind_expr))
  41. but this is never set up when the block is created in
  42. c_end_compound_stmt's pop_scope.
  43. TODO: fix this missing location information.
  44. For now, as a hackish workaround, use the location of the end of
  45. the function. */
  46. if (gimple_clobber_p (stmt) && fun)
  47. return fun->function_end_locus;
  48. }
  49. return stmt->location;
  50. }
  51. static tree
  52. fixup_tree_for_diagnostic_1 (tree expr, hash_set<tree> *visited);
  53. /* Attemp to generate a tree for the LHS of ASSIGN_STMT.
  54. VISITED must be non-NULL; it is used to ensure termination. */
  55. static tree
  56. get_diagnostic_tree_for_gassign_1 (const gassign *assign_stmt,
  57. hash_set<tree> *visited)
  58. {
  59. enum tree_code code = gimple_assign_rhs_code (assign_stmt);
  60. /* Reverse the effect of extract_ops_from_tree during
  61. gimplification. */
  62. switch (get_gimple_rhs_class (code))
  63. {
  64. default:
  65. case GIMPLE_INVALID_RHS:
  66. gcc_unreachable ();
  67. case GIMPLE_TERNARY_RHS:
  68. case GIMPLE_BINARY_RHS:
  69. case GIMPLE_UNARY_RHS:
  70. {
  71. tree t = make_node (code);
  72. TREE_TYPE (t) = TREE_TYPE (gimple_assign_lhs (assign_stmt));
  73. unsigned num_rhs_args = gimple_num_ops (assign_stmt) - 1;
  74. for (unsigned i = 0; i < num_rhs_args; i++)
  75. {
  76. tree op = gimple_op (assign_stmt, i + 1);
  77. if (op)
  78. {
  79. op = fixup_tree_for_diagnostic_1 (op, visited);
  80. if (op == NULL_TREE)
  81. return NULL_TREE;
  82. }
  83. TREE_OPERAND (t, i) = op;
  84. }
  85. return t;
  86. }
  87. case GIMPLE_SINGLE_RHS:
  88. {
  89. tree op = gimple_op (assign_stmt, 1);
  90. op = fixup_tree_for_diagnostic_1 (op, visited);
  91. return op;
  92. }
  93. }
  94. }
  95. /* Subroutine of fixup_tree_for_diagnostic_1, called on SSA names.
  96. Attempt to reconstruct a tree expression for SSA_NAME
  97. based on its def-stmt.
  98. SSA_NAME must be non-NULL.
  99. VISITED must be non-NULL; it is used to ensure termination.
  100. Return NULL_TREE if there is a problem. */
  101. static tree
  102. maybe_reconstruct_from_def_stmt (tree ssa_name,
  103. hash_set<tree> *visited)
  104. {
  105. /* Ensure termination. */
  106. if (visited->contains (ssa_name))
  107. return NULL_TREE;
  108. visited->add (ssa_name);
  109. gimple *def_stmt = SSA_NAME_DEF_STMT (ssa_name);
  110. switch (gimple_code (def_stmt))
  111. {
  112. default:
  113. gcc_unreachable ();
  114. case GIMPLE_ASM:
  115. case GIMPLE_NOP:
  116. case GIMPLE_PHI:
  117. /* Can't handle these. */
  118. return NULL_TREE;
  119. case GIMPLE_ASSIGN:
  120. return get_diagnostic_tree_for_gassign_1
  121. (as_a <const gassign *> (def_stmt), visited);
  122. case GIMPLE_CALL:
  123. {
  124. gcall *call_stmt = as_a <gcall *> (def_stmt);
  125. tree return_type = gimple_call_return_type (call_stmt);
  126. tree fn = fixup_tree_for_diagnostic_1 (gimple_call_fn (call_stmt),
  127. visited);
  128. if (fn == NULL_TREE)
  129. return NULL_TREE;
  130. unsigned num_args = gimple_call_num_args (call_stmt);
  131. auto_vec<tree> args (num_args);
  132. for (unsigned i = 0; i < num_args; i++)
  133. {
  134. tree arg = gimple_call_arg (call_stmt, i);
  135. arg = fixup_tree_for_diagnostic_1 (arg, visited);
  136. if (arg == NULL_TREE)
  137. return NULL_TREE;
  138. args.quick_push (arg);
  139. }
  140. gcc_assert (fn);
  141. return build_call_array_loc (gimple_location (call_stmt),
  142. return_type, fn,
  143. num_args, args.address ());
  144. }
  145. break;
  146. }
  147. }
  148. /* Subroutine of fixup_tree_for_diagnostic: attempt to fixup EXPR,
  149. which can be NULL.
  150. VISITED must be non-NULL; it is used to ensure termination. */
  151. static tree
  152. fixup_tree_for_diagnostic_1 (tree expr, hash_set<tree> *visited)
  153. {
  154. if (expr
  155. && TREE_CODE (expr) == SSA_NAME
  156. && (SSA_NAME_VAR (expr) == NULL_TREE
  157. || DECL_ARTIFICIAL (SSA_NAME_VAR (expr))))
  158. {
  159. if (tree var = SSA_NAME_VAR (expr))
  160. if (VAR_P (var) && DECL_HAS_DEBUG_EXPR_P (var))
  161. return DECL_DEBUG_EXPR (var);
  162. if (tree expr2 = maybe_reconstruct_from_def_stmt (expr, visited))
  163. return expr2;
  164. }
  165. return expr;
  166. }
  167. /* We don't want to print '<unknown>' in our diagnostics (PR analyzer/99771),
  168. but sometimes we generate diagnostics involving an ssa name for a
  169. temporary.
  170. Work around this by attempting to reconstruct a tree expression for
  171. such temporaries based on their def-stmts.
  172. Otherwise return EXPR.
  173. EXPR can be NULL. */
  174. tree
  175. fixup_tree_for_diagnostic (tree expr)
  176. {
  177. hash_set<tree> visited;
  178. return fixup_tree_for_diagnostic_1 (expr, &visited);
  179. }
  180. /* Attempt to generate a tree for the LHS of ASSIGN_STMT. */
  181. tree
  182. get_diagnostic_tree_for_gassign (const gassign *assign_stmt)
  183. {
  184. hash_set<tree> visited;
  185. return get_diagnostic_tree_for_gassign_1 (assign_stmt, &visited);
  186. }
  187. } // namespace ana
  188. /* Helper function for checkers. Is the CALL to the given function name,
  189. and with the given number of arguments?
  190. This doesn't resolve function pointers via the region model;
  191. is_named_call_p should be used instead, using a fndecl from
  192. get_fndecl_for_call; this function should only be used for special cases
  193. where it's not practical to get at the region model, or for special
  194. analyzer functions such as __analyzer_dump. */
  195. bool
  196. is_special_named_call_p (const gcall *call, const char *funcname,
  197. unsigned int num_args)
  198. {
  199. gcc_assert (funcname);
  200. tree fndecl = gimple_call_fndecl (call);
  201. if (!fndecl)
  202. return false;
  203. return is_named_call_p (fndecl, funcname, call, num_args);
  204. }
  205. /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
  206. that has the given FUNCNAME?
  207. Compare with special_function_p in calls.cc. */
  208. bool
  209. is_named_call_p (const_tree fndecl, const char *funcname)
  210. {
  211. gcc_assert (fndecl);
  212. gcc_assert (funcname);
  213. if (!maybe_special_function_p (fndecl))
  214. return false;
  215. tree identifier = DECL_NAME (fndecl);
  216. const char *name = IDENTIFIER_POINTER (identifier);
  217. const char *tname = name;
  218. /* Potentially disregard prefix _ or __ in FNDECL's name, but not if
  219. FUNCNAME itself has leading underscores (e.g. when looking for
  220. "__analyzer_eval"). */
  221. if (funcname[0] != '_' && name[0] == '_')
  222. {
  223. if (name[1] == '_')
  224. tname += 2;
  225. else
  226. tname += 1;
  227. }
  228. return 0 == strcmp (tname, funcname);
  229. }
  230. /* Return true if FNDECL is within the namespace "std".
  231. Compare with cp/typeck.cc: decl_in_std_namespace_p, but this doesn't
  232. rely on being the C++ FE (or handle inline namespaces inside of std). */
  233. static inline bool
  234. is_std_function_p (const_tree fndecl)
  235. {
  236. tree name_decl = DECL_NAME (fndecl);
  237. if (!name_decl)
  238. return false;
  239. if (!DECL_CONTEXT (fndecl))
  240. return false;
  241. if (TREE_CODE (DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
  242. return false;
  243. tree ns = DECL_CONTEXT (fndecl);
  244. if (!(DECL_CONTEXT (ns) == NULL_TREE
  245. || TREE_CODE (DECL_CONTEXT (ns)) == TRANSLATION_UNIT_DECL))
  246. return false;
  247. if (!DECL_NAME (ns))
  248. return false;
  249. return id_equal ("std", DECL_NAME (ns));
  250. }
  251. /* Like is_named_call_p, but look for std::FUNCNAME. */
  252. bool
  253. is_std_named_call_p (const_tree fndecl, const char *funcname)
  254. {
  255. gcc_assert (fndecl);
  256. gcc_assert (funcname);
  257. if (!is_std_function_p (fndecl))
  258. return false;
  259. tree identifier = DECL_NAME (fndecl);
  260. const char *name = IDENTIFIER_POINTER (identifier);
  261. const char *tname = name;
  262. /* Don't disregard prefix _ or __ in FNDECL's name. */
  263. return 0 == strcmp (tname, funcname);
  264. }
  265. /* Helper function for checkers. Is FNDECL an extern fndecl at file scope
  266. that has the given FUNCNAME, and does CALL have the given number of
  267. arguments? */
  268. bool
  269. is_named_call_p (const_tree fndecl, const char *funcname,
  270. const gcall *call, unsigned int num_args)
  271. {
  272. gcc_assert (fndecl);
  273. gcc_assert (funcname);
  274. if (!is_named_call_p (fndecl, funcname))
  275. return false;
  276. if (gimple_call_num_args (call) != num_args)
  277. return false;
  278. return true;
  279. }
  280. /* Like is_named_call_p, but check for std::FUNCNAME. */
  281. bool
  282. is_std_named_call_p (const_tree fndecl, const char *funcname,
  283. const gcall *call, unsigned int num_args)
  284. {
  285. gcc_assert (fndecl);
  286. gcc_assert (funcname);
  287. if (!is_std_named_call_p (fndecl, funcname))
  288. return false;
  289. if (gimple_call_num_args (call) != num_args)
  290. return false;
  291. return true;
  292. }
  293. /* Return true if stmt is a setjmp or sigsetjmp call. */
  294. bool
  295. is_setjmp_call_p (const gcall *call)
  296. {
  297. if (is_special_named_call_p (call, "setjmp", 1)
  298. || is_special_named_call_p (call, "sigsetjmp", 2))
  299. /* region_model::on_setjmp requires a pointer. */
  300. if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
  301. return true;
  302. return false;
  303. }
  304. /* Return true if stmt is a longjmp or siglongjmp call. */
  305. bool
  306. is_longjmp_call_p (const gcall *call)
  307. {
  308. if (is_special_named_call_p (call, "longjmp", 2)
  309. || is_special_named_call_p (call, "siglongjmp", 2))
  310. /* exploded_node::on_longjmp requires a pointer for the initial
  311. argument. */
  312. if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
  313. return true;
  314. return false;
  315. }
  316. /* For a CALL that matched is_special_named_call_p or is_named_call_p for
  317. some name, return a name for the called function suitable for use in
  318. diagnostics (stripping the leading underscores). */
  319. const char *
  320. get_user_facing_name (const gcall *call)
  321. {
  322. tree fndecl = gimple_call_fndecl (call);
  323. gcc_assert (fndecl);
  324. tree identifier = DECL_NAME (fndecl);
  325. gcc_assert (identifier);
  326. const char *name = IDENTIFIER_POINTER (identifier);
  327. /* Strip prefix _ or __ in FNDECL's name. */
  328. if (name[0] == '_')
  329. {
  330. if (name[1] == '_')
  331. return name + 2;
  332. else
  333. return name + 1;
  334. }
  335. return name;
  336. }
  337. /* Generate a label_text instance by formatting FMT, using a
  338. temporary clone of the global_dc's printer (thus using its
  339. formatting callbacks).
  340. Colorize if the global_dc supports colorization and CAN_COLORIZE is
  341. true. */
  342. label_text
  343. make_label_text (bool can_colorize, const char *fmt, ...)
  344. {
  345. pretty_printer *pp = global_dc->printer->clone ();
  346. pp_clear_output_area (pp);
  347. if (!can_colorize)
  348. pp_show_color (pp) = false;
  349. text_info ti;
  350. rich_location rich_loc (line_table, UNKNOWN_LOCATION);
  351. va_list ap;
  352. va_start (ap, fmt);
  353. ti.format_spec = _(fmt);
  354. ti.args_ptr = &ap;
  355. ti.err_no = 0;
  356. ti.x_data = NULL;
  357. ti.m_richloc = &rich_loc;
  358. pp_format (pp, &ti);
  359. pp_output_formatted_text (pp);
  360. va_end (ap);
  361. label_text result = label_text::take (xstrdup (pp_formatted_text (pp)));
  362. delete pp;
  363. return result;
  364. }
  365. #endif /* #if ENABLE_ANALYZER */