DebugChecks.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. ============
  2. Debug Checks
  3. ============
  4. .. contents::
  5. :local:
  6. NOTE: this document applies to the original Clang project, not the DirectX
  7. Compiler. It's made available for informational purposes only.
  8. The analyzer contains a number of checkers which can aid in debugging. Enable
  9. them by using the "-analyzer-checker=" flag, followed by the name of the
  10. checker.
  11. General Analysis Dumpers
  12. ========================
  13. These checkers are used to dump the results of various infrastructural analyses
  14. to stderr. Some checkers also have "view" variants, which will display a graph
  15. using a 'dot' format viewer (such as Graphviz on OS X) instead.
  16. - debug.DumpCallGraph, debug.ViewCallGraph: Show the call graph generated for
  17. the current translation unit. This is used to determine the order in which to
  18. analyze functions when inlining is enabled.
  19. - debug.DumpCFG, debug.ViewCFG: Show the CFG generated for each top-level
  20. function being analyzed.
  21. - debug.DumpDominators: Shows the dominance tree for the CFG of each top-level
  22. function.
  23. - debug.DumpLiveVars: Show the results of live variable analysis for each
  24. top-level function being analyzed.
  25. - debug.ViewExplodedGraph: Show the Exploded Graphs generated for the
  26. analysis of different functions in the input translation unit. When there
  27. are several functions analyzed, display one graph per function. Beware
  28. that these graphs may grow very large, even for small functions.
  29. Path Tracking
  30. =============
  31. These checkers print information about the path taken by the analyzer engine.
  32. - debug.DumpCalls: Prints out every function or method call encountered during a
  33. path traversal. This is indented to show the call stack, but does NOT do any
  34. special handling of branches, meaning different paths could end up
  35. interleaved.
  36. - debug.DumpTraversal: Prints the name of each branch statement encountered
  37. during a path traversal ("IfStmt", "WhileStmt", etc). Currently used to check
  38. whether the analysis engine is doing BFS or DFS.
  39. State Checking
  40. ==============
  41. These checkers will print out information about the analyzer state in the form
  42. of analysis warnings. They are intended for use with the -verify functionality
  43. in regression tests.
  44. - debug.TaintTest: Prints out the word "tainted" for every expression that
  45. carries taint. At the time of this writing, taint was only introduced by the
  46. checks under experimental.security.taint.TaintPropagation; this checker may
  47. eventually move to the security.taint package.
  48. - debug.ExprInspection: Responds to certain function calls, which are modeled
  49. after builtins. These function calls should affect the program state other
  50. than the evaluation of their arguments; to use them, you will need to declare
  51. them within your test file. The available functions are described below.
  52. (FIXME: debug.ExprInspection should probably be renamed, since it no longer only
  53. inspects expressions.)
  54. ExprInspection checks
  55. ---------------------
  56. - void clang_analyzer_eval(bool);
  57. Prints TRUE if the argument is known to have a non-zero value, FALSE if the
  58. argument is known to have a zero or null value, and UNKNOWN if the argument
  59. isn't sufficiently constrained on this path. You can use this to test other
  60. values by using expressions like "x == 5". Note that this functionality is
  61. currently DISABLED in inlined functions, since different calls to the same
  62. inlined function could provide different information, making it difficult to
  63. write proper -verify directives.
  64. In C, the argument can be typed as 'int' or as '_Bool'.
  65. Example usage::
  66. clang_analyzer_eval(x); // expected-warning{{UNKNOWN}}
  67. if (!x) return;
  68. clang_analyzer_eval(x); // expected-warning{{TRUE}}
  69. - void clang_analyzer_checkInlined(bool);
  70. If a call occurs within an inlined function, prints TRUE or FALSE according to
  71. the value of its argument. If a call occurs outside an inlined function,
  72. nothing is printed.
  73. The intended use of this checker is to assert that a function is inlined at
  74. least once (by passing 'true' and expecting a warning), or to assert that a
  75. function is never inlined (by passing 'false' and expecting no warning). The
  76. argument is technically unnecessary but is intended to clarify intent.
  77. You might wonder why we can't print TRUE if a function is ever inlined and
  78. FALSE if it is not. The problem is that any inlined function could conceivably
  79. also be analyzed as a top-level function (in which case both TRUE and FALSE
  80. would be printed), depending on the value of the -analyzer-inlining option.
  81. In C, the argument can be typed as 'int' or as '_Bool'.
  82. Example usage::
  83. int inlined() {
  84. clang_analyzer_checkInlined(true); // expected-warning{{TRUE}}
  85. return 42;
  86. }
  87. void topLevel() {
  88. clang_analyzer_checkInlined(false); // no-warning (not inlined)
  89. int value = inlined();
  90. // This assertion will not be valid if the previous call was not inlined.
  91. clang_analyzer_eval(value == 42); // expected-warning{{TRUE}}
  92. }
  93. - void clang_analyzer_warnIfReached();
  94. Generate a warning if this line of code gets reached by the analyzer.
  95. Example usage::
  96. if (true) {
  97. clang_analyzer_warnIfReached(); // expected-warning{{REACHABLE}}
  98. }
  99. else {
  100. clang_analyzer_warnIfReached(); // no-warning
  101. }
  102. Statistics
  103. ==========
  104. The debug.Stats checker collects various information about the analysis of each
  105. function, such as how many blocks were reached and if the analyzer timed out.
  106. There is also an additional -analyzer-stats flag, which enables various
  107. statistics within the analyzer engine. Note the Stats checker (which produces at
  108. least one bug report per function) may actually change the values reported by
  109. -analyzer-stats.