bugon.cocci 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /// Use BUG_ON instead of a if condition followed by BUG.
  2. ///
  3. //# This makes an effort to find cases where BUG() follows an if
  4. //# condition on an expression and replaces the if condition and BUG()
  5. //# with a BUG_ON having the conditional expression of the if statement
  6. //# as argument.
  7. //
  8. // Confidence: High
  9. // Copyright: (C) 2014 Himangi Saraogi. GPLv2.
  10. // Comments:
  11. // Options: --no-includes --include-headers
  12. virtual patch
  13. virtual context
  14. virtual org
  15. virtual report
  16. //----------------------------------------------------------
  17. // For context mode
  18. //----------------------------------------------------------
  19. @depends on context@
  20. expression e;
  21. @@
  22. *if (e) BUG();
  23. //----------------------------------------------------------
  24. // For patch mode
  25. //----------------------------------------------------------
  26. @depends on patch@
  27. expression e;
  28. @@
  29. -if (e) BUG();
  30. +BUG_ON(e);
  31. //----------------------------------------------------------
  32. // For org and report mode
  33. //----------------------------------------------------------
  34. @r@
  35. expression e;
  36. position p;
  37. @@
  38. if (e) BUG@p ();
  39. @script:python depends on org@
  40. p << r.p;
  41. @@
  42. coccilib.org.print_todo(p[0], "WARNING use BUG_ON")
  43. @script:python depends on report@
  44. p << r.p;
  45. @@
  46. msg="WARNING: Use BUG_ON instead of if condition followed by BUG.\nPlease make sure the condition has no side effects (see conditional BUG_ON definition in include/asm-generic/bug.h)"
  47. coccilib.report.print_report(p[0], msg)