[PATCH] D24507: Add attribute for return values that shouldn't be cast to bool

Anna Zaks via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 15 21:34:49 PDT 2016


zaks.anna added inline comments.

================
Comment at: include/clang/Basic/AttrDocs.td:2055
@@ -2054,1 +2054,3 @@
 }
+def WarnImpcastToBoolDocs : Documentation {
+  let Category = DocCatFunction;
----------------
You probably need to "propose" the attribute to the clang community. I'd send an email to the cfe-dev as it might not have enough attention if it's just the patch.  

================
Comment at: test/ReturnNonBoolTest.c:74
@@ +73,3 @@
+  // no good way to get those from the program state.
+  if (restricted_wrap(2)) // expected-warning{{implicit cast to bool is dangerous for this value}}
+    return;
----------------
I do not understand why this is a false positive. In restricted_wrap, r can be any value. You only return '0' if r is '-1', but it could be '-2' or '100', which are also not bool and this values would just get returned.

You should be able to query the state to check if a value is a zero or one using code like this from CStringChecker.cpp:
"
  SValBuilder &svalBuilder = C.getSValBuilder();
  DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
  return state->assume(svalBuilder.evalEQ(state, *val, zero))
"



Repository:
  rL LLVM

https://reviews.llvm.org/D24507





More information about the cfe-commits mailing list