[PATCH] D83362: Fix warning caused by __builtin_expect_with_probability was not handled in places such as constant folding

Zhi Zhuang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 8 12:15:23 PDT 2020


LukeZhuang updated this revision to Diff 276517.
LukeZhuang added a comment.

**updated: 07/08/2020**
(1) improve test case


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83362/new/

https://reviews.llvm.org/D83362

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
  clang/test/Sema/builtin-expect-with-probability.cpp


Index: clang/test/Sema/builtin-expect-with-probability.cpp
===================================================================
--- clang/test/Sema/builtin-expect-with-probability.cpp
+++ clang/test/Sema/builtin-expect-with-probability.cpp
@@ -1,4 +1,26 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+
+__attribute__((noreturn)) extern void bar();
+
+int test_no_warn(int x) {
+  if (x) {
+    if (__builtin_expect_with_probability(1, 1, 1))
+      bar();
+  } else {
+    return 0;
+  }
+} // should not emit warn "control may reach end of non-void function" here since expr is constantly true, so the "if(__bui..)" should be constantly true condition and be ignored
+
+extern void f(int x);
+
+constexpr int constf() {
+  return __builtin_expect_with_probability(1, 1, 1); // should not have error here
+}
+
+void foo() {
+  f(constf());
+}
+
 extern int global;
 
 struct S {
Index: clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
@@ -64,10 +64,12 @@
 
   case Builtin::BI__builtin_unpredictable:
   case Builtin::BI__builtin_expect:
+  case Builtin::BI__builtin_expect_with_probability:
   case Builtin::BI__builtin_assume_aligned:
   case Builtin::BI__builtin_addressof: {
-    // For __builtin_unpredictable, __builtin_expect, and
-    // __builtin_assume_aligned, just return the value of the subexpression.
+    // For __builtin_unpredictable, __builtin_expect,
+    // __builtin_expect_with_probability and __builtin_assume_aligned,
+    // just return the value of the subexpression.
     // __builtin_addressof is going from a reference to a pointer, but those
     // are represented the same way in the analyzer.
     assert (Call.getNumArgs() > 0);
Index: clang/lib/AST/ExprConstant.cpp
===================================================================
--- clang/lib/AST/ExprConstant.cpp
+++ clang/lib/AST/ExprConstant.cpp
@@ -11200,6 +11200,7 @@
   }
 
   case Builtin::BI__builtin_expect:
+  case Builtin::BI__builtin_expect_with_probability:
     return Visit(E->getArg(0));
 
   case Builtin::BI__builtin_ffs:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83362.276517.patch
Type: text/x-patch
Size: 2242 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200708/cd189b5f/attachment.bin>


More information about the cfe-commits mailing list