Author: eszasip
Date: Fri May 29 04:49:59 2015
New Revision: 238548

URL: http://llvm.org/viewvc/llvm-project?rev=238548&view=rev
Log:
[clang-tidy] Fix for llvm.org/PR23355

misc-static-assert and misc-assert-side-effect will handle __builtin_expect 
based asserts correctly.

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp
    clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp?rev=238548&r1=238547&r2=238548&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp Fri May 
29 04:49:59 2015
@@ -55,9 +55,13 @@ AST_MATCHER_P(Expr, hasSideEffect, bool,
 
   if (const auto *CExpr = dyn_cast<CallExpr>(E)) {
     bool Result = CheckFunctionCalls;
-    if (const auto *FuncDecl = CExpr->getDirectCallee())
-      if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
+    if (const auto *FuncDecl = CExpr->getDirectCallee()) {
+      if (FuncDecl->getDeclName().isIdentifier() &&
+          FuncDecl->getName() == "__builtin_expect") // exceptions come here
+        Result = false;
+      else if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl))
         Result &= !MethodDecl->isConst();
+    }
     return Result;
   }
 

Modified: clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp?rev=238548&r1=238547&r2=238548&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp Fri May 29 
04:49:59 2015
@@ -41,15 +41,18 @@ void StaticAssertCheck::registerMatchers
       IsAlwaysFalse);
   auto NonConstexprFunctionCall =
       callExpr(hasDeclaration(functionDecl(unless(isConstexpr()))));
-  auto Condition = expr(anyOf(
+  auto AssertCondition = expr(anyOf(
       expr(ignoringParenCasts(anyOf(
           AssertExprRoot,
           
unaryOperator(hasUnaryOperand(ignoringParenCasts(AssertExprRoot)))))),
-      anything()), unless(findAll(NonConstexprFunctionCall)));
+      anything()), 
unless(findAll(NonConstexprFunctionCall))).bind("condition");
+  auto Condition = anyOf(ignoringParenImpCasts(callExpr(
+      hasDeclaration(functionDecl(hasName("__builtin_expect"))),
+      hasArgument(0, AssertCondition))), AssertCondition);
 
   Finder->addMatcher(
-      
stmt(anyOf(conditionalOperator(hasCondition(Condition.bind("condition"))),
-                 ifStmt(hasCondition(Condition.bind("condition")))),
+      stmt(anyOf(conditionalOperator(hasCondition(Condition)),
+                 ifStmt(hasCondition(Condition))),
            unless(isInTemplateInstantiation())).bind("condStmt"),
       this);
 }

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp?rev=238548&r1=238547&r2=238548&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp 
(original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp Fri May 
29 04:49:59 2015
@@ -1,4 +1,4 @@
-// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-assert-side-effect %t 
-config="{CheckOptions: [{key: misc-assert-side-effect.CheckFunctionCalls, 
value: 1}, {key: misc-assert-side-effect.AssertMacros, value: 
'assert,my_assert'}]}" -- -fexceptions
+// RUN: $(dirname %s)/check_clang_tidy.sh %s misc-assert-side-effect %t 
-config="{CheckOptions: [{key: misc-assert-side-effect.CheckFunctionCalls, 
value: 1}, {key: misc-assert-side-effect.AssertMacros, value: 
'assert,assert2,my_assert'}]}" -- -fexceptions
 // REQUIRES: shell
 
 //===--- assert definition block 
------------------------------------------===//
@@ -12,6 +12,10 @@ int abort() { return 0; }
   (void)abort()
 #endif
 
+void print(...);
+#define assert2(e) (__builtin_expect(!(e), 0) ?                                
\
+                       print (#e, __FILE__, __LINE__) : (void)0)
+
 #ifdef NDEBUG
 #define my_assert(x) 1
 #else
@@ -88,5 +92,7 @@ int main() {
   assert((throw 1, false));
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() with side effect
 
+  assert2(1 == 2 - 1);
+
   return 0;
 }

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp
URL: 
http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp?rev=238548&r1=238547&r2=238548&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.cpp Fri May 29 
04:49:59 2015
@@ -10,6 +10,8 @@ void abort() {}
   abort()
 #endif
 
+void print(...);
+
 #define ZERO_MACRO 0
 
 #define False false
@@ -126,5 +128,14 @@ int main() {
   assert(strlen("12345") == 5);
   // CHECK-FIXES: {{^  }}assert(strlen("12345") == 5);
 
+#define assert(e) (__builtin_expect(!(e), 0) ? print (#e, __FILE__, __LINE__) 
: (void)0)
+  assert(false);
+  // CHECK-FIXES: {{^  }}assert(false);
+
+  assert(10 == 5 + 5);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
+  // CHECK-FIXES: {{^  }}static_assert(10 == 5 + 5, "");
+#undef assert
+
   return 0;
 }


_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to