[PATCH] [clang-tidy] Fix for llvm.org/PR23572

Szabolcs Sipos szabolcs.sipos at ericsson.com
Fri May 22 17:38:33 PDT 2015


Hi alexfh,

misc-static-assert won't report asserts whose conditions contain calls to non constexpr functions.

http://reviews.llvm.org/D9952

Files:
  clang-tidy/misc/StaticAssertCheck.cpp
  test/clang-tidy/misc-static-assert.cpp

Index: clang-tidy/misc/StaticAssertCheck.cpp
===================================================================
--- clang-tidy/misc/StaticAssertCheck.cpp
+++ clang-tidy/misc/StaticAssertCheck.cpp
@@ -39,11 +39,13 @@
           anyOf(binaryOperator(hasEitherOperand(IsAlwaysFalseWithCast)),
           anything())).bind("assertExprRoot"),
       IsAlwaysFalse);
+  auto NonConstexprFunctionCall =
+      callExpr(hasDeclaration(functionDecl(unless(isConstexpr()))));
   auto Condition = expr(anyOf(
       expr(ignoringParenCasts(anyOf(
           AssertExprRoot,
           unaryOperator(hasUnaryOperand(ignoringParenCasts(AssertExprRoot)))))),
-      anything()));
+      anything()), unless(findAll(NonConstexprFunctionCall)));
 
   Finder->addMatcher(
       stmt(anyOf(conditionalOperator(hasCondition(Condition.bind("condition"))),
Index: test/clang-tidy/misc-static-assert.cpp
===================================================================
--- test/clang-tidy/misc-static-assert.cpp
+++ test/clang-tidy/misc-static-assert.cpp
@@ -20,6 +20,9 @@
 
 constexpr bool myfunc(int a, int b) { return a * b == 0; }
 
+typedef __SIZE_TYPE__ size_t;
+extern "C" size_t strlen(const char *s);
+
 class A {
 public:
   bool method() { return true; }
@@ -120,5 +123,8 @@
   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
   // CHECK-FIXES: {{^  }}static_assert(10==5 , "Report me!");
 
+  assert(strlen("12345") == 5);
+  // CHECK-FIXES: {{^  }}assert(strlen("12345") == 5);
+
   return 0;
 }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9952.26365.patch
Type: text/x-patch
Size: 1517 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150523/65ab7cb1/attachment.bin>


More information about the cfe-commits mailing list