[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Phabricator via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL307791: [clang-tidy] Add new modernize use unary assert 
check (authored by xazax).

Changed prior to commit:
  https://reviews.llvm.org/D35257?vs=106195=106197#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D35257

Files:
  clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt
  clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h
  clang-tools-extra/trunk/docs/ReleaseNotes.rst
  clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
  
clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-unary-static-assert.rst
  clang-tools-extra/trunk/test/clang-tidy/modernize-unary-static-assert.cpp

Index: clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp
===
--- clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -22,6 +22,7 @@
 #include "ReplaceRandomShuffleCheck.h"
 #include "ReturnBracedInitListCheck.h"
 #include "ShrinkToFitCheck.h"
+#include "UnaryStaticAssertCheck.h"
 #include "UseAutoCheck.h"
 #include "UseBoolLiteralsCheck.h"
 #include "UseDefaultMemberInitCheck.h"
@@ -61,6 +62,8 @@
 CheckFactories.registerCheck(
 "modernize-return-braced-init-list");
 CheckFactories.registerCheck("modernize-shrink-to-fit");
+CheckFactories.registerCheck(
+"modernize-unary-static-assert");
 CheckFactories.registerCheck("modernize-use-auto");
 CheckFactories.registerCheck(
 "modernize-use-bool-literals");
Index: clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h
+++ clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces a static_assert declaration with an empty message
+/// with the unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public ClangTidyCheck {
+public:
+  UnaryStaticAssertCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+};
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
Index: clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt
===
--- clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt
+++ clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt
@@ -16,6 +16,7 @@
   ReplaceRandomShuffleCheck.cpp
   ReturnBracedInitListCheck.cpp
   ShrinkToFitCheck.cpp
+  UnaryStaticAssertCheck.cpp
   UseAutoCheck.cpp
   UseBoolLiteralsCheck.cpp
   UseDefaultMemberInitCheck.cpp
Index: clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
===
--- clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
+++ clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
@@ -0,0 +1,45 @@
+//===--- UnaryStaticAssertCheck.cpp - clang-tidy---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#include "UnaryStaticAssertCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
+  if (!getLangOpts().CPlusPlus1z)
+return;
+
+  Finder->addMatcher(staticAssertDecl().bind("static_assert"), this);
+}
+
+void 

[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

LGTM!


https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk added inline comments.



Comment at: test/clang-tidy/modernize-unary-static-assert.cpp:16
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+  static_assert(sizeof(a) <= 17, MSG);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when

aaron.ballman wrote:
> This probably should not diagnose, but definitely should not provide a fixit 
> -- just because the macro is empty doesn't mean it will *always* be empty. 
> Consider:
> ```
> #if FOO
> #define MSG ""
> #else
> #define MSG "Not empty"
> #endif
> ```
> I think diagnosing this case is just as likely to be a false-positive as not 
> diagnosing, so my preference is to be silent instead of chatty. However, 
> maybe there are other opinions.
You are right. Unfortunately, the message is also from a macro expansion when 
the whole static assert is from a macro expansion. Maybe it is possible to 
check whether they are from the same macro, but I think it might better be 
addressed in a separate patch if there is a need to support that case. 


https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk updated this revision to Diff 106195.
barancsuk marked 4 inline comments as done.
barancsuk added a comment.

- No longer warn when the message is from a macro expansion


https://reviews.llvm.org/D35257

Files:
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-unary-static-assert.rst
  test/clang-tidy/modernize-unary-static-assert.cpp

Index: test/clang-tidy/modernize-unary-static-assert.cpp
===
--- /dev/null
+++ test/clang-tidy/modernize-unary-static-assert.cpp
@@ -0,0 +1,25 @@
+// RUN: %check_clang_tidy %s modernize-unary-static-assert %t -- -- -std=c++1z
+
+#define FOO static_assert(sizeof(a) <= 15, "");
+#define MSG ""
+
+void f_textless(int a) {
+  static_assert(sizeof(a) <= 10, "");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when the string literal is an empty string [modernize-unary-static-assert]
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 10 );{{$}}
+  static_assert(sizeof(a) <= 12, L"");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 12 );{{$}}
+  FOO
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+  static_assert(sizeof(a) <= 17, MSG);
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 17, MSG);{{$}}
+}
+
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Size of variable a is out of range!");
+}
+
+void f_unary(int a) { static_assert(sizeof(a) <= 10); }
+
+void f_incorrect_assert() { static_assert(""); }
Index: docs/clang-tidy/checks/modernize-unary-static-assert.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/modernize-unary-static-assert.rst
@@ -0,0 +1,25 @@
+.. title:: clang-tidy - modernize-unary-static-assert
+
+modernize-unary-static-assert
+=
+
+The check diagnoses any ``static_assert`` declaration with an empty string literal
+and provides a fix-it to replace the declaration with a single-argument ``static_assert`` declaration.
+
+The check is only applicable for C++17 and later code.
+
+The following code:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10, "");
+  }
+
+is replaced by:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10);
+  }
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -132,6 +132,7 @@
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
+   modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -108,6 +108,12 @@
   Finds and replaces explicit calls to the constructor in a return statement by
   a braced initializer list so that the return type is not needlessly repeated.
 
+- New `modernize-unary-static-assert-check
+  `_ check
+
+  The check diagnoses any ``static_assert`` declaration with an empty string literal
+  and provides a fix-it to replace the declaration with a single-argument ``static_assert`` declaration.
+
 - Improved `modernize-use-emplace
   `_ check
 
Index: clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces a static_assert declaration with an empty message
+/// with the unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public ClangTidyCheck {
+public:
+  UnaryStaticAssertCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, 

[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:32
+
+  if (!AssertMessage || AssertMessage->getLength())
+return;

barancsuk wrote:
> aaron.ballman wrote:
> > I think this should be `!AssertMessage->getLength()`
> It works correctly without the negation, otherwise the tests fail. 
Sorry about the brain hiccup, you're correct.



Comment at: test/clang-tidy/modernize-unary-static-assert.cpp:16
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+  static_assert(sizeof(a) <= 17, MSG);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when

This probably should not diagnose, but definitely should not provide a fixit -- 
just because the macro is empty doesn't mean it will *always* be empty. 
Consider:
```
#if FOO
#define MSG ""
#else
#define MSG "Not empty"
#endif
```
I think diagnosing this case is just as likely to be a false-positive as not 
diagnosing, so my preference is to be silent instead of chatty. However, maybe 
there are other opinions.


https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk added inline comments.



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:32
+
+  if (!AssertMessage || AssertMessage->getLength())
+return;

aaron.ballman wrote:
> I think this should be `!AssertMessage->getLength()`
It works correctly without the negation, otherwise the tests fail. 


https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-12 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk updated this revision to Diff 106148.
barancsuk marked 2 inline comments as done.
barancsuk added a comment.

- Further reviews addressed


https://reviews.llvm.org/D35257

Files:
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-unary-static-assert.rst
  test/clang-tidy/modernize-unary-static-assert.cpp

Index: test/clang-tidy/modernize-unary-static-assert.cpp
===
--- /dev/null
+++ test/clang-tidy/modernize-unary-static-assert.cpp
@@ -0,0 +1,27 @@
+// RUN: %check_clang_tidy %s modernize-unary-static-assert %t -- -- -std=c++1z
+
+#define FOO static_assert(sizeof(a) <= 15, "");
+#define MSG ""
+
+void f_textless(int a) {
+  static_assert(sizeof(a) <= 10, "");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when the string literal is an empty string [modernize-unary-static-assert]
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 10 );{{$}}
+  static_assert(sizeof(a) <= 12, L"");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 12 );{{$}}
+  FOO
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+  static_assert(sizeof(a) <= 17, MSG);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 17, MSG);{{$}}
+}
+
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Size of variable a is out of range!");
+}
+
+void f_unary(int a) { static_assert(sizeof(a) <= 10); }
+
+void f_incorrect_assert() { static_assert(""); }
Index: docs/clang-tidy/checks/modernize-unary-static-assert.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/modernize-unary-static-assert.rst
@@ -0,0 +1,25 @@
+.. title:: clang-tidy - modernize-unary-static-assert
+
+modernize-unary-static-assert
+=
+
+The check diagnoses any ``static_assert`` declaration with an empty string literal
+and provides a fix-it to replace the declaration with a single-argument ``static_assert`` declaration.
+
+The check is only applicable for C++17 and later code.
+
+The following code:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10, "");
+  }
+
+is replaced by:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10);
+  }
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -132,6 +132,7 @@
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
+   modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -108,6 +108,12 @@
   Finds and replaces explicit calls to the constructor in a return statement by
   a braced initializer list so that the return type is not needlessly repeated.
 
+- New `modernize-unary-static-assert-check
+  `_ check
+
+  The check diagnoses any ``static_assert`` declaration with an empty string literal
+  and provides a fix-it to replace the declaration with a single-argument ``static_assert`` declaration.
+
 - Improved `modernize-use-emplace
   `_ check
 
Index: clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces a static_assert declaration with an empty message
+/// with the unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public 

[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:32
+
+  if (!AssertMessage || AssertMessage->getLength())
+return;

I think this should be `!AssertMessage->getLength()`



Comment at: test/clang-tidy/modernize-unary-static-assert.cpp:18
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Sie of variable a is out of range!");
+}

typo: Sie



Comment at: test/clang-tidy/modernize-unary-static-assert.cpp:23
+
+void f_incorrect_assert() { static_assert(""); }

I think you also need to handle the case where the string literal is empty but 
produces from a macro expansion. e.g.,
```
#define MSG ""

static_assert(condition, MSG);
```
This should not suggest removing the empty string literal.


https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk updated this revision to Diff 106026.
barancsuk added a comment.

- Missed some fixes last time


https://reviews.llvm.org/D35257

Files:
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-unary-static-assert.rst
  test/clang-tidy/modernize-unary-static-assert.cpp

Index: test/clang-tidy/modernize-unary-static-assert.cpp
===
--- /dev/null
+++ test/clang-tidy/modernize-unary-static-assert.cpp
@@ -0,0 +1,23 @@
+// RUN: %check_clang_tidy %s modernize-unary-static-assert %t -- -- -std=c++1z
+
+#define FOO static_assert(sizeof(a) <= 15, "");
+
+void f_textless(int a) {
+  static_assert(sizeof(a) <= 10, "");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when the string literal is an empty string [modernize-unary-static-assert]
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 10 );{{$}}
+  static_assert(sizeof(a) <= 12, L"");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 12 );{{$}}
+  FOO
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+}
+
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Sie of variable a is out of range!");
+}
+
+void f_unary(int a) { static_assert(sizeof(a) <= 10); }
+
+void f_incorrect_assert() { static_assert(""); }
Index: docs/clang-tidy/checks/modernize-unary-static-assert.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/modernize-unary-static-assert.rst
@@ -0,0 +1,25 @@
+.. title:: clang-tidy - modernize-unary-static-assert
+
+modernize-unary-static-assert
+=
+
+The check diagnoses any ``static_assert`` declaration with an empty string literal
+and provides a fix-it to replace the declaration with a single-argument ``static_assert`` declaration.
+
+The check is only applicable for C++17 and later code.
+
+The following code:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10, "");
+  }
+
+is replaced by:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10);
+  }
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -132,6 +132,7 @@
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
+   modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -108,6 +108,12 @@
   Finds and replaces explicit calls to the constructor in a return statement by
   a braced initializer list so that the return type is not needlessly repeated.
 
+- New `modernize-unary-static-assert-check
+  `_ check
+
+  The check diagnoses any ``static_assert`` declaration with an empty string literal
+  and provides a fix-it to replace the declaration with a single-argument ``static_assert`` declaration.
+
 - Improved `modernize-use-emplace
   `_ check
 
Index: clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces a static_assert declaration with an empty message
+/// with the unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public ClangTidyCheck {
+public:
+  UnaryStaticAssertCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const 

[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk updated this revision to Diff 106025.
barancsuk marked 10 inline comments as done.
barancsuk added a comment.

- Address review comments


https://reviews.llvm.org/D35257

Files:
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.h
  docs/ReleaseNotes.rst
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-unary-static-assert.rst
  test/clang-tidy/modernize-unary-static-assert.cpp

Index: test/clang-tidy/modernize-unary-static-assert.cpp
===
--- /dev/null
+++ test/clang-tidy/modernize-unary-static-assert.cpp
@@ -0,0 +1,23 @@
+// RUN: %check_clang_tidy %s modernize-unary-static-assert %t -- -- -std=c++1z
+
+#define FOO static_assert(sizeof(a) <= 15, "");
+
+void f_textless(int a) {
+  static_assert(sizeof(a) <= 10, "");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when the string literal is an empty string [modernize-unary-static-assert]
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 10 );{{$}}
+  static_assert(sizeof(a) <= 12, L"");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 12 );{{$}}
+  FOO
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+}
+
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Sie of variable a is out of range!");
+}
+
+void f_unary(int a) { static_assert(sizeof(a) <= 10); }
+
+void f_incorrect_assert() { static_assert(""); }
Index: docs/clang-tidy/checks/modernize-unary-static-assert.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/modernize-unary-static-assert.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - modernize-unary-static-assert
+
+modernize-unary-static-assert
+=
+
+The check finds ``static_assert`` declarations with empty messages, and
+replaces them with an unary ``static_assert`` declaration.
+
+The check is only applicable for C++17 code.
+
+The following code:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10, "");
+  }
+
+is replaced by:
+
+.. code-block:: c++
+
+  void f_textless(int a) {
+static_assert(sizeof(a) <= 10);
+  }
+
+
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -132,6 +132,7 @@
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
+   modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
Index: docs/ReleaseNotes.rst
===
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -108,6 +108,11 @@
   Finds and replaces explicit calls to the constructor in a return statement by
   a braced initializer list so that the return type is not needlessly repeated.
 
+- New `modernize-unary-static-assert-check
+  `_ check
+
+  The check finds ``static_assert`` declarations with empty messages, and replaces them with an unary ``static_assert`` declaration.
+
 - Improved `modernize-use-emplace
   `_ check
 
Index: clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces a static_assert declaration with an empty message
+/// with the unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public ClangTidyCheck {
+public:
+  UnaryStaticAssertCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+};
+
+} // namespace modernize
+} // namespace 

[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman added inline comments.



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:28
+void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult ) {
+
+  const auto *MatchedDecl =

Can remove the empty line.



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:36
+
+  if (AssertMessage->getString() != "")
+return;

I would hoist this up to the previous if statement and use 
`!AssertMessage->getLength()`. This eliminates the possibility of an assert 
failing because the string literal is not an ASCII string.

You should add a test with `L""`, which I would expect to also be replaced.



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:40
+  diag(MatchedDecl->getLocation(),
+   "use unary 'static_assert' when the assert message is unused")
+  << FixItHint::CreateRemoval(AssertMessage->getSourceRange());

How about:

use unary 'static_assert' when the string literal is an empty string



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.h:19
+
+/// Replaces static assert declaration with empty message
+/// with their unary version.

static assert -> a static_assert
with empty -> with an empty



Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.h:20
+/// Replaces static assert declaration with empty message
+/// with their unary version.
+///

with the unary version.



Comment at: docs/clang-tidy/checks/modernize-unary-static-assert.rst:6-7
+
+The check finds ``static_assert`` declarations with empty messages, and
+replaces them with an unary ``static_assert`` declaration.
+

This mixes singular and plural a bit oddly. How about:

The check diagnoses any static_assert declaration with an empty string literal 
and provides a fix-it to replace the declaration with a single-argument 
static_assert declaration.



Comment at: docs/clang-tidy/checks/modernize-unary-static-assert.rst:9
+
+The check is only applicable for C++17 code.
+

C++17 and later


https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk updated this revision to Diff 106021.
barancsuk added a comment.

- Do not rewrite macro expansions


https://reviews.llvm.org/D35257

Files:
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.h
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-unary-static-assert.rst
  test/clang-tidy/modernize-unary-static-assert.cpp

Index: test/clang-tidy/modernize-unary-static-assert.cpp
===
--- /dev/null
+++ test/clang-tidy/modernize-unary-static-assert.cpp
@@ -0,0 +1,20 @@
+// RUN: %check_clang_tidy %s modernize-unary-static-assert %t -- -- -std=c++1z
+
+#define FOO static_assert(sizeof(a) <= 15, "");
+
+void f_textless(int a) {
+  static_assert(sizeof(a) <= 10, "");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when the assert message is unused [modernize-unary-static-assert]
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 10 );{{$}}
+  FOO
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when
+  // CHECK-FIXES: {{^}}  FOO{{$}}
+}
+
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Sie of variable a is out of range!");
+}
+
+void f_unary(int a) { static_assert(sizeof(a) <= 10); }
+
+void f_incorrect_assert() { static_assert(""); }
Index: docs/clang-tidy/checks/modernize-unary-static-assert.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/modernize-unary-static-assert.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - modernize-unary-static-assert
+
+modernize-unary-static-assert
+=
+
+The check finds ``static_assert`` declarations with empty messages, and
+replaces them with an unary ``static_assert`` declaration.
+
+The check is only applicable for C++17 code.
+
+The following code:
+
+.. code-block:: c++
+
+  void f_textless(int a){
+static_assert(sizeof(a) <= 10, "");
+  }
+
+is replaced by:
+
+.. code-block:: c++
+
+  void f_textless(int a){
+static_assert(sizeof(a) <= 10 );
+  }
+
+
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -132,6 +132,7 @@
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
+   modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
Index: clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces static assert declaration with empty message
+/// with their unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public ClangTidyCheck {
+public:
+  UnaryStaticAssertCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+};
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
Index: clang-tidy/modernize/UnaryStaticAssertCheck.cpp
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.cpp
@@ -0,0 +1,50 @@
+//===--- UnaryStaticAssertCheck.cpp - clang-tidy---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#include "UnaryStaticAssertCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+void UnaryStaticAssertCheck::registerMatchers(MatchFinder *Finder) {
+  if 

[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Eugene Zelenko via Phabricator via cfe-commits
Eugene.Zelenko added a comment.

Please mention this check in docs/ReleaseNotes.rst (in alphabetical order).




Comment at: clang-tidy/modernize/UnaryStaticAssertCheck.cpp:28
+void UnaryStaticAssertCheck::check(const MatchFinder::MatchResult ) {
+
+  const auto *MatchedDecl =

Please remove empty line.



Comment at: docs/clang-tidy/checks/modernize-unary-static-assert.rst:15
+
+  void f_textless(int a){
+static_assert(sizeof(a) <= 10, "");

Please Clang-format snippets. Same below.



Comment at: docs/clang-tidy/checks/modernize-unary-static-assert.rst:26
+  }
+
+

Please remove empty line.


Repository:
  rL LLVM

https://reviews.llvm.org/D35257



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D35257: [clang-tidy] Add new modernize use unary assert check

2017-07-11 Thread Barancsuk Lilla via Phabricator via cfe-commits
barancsuk created this revision.
barancsuk added a project: clang-tools-extra.
Herald added subscribers: xazax.hun, JDevlieghere, mgorny.

The check finds `static_assert` declarations with empty messages, and
 replaces them with an unary `static_assert` declaration.

The check is only applicable for C++17 code.

The following code:

  void f_textless(int a){
static_assert(sizeof(a) <= 10, "");
  }
   

is replaced by:

  void f_textless(int a){
static_assert(sizeof(a) <= 10 );
  }


Repository:
  rL LLVM

https://reviews.llvm.org/D35257

Files:
  clang-tidy/modernize/CMakeLists.txt
  clang-tidy/modernize/ModernizeTidyModule.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.cpp
  clang-tidy/modernize/UnaryStaticAssertCheck.h
  docs/clang-tidy/checks/list.rst
  docs/clang-tidy/checks/modernize-unary-static-assert.rst
  test/clang-tidy/modernize-unary-static-assert.cpp

Index: test/clang-tidy/modernize-unary-static-assert.cpp
===
--- /dev/null
+++ test/clang-tidy/modernize-unary-static-assert.cpp
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy %s modernize-unary-static-assert %t -- -- -std=c++1z
+
+void f_textless(int a) {
+  static_assert(sizeof(a) <= 10, "");
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: use unary 'static_assert' when the assert message is unused [modernize-unary-static-assert]
+  // CHECK-FIXES: {{^}}  static_assert(sizeof(a) <= 10 );{{$}}
+}
+
+void f_with_tex(int a) {
+  static_assert(sizeof(a) <= 10, "Sie of variable a is out of range!");
+}
+
+void f_unary(int a) { static_assert(sizeof(a) <= 10); }
+
+void f_incorrect_assert() { static_assert(""); }
Index: docs/clang-tidy/checks/modernize-unary-static-assert.rst
===
--- /dev/null
+++ docs/clang-tidy/checks/modernize-unary-static-assert.rst
@@ -0,0 +1,27 @@
+.. title:: clang-tidy - modernize-unary-static-assert
+
+modernize-unary-static-assert
+=
+
+The check finds ``static_assert`` declarations with empty messages, and
+replaces them with an unary ``static_assert`` declaration.
+
+The check is only applicable for C++17 code.
+
+The following code:
+
+.. code-block:: c++
+
+  void f_textless(int a){
+static_assert(sizeof(a) <= 10, "");
+  }
+
+is replaced by:
+
+.. code-block:: c++
+
+  void f_textless(int a){
+static_assert(sizeof(a) <= 10 );
+  }
+
+
Index: docs/clang-tidy/checks/list.rst
===
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -132,6 +132,7 @@
modernize-replace-random-shuffle
modernize-return-braced-init-list
modernize-shrink-to-fit
+   modernize-unary-static-assert
modernize-use-auto
modernize-use-bool-literals
modernize-use-default-member-init
Index: clang-tidy/modernize/UnaryStaticAssertCheck.h
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.h
@@ -0,0 +1,36 @@
+//===--- UnaryStaticAssertCheck.h - clang-tidy---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+/// Replaces static assert declaration with empty message
+/// with their unary version.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html
+class UnaryStaticAssertCheck : public ClangTidyCheck {
+public:
+  UnaryStaticAssertCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult ) override;
+};
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
Index: clang-tidy/modernize/UnaryStaticAssertCheck.cpp
===
--- /dev/null
+++ clang-tidy/modernize/UnaryStaticAssertCheck.cpp
@@ -0,0 +1,46 @@
+//===--- UnaryStaticAssertCheck.cpp - clang-tidy---===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+
+#include "UnaryStaticAssertCheck.h"
+#include "clang/AST/ASTContext.h"
+#include