[clang-tools-extra] r307791 - [clang-tidy] Add new modernize use unary assert check
Gabor Horvath via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 12 06:43:35 PDT 2017
Author: xazax
Date: Wed Jul 12 06:43:35 2017
New Revision: 307791
URL: http://llvm.org/viewvc/llvm-project?rev=307791&view=rev
Log:
[clang-tidy] Add new modernize use unary assert check
Patch by: Lilla Barancsuk
Differential Revision: https://reviews.llvm.org/D35257
Added:
clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h
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
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt
clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp
clang-tools-extra/trunk/docs/ReleaseNotes.rst
clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
Modified: clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt?rev=307791&r1=307790&r2=307791&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt Wed Jul 12 06:43:35 2017
@@ -16,6 +16,7 @@ add_clang_library(clangTidyModernizeModu
ReplaceRandomShuffleCheck.cpp
ReturnBracedInitListCheck.cpp
ShrinkToFitCheck.cpp
+ UnaryStaticAssertCheck.cpp
UseAutoCheck.cpp
UseBoolLiteralsCheck.cpp
UseDefaultMemberInitCheck.cpp
Modified: clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp?rev=307791&r1=307790&r2=307791&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp Wed Jul 12 06:43:35 2017
@@ -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 @@ public:
CheckFactories.registerCheck<ReturnBracedInitListCheck>(
"modernize-return-braced-init-list");
CheckFactories.registerCheck<ShrinkToFitCheck>("modernize-shrink-to-fit");
+ CheckFactories.registerCheck<UnaryStaticAssertCheck>(
+ "modernize-unary-static-assert");
CheckFactories.registerCheck<UseAutoCheck>("modernize-use-auto");
CheckFactories.registerCheck<UseBoolLiteralsCheck>(
"modernize-use-bool-literals");
Added: clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp?rev=307791&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.cpp Wed Jul 12 06:43:35 2017
@@ -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 UnaryStaticAssertCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *MatchedDecl =
+ Result.Nodes.getNodeAs<StaticAssertDecl>("static_assert");
+ const StringLiteral *AssertMessage = MatchedDecl->getMessage();
+
+ SourceLocation Loc = MatchedDecl->getLocation();
+
+ if (!AssertMessage || AssertMessage->getLength() ||
+ AssertMessage->getLocStart().isMacroID() || Loc.isMacroID())
+ return;
+
+ diag(Loc,
+ "use unary 'static_assert' when the string literal is an empty string")
+ << FixItHint::CreateRemoval(AssertMessage->getSourceRange());
+}
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
Added: clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h?rev=307791&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UnaryStaticAssertCheck.h Wed Jul 12 06:43:35 2017
@@ -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 &Result) override;
+};
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_UNARY_STATIC_ASSERT_H
Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=307791&r1=307790&r2=307791&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Wed Jul 12 06:43:35 2017
@@ -108,6 +108,12 @@ Improvements to clang-tidy
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
+ <http://clang.llvm.org/extra/clang-tidy/checks/modernize-unary-static-assert.html>`_ 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
<http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-emplace.html>`_ check
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst?rev=307791&r1=307790&r2=307791&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Wed Jul 12 06:43:35 2017
@@ -132,6 +132,7 @@ Clang-Tidy Checks
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
Added: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-unary-static-assert.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-unary-static-assert.rst?rev=307791&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-unary-static-assert.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-unary-static-assert.rst Wed Jul 12 06:43:35 2017
@@ -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);
+ }
Added: clang-tools-extra/trunk/test/clang-tidy/modernize-unary-static-assert.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-unary-static-assert.cpp?rev=307791&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-unary-static-assert.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-unary-static-assert.cpp Wed Jul 12 06:43:35 2017
@@ -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(""); }
More information about the cfe-commits
mailing list