koldaniel updated this revision to Diff 190377.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D33841/new/
https://reviews.llvm.org/D33841
Files:
clang-tidy/readability/CMakeLists.txt
clang-tidy/readability/ReadabilityTidyModule.cpp
clang-tidy/readability/RedundantExternCheck.cpp
clang-tidy/readability/RedundantExternCheck.h
docs/ReleaseNotes.rst
docs/clang-tidy/checks/list.rst
docs/clang-tidy/checks/readability-redundant-extern.rst
test/clang-tidy/readability-redundant-extern.cpp
Index: test/clang-tidy/readability-redundant-extern.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/readability-redundant-extern.cpp
@@ -0,0 +1,37 @@
+// RUN: %check_clang_tidy %s readability-redundant-extern %t
+
+extern int f();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: redundant 'extern' keyword [readability-redundant-extern]
+// CHECK-FIXES: int f();
+
+extern int f() { return 0; }
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: redundant 'extern' keyword [readability-redundant-extern]
+// CHECK-FIXES: int f() { return 0; }
+
+extern "C" int g();
+
+extern "C" int g() { return 0; }
+
+extern "C++" int h();
+
+extern "C++" int h() { return 0; }
+
+inline extern void foo_inline();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: redundant 'extern' keyword [readability-redundant-extern]
+// CHECK-FIXES: inline void foo_inline();
+
+#define FOO_EXTERN extern
+FOO_EXTERN void foo_macro_1();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: redundant 'extern' keyword [readability-redundant-extern]
+
+#define FOO_INLINE inline
+FOO_INLINE extern void foo_macro_3();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: redundant 'extern' keyword [readability-redundant-extern]
+
+#define FOO_EXTERN_INLINE inline extern
+FOO_EXTERN_INLINE void foo_macro_2();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: redundant 'extern' keyword [readability-redundant-extern]
+
+void file_scope();
+
+void another_file_scope(int _extern);
Index: docs/clang-tidy/checks/readability-redundant-extern.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/readability-redundant-extern.rst
@@ -0,0 +1,13 @@
+.. title:: clang-tidy - readability-redundant-extern
+
+readability-redundant-extern
+============================
+
+Removes the redundant ``extern`` keywords from code.
+
+``extern`` is redundant in function declarations
+
+.. code-block:: c++
+
+ extern void h();
+
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -258,6 +258,7 @@
readability-non-const-parameter
readability-redundant-control-flow
readability-redundant-declaration
+ readability-redundant-extern
readability-redundant-function-ptr-dereference
readability-redundant-member-init
readability-redundant-preprocessor
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -117,6 +117,11 @@
<clang-tidy/checks/modernize-use-override>` now supports `OverrideSpelling`
and `FinalSpelling` options.
+- New :doc:`readability-redundant-extern
+ <clang-tidy/checks/readability-redundant-extern>` check.
+
+ Removes the redundant ``extern`` keywords.
+
Improvements to include-fixer
-----------------------------
Index: clang-tidy/readability/RedundantExternCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/readability/RedundantExternCheck.h
@@ -0,0 +1,34 @@
+//===--- RedundantExternCheck.h - clang-tidy --------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANT_EXTERN_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANT_EXTERN_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+/// Finds redundant 'extern' keywords
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/readability-redundant-extern.html
+class RedundantExternCheck : public ClangTidyCheck {
+public:
+ RedundantExternCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_REDUNDANT_EXTERN_H
Index: clang-tidy/readability/RedundantExternCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/readability/RedundantExternCheck.cpp
@@ -0,0 +1,54 @@
+//===--- RedundantExternCheck.cpp - clang-tidy ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RedundantExternCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace readability {
+
+void RedundantExternCheck::registerMatchers(MatchFinder *Finder) {
+ Finder->addMatcher(
+ functionDecl(unless(isExternC())).bind("redundant_extern"),
+ this);
+}
+
+void RedundantExternCheck::check(const MatchFinder::MatchResult &Result) {
+ auto* FD =
+ Result.Nodes.getNodeAs<FunctionDecl>("redundant_extern");
+ SourceLocation BeginLoc = FD->getBeginLoc();
+ SourceLocation EndLoc = FD->getEndLoc();
+
+ if(!(FD->getStorageClass() == SC_Extern))
+ return;
+
+ auto Diag = diag(FD->getBeginLoc(), "redundant 'extern' keyword");
+
+ if (FD->getBeginLoc().isMacroID())
+ return;
+
+ StringRef Text =
+ Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
+ *Result.SourceManager, getLangOpts());
+
+ int Offset = Text.find("extern");
+
+ Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
+ BeginLoc.getLocWithOffset(Offset),
+ BeginLoc.getLocWithOffset(Offset + strlen("extern"))));
+}
+
+} // namespace readability
+} // namespace tidy
+} // namespace clang
+
Index: clang-tidy/readability/ReadabilityTidyModule.cpp
===================================================================
--- clang-tidy/readability/ReadabilityTidyModule.cpp
+++ clang-tidy/readability/ReadabilityTidyModule.cpp
@@ -29,6 +29,7 @@
#include "RedundantControlFlowCheck.h"
#include "RedundantDeclarationCheck.h"
#include "RedundantFunctionPtrDereferenceCheck.h"
+#include "RedundantExternCheck.h"
#include "RedundantMemberInitCheck.h"
#include "RedundantPreprocessorCheck.h"
#include "RedundantSmartptrGetCheck.h"
@@ -79,6 +80,8 @@
"readability-misleading-indentation");
CheckFactories.registerCheck<MisplacedArrayIndexCheck>(
"readability-misplaced-array-index");
+ CheckFactories.registerCheck<RedundantExternCheck>(
+ "readability-redundant-extern");
CheckFactories.registerCheck<RedundantFunctionPtrDereferenceCheck>(
"readability-redundant-function-ptr-dereference");
CheckFactories.registerCheck<RedundantMemberInitCheck>(
Index: clang-tidy/readability/CMakeLists.txt
===================================================================
--- clang-tidy/readability/CMakeLists.txt
+++ clang-tidy/readability/CMakeLists.txt
@@ -22,6 +22,7 @@
ReadabilityTidyModule.cpp
RedundantControlFlowCheck.cpp
RedundantDeclarationCheck.cpp
+ RedundantExternCheck.cpp
RedundantFunctionPtrDereferenceCheck.cpp
RedundantMemberInitCheck.cpp
RedundantPreprocessorCheck.cpp
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits