juliehockett updated this revision to Diff 130027.
juliehockett marked an inline comment as done.
juliehockett added a comment.
Updating checker to not warn for trailing returns on lambdas.
https://reviews.llvm.org/D42116
Files:
clang-tidy/fuchsia/CMakeLists.txt
clang-tidy/fuchsia/FuchsiaTidyModule.cpp
clang-tidy/fuchsia/TrailingReturnCheck.cpp
clang-tidy/fuchsia/TrailingReturnCheck.h
docs/ReleaseNotes.rst
docs/clang-tidy/checks/fuchsia-trailing-return.rst
docs/clang-tidy/checks/list.rst
test/clang-tidy/fuchsia-trailing-return.cpp
Index: test/clang-tidy/fuchsia-trailing-return.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/fuchsia-trailing-return.cpp
@@ -0,0 +1,18 @@
+// RUN: %check_clang_tidy %s fuchsia-trailing-return %t
+
+int add_one(const int arg) { return arg; }
+
+auto get_add_one() -> int (*)(const int) {
+ // CHECK-MESSAGES: [[@LINE-1]]:1: warning: trailing returns are disallowed
+ // CHECK-NEXT: auto get_add_one() -> int (*)(const int) {
+ return add_one;
+}
+
+auto lambda = [](double x, double y) {return x + y;};
+
+auto lambda2 = [](double x, double y) -> double {return x + y;};
+
+int main() {
+ get_add_one()(5);
+ return 0;
+}
Index: docs/clang-tidy/checks/list.rst
===================================================================
--- docs/clang-tidy/checks/list.rst
+++ docs/clang-tidy/checks/list.rst
@@ -71,6 +71,7 @@
fuchsia-default-arguments
fuchsia-overloaded-operator
fuchsia-statically-constructed-objects
+ fuchsia-trailing-return
fuchsia-virtual-inheritance
google-build-explicit-make-pair
google-build-namespaces
Index: docs/clang-tidy/checks/fuchsia-trailing-return.rst
===================================================================
--- /dev/null
+++ docs/clang-tidy/checks/fuchsia-trailing-return.rst
@@ -0,0 +1,29 @@
+.. title:: clang-tidy - fuchsia-trailing-return
+
+fuchsia-trailing-return
+=======================
+
+Warns if a function has a trailing return, except for lambdas with otherwise
+unutterable return types.
+
+For example:
+
+.. code-block:: c++
+
+ // No warning
+ int add_one(const int arg) { return arg; }
+
+ // Warning
+ auto get_add_one() -> int (*)(const int) {
+ return add_one;
+ }
+
+An exception is made for lambdas:
+
+.. code-block:: c++
+
+ // No warning
+ auto lambda = [](double x, double y) -> double {return x + y;};
+
+
+See the features disallowed in Fuchsia at https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md
Index: docs/ReleaseNotes.rst
===================================================================
--- docs/ReleaseNotes.rst
+++ docs/ReleaseNotes.rst
@@ -64,6 +64,12 @@
object is statically initialized with a ``constexpr`` constructor or has no
explicit constructor.
+- New `fuchsia-trailing-return
+ <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-trailing-return.html>`_ check
+
+ Warns if a function has a trailing return, except for lambdas with otherwise
+ unutterable return types.
+
Improvements to include-fixer
-----------------------------
Index: clang-tidy/fuchsia/TrailingReturnCheck.h
===================================================================
--- /dev/null
+++ clang-tidy/fuchsia/TrailingReturnCheck.h
@@ -0,0 +1,36 @@
+//===--- TrailingReturnCheck.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_FUCHSIA_TRAILING_RETURN_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_TRAILING_RETURN_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+/// Functions which have trailing returns are disallowed, except for lambdas
+/// with otherwise unutterable return types.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-trailing-return.html
+class TrailingReturnCheck : public ClangTidyCheck {
+public:
+ TrailingReturnCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context) {}
+ void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+ void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+};
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_TRAILING_RETURN_H
Index: clang-tidy/fuchsia/TrailingReturnCheck.cpp
===================================================================
--- /dev/null
+++ clang-tidy/fuchsia/TrailingReturnCheck.cpp
@@ -0,0 +1,43 @@
+//===--- TrailingReturnCheck.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 "TrailingReturnCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+AST_MATCHER(FunctionDecl, hasTrailingReturn) {
+ const Type *T = Node.getType().getTypePtr();
+ const auto *F = cast<FunctionProtoType>(T);
+ return F->hasTrailingReturn();
+}
+
+void TrailingReturnCheck::registerMatchers(MatchFinder *Finder) {
+ // Functions which have trailing returns are disallowed, except for lambdas
+ // with otherwise unutterable return types.
+ Finder->addMatcher(
+ functionDecl(allOf(hasTrailingReturn(),
+ unless(hasParent(cxxRecordDecl(isLambda())))))
+ .bind("decl"),
+ this);
+}
+
+void TrailingReturnCheck::check(const MatchFinder::MatchResult &Result) {
+ if (const auto *D = Result.Nodes.getNodeAs<Decl>("decl"))
+ diag(D->getLocStart(), "trailing returns are disallowed");
+}
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang
Index: clang-tidy/fuchsia/FuchsiaTidyModule.cpp
===================================================================
--- clang-tidy/fuchsia/FuchsiaTidyModule.cpp
+++ clang-tidy/fuchsia/FuchsiaTidyModule.cpp
@@ -13,6 +13,7 @@
#include "DefaultArgumentsCheck.h"
#include "OverloadedOperatorCheck.h"
#include "StaticallyConstructedObjectsCheck.h"
+#include "TrailingReturnCheck.h"
#include "VirtualInheritanceCheck.h"
using namespace clang::ast_matchers;
@@ -31,6 +32,8 @@
"fuchsia-overloaded-operator");
CheckFactories.registerCheck<StaticallyConstructedObjectsCheck>(
"fuchsia-statically-constructed-objects");
+ CheckFactories.registerCheck<TrailingReturnCheck>(
+ "fuchsia-trailing-return");
CheckFactories.registerCheck<VirtualInheritanceCheck>(
"fuchsia-virtual-inheritance");
}
Index: clang-tidy/fuchsia/CMakeLists.txt
===================================================================
--- clang-tidy/fuchsia/CMakeLists.txt
+++ clang-tidy/fuchsia/CMakeLists.txt
@@ -5,6 +5,7 @@
FuchsiaTidyModule.cpp
OverloadedOperatorCheck.cpp
StaticallyConstructedObjectsCheck.cpp
+ TrailingReturnCheck.cpp
VirtualInheritanceCheck.cpp
LINK_LIBS
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits