[clang-tools-extra] r319225 - Add a new clang-tidy module for Fuchsia as an umbrella to diagnose issues with the Fuschia and Zircon coding guidelines (https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md). Adds the first of such checkers, which detects when default arguments are declared in a function declaration or when default arguments are used at call sites.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 28 13:09:25 PST 2017


Author: aaronballman
Date: Tue Nov 28 13:09:25 2017
New Revision: 319225

URL: http://llvm.org/viewvc/llvm-project?rev=319225&view=rev
Log:
Add a new clang-tidy module for Fuchsia as an umbrella to diagnose issues with the Fuschia and Zircon coding guidelines (https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md). Adds the first of such checkers, which detects when default arguments are declared in a function declaration or when default arguments are used at call sites.

Patch by Julie Hockett

Added:
    clang-tools-extra/trunk/clang-tidy/fuchsia/
    clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
    clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.h
    clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp
    clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-default-arguments.rst
    clang-tools-extra/trunk/test/clang-tidy/fuchsia-default-arguments.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt
    clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst
    clang-tools-extra/trunk/docs/clang-tidy/index.rst

Modified: clang-tools-extra/trunk/clang-tidy/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/CMakeLists.txt?rev=319225&r1=319224&r2=319225&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/CMakeLists.txt Tue Nov 28 13:09:25 2017
@@ -31,6 +31,7 @@ add_subdirectory(boost)
 add_subdirectory(bugprone)
 add_subdirectory(cert)
 add_subdirectory(cppcoreguidelines)
+add_subdirectory(fuchsia)
 add_subdirectory(google)
 add_subdirectory(hicpp)
 add_subdirectory(llvm)

Added: clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt?rev=319225&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt (added)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/CMakeLists.txt Tue Nov 28 13:09:25 2017
@@ -0,0 +1,14 @@
+set(LLVM_LINK_COMPONENTS support)
+
+add_clang_library(clangTidyFuchsiaModule
+  DefaultArgumentsCheck.cpp
+  FuchsiaTidyModule.cpp
+
+  LINK_LIBS
+  clangAST
+  clangASTMatchers
+  clangBasic
+  clangLex
+  clangTidy
+  clangTidyUtils
+  )

Added: clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.cpp?rev=319225&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.cpp Tue Nov 28 13:09:25 2017
@@ -0,0 +1,64 @@
+//===--- DefaultArgumentsCheck.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 "DefaultArgumentsCheck.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+void DefaultArgumentsCheck::registerMatchers(MatchFinder *Finder) {
+  // Calling a function which uses default arguments is disallowed.
+  Finder->addMatcher(cxxDefaultArgExpr().bind("stmt"), this);
+  // Declaring default parameters is disallowed.
+  Finder->addMatcher(parmVarDecl(hasDefaultArgument()).bind("decl"), this);
+}
+
+void DefaultArgumentsCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *S =
+          Result.Nodes.getNodeAs<CXXDefaultArgExpr>("stmt")) {
+    diag(S->getUsedLocation(),
+         "calling a function that uses a default argument is disallowed");
+    diag(S->getParam()->getLocStart(),
+        "default parameter was declared here",
+         DiagnosticIDs::Note);
+  } else if (const ParmVarDecl *D =
+          Result.Nodes.getNodeAs<ParmVarDecl>("decl")) {
+    SourceRange DefaultArgRange = D->getDefaultArgRange();
+
+    if (DefaultArgRange.getEnd() != D->getLocEnd()) {
+      return;
+    } else if (DefaultArgRange.getBegin().isMacroID()) {
+      diag(D->getLocStart(),
+           "declaring a parameter with a default argument is disallowed");
+    } else {
+      SourceLocation StartLocation = D->getName().empty() ?
+              D->getLocStart() : D->getLocation();
+
+      SourceRange RemovalRange(Lexer::getLocForEndOfToken(
+             StartLocation, 0,
+             *Result.SourceManager,
+             Result.Context->getLangOpts()
+           ),
+           DefaultArgRange.getEnd()
+         );
+
+      diag(D->getLocStart(),
+          "declaring a parameter with a default argument is disallowed")
+          << D
+          << FixItHint::CreateRemoval(RemovalRange);
+    }
+  }
+}
+
+} // namespace fuchsia
+} // namespace tidy
+} // namespace clang

Added: clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.h?rev=319225&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.h (added)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/DefaultArgumentsCheck.h Tue Nov 28 13:09:25 2017
@@ -0,0 +1,35 @@
+//===--- DefaultArgumentsCheck.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_DEFAULT_ARGUMENTS_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_H
+
+#include "../ClangTidy.h"
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+/// Default arguments are not allowed in declared or called functions.
+///
+/// For the user-facing documentation see:
+/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-arguments.html
+class DefaultArgumentsCheck : public ClangTidyCheck {
+public:
+  DefaultArgumentsCheck(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_DEFAULT_ARGUMENTS_H

Added: clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp?rev=319225&view=auto
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp (added)
+++ clang-tools-extra/trunk/clang-tidy/fuchsia/FuchsiaTidyModule.cpp Tue Nov 28 13:09:25 2017
@@ -0,0 +1,39 @@
+//===--- FuchsiaTidyModule.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 "../ClangTidy.h"
+#include "../ClangTidyModule.h"
+#include "../ClangTidyModuleRegistry.h"
+#include "DefaultArgumentsCheck.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang {
+namespace tidy {
+namespace fuchsia {
+
+/// This module is for Fuchsia specific checks.
+class FuchsiaModule : public ClangTidyModule {
+public:
+  void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
+    CheckFactories.registerCheck<DefaultArgumentsCheck>(
+        "fuchsia-default-arguments");
+  }
+};
+// Register the FuchsiaTidyModule using this statically initialized variable.
+static ClangTidyModuleRegistry::Add<FuchsiaModule>
+    X("fuchsia-module", "Adds Fuchsia platform checks.");
+} // namespace fuchsia
+
+// This anchor is used to force the linker to link in the generated object file
+// and thus register the FuchsiaModule.
+volatile int FuchsiaModuleAnchorSource = 0;
+
+} // namespace tidy
+} // namespace clang

Modified: clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt?rev=319225&r1=319224&r2=319225&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/CMakeLists.txt Tue Nov 28 13:09:25 2017
@@ -21,6 +21,7 @@ target_link_libraries(clang-tidy
   clangTidyBugproneModule
   clangTidyCERTModule
   clangTidyCppCoreGuidelinesModule
+  clangTidyFuchsiaModule
   clangTidyGoogleModule
   clangTidyHICPPModule
   clangTidyLLVMModule

Modified: clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp?rev=319225&r1=319224&r2=319225&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/ClangTidyMain.cpp Tue Nov 28 13:09:25 2017
@@ -483,6 +483,11 @@ static int LLVM_ATTRIBUTE_UNUSED CppCore
     CppCoreGuidelinesModuleAnchorSource;
 
 // This anchor is used to force the linker to link the GoogleModule.
+extern volatile int FuchsiaModuleAnchorSource;
+static int LLVM_ATTRIBUTE_UNUSED FuchsiaModuleAnchorDestination =
+    FuchsiaModuleAnchorSource;
+
+// This anchor is used to force the linker to link the GoogleModule.
 extern volatile int GoogleModuleAnchorSource;
 static int LLVM_ATTRIBUTE_UNUSED GoogleModuleAnchorDestination =
     GoogleModuleAnchorSource;

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=319225&r1=319224&r2=319225&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Tue Nov 28 13:09:25 2017
@@ -110,7 +110,12 @@ Improvements to clang-tidy
 - The 'misc-string-constructor' check was renamed to `bugprone-string-constructor
   <http://clang.llvm.org/extra/clang-tidy/checks/bugprone-string-constructor.html>`_
 
-- New `google-objc-avoid-throwing-exception
+- New `fuchsia-default-arguments
+  <http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-arguments.html>`_ check
+
+  Warns if a function or method is declared or called with default arguments.
+
+- New `google-avoid-throwing-objc-exception
   <http://clang.llvm.org/extra/clang-tidy/checks/google-objc-avoid-throwing-exception.html>`_ check
 
   Add new check to detect throwing exceptions in Objective-C code, which should be avoided.

Added: clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-default-arguments.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-default-arguments.rst?rev=319225&view=auto
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-default-arguments.rst (added)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/fuchsia-default-arguments.rst Tue Nov 28 13:09:25 2017
@@ -0,0 +1,24 @@
+.. title:: clang-tidy - fuchsia-default-arguments
+
+fuchsia-default-arguments
+=========================
+
+Warns if a function or method is declared or called with default arguments.
+
+For example, the declaration:
+
+.. code-block:: c++
+
+  int foo(int value = 5) { return value; }
+
+will cause a warning.
+
+A function call expression that uses a default argument will be diagnosed.
+Calling it without defaults will not cause a warning:
+
+.. code-block:: c++
+
+  foo();  // warning
+  foo(0); // no warning
+
+See the features disallowed in Fuchsia at https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md

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=319225&r1=319224&r2=319225&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/list.rst Tue Nov 28 13:09:25 2017
@@ -68,6 +68,7 @@ Clang-Tidy Checks
    cppcoreguidelines-pro-type-vararg
    cppcoreguidelines-slicing
    cppcoreguidelines-special-member-functions
+   fuchsia-default-arguments
    google-build-explicit-make-pair
    google-build-namespaces
    google-build-using-namespace

Modified: clang-tools-extra/trunk/docs/clang-tidy/index.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/index.rst?rev=319225&r1=319224&r2=319225&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/index.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/index.rst Tue Nov 28 13:09:25 2017
@@ -61,6 +61,7 @@ Name prefix            Description
 ``cert-``              Checks related to CERT Secure Coding Guidelines.
 ``cppcoreguidelines-`` Checks related to C++ Core Guidelines.
 ``clang-analyzer-``    Clang Static Analyzer checks.
+``fuchsia-``           Checks related to Fuchsia coding conventions.
 ``google-``            Checks related to Google coding conventions.
 ``hicpp-``             Checks related to High Integrity C++ Coding Standard.
 ``llvm-``              Checks related to the LLVM coding conventions.

Added: clang-tools-extra/trunk/test/clang-tidy/fuchsia-default-arguments.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/fuchsia-default-arguments.cpp?rev=319225&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/fuchsia-default-arguments.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/fuchsia-default-arguments.cpp Tue Nov 28 13:09:25 2017
@@ -0,0 +1,81 @@
+// RUN: %check_clang_tidy %s fuchsia-default-arguments %t
+
+int foo(int value = 5) { return value; }
+// CHECK-MESSAGES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+// CHECK-FIXES: int foo(int value) { return value; }
+
+int f() {
+  foo();
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
+  // CHECK-NEXT: note: default parameter was declared here:
+  // CHECK-NEXT: int foo(int value = 5) { return value; }
+}
+
+int bar(int value) { return value; }
+
+int n() {
+  foo(0);
+  bar(0);
+}
+
+class Baz {
+public:
+  int a(int value = 5) { return value; }
+  // CHECK-MESSAGES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+  // CHECK-FIXES: int a(int value) { return value; }
+
+  int b(int value) { return value; }
+};
+
+class Foo {
+  // Fix should be suggested in declaration
+  int a(int value = 53);
+  // CHECK-MESSAGES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+  // CHECK-FIXES: int a(int value);
+};
+
+// Fix shouldn't be suggested in implementation
+int Foo::a(int value) {
+  return value;
+}
+
+// Elided functions
+void f(int = 5) {};
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+// CHECK-FIXES: void f(int) {};
+
+void g(int) {};
+
+// Should not suggest fix for macro-defined parameters
+#define D(val) = val
+
+void h(int i D(5));
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+// CHECK-FIXES-NOT: void h(int i);
+
+void x(int i);
+void x(int i = 12);
+// CHECK-MESSAGES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+// CHECK-FIXES: void x(int i);
+
+void x(int i) {}
+
+struct S {
+  void x(int i);
+};
+
+void S::x(int i = 12) {}
+// CHECK-MESSAGES: [[@LINE-1]]:11: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
+// CHECK-FIXES: void S::x(int i) {}
+
+int main() {
+  S s;
+  s.x();
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
+  // CHECK-NEXT: note: default parameter was declared here:
+  // CHECK-NEXT: void S::x(int i = 12) {}
+  x();
+  // CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
+  // CHECK-NEXT: note: default parameter was declared here:
+  // CHECK-NEXT: void x(int i = 12);
+}




More information about the cfe-commits mailing list