Index: clang-tidy/CMakeLists.txt =================================================================== --- clang-tidy/CMakeLists.txt +++ clang-tidy/CMakeLists.txt @@ -31,6 +31,7 @@ add_subdirectory(bugprone) add_subdirectory(cert) add_subdirectory(cppcoreguidelines) +add_subdirectory(fuchsia) add_subdirectory(google) add_subdirectory(hicpp) add_subdirectory(llvm) Index: clang-tidy/fuchsia/CMakeLists.txt =================================================================== --- /dev/null +++ clang-tidy/fuchsia/CMakeLists.txt @@ -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 + ) Index: clang-tidy/fuchsia/DefaultArgumentsCheck.h =================================================================== --- /dev/null +++ clang-tidy/fuchsia/DefaultArgumentsCheck.h @@ -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 Index: clang-tidy/fuchsia/DefaultArgumentsCheck.cpp =================================================================== --- /dev/null +++ clang-tidy/fuchsia/DefaultArgumentsCheck.cpp @@ -0,0 +1,42 @@ +//===--- 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 { + +AST_MATCHER(ParmVarDecl, hasDefaultArgument) { return Node.hasDefaultArg(); } + +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 CXXDefaultArgExpr *S = + Result.Nodes.getNodeAs("stmt")) { + diag(S->getUsedLocation(), + "calling functions which use default arguments is disallowed"); + diag(S->getParam()->getLocStart(), "the default parameter was declared here", + DiagnosticIDs::Note); + } else if (const ParmVarDecl *D = Result.Nodes.getNodeAs("decl")) { + diag(D->getLocStart(), + "declaring functions which use default arguments is disallowed"); + } +} + +} // namespace fuchsia +} // namespace tidy +} // namespace clang Index: clang-tidy/fuchsia/FuchsiaTidyModule.cpp =================================================================== --- /dev/null +++ clang-tidy/fuchsia/FuchsiaTidyModule.cpp @@ -0,0 +1,40 @@ +//===--- 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( + "fuchsia-default-arguments"); + } +}; +// Register the FuchsiaTidyModule using this statically initialized variable. +static ClangTidyModuleRegistry::Add + 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 Index: clang-tidy/tool/CMakeLists.txt =================================================================== --- clang-tidy/tool/CMakeLists.txt +++ clang-tidy/tool/CMakeLists.txt @@ -21,6 +21,7 @@ clangTidyBugproneModule clangTidyCERTModule clangTidyCppCoreGuidelinesModule + clangTidyFuchsiaModule clangTidyGoogleModule clangTidyHICPPModule clangTidyLLVMModule Index: clang-tidy/tool/ClangTidyMain.cpp =================================================================== --- clang-tidy/tool/ClangTidyMain.cpp +++ clang-tidy/tool/ClangTidyMain.cpp @@ -482,6 +482,11 @@ static int LLVM_ATTRIBUTE_UNUSED CppCoreGuidelinesModuleAnchorDestination = 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 = Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,11 @@ Improvements to clang-tidy -------------------------- +- New `fuchsia-default-arguments + `_ check + + Prevent use of default arguments in declared or called functions in Fuchsia. + - New `objc-property-declaration `_ check @@ -67,8 +72,8 @@ - New `google-objc-global-variable-declaration `_ check - Add new check for Objective-C code to ensure global - variables follow the naming convention of 'k[A-Z].*' (for constants) + Add new check for Objective-C code to ensure global + variables follow the naming convention of 'k[A-Z].*' (for constants) or 'g[A-Z].*' (for variables). - New module `objc` for Objective-C style checks. @@ -137,7 +142,7 @@ Finds cases where integer division in a floating point context is likely to cause unintended loss of precision. -- New `cppcoreguidelines-owning-memory `_ check +- New `cppcoreguidelines-owning-memory `_ check This check implements the type-based semantic of ``gsl::owner``, but without flow analysis. @@ -145,13 +150,13 @@ - New `hicpp-exception-baseclass `_ check - Ensures that all exception will be instances of ``std::exception`` and classes + Ensures that all exception will be instances of ``std::exception`` and classes that are derived from it. - New `hicpp-signed-bitwise `_ check - Finds uses of bitwise operations on signed integer types, which may lead to + Finds uses of bitwise operations on signed integer types, which may lead to undefined or implementation defined behaviour. - New `android-cloexec-inotify-init1 @@ -170,7 +175,7 @@ `_ option. -- Added aliases for the `High Integrity C++ Coding Standard `_ +- Added aliases for the `High Integrity C++ Coding Standard `_ to already implemented checks in other modules. - `hicpp-deprecated-headers `_ Index: docs/clang-tidy/checks/fuchsia-default-arguments.rst =================================================================== --- /dev/null +++ docs/clang-tidy/checks/fuchsia-default-arguments.rst @@ -0,0 +1,25 @@ +.. title:: clang-tidy - fuchsia-default-arguments + +fuchsia-default-arguments +========================= + +Warns if a function is declared or called with default arguments. + +Example: The declaration: + +.. code-block:: c++ + + int foo(int value = 5) { return value; } + +will cause a warning. + +If a function with default arguments is already defined, calling it with no +arguments will also cause a warning. 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 Index: docs/clang-tidy/checks/list.rst =================================================================== --- docs/clang-tidy/checks/list.rst +++ docs/clang-tidy/checks/list.rst @@ -54,6 +54,7 @@ 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 Index: docs/clang-tidy/index.rst =================================================================== --- docs/clang-tidy/index.rst +++ docs/clang-tidy/index.rst @@ -61,6 +61,7 @@ ``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. @@ -669,4 +670,3 @@ * To apply suggested fixes ``-fix`` can be passed as an argument. This gathers all changes in a temporary directory and applies them. Passing ``-format`` will run clang-format over changed lines. - Index: test/clang-tidy/fuchsia-default-arguments.cpp =================================================================== --- /dev/null +++ test/clang-tidy/fuchsia-default-arguments.cpp @@ -0,0 +1,19 @@ +// RUN: %check_clang_tidy %s fuchsia-default-arguments %t + +int foo(int value = 5) { return value; } +// CHECK-MESSAGES: [[@LINE-1]]:9: warning: declaring functions which use default arguments is disallowed [fuchsia-default-arguments] + +int f(void) { + foo(); + // CHECK-MESSAGES: [[@LINE-1]]:3: warning: calling functions which use default arguments is disallowed [fuchsia-default-arguments] + // CHECK-NEXT: note: the default parameter was declared here: + // CHECK-NEXT: int foo(int value = 5) { return value; } +} + +// Negatives. +int bar(int value) { return value; } + +int n(void) { + foo(0); + bar(0); +}