[clang-tidy] Split fuchsia-default-arguments

Splits fuchsia-default-arguments check into two checks. fuchsia-default-arguments-calls warns if a function or method is called with default arguments. fuchsia-default-arguments-declarations warns if a function or method is declared with default parameters.

Committed on behalf of @diegoast (Diego Astiazarán).

Resolves b38051.

Differential Revision: https://reviews.llvm.org/D62437

llvm-svn: 363712
This commit is contained in:
Julie Hockett 2019-06-18 18:07:33 +00:00
parent 76a149ef81
commit d9b3d08a9a
13 changed files with 229 additions and 113 deletions

View File

@ -1,7 +1,8 @@
set(LLVM_LINK_COMPONENTS support)
add_clang_library(clangTidyFuchsiaModule
DefaultArgumentsCheck.cpp
DefaultArgumentsCallsCheck.cpp
DefaultArgumentsDeclarationsCheck.cpp
FuchsiaTidyModule.cpp
MultipleInheritanceCheck.cpp
OverloadedOperatorCheck.cpp

View File

@ -0,0 +1,35 @@
//===--- DefaultArgumentsCallsCheck.cpp - clang-tidy-----------------------===//
//
// 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 "DefaultArgumentsCallsCheck.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace fuchsia {
void DefaultArgumentsCallsCheck::registerMatchers(MatchFinder *Finder) {
// Calling a function which uses default arguments is disallowed.
Finder->addMatcher(cxxDefaultArgExpr().bind("stmt"), this);
}
void DefaultArgumentsCallsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *S = Result.Nodes.getNodeAs<CXXDefaultArgExpr>("stmt");
if (!S)
return;
diag(S->getUsedLocation(),
"calling a function that uses a default argument is disallowed");
diag(S->getParam()->getBeginLoc(), "default parameter was declared here",
DiagnosticIDs::Note);
}
} // namespace fuchsia
} // namespace tidy
} // namespace clang

View File

@ -1,4 +1,4 @@
//===--- DefaultArgumentsCheck.h - clang-tidy--------------------*- C++ -*-===//
//===--- DefaultArgumentsCallsCheck.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.
@ -6,8 +6,8 @@
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_H
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_CALLS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_CALLS_H
#include "../ClangTidyCheck.h"
@ -15,13 +15,13 @@ namespace clang {
namespace tidy {
namespace fuchsia {
/// Default arguments are not allowed in declared or called functions.
/// Default arguments are not allowed in called functions.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-arguments.html
class DefaultArgumentsCheck : public ClangTidyCheck {
/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-arguments-calls.html
class DefaultArgumentsCallsCheck : public ClangTidyCheck {
public:
DefaultArgumentsCheck(StringRef Name, ClangTidyContext *Context)
DefaultArgumentsCallsCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
@ -31,4 +31,4 @@ public:
} // namespace tidy
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_H
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_CALLS_H

View File

@ -1,61 +0,0 @@
//===--- DefaultArgumentsCheck.cpp - clang-tidy----------------------------===//
//
// 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 "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()->getBeginLoc(), "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->getEndLoc()) {
return;
} else if (DefaultArgRange.getBegin().isMacroID()) {
diag(D->getBeginLoc(),
"declaring a parameter with a default argument is disallowed");
} else {
SourceLocation StartLocation =
D->getName().empty() ? D->getBeginLoc() : D->getLocation();
SourceRange RemovalRange(Lexer::getLocForEndOfToken(
StartLocation, 0,
*Result.SourceManager,
Result.Context->getLangOpts()
),
DefaultArgRange.getEnd()
);
diag(D->getBeginLoc(),
"declaring a parameter with a default argument is disallowed")
<< D << FixItHint::CreateRemoval(RemovalRange);
}
}
}
} // namespace fuchsia
} // namespace tidy
} // namespace clang

View File

@ -0,0 +1,54 @@
//===--- DefaultArgumentsDeclarationsCheck.cpp - clang-tidy ---------------===//
//
// 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 "DefaultArgumentsDeclarationsCheck.h"
using namespace clang::ast_matchers;
namespace clang {
namespace tidy {
namespace fuchsia {
void DefaultArgumentsDeclarationsCheck::registerMatchers(MatchFinder *Finder) {
// Declaring default parameters is disallowed.
Finder->addMatcher(parmVarDecl(hasDefaultArgument()).bind("decl"), this);
}
void DefaultArgumentsDeclarationsCheck::check(
const MatchFinder::MatchResult &Result) {
const auto *D = Result.Nodes.getNodeAs<ParmVarDecl>("decl");
if (!D)
return;
SourceRange DefaultArgRange = D->getDefaultArgRange();
if (DefaultArgRange.getEnd() != D->getEndLoc())
return;
if (DefaultArgRange.getBegin().isMacroID()) {
diag(D->getBeginLoc(),
"declaring a parameter with a default argument is disallowed");
return;
}
SourceLocation StartLocation =
D->getName().empty() ? D->getBeginLoc() : D->getLocation();
SourceRange RemovalRange(
Lexer::getLocForEndOfToken(StartLocation, 0, *Result.SourceManager,
Result.Context->getLangOpts()),
DefaultArgRange.getEnd());
diag(D->getBeginLoc(),
"declaring a parameter with a default argument is disallowed")
<< D << FixItHint::CreateRemoval(RemovalRange);
}
} // namespace fuchsia
} // namespace tidy
} // namespace clang

View File

@ -0,0 +1,34 @@
//===--- DefaultArgumentsDeclarationsCheck.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_FUCHSIA_DEFAULT_ARGUMENTS_DECLARATIONS_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_FUCHSIA_DEFAULT_ARGUMENTS_DECLARATIONS_H
#include "../ClangTidyCheck.h"
namespace clang {
namespace tidy {
namespace fuchsia {
/// Default parameters are not allowed in declared functions.
///
/// For the user-facing documentation see:
/// http://clang.llvm.org/extra/clang-tidy/checks/fuchsia-default-parameters.html
class DefaultArgumentsDeclarationsCheck : public ClangTidyCheck {
public:
DefaultArgumentsDeclarationsCheck(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_DECLARATIONS_H

View File

@ -1,4 +1,4 @@
//===--- FuchsiaTidyModule.cpp - clang-tidy--------------------------------===//
//===--- FuchsiaTidyModule.cpp - clang-tidy -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@ -10,7 +10,8 @@
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "../google/UnnamedNamespaceInHeaderCheck.h"
#include "DefaultArgumentsCheck.h"
#include "DefaultArgumentsCallsCheck.h"
#include "DefaultArgumentsDeclarationsCheck.h"
#include "MultipleInheritanceCheck.h"
#include "OverloadedOperatorCheck.h"
#include "RestrictSystemIncludesCheck.h"
@ -28,8 +29,10 @@ namespace fuchsia {
class FuchsiaModule : public ClangTidyModule {
public:
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
CheckFactories.registerCheck<DefaultArgumentsCheck>(
"fuchsia-default-arguments");
CheckFactories.registerCheck<DefaultArgumentsCallsCheck>(
"fuchsia-default-arguments-calls");
CheckFactories.registerCheck<DefaultArgumentsDeclarationsCheck>(
"fuchsia-default-arguments-declarations");
CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
"fuchsia-header-anon-namespaces");
CheckFactories.registerCheck<MultipleInheritanceCheck>(

View File

@ -125,6 +125,20 @@ Improvements to clang-tidy
repeated branches in ``switch`` statements and indentical true and false
branches in conditional operators.
- New :doc:`fuchsia-default-arguments-calls
<clang-tidy/checks/fuchsia-default-arguments-calls>` check.
Warns if a function or method is called with default arguments.
This was previously done by `fuchsia-default-arguments check`, which has been
removed.
- New :doc:`fuchsia-default-arguments-calls
<clang-tidy/checks/fuchsia-default-arguments-calls>` check.
Warns if a function or method is declared with default parameters.
This was previously done by `fuchsia-default-arguments check` check, which has
been removed.
- New :doc:`google-readability-avoid-underscore-in-googletest-name
<clang-tidy/checks/google-readability-avoid-underscore-in-googletest-name>`
check.
@ -166,6 +180,14 @@ Improvements to clang-tidy
which greatly reduces warnings related to loops which are unlikely to
cause an actual functional bug.
- The fuchsia-default-arguments check has been removed.
Warnings of function or method calls and declarations with default arguments
were moved to :doc:`fuchsia-default-arguments-calls
<clang-tidy/checks/fuchsia-default-arguments-calls>` and
:doc:`fuchsia-default-arguments-calls
<clang-tidy/checks/fuchsia-default-arguments-calls>` checks respectively.
- The :doc:`google-runtime-int <clang-tidy/checks/google-runtime-int>`
check has been disabled in Objective-C++.

View File

@ -1,18 +1,16 @@
.. title:: clang-tidy - fuchsia-default-arguments
.. title:: clang-tidy - fuchsia-default-arguments-calls
fuchsia-default-arguments
=========================
fuchsia-default-arguments-calls
===============================
Warns if a function or method is declared or called with default arguments.
Warns if a function or method is called with default arguments.
For example, the declaration:
For example, given 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:

View File

@ -0,0 +1,16 @@
.. title:: clang-tidy - fuchsia-default-arguments-declarations
fuchsia-default-arguments-declarations
======================================
Warns if a function or method is declared with default parameters.
For example, the declaration:
.. code-block:: c++
int foo(int value = 5) { return value; }
will cause a warning.
See the features disallowed in Fuchsia at https://fuchsia.googlesource.com/zircon/+/master/docs/cxx.md

View File

@ -124,7 +124,8 @@ Clang-Tidy Checks
cppcoreguidelines-pro-type-vararg
cppcoreguidelines-slicing
cppcoreguidelines-special-member-functions
fuchsia-default-arguments
fuchsia-default-arguments-calls
fuchsia-default-arguments-declarations
fuchsia-header-anon-namespaces (redirects to google-build-namespaces) <fuchsia-header-anon-namespaces>
fuchsia-multiple-inheritance
fuchsia-overloaded-operator

View File

@ -0,0 +1,36 @@
// RUN: %check_clang_tidy %s fuchsia-default-arguments-calls %t
int foo(int value = 5) { return value; }
int f() {
foo();
// CHECK-NOTES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments-calls]
// CHECK-NOTES: [[@LINE-5]]:9: note: default parameter was declared here
}
int bar(int value) { return value; }
int n() {
foo(0);
bar(0);
}
void x(int i = 12);
struct S {
void x(int i);
};
void S::x(int i = 12) {}
int main() {
S s;
s.x();
// CHECK-NOTES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments-calls]
// CHECK-NOTES: [[@LINE-6]]:11: note: default parameter was declared here
// CHECK-NEXT: void S::x(int i = 12) {}
x();
// CHECK-NOTES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments-calls]
// CHECK-NOTES: [[@LINE-16]]:8: note: default parameter was declared here
// CHECK-NEXT: void x(int i = 12);
}

View File

@ -1,26 +1,15 @@
// RUN: %check_clang_tidy %s fuchsia-default-arguments %t
// RUN: %check_clang_tidy %s fuchsia-default-arguments-declarations %t
int foo(int value = 5) { return value; }
// CHECK-NOTES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES: int foo(int value) { return value; }
int f() {
foo();
// CHECK-NOTES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-7]]:9: note: default parameter was declared here
}
int bar(int value) { return value; }
int n() {
foo(0);
bar(0);
}
class Baz {
public:
int a(int value = 5) { return value; }
// CHECK-NOTES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES: int a(int value) { return value; }
int b(int value) { return value; }
@ -29,7 +18,7 @@ public:
class Foo {
// Fix should be suggested in declaration
int a(int value = 53);
// CHECK-NOTES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:9: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES: int a(int value);
};
@ -40,7 +29,7 @@ int Foo::a(int value) {
// Elided functions
void f(int = 5) {};
// CHECK-NOTES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES: void f(int) {};
void g(int) {};
@ -49,12 +38,12 @@ void g(int) {};
#define D(val) = val
void h(int i D(5));
// CHECK-NOTES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES-NOT: void h(int i);
void x(int i);
void x(int i = 12);
// CHECK-NOTES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:8: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES: void x(int i);
void x(int i) {}
@ -64,17 +53,5 @@ struct S {
};
void S::x(int i = 12) {}
// CHECK-NOTES: [[@LINE-1]]:11: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-1]]:11: warning: declaring a parameter with a default argument is disallowed [fuchsia-default-arguments-declarations]
// CHECK-FIXES: void S::x(int i) {}
int main() {
S s;
s.x();
// CHECK-NOTES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-8]]:11: note: default parameter was declared here
// CHECK-NEXT: void S::x(int i = 12) {}
x();
// CHECK-NOTES: [[@LINE-1]]:3: warning: calling a function that uses a default argument is disallowed [fuchsia-default-arguments]
// CHECK-NOTES: [[@LINE-18]]:8: note: default parameter was declared here
// CHECK-NEXT: void x(int i = 12);
}