Skip to content

Commit e3ae0c6

Browse files
committedMar 30, 2016
[clang-tidy] readability check for const params in declarations
Summary: Adds a clang-tidy warning for top-level consts in function declarations. Reviewers: hokein, sbenza, alexfh Subscribers: cfe-commits Patch by Matt Kulukundis! Differential Revision: http://reviews.llvm.org/D18408 llvm-svn: 264856
1 parent 1b654f2 commit e3ae0c6

File tree

7 files changed

+205
-1
lines changed

7 files changed

+205
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//===--- AvoidConstParamsInDecls.cpp - clang-tidy--------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "AvoidConstParamsInDecls.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
13+
#include "clang/Lex/Lexer.h"
14+
15+
using namespace clang::ast_matchers;
16+
17+
namespace clang {
18+
namespace tidy {
19+
namespace readability {
20+
namespace {
21+
22+
SourceRange getTypeRange(const ParmVarDecl &Param) {
23+
if (Param.getIdentifier() != nullptr)
24+
return SourceRange(Param.getLocStart(),
25+
Param.getLocEnd().getLocWithOffset(-1));
26+
return Param.getSourceRange();
27+
}
28+
29+
} // namespace
30+
31+
32+
void AvoidConstParamsInDecls::registerMatchers(MatchFinder *Finder) {
33+
const auto ConstParamDecl =
34+
parmVarDecl(hasType(qualType(isConstQualified()))).bind("param");
35+
Finder->addMatcher(functionDecl(unless(isDefinition()),
36+
has(typeLoc(forEach(ConstParamDecl))))
37+
.bind("func"),
38+
this);
39+
}
40+
41+
void AvoidConstParamsInDecls::check(const MatchFinder::MatchResult &Result) {
42+
const auto *Func = Result.Nodes.getNodeAs<FunctionDecl>("func");
43+
const auto *Param = Result.Nodes.getNodeAs<ParmVarDecl>("param");
44+
45+
QualType Type = Param->getType();
46+
if (!Type.isLocalConstQualified())
47+
return;
48+
49+
Type.removeLocalConst();
50+
51+
auto Diag = diag(Param->getLocStart(),
52+
"parameter %0 is const-qualified in the function "
53+
"declaration; const-qualification of parameters only has an "
54+
"effect in function definitions");
55+
if (Param->getName().empty()) {
56+
for (unsigned int i = 0; i < Func->getNumParams(); ++i) {
57+
if (Param == Func->getParamDecl(i)) {
58+
Diag << (i + 1);
59+
break;
60+
}
61+
}
62+
} else {
63+
Diag << Param;
64+
}
65+
Diag << FixItHint::CreateReplacement(getTypeRange(*Param),
66+
Type.getAsString());
67+
}
68+
69+
} // namespace readability
70+
} // namespace tidy
71+
} // namespace clang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===--- AvoidConstParamsInDecls.h - clang-tidy----------------------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AVOID_CONST_PARAMS_IN_DECLS_H
11+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AVOID_CONST_PARAMS_IN_DECLS_H
12+
13+
#include "../ClangTidy.h"
14+
15+
namespace clang {
16+
namespace tidy {
17+
namespace readability {
18+
19+
// Detect function declarations that have const value parameters and discourage
20+
// them.
21+
class AvoidConstParamsInDecls : public ClangTidyCheck {
22+
public:
23+
using ClangTidyCheck::ClangTidyCheck;
24+
25+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
26+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
27+
};
28+
29+
} // namespace readability
30+
} // namespace tidy
31+
} // namespace clang
32+
33+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AVOID_CONST_PARAMS_IN_DECLS_H

‎clang-tools-extra/clang-tidy/readability/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
set(LLVM_LINK_COMPONENTS support)
22

33
add_clang_library(clangTidyReadabilityModule
4+
AvoidConstParamsInDecls.cpp
45
BracesAroundStatementsCheck.cpp
56
ContainerSizeEmptyCheck.cpp
67
ElseAfterReturnCheck.cpp

‎clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "../ClangTidy.h"
1111
#include "../ClangTidyModule.h"
1212
#include "../ClangTidyModuleRegistry.h"
13+
#include "AvoidConstParamsInDecls.h"
1314
#include "BracesAroundStatementsCheck.h"
1415
#include "ContainerSizeEmptyCheck.h"
1516
#include "ElseAfterReturnCheck.h"
@@ -32,6 +33,8 @@ namespace readability {
3233
class ReadabilityModule : public ClangTidyModule {
3334
public:
3435
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
36+
CheckFactories.registerCheck<AvoidConstParamsInDecls>(
37+
"readability-avoid-const-params-in-decls");
3538
CheckFactories.registerCheck<BracesAroundStatementsCheck>(
3639
"readability-braces-around-statements");
3740
CheckFactories.registerCheck<ContainerSizeEmptyCheck>(

‎clang-tools-extra/docs/clang-tidy/checks/list.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ Clang-Tidy Checks
9090
performance-faster-string-find
9191
performance-for-range-copy
9292
performance-implicit-cast-in-loop
93-
performance-unnecessary-value-param
9493
performance-unnecessary-copy-initialization
94+
performance-unnecessary-value-param
95+
readability-avoid-const-params-in-decls
9596
readability-braces-around-statements
9697
readability-container-size-empty
9798
readability-else-after-return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. title:: clang-tidy - readability-avoid-const-params-in-decls
2+
3+
readability-avoid-const-params-in-decls
4+
=======================================
5+
6+
Checks whether a function declaration has parameters that are top level const.
7+
8+
`const` values in declarations do not affect the signature of a function, so
9+
they should not be put there. For example:
10+
11+
Examples:
12+
13+
.. code:: c++
14+
15+
void f(const string); // Bad: const is top level.
16+
void f(const string&); // Good: const is not top level.
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// RUN: %check_clang_tidy %s readability-avoid-const-params-in-decls %t
2+
3+
using alias_type = bool;
4+
using alias_const_type = const bool;
5+
6+
7+
void F1(const int i);
8+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]
9+
// CHECK-FIXES: void F1(int i);
10+
11+
void F2(const int *const i);
12+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
13+
// CHECK-FIXES: void F2(const int * i);
14+
15+
void F3(int const i);
16+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
17+
// CHECK-FIXES: void F3(int i);
18+
19+
void F4(alias_type const i);
20+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'i' is const-qualified
21+
// CHECK-FIXES: void F4(alias_type i);
22+
23+
void F5(const int);
24+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
25+
// CHECK-FIXES: void F5(int);
26+
27+
void F6(const int *const);
28+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
29+
// BUG(b/27584482): void F6(const int *); should be produced
30+
31+
void F7(int, const int);
32+
// CHECK-MESSAGES: :[[@LINE-1]]:14: warning: parameter 2 is const-qualified
33+
// CHECK-FIXES: void F7(int, int);
34+
35+
void F8(const int, const int);
36+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 1 is const-qualified
37+
// CHECK-MESSAGES: :[[@LINE-2]]:20: warning: parameter 2 is const-qualified
38+
// CHECK-FIXES: void F8(int, int);
39+
40+
void F9(const int long_name);
41+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: parameter 'long_name'
42+
// CHECK-FIXES: void F9(int long_name);
43+
44+
void F10(const int *const *const long_name);
45+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: parameter 'long_name'
46+
// CHECK-FIXES: void F10(const int *const * long_name);
47+
48+
49+
struct Foo {
50+
Foo(const int i);
51+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: parameter 'i'
52+
// CHECK-FIXES: Foo(int i);
53+
54+
void operator()(const int i);
55+
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: parameter 'i'
56+
// CHECK-FIXES: void operator()(int i);
57+
};
58+
59+
// Do not match on definitions
60+
void NF1(const int i) {}
61+
void NF2(const int *const i) {}
62+
void NF3(int const i) {}
63+
void NF4(alias_type const i) {}
64+
void NF5(const int) {}
65+
void NF6(const int *const) {}
66+
void NF7(int, const int) {}
67+
void NF8(const int, const int) {}
68+
69+
// Do not match on other stuff
70+
void NF(const alias_type& i);
71+
void NF(const int &i);
72+
void NF(const int *i);
73+
void NF(alias_const_type i);
74+
void NF(const alias_type&);
75+
void NF(const int&);
76+
void NF(const int*);
77+
void NF(const int* const*);
78+
void NF(alias_const_type);

0 commit comments

Comments
 (0)
Please sign in to comment.