diff options
author | Peter Collingbourne <peter@pcc.me.uk> | 2014-03-18 04:46:45 +0000 |
---|---|---|
committer | Peter Collingbourne <peter@pcc.me.uk> | 2014-03-18 04:46:45 +0000 |
commit | 35c3f61d749d78917ee572f672c89993692047b9 (patch) | |
tree | 75813f26078a3f71cf28c2174676e7c9ffcdd1c7 /clang-tools-extra/unittests/clang-tidy/MiscModuleTest.cpp | |
parent | 47a35dfd7cfcffe67e6cb716696d855970d0eb1f (diff) | |
download | bcm5719-llvm-35c3f61d749d78917ee572f672c89993692047b9.tar.gz bcm5719-llvm-35c3f61d749d78917ee572f672c89993692047b9.zip |
Add an argument comment checker to clang-tidy.
This checks that parameters named in comments that appear before arguments in
function and constructor calls match the parameter name used in the callee's
declaration. For example:
void f(int x, int y);
void g() {
f(/*y=*/0, /*z=*/0);
}
contains two violations of the policy, as the names 'x' and 'y' used in the
declaration do not match names 'y' and 'z' used at the call site.
I think there is significant value in being able to check/enforce this policy
as a way of guarding against accidental API misuse and silent breakages
caused by API changes.
Although this pattern appears somewhat frequently in the LLVM codebase,
this policy is not prescribed by the LLVM coding standards at the moment,
so it lives under 'misc'.
Differential Revision: http://llvm-reviews.chandlerc.com/D2914
llvm-svn: 204113
Diffstat (limited to 'clang-tools-extra/unittests/clang-tidy/MiscModuleTest.cpp')
-rw-r--r-- | clang-tools-extra/unittests/clang-tidy/MiscModuleTest.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/clang-tools-extra/unittests/clang-tidy/MiscModuleTest.cpp b/clang-tools-extra/unittests/clang-tidy/MiscModuleTest.cpp new file mode 100644 index 00000000000..c26783dc724 --- /dev/null +++ b/clang-tools-extra/unittests/clang-tidy/MiscModuleTest.cpp @@ -0,0 +1,41 @@ +#include "ClangTidyTest.h" +#include "google/GoogleTidyModule.h" +#include "misc/ArgumentCommentCheck.h" +#include "gtest/gtest.h" + +namespace clang { +namespace tidy { +namespace test { + +#define EXPECT_NO_CHANGES(Check, Code) \ + EXPECT_EQ(Code, runCheckOnCode<Check>(Code)) + +TEST(ArgumentCommentCheckTest, CorrectComments) { + EXPECT_NO_CHANGES(ArgumentCommentCheck, + "void f(int x, int y); void g() { f(/*x=*/0, /*y=*/0); }"); + EXPECT_NO_CHANGES(ArgumentCommentCheck, + "struct C { C(int x, int y); }; C c(/*x=*/0, /*y=*/0);"); +} + +TEST(ArgumentCommentCheckTest, ThisEditDistanceAboveThreshold) { + EXPECT_NO_CHANGES(ArgumentCommentCheck, + "void f(int xxx); void g() { f(/*xyz=*/0); }"); +} + +TEST(ArgumentCommentCheckTest, OtherEditDistanceAboveThreshold) { + EXPECT_EQ("void f(int xxx, int yyy); void g() { f(/*xxx=*/0, 0); }", + runCheckOnCode<ArgumentCommentCheck>( + "void f(int xxx, int yyy); void g() { f(/*Xxx=*/0, 0); }")); + EXPECT_EQ("struct C { C(int xxx, int yyy); }; C c(/*xxx=*/0, 0);", + runCheckOnCode<ArgumentCommentCheck>( + "struct C { C(int xxx, int yyy); }; C c(/*Xxx=*/0, 0);")); +} + +TEST(ArgumentCommentCheckTest, OtherEditDistanceBelowThreshold) { + EXPECT_NO_CHANGES(ArgumentCommentCheck, + "void f(int xxx, int yyy); void g() { f(/*xxy=*/0, 0); }"); +} + +} // namespace test +} // namespace tidy +} // namespace clang |