[clang-tools-extra] r283777 - [clang-tidy] Add a whitelist option in google-runtime-references.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 10 09:38:11 PDT 2016
Author: hokein
Date: Mon Oct 10 11:38:11 2016
New Revision: 283777
URL: http://llvm.org/viewvc/llvm-project?rev=283777&view=rev
Log:
[clang-tidy] Add a whitelist option in google-runtime-references.
Reviewers: aaron.ballman
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D25244
Modified:
clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.cpp
clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.h
clang-tools-extra/trunk/docs/clang-tidy/checks/google-runtime-references.rst
clang-tools-extra/trunk/test/clang-tidy/google-runtime-references.cpp
Modified: clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.cpp?rev=283777&r1=283776&r2=283777&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.cpp Mon Oct 10 11:38:11 2016
@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "NonConstReferences.h"
+#include "../utils/OptionsUtils.h"
#include "clang/AST/DeclBase.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
@@ -19,7 +20,21 @@ namespace tidy {
namespace google {
namespace runtime {
+NonConstReferences::NonConstReferences(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ WhiteListTypes(
+ utils::options::parseStringList(Options.get("WhiteListTypes", ""))) {}
+
+void NonConstReferences::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "WhiteListTypes",
+ utils::options::serializeStringList(WhiteListTypes));
+}
+
void NonConstReferences::registerMatchers(MatchFinder *Finder) {
+ if (!getLangOpts().CPlusPlus)
+ return;
+
Finder->addMatcher(
parmVarDecl(
unless(isInstantiated()),
@@ -52,6 +67,15 @@ void NonConstReferences::check(const Mat
}
auto ReferencedType = *Result.Nodes.getNodeAs<QualType>("referenced_type");
+
+ if (std::find_if(WhiteListTypes.begin(), WhiteListTypes.end(),
+ [&](llvm::StringRef WhiteListType) {
+ return ReferencedType.getCanonicalType().getAsString(
+ Result.Context->getPrintingPolicy()) ==
+ WhiteListType;
+ }) != WhiteListTypes.end())
+ return;
+
// Don't warn on function references, they shouldn't be constant.
if (ReferencedType->isFunctionProtoType())
return;
Modified: clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.h?rev=283777&r1=283776&r2=283777&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.h (original)
+++ clang-tools-extra/trunk/clang-tidy/google/NonConstReferences.h Mon Oct 10 11:38:11 2016
@@ -22,10 +22,13 @@ namespace runtime {
/// https://google.github.io/styleguide/cppguide.html#Reference_Arguments
class NonConstReferences : public ClangTidyCheck {
public:
- NonConstReferences(StringRef Name, ClangTidyContext *Context)
- : ClangTidyCheck(Name, Context) {}
+ NonConstReferences(StringRef Name, ClangTidyContext *Context);
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+
+private:
+ const std::vector<std::string> WhiteListTypes;
};
} // namespace runtime
Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/google-runtime-references.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/google-runtime-references.rst?rev=283777&r1=283776&r2=283777&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/google-runtime-references.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/google-runtime-references.rst Mon Oct 10 11:38:11 2016
@@ -7,3 +7,11 @@ Checks the usage of non-constant referen
The corresponding style guide rule:
https://google.github.io/styleguide/cppguide.html#Reference_Arguments
+
+
+Options
+-------
+
+.. option:: WhiteListTypes
+
+ A semicolon-separated list of names of whitelist types. Defualt is empty.
Modified: clang-tools-extra/trunk/test/clang-tidy/google-runtime-references.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/google-runtime-references.cpp?rev=283777&r1=283776&r2=283777&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/google-runtime-references.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/google-runtime-references.cpp Mon Oct 10 11:38:11 2016
@@ -1,4 +1,8 @@
-// RUN: %check_clang_tidy %s google-runtime-references %t
+// RUN: %check_clang_tidy %s google-runtime-references %t -- \
+// RUN: -extra-arg="-std=c++11" \
+// RUN: -config="{CheckOptions: \
+// RUN: [{key: google-runtime-references.WhiteListTypes, \
+// RUN: value: 'whitelist::A; whitelist::B'}]}" --
int a;
int &b = a;
@@ -137,3 +141,12 @@ A& operator>>=(A& a, const A& b) { retur
A& operator|=(A& a, const A& b) { return a; }
A& operator^=(A& a, const A& b) { return a; }
A& operator&=(A& a, const A& b) { return a; }
+
+namespace whitelist {
+class A {};
+class B {};
+void f7(A &);
+void f8(B &);
+}
+void f9(whitelist::A &);
+void f10(whitelist::B &);
More information about the cfe-commits
mailing list