//===----------------------------------------------------------------------===//
#include "NonConstReferences.h"
+#include "../utils/OptionsUtils.h"
#include "clang/AST/DeclBase.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
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()),
}
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;
/// 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
-// 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;
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 &);