11 #include "../utils/OptionsUtils.h"
12 #include "clang/AST/DeclBase.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/ASTMatchers/ASTMatchers.h"
16 using namespace clang::ast_matchers;
23 NonConstReferences::NonConstReferences(StringRef
Name,
40 unless(isInstantiated()),
42 qualType(unless(isConstQualified())).bind(
"referenced_type"))),
43 unless(hasType(rValueReferenceType())))
49 const auto *Parameter = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
50 const auto *Function =
51 dyn_cast_or_null<FunctionDecl>(Parameter->getParentFunctionOrMethod());
53 if (Function ==
nullptr || Function->isImplicit())
56 if (!Function->isCanonicalDecl())
59 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
61 if (Method->begin_overridden_methods() != Method->end_overridden_methods())
65 if (Method->getParent()->isLambda())
69 auto ReferencedType = *Result.Nodes.getNodeAs<QualType>(
"referenced_type");
71 if (std::find_if(WhiteListTypes.begin(), WhiteListTypes.end(),
72 [&](llvm::StringRef WhiteListType) {
73 return ReferencedType.getCanonicalType().getAsString(
74 Result.Context->getPrintingPolicy()) ==
76 }) != WhiteListTypes.end())
80 if (ReferencedType->isFunctionProtoType())
84 if (ReferencedType->isDependentType())
87 if (Function->isOverloadedOperator()) {
88 switch (Function->getOverloadedOperator()) {
89 case clang::OO_LessLess:
90 case clang::OO_PlusPlus:
91 case clang::OO_MinusMinus:
92 case clang::OO_PlusEqual:
93 case clang::OO_MinusEqual:
94 case clang::OO_StarEqual:
95 case clang::OO_SlashEqual:
96 case clang::OO_PercentEqual:
97 case clang::OO_LessLessEqual:
98 case clang::OO_GreaterGreaterEqual:
99 case clang::OO_PipeEqual:
100 case clang::OO_CaretEqual:
101 case clang::OO_AmpEqual:
104 if (Function->getParamDecl(0) == Parameter)
107 case clang::OO_GreaterGreater: {
108 auto isNonConstRef = [](clang::QualType T) {
109 return T->isReferenceType() &&
110 !T.getNonReferenceType().isConstQualified();
115 if (isNonConstRef(Function->getParamDecl(0)->getType()) &&
116 (Function->getNumParams() < 2 ||
117 isNonConstRef(Function->getParamDecl(1)->getType())) &&
118 isNonConstRef(Function->getReturnType()))
128 if (Function->getDeclName().isIdentifier() && Function->getName() ==
"swap")
132 if (StringRef(ReferencedType.getAsString()).endswith(
"stream"))
135 if (Parameter->getName().empty()) {
136 diag(Parameter->getLocation(),
"non-const reference parameter at index %0, "
137 "make it const or use a pointer")
138 << Parameter->getFunctionScopeIndex();
140 diag(Parameter->getLocation(),
141 "non-const reference parameter %0, make it const or use a pointer")
LangOptions getLangOpts() const
Returns the language options from the context.
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
std::unique_ptr< ast_matchers::MatchFinder > Finder
Base class for all clang-tidy checks.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
std::map< std::string, std::string > OptionMap
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
ClangTidyContext & Context
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.