10 #include "../utils/OptionsUtils.h" 11 #include "clang/AST/DeclBase.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 22 NonConstReferences::NonConstReferences(StringRef
Name,
39 unless(isInstantiated()),
41 qualType(unless(isConstQualified())).bind(
"referenced_type"))),
42 unless(hasType(rValueReferenceType())))
48 const auto *Parameter = Result.Nodes.getNodeAs<ParmVarDecl>(
"param");
49 const auto *Function =
50 dyn_cast_or_null<FunctionDecl>(Parameter->getParentFunctionOrMethod());
52 if (Function ==
nullptr || Function->isImplicit())
55 if (Function->getLocation().isMacroID())
58 if (!Function->isCanonicalDecl())
61 if (
const auto *Method = dyn_cast<CXXMethodDecl>(Function)) {
63 if (Method->begin_overridden_methods() != Method->end_overridden_methods())
67 if (Method->getParent()->isLambda())
71 auto ReferencedType = *Result.Nodes.getNodeAs<QualType>(
"referenced_type");
73 if (std::find_if(WhiteListTypes.begin(), WhiteListTypes.end(),
74 [&](llvm::StringRef WhiteListType) {
75 return ReferencedType.getCanonicalType().getAsString(
76 Result.Context->getPrintingPolicy()) ==
78 }) != WhiteListTypes.end())
82 if (ReferencedType->isFunctionProtoType())
86 if (ReferencedType->isDependentType())
89 if (Function->isOverloadedOperator()) {
90 switch (Function->getOverloadedOperator()) {
91 case clang::OO_LessLess:
92 case clang::OO_PlusPlus:
93 case clang::OO_MinusMinus:
94 case clang::OO_PlusEqual:
95 case clang::OO_MinusEqual:
96 case clang::OO_StarEqual:
97 case clang::OO_SlashEqual:
98 case clang::OO_PercentEqual:
99 case clang::OO_LessLessEqual:
100 case clang::OO_GreaterGreaterEqual:
101 case clang::OO_PipeEqual:
102 case clang::OO_CaretEqual:
103 case clang::OO_AmpEqual:
106 if (Function->getParamDecl(0) == Parameter)
109 case clang::OO_GreaterGreater: {
110 auto isNonConstRef = [](clang::QualType T) {
111 return T->isReferenceType() &&
112 !T.getNonReferenceType().isConstQualified();
117 if (isNonConstRef(Function->getParamDecl(0)->getType()) &&
118 (Function->getNumParams() < 2 ||
119 isNonConstRef(Function->getParamDecl(1)->getType())) &&
120 isNonConstRef(Function->getReturnType()))
130 if (Function->getDeclName().isIdentifier() && Function->getName() ==
"swap")
134 if (StringRef(ReferencedType.getAsString()).endswith(
"stream"))
137 if (Parameter->getName().empty()) {
138 diag(Parameter->getLocation(),
"non-const reference parameter at index %0, " 139 "make it const or use a pointer")
140 << Parameter->getFunctionScopeIndex();
142 diag(Parameter->getLocation(),
143 "non-const reference parameter %0, make it const or use a pointer")
std::string serializeStringList(ArrayRef< std::string > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
std::vector< std::string > parseStringList(StringRef Option)
Parse a semicolon separated list of strings.
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.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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.