12 #include "../utils/LexerUtils.h"
13 #include "../utils/Matchers.h"
17 namespace performance {
19 using namespace ::clang::ast_matchers;
22 AST_MATCHER(QualType, isPointerType) {
return Node->isPointerType(); }
26 ast_matchers::MatchFinder *
Finder) {
27 auto ConstReference = referenceType(pointee(qualType(isConstQualified())));
28 auto ConstOrConstReference =
29 allOf(anyOf(ConstReference, isConstQualified()),
30 unless(allOf(isPointerType(), unless(pointerType(pointee(qualType(
31 isConstQualified())))))));
37 auto ConstRefReturningMethodCallOfConstParam = cxxMemberCallExpr(
38 callee(cxxMethodDecl(returns(ConstReference))),
39 on(declRefExpr(to(varDecl(hasType(qualType(ConstOrConstReference)))))));
40 auto ConstRefReturningFunctionCall =
41 callExpr(callee(functionDecl(returns(ConstReference))),
42 unless(callee(cxxMethodDecl())));
45 hasLocalStorage(), hasType(isConstQualified()),
47 hasInitializer(cxxConstructExpr(
48 hasDeclaration(cxxConstructorDecl(isCopyConstructor())),
49 hasArgument(0, anyOf(ConstRefReturningFunctionCall,
50 ConstRefReturningMethodCallOfConstParam)))))
56 const ast_matchers::MatchFinder::MatchResult &
Result) {
57 const auto *Var = Result.Nodes.getNodeAs<VarDecl>(
"varDecl");
58 SourceLocation AmpLocation = Var->getLocation();
61 if (!Token.is(tok::unknown)) {
62 AmpLocation = Token.getLocation().getLocWithOffset(Token.getLength());
64 diag(Var->getLocation(),
65 "the const qualified variable '%0' is copy-constructed from a "
66 "const reference; consider making it a const reference")
67 << Var->getName() << FixItHint::CreateInsertion(AmpLocation,
"&");
std::unique_ptr< ast_matchers::MatchFinder > Finder
Token getPreviousNonCommentToken(const ASTContext &Context, SourceLocation Location)
AST_MATCHER(Stmt, isInsideOfRangeBeginEndStmt)
llvm::Optional< bool > isExpensiveToCopy(QualType Type, ASTContext &Context)
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.