clang-tools 20.0.0git
SmartPtrArrayMismatchCheck.cpp
Go to the documentation of this file.
1//===--- SmartPtrArrayMismatchCheck.cpp - clang-tidy ----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "../utils/ASTUtils.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::bugprone {
17
18namespace {
19
20constexpr char ConstructExprN[] = "found_construct_expr";
21constexpr char NewExprN[] = "found_new_expr";
22constexpr char ConstructorN[] = "found_constructor";
23
24bool isInSingleDeclStmt(const DeclaratorDecl *D) {
25 const DynTypedNodeList Parents =
26 D->getASTContext().getParentMapContext().getParents(*D);
27 for (const DynTypedNode &PNode : Parents)
28 if (const auto *PDecl = PNode.get<DeclStmt>())
29 return PDecl->isSingleDecl();
30 return false;
31}
32
33const DeclaratorDecl *getConstructedVarOrField(const Expr *FoundConstructExpr,
34 ASTContext &Ctx) {
35 const DynTypedNodeList ConstructParents =
36 Ctx.getParentMapContext().getParents(*FoundConstructExpr);
37 if (ConstructParents.size() != 1)
38 return nullptr;
39 const auto *ParentDecl = ConstructParents.begin()->get<DeclaratorDecl>();
40 if (isa_and_nonnull<VarDecl, FieldDecl>(ParentDecl))
41 return ParentDecl;
42
43 return nullptr;
44}
45
46} // namespace
47
48const char SmartPtrArrayMismatchCheck::PointerTypeN[] = "pointer_type";
49
51 StringRef Name, ClangTidyContext *Context, StringRef SmartPointerName)
52 : ClangTidyCheck(Name, Context), SmartPointerName(SmartPointerName) {}
53
56
58 // For both shared and unique pointers, we need to find constructor with
59 // exactly one parameter that has the pointer type. Other constructors are
60 // not applicable for this check.
61 auto FindConstructor =
62 cxxConstructorDecl(ofClass(getSmartPointerClassMatcher()),
63 parameterCountIs(1), isExplicit())
64 .bind(ConstructorN);
65 auto FindConstructExpr =
66 cxxConstructExpr(
67 hasDeclaration(FindConstructor), argumentCountIs(1),
68 hasArgument(0,
69 cxxNewExpr(isArray(),
70 hasType(hasCanonicalType(pointerType(
71 pointee(equalsBoundNode(PointerTypeN))))))
72 .bind(NewExprN)))
73 .bind(ConstructExprN);
74 Finder->addMatcher(FindConstructExpr, this);
75}
76
77void SmartPtrArrayMismatchCheck::check(const MatchFinder::MatchResult &Result) {
78 const auto *FoundNewExpr = Result.Nodes.getNodeAs<CXXNewExpr>(NewExprN);
79 const auto *FoundConstructExpr =
80 Result.Nodes.getNodeAs<CXXConstructExpr>(ConstructExprN);
81 const auto *FoundConstructorDecl =
82 Result.Nodes.getNodeAs<CXXConstructorDecl>(ConstructorN);
83
84 ASTContext &Ctx = FoundConstructorDecl->getASTContext();
85 const DeclaratorDecl *VarOrField =
86 getConstructedVarOrField(FoundConstructExpr, Ctx);
87
88 auto D = diag(FoundNewExpr->getBeginLoc(),
89 "%0 pointer to non-array is initialized with array")
90 << SmartPointerName;
91 D << FoundNewExpr->getSourceRange();
92
93 if (VarOrField) {
94 auto TSTypeLoc = VarOrField->getTypeSourceInfo()
95 ->getTypeLoc()
96 .getAsAdjusted<clang::TemplateSpecializationTypeLoc>();
97 assert(TSTypeLoc.getNumArgs() >= 1 &&
98 "Matched type should have at least 1 template argument.");
99
100 SourceRange TemplateArgumentRange = TSTypeLoc.getArgLoc(0)
101 .getTypeSourceInfo()
102 ->getTypeLoc()
103 .getSourceRange();
104 D << TemplateArgumentRange;
105
106 if (isInSingleDeclStmt(VarOrField)) {
107 const SourceManager &SM = Ctx.getSourceManager();
108 if (!utils::rangeCanBeFixed(TemplateArgumentRange, &SM))
109 return;
110
111 SourceLocation InsertLoc = Lexer::getLocForEndOfToken(
112 TemplateArgumentRange.getEnd(), 0, SM, Ctx.getLangOpts());
113 D << FixItHint::CreateInsertion(InsertLoc, "[]");
114 }
115 }
116}
117
118} // namespace clang::tidy::bugprone
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
virtual SmartPtrClassMatcher getSmartPointerClassMatcher() const =0
Returns matcher that match with different smart pointer classes.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
SmartPtrArrayMismatchCheck(StringRef Name, ClangTidyContext *Context, StringRef SmartPointerName)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool rangeCanBeFixed(SourceRange Range, const SourceManager *SM)
Definition: ASTUtils.cpp:86
llvm::StringMap< ClangTidyValue > OptionMap