19#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/raw_ostream.h"
51 T = QT->getNamedType();
62 return TD->
getName() ==
"string";
92 return TD->
getName() ==
"vector";
106 return TD->
getName() ==
"SmallVector";
115class StringRefCheckerVisitor :
public StmtVisitor<StringRefCheckerVisitor> {
116 const Decl *DeclWithIssue;
123 : DeclWithIssue(declWithIssue), BR(br),
Checker(checker) {}
124 void VisitChildren(
Stmt *S) {
125 for (
Stmt *Child : S->children())
129 void VisitStmt(
Stmt *S) { VisitChildren(S); }
132 void VisitVarDecl(
VarDecl *VD);
138 StringRefCheckerVisitor walker(
D, BR,
Checker);
142void StringRefCheckerVisitor::VisitDeclStmt(
DeclStmt *S) {
145 for (
auto *I : S->decls())
146 if (
VarDecl *VD = dyn_cast<VarDecl>(I))
150void StringRefCheckerVisitor::VisitVarDecl(
VarDecl *VD) {
179 const char *desc =
"StringRef should not be bound to temporary "
180 "std::string that it outlives";
184 VDLoc,
Init->getSourceRange());
201 for (
const auto &BS : R->
bases()) {
204 CXXRecordDecl *baseD = cast<CXXRecordDecl>(baseT->getDecl());
214class ASTFieldVisitor {
223 : Root(root), BR(br),
Checker(checker) {}
235 for (
auto *I : R->
fields()) {
236 ASTFieldVisitor walker(R, BR,
Checker);
242 FieldChain.push_back(
D);
251 for (
auto *I : RD->
fields())
255 FieldChain.pop_back();
258void ASTFieldVisitor::ReportError(
QualType T) {
260 llvm::raw_svector_ostream os(buf);
262 os <<
"AST class '" << Root->getName() <<
"' has a field '"
263 << FieldChain.front()->getName() <<
"' that allocates heap memory";
264 if (FieldChain.size() > 1) {
265 os <<
" via the following chain: ";
268 E=FieldChain.end(); I!=
E; ++I) {
273 os << (*I)->getName();
276 os <<
" (type " << FieldChain.back()->getType() <<
")";
288 "LLVM Conventions", os.str(), L);
296class LLVMConventionsChecker :
public Checker<
297 check::ASTDecl<CXXRecordDecl>,
298 check::ASTCodeBody > {
317bool ento::shouldRegisterLLVMConventionsChecker(
const CheckerManager &mgr) {
Defines the C++ template declaration subclasses.
static bool IsStdVector(QualType T)
static bool IsClangType(const RecordDecl *RD)
static bool AllocatesMemory(QualType T)
static bool IsClangStmt(const RecordDecl *RD)
static bool InNamespace(const Decl *D, StringRef NS)
Check whether the declaration is semantically inside the top-level namespace named by ns.
static bool IsClangAttr(const RecordDecl *RD)
static bool IsStdString(QualType T)
static bool IsClangDecl(const RecordDecl *RD)
static void CheckASTMemory(const CXXRecordDecl *R, BugReporter &BR, const CheckerBase *Checker)
static bool IsPartOfAST(const CXXRecordDecl *R)
static bool IsSmallVector(QualType T)
static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR, const CheckerBase *Checker)
static bool IsLLVMStringRef(QualType T)
Represents binding an expression to a temporary.
Represents a call to a C++ constructor.
Expr * getArg(unsigned Arg)
Return the specified argument.
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
Represents a C++ struct/union/class.
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Decl - This represents one declaration (or definition), e.g.
bool isInStdNamespace() const
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
DeclContext * getDeclContext()
Represents a type that was referred to using an elaborated type keyword, e.g., struct S,...
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
This represents one expression.
Represents a member of a struct/union/class.
const Expr * getSubExpr() const
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Represent a C++ namespace.
A (possibly-)qualified type.
Represents a struct/union/class.
field_range fields() const
RecordDecl * getDefinition() const
Returns the RecordDecl that actually defines this struct/union/class.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
RetTy Visit(PTR(Stmt) S, ParamTys... P)
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Stmt - This represents one statement.
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Represents a C++ template name within the type system.
TemplateDecl * getAsTemplateDecl() const
Retrieve the underlying template declaration that this template name refers to, if known.
Represents a type template specialization; the template must be a class template, a type alias templa...
TemplateName getTemplateName() const
Retrieve the name of the template that we are specializing.
const T * getAs() const
Member-template getAs<specific type>'.
Base class for declarations which introduce a typedef-name.
TypedefNameDecl * getDecl() const
Represents a variable declaration or definition.
const Expr * getInit() const
BugReporter is a utility class for generating PathDiagnostics for analysis.
const SourceManager & getSourceManager()
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerBase *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges=std::nullopt, ArrayRef< FixItHint > Fixits=std::nullopt)
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
static PathDiagnosticLocation createBegin(const Decl *D, const SourceManager &SM)
Create a location for the beginning of the declaration.
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
llvm::StringRef getAsString(SyncScope S)