blob: 757a4ce2881702ecb1b5b127935beffb9536b391 [file] [log] [blame]
Ted Kremenek6dd66ed2010-02-14 02:45:18 +00001//=== LLVMConventionsChecker.cpp - Check LLVM codebase conventions ---*- C++ -*-
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This defines LLVMConventionsChecker, a bunch of small little checks
11// for checking specific coding conventions in the LLVM/Clang codebase.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +000018#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/StmtVisitor.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000020#include "llvm/ADT/SmallString.h"
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000021
22using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000024
25//===----------------------------------------------------------------------===//
Ted Kremenek55abf162010-02-14 19:08:36 +000026// Generic type checking routines.
27//===----------------------------------------------------------------------===//
28
Ted Kremeneka6b87932010-02-14 19:09:13 +000029static bool IsLLVMStringRef(QualType T) {
Ted Kremenek55abf162010-02-14 19:08:36 +000030 const RecordType *RT = T->getAs<RecordType>();
31 if (!RT)
32 return false;
33
Chris Lattner5f9e2722011-07-23 10:55:15 +000034 return StringRef(QualType(RT, 0).getAsString()) ==
35 "class StringRef";
Ted Kremeneka6b87932010-02-14 19:09:13 +000036}
37
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000038/// Check whether the declaration is semantically inside the top-level
39/// namespace named by ns.
Chris Lattner5f9e2722011-07-23 10:55:15 +000040static bool InNamespace(const Decl *D, StringRef NS) {
Ted Kremeneka6b87932010-02-14 19:09:13 +000041 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext());
42 if (!ND)
43 return false;
44 const IdentifierInfo *II = ND->getIdentifier();
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000045 if (!II || !II->getName().equals(NS))
Ted Kremeneka6b87932010-02-14 19:09:13 +000046 return false;
Ted Kremenekc39b5e82011-01-14 22:31:41 +000047 return isa<TranslationUnitDecl>(ND->getDeclContext());
Ted Kremenek55abf162010-02-14 19:08:36 +000048}
49
50static bool IsStdString(QualType T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +000051 if (const ElaboratedType *QT = T->getAs<ElaboratedType>())
Ted Kremenek55abf162010-02-14 19:08:36 +000052 T = QT->getNamedType();
53
54 const TypedefType *TT = T->getAs<TypedefType>();
55 if (!TT)
56 return false;
57
Richard Smith162e1c12011-04-15 14:24:37 +000058 const TypedefNameDecl *TD = TT->getDecl();
Ted Kremeneka6b87932010-02-14 19:09:13 +000059
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000060 if (!InNamespace(TD, "std"))
Ted Kremeneka6b87932010-02-14 19:09:13 +000061 return false;
62
63 return TD->getName() == "string";
64}
65
Ted Kremeneka6b87932010-02-14 19:09:13 +000066static bool IsClangType(const RecordDecl *RD) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000067 return RD->getName() == "Type" && InNamespace(RD, "clang");
Ted Kremeneka6b87932010-02-14 19:09:13 +000068}
69
70static bool IsClangDecl(const RecordDecl *RD) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000071 return RD->getName() == "Decl" && InNamespace(RD, "clang");
Ted Kremeneka6b87932010-02-14 19:09:13 +000072}
73
74static bool IsClangStmt(const RecordDecl *RD) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000075 return RD->getName() == "Stmt" && InNamespace(RD, "clang");
Ted Kremeneka6b87932010-02-14 19:09:13 +000076}
77
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000078static bool IsClangAttr(const RecordDecl *RD) {
79 return RD->getName() == "Attr" && InNamespace(RD, "clang");
Ted Kremenek1ed482b2010-02-14 22:58:16 +000080}
81
Ted Kremeneka6b87932010-02-14 19:09:13 +000082static bool IsStdVector(QualType T) {
83 const TemplateSpecializationType *TS = T->getAs<TemplateSpecializationType>();
84 if (!TS)
Ted Kremenek55abf162010-02-14 19:08:36 +000085 return false;
86
Ted Kremeneka6b87932010-02-14 19:09:13 +000087 TemplateName TM = TS->getTemplateName();
88 TemplateDecl *TD = TM.getAsTemplateDecl();
89
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000090 if (!TD || !InNamespace(TD, "std"))
Ted Kremeneka6b87932010-02-14 19:09:13 +000091 return false;
92
93 return TD->getName() == "vector";
94}
95
96static bool IsSmallVector(QualType T) {
97 const TemplateSpecializationType *TS = T->getAs<TemplateSpecializationType>();
98 if (!TS)
99 return false;
100
101 TemplateName TM = TS->getTemplateName();
102 TemplateDecl *TD = TM.getAsTemplateDecl();
103
Nick Lewycky31b5c4b2010-05-30 18:05:23 +0000104 if (!TD || !InNamespace(TD, "llvm"))
Ted Kremeneka6b87932010-02-14 19:09:13 +0000105 return false;
106
107 return TD->getName() == "SmallVector";
Ted Kremenek55abf162010-02-14 19:08:36 +0000108}
109
110//===----------------------------------------------------------------------===//
Chris Lattner5f9e2722011-07-23 10:55:15 +0000111// CHECK: a StringRef should not be bound to a temporary std::string whose
Ted Kremenek55abf162010-02-14 19:08:36 +0000112// lifetime is shorter than the StringRef's.
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000113//===----------------------------------------------------------------------===//
114
115namespace {
116class StringRefCheckerVisitor : public StmtVisitor<StringRefCheckerVisitor> {
117 BugReporter &BR;
Ted Kremenek07189522012-04-04 18:11:35 +0000118 const Decl *DeclWithIssue;
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000119public:
Ted Kremenek07189522012-04-04 18:11:35 +0000120 StringRefCheckerVisitor(const Decl *declWithIssue, BugReporter &br)
121 : BR(br), DeclWithIssue(declWithIssue) {}
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000122 void VisitChildren(Stmt *S) {
123 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
124 I != E; ++I)
125 if (Stmt *child = *I)
126 Visit(child);
127 }
128 void VisitStmt(Stmt *S) { VisitChildren(S); }
129 void VisitDeclStmt(DeclStmt *DS);
130private:
131 void VisitVarDecl(VarDecl *VD);
132};
133} // end anonymous namespace
134
135static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR) {
Ted Kremenek07189522012-04-04 18:11:35 +0000136 StringRefCheckerVisitor walker(D, BR);
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000137 walker.Visit(D->getBody());
138}
139
140void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) {
Ted Kremenek005f09b2010-02-14 19:08:43 +0000141 VisitChildren(S);
142
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000143 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();I!=E; ++I)
144 if (VarDecl *VD = dyn_cast<VarDecl>(*I))
145 VisitVarDecl(VD);
146}
147
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000148void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) {
149 Expr *Init = VD->getInit();
150 if (!Init)
Ted Kremenek676ca152010-02-14 19:09:05 +0000151 return;
Ted Kremenek55abf162010-02-14 19:08:36 +0000152
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000153 // Pattern match for:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000154 // StringRef x = call() (where call returns std::string)
Ted Kremeneka6b87932010-02-14 19:09:13 +0000155 if (!IsLLVMStringRef(VD->getType()))
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000156 return;
John McCall4765fa02010-12-06 08:20:24 +0000157 ExprWithCleanups *Ex1 = dyn_cast<ExprWithCleanups>(Init);
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000158 if (!Ex1)
159 return;
160 CXXConstructExpr *Ex2 = dyn_cast<CXXConstructExpr>(Ex1->getSubExpr());
161 if (!Ex2 || Ex2->getNumArgs() != 1)
162 return;
163 ImplicitCastExpr *Ex3 = dyn_cast<ImplicitCastExpr>(Ex2->getArg(0));
164 if (!Ex3)
165 return;
166 CXXConstructExpr *Ex4 = dyn_cast<CXXConstructExpr>(Ex3->getSubExpr());
167 if (!Ex4 || Ex4->getNumArgs() != 1)
168 return;
169 ImplicitCastExpr *Ex5 = dyn_cast<ImplicitCastExpr>(Ex4->getArg(0));
170 if (!Ex5)
171 return;
172 CXXBindTemporaryExpr *Ex6 = dyn_cast<CXXBindTemporaryExpr>(Ex5->getSubExpr());
173 if (!Ex6 || !IsStdString(Ex6->getType()))
174 return;
Ted Kremenek55abf162010-02-14 19:08:36 +0000175
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000176 // Okay, badness! Report an error.
Ted Kremeneka6b87932010-02-14 19:09:13 +0000177 const char *desc = "StringRef should not be bound to temporary "
178 "std::string that it outlives";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000179 PathDiagnosticLocation VDLoc =
180 PathDiagnosticLocation::createBegin(VD, BR.getSourceManager());
Ted Kremenek07189522012-04-04 18:11:35 +0000181 BR.EmitBasicReport(DeclWithIssue, desc, "LLVM Conventions", desc,
Anna Zaks590dd8e2011-09-20 21:38:35 +0000182 VDLoc, Init->getSourceRange());
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000183}
184
185//===----------------------------------------------------------------------===//
Ted Kremeneka6b87932010-02-14 19:09:13 +0000186// CHECK: Clang AST nodes should not have fields that can allocate
187// memory.
188//===----------------------------------------------------------------------===//
189
190static bool AllocatesMemory(QualType T) {
191 return IsStdVector(T) || IsStdString(T) || IsSmallVector(T);
192}
193
194// This type checking could be sped up via dynamic programming.
195static bool IsPartOfAST(const CXXRecordDecl *R) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +0000196 if (IsClangStmt(R) || IsClangType(R) || IsClangDecl(R) || IsClangAttr(R))
Ted Kremeneka6b87932010-02-14 19:09:13 +0000197 return true;
198
199 for (CXXRecordDecl::base_class_const_iterator I = R->bases_begin(),
200 E = R->bases_end(); I!=E; ++I) {
201 CXXBaseSpecifier BS = *I;
202 QualType T = BS.getType();
203 if (const RecordType *baseT = T->getAs<RecordType>()) {
204 CXXRecordDecl *baseD = cast<CXXRecordDecl>(baseT->getDecl());
205 if (IsPartOfAST(baseD))
206 return true;
207 }
208 }
209
210 return false;
211}
212
213namespace {
214class ASTFieldVisitor {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000215 SmallVector<FieldDecl*, 10> FieldChain;
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000216 const CXXRecordDecl *Root;
Ted Kremeneka6b87932010-02-14 19:09:13 +0000217 BugReporter &BR;
218public:
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000219 ASTFieldVisitor(const CXXRecordDecl *root, BugReporter &br)
Ted Kremeneka6b87932010-02-14 19:09:13 +0000220 : Root(root), BR(br) {}
221
222 void Visit(FieldDecl *D);
223 void ReportError(QualType T);
224};
225} // end anonymous namespace
226
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000227static void CheckASTMemory(const CXXRecordDecl *R, BugReporter &BR) {
Ted Kremeneka6b87932010-02-14 19:09:13 +0000228 if (!IsPartOfAST(R))
229 return;
230
231 for (RecordDecl::field_iterator I = R->field_begin(), E = R->field_end();
232 I != E; ++I) {
233 ASTFieldVisitor walker(R, BR);
234 walker.Visit(*I);
235 }
236}
237
238void ASTFieldVisitor::Visit(FieldDecl *D) {
239 FieldChain.push_back(D);
240
241 QualType T = D->getType();
242
243 if (AllocatesMemory(T))
244 ReportError(T);
245
246 if (const RecordType *RT = T->getAs<RecordType>()) {
247 const RecordDecl *RD = RT->getDecl()->getDefinition();
248 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
249 I != E; ++I)
250 Visit(*I);
251 }
252
253 FieldChain.pop_back();
254}
255
256void ASTFieldVisitor::ReportError(QualType T) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000257 SmallString<1024> buf;
Ted Kremeneka6b87932010-02-14 19:09:13 +0000258 llvm::raw_svector_ostream os(buf);
259
260 os << "AST class '" << Root->getName() << "' has a field '"
261 << FieldChain.front()->getName() << "' that allocates heap memory";
262 if (FieldChain.size() > 1) {
263 os << " via the following chain: ";
264 bool isFirst = true;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000265 for (SmallVectorImpl<FieldDecl*>::iterator I=FieldChain.begin(),
Ted Kremeneka6b87932010-02-14 19:09:13 +0000266 E=FieldChain.end(); I!=E; ++I) {
267 if (!isFirst)
268 os << '.';
269 else
270 isFirst = false;
271 os << (*I)->getName();
272 }
273 }
274 os << " (type " << FieldChain.back()->getType().getAsString() << ")";
275 os.flush();
276
277 // Note that this will fire for every translation unit that uses this
278 // class. This is suboptimal, but at least scan-build will merge
279 // duplicate HTML reports. In the future we need a unified way of merging
280 // duplicate reports across translation units. For C++ classes we cannot
281 // just report warnings when we see an out-of-line method definition for a
282 // class, as that heuristic doesn't always work (the complete definition of
283 // the class may be in the header file, for example).
Anna Zaks590dd8e2011-09-20 21:38:35 +0000284 PathDiagnosticLocation L = PathDiagnosticLocation::createBegin(
285 FieldChain.front(), BR.getSourceManager());
Ted Kremenek07189522012-04-04 18:11:35 +0000286 BR.EmitBasicReport(Root, "AST node allocates heap memory", "LLVM Conventions",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000287 os.str(), L);
Ted Kremeneka6b87932010-02-14 19:09:13 +0000288}
289
290//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000291// LLVMConventionsChecker
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000292//===----------------------------------------------------------------------===//
293
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000294namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000295class LLVMConventionsChecker : public Checker<
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000296 check::ASTDecl<CXXRecordDecl>,
297 check::ASTCodeBody > {
298public:
299 void checkASTDecl(const CXXRecordDecl *R, AnalysisManager& mgr,
300 BugReporter &BR) const {
John McCall5e1cdac2011-10-07 06:10:15 +0000301 if (R->isCompleteDefinition())
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000302 CheckASTMemory(R, BR);
Ted Kremenek676ca152010-02-14 19:09:05 +0000303 }
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000304
305 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
306 BugReporter &BR) const {
307 CheckStringRefAssignedTemporary(D, BR);
308 }
309};
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000310}
Ted Kremenek676ca152010-02-14 19:09:05 +0000311
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000312void ento::registerLLVMConventionsChecker(CheckerManager &mgr) {
313 mgr.registerChecker<LLVMConventionsChecker>();
Ted Kremenek676ca152010-02-14 19:09:05 +0000314}