blob: 4113a019659370e9cba54606cda9bad2e294bd7c [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"
Benjamin Kramera93d0f22012-12-01 17:12:56 +000021#include "llvm/Support/raw_ostream.h"
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000022
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000025
26//===----------------------------------------------------------------------===//
Ted Kremenek55abf162010-02-14 19:08:36 +000027// Generic type checking routines.
28//===----------------------------------------------------------------------===//
29
Ted Kremeneka6b87932010-02-14 19:09:13 +000030static bool IsLLVMStringRef(QualType T) {
Ted Kremenek55abf162010-02-14 19:08:36 +000031 const RecordType *RT = T->getAs<RecordType>();
32 if (!RT)
33 return false;
34
Chris Lattner5f9e2722011-07-23 10:55:15 +000035 return StringRef(QualType(RT, 0).getAsString()) ==
36 "class StringRef";
Ted Kremeneka6b87932010-02-14 19:09:13 +000037}
38
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000039/// Check whether the declaration is semantically inside the top-level
40/// namespace named by ns.
Chris Lattner5f9e2722011-07-23 10:55:15 +000041static bool InNamespace(const Decl *D, StringRef NS) {
Ted Kremeneka6b87932010-02-14 19:09:13 +000042 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext());
43 if (!ND)
44 return false;
45 const IdentifierInfo *II = ND->getIdentifier();
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000046 if (!II || !II->getName().equals(NS))
Ted Kremeneka6b87932010-02-14 19:09:13 +000047 return false;
Ted Kremenekc39b5e82011-01-14 22:31:41 +000048 return isa<TranslationUnitDecl>(ND->getDeclContext());
Ted Kremenek55abf162010-02-14 19:08:36 +000049}
50
51static bool IsStdString(QualType T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +000052 if (const ElaboratedType *QT = T->getAs<ElaboratedType>())
Ted Kremenek55abf162010-02-14 19:08:36 +000053 T = QT->getNamedType();
54
55 const TypedefType *TT = T->getAs<TypedefType>();
56 if (!TT)
57 return false;
58
Richard Smith162e1c12011-04-15 14:24:37 +000059 const TypedefNameDecl *TD = TT->getDecl();
Ted Kremeneka6b87932010-02-14 19:09:13 +000060
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000061 if (!InNamespace(TD, "std"))
Ted Kremeneka6b87932010-02-14 19:09:13 +000062 return false;
63
64 return TD->getName() == "string";
65}
66
Ted Kremeneka6b87932010-02-14 19:09:13 +000067static bool IsClangType(const RecordDecl *RD) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000068 return RD->getName() == "Type" && InNamespace(RD, "clang");
Ted Kremeneka6b87932010-02-14 19:09:13 +000069}
70
71static bool IsClangDecl(const RecordDecl *RD) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000072 return RD->getName() == "Decl" && InNamespace(RD, "clang");
Ted Kremeneka6b87932010-02-14 19:09:13 +000073}
74
75static bool IsClangStmt(const RecordDecl *RD) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000076 return RD->getName() == "Stmt" && InNamespace(RD, "clang");
Ted Kremeneka6b87932010-02-14 19:09:13 +000077}
78
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000079static bool IsClangAttr(const RecordDecl *RD) {
80 return RD->getName() == "Attr" && InNamespace(RD, "clang");
Ted Kremenek1ed482b2010-02-14 22:58:16 +000081}
82
Ted Kremeneka6b87932010-02-14 19:09:13 +000083static bool IsStdVector(QualType T) {
84 const TemplateSpecializationType *TS = T->getAs<TemplateSpecializationType>();
85 if (!TS)
Ted Kremenek55abf162010-02-14 19:08:36 +000086 return false;
87
Ted Kremeneka6b87932010-02-14 19:09:13 +000088 TemplateName TM = TS->getTemplateName();
89 TemplateDecl *TD = TM.getAsTemplateDecl();
90
Nick Lewycky31b5c4b2010-05-30 18:05:23 +000091 if (!TD || !InNamespace(TD, "std"))
Ted Kremeneka6b87932010-02-14 19:09:13 +000092 return false;
93
94 return TD->getName() == "vector";
95}
96
97static bool IsSmallVector(QualType T) {
98 const TemplateSpecializationType *TS = T->getAs<TemplateSpecializationType>();
99 if (!TS)
100 return false;
101
102 TemplateName TM = TS->getTemplateName();
103 TemplateDecl *TD = TM.getAsTemplateDecl();
104
Nick Lewycky31b5c4b2010-05-30 18:05:23 +0000105 if (!TD || !InNamespace(TD, "llvm"))
Ted Kremeneka6b87932010-02-14 19:09:13 +0000106 return false;
107
108 return TD->getName() == "SmallVector";
Ted Kremenek55abf162010-02-14 19:08:36 +0000109}
110
111//===----------------------------------------------------------------------===//
Chris Lattner5f9e2722011-07-23 10:55:15 +0000112// CHECK: a StringRef should not be bound to a temporary std::string whose
Ted Kremenek55abf162010-02-14 19:08:36 +0000113// lifetime is shorter than the StringRef's.
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000114//===----------------------------------------------------------------------===//
115
116namespace {
117class StringRefCheckerVisitor : public StmtVisitor<StringRefCheckerVisitor> {
118 BugReporter &BR;
Ted Kremenek07189522012-04-04 18:11:35 +0000119 const Decl *DeclWithIssue;
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000120public:
Ted Kremenek07189522012-04-04 18:11:35 +0000121 StringRefCheckerVisitor(const Decl *declWithIssue, BugReporter &br)
122 : BR(br), DeclWithIssue(declWithIssue) {}
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000123 void VisitChildren(Stmt *S) {
124 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
125 I != E; ++I)
126 if (Stmt *child = *I)
127 Visit(child);
128 }
129 void VisitStmt(Stmt *S) { VisitChildren(S); }
130 void VisitDeclStmt(DeclStmt *DS);
131private:
132 void VisitVarDecl(VarDecl *VD);
133};
134} // end anonymous namespace
135
136static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR) {
Ted Kremenek07189522012-04-04 18:11:35 +0000137 StringRefCheckerVisitor walker(D, BR);
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000138 walker.Visit(D->getBody());
139}
140
141void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) {
Ted Kremenek005f09b2010-02-14 19:08:43 +0000142 VisitChildren(S);
143
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000144 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();I!=E; ++I)
145 if (VarDecl *VD = dyn_cast<VarDecl>(*I))
146 VisitVarDecl(VD);
147}
148
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000149void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) {
150 Expr *Init = VD->getInit();
151 if (!Init)
Ted Kremenek676ca152010-02-14 19:09:05 +0000152 return;
Ted Kremenek55abf162010-02-14 19:08:36 +0000153
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000154 // Pattern match for:
Chris Lattner5f9e2722011-07-23 10:55:15 +0000155 // StringRef x = call() (where call returns std::string)
Ted Kremeneka6b87932010-02-14 19:09:13 +0000156 if (!IsLLVMStringRef(VD->getType()))
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000157 return;
John McCall4765fa02010-12-06 08:20:24 +0000158 ExprWithCleanups *Ex1 = dyn_cast<ExprWithCleanups>(Init);
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000159 if (!Ex1)
160 return;
161 CXXConstructExpr *Ex2 = dyn_cast<CXXConstructExpr>(Ex1->getSubExpr());
162 if (!Ex2 || Ex2->getNumArgs() != 1)
163 return;
164 ImplicitCastExpr *Ex3 = dyn_cast<ImplicitCastExpr>(Ex2->getArg(0));
165 if (!Ex3)
166 return;
167 CXXConstructExpr *Ex4 = dyn_cast<CXXConstructExpr>(Ex3->getSubExpr());
168 if (!Ex4 || Ex4->getNumArgs() != 1)
169 return;
170 ImplicitCastExpr *Ex5 = dyn_cast<ImplicitCastExpr>(Ex4->getArg(0));
171 if (!Ex5)
172 return;
173 CXXBindTemporaryExpr *Ex6 = dyn_cast<CXXBindTemporaryExpr>(Ex5->getSubExpr());
174 if (!Ex6 || !IsStdString(Ex6->getType()))
175 return;
Ted Kremenek55abf162010-02-14 19:08:36 +0000176
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000177 // Okay, badness! Report an error.
Ted Kremeneka6b87932010-02-14 19:09:13 +0000178 const char *desc = "StringRef should not be bound to temporary "
179 "std::string that it outlives";
Anna Zaks590dd8e2011-09-20 21:38:35 +0000180 PathDiagnosticLocation VDLoc =
181 PathDiagnosticLocation::createBegin(VD, BR.getSourceManager());
Ted Kremenek07189522012-04-04 18:11:35 +0000182 BR.EmitBasicReport(DeclWithIssue, desc, "LLVM Conventions", desc,
Anna Zaks590dd8e2011-09-20 21:38:35 +0000183 VDLoc, Init->getSourceRange());
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000184}
185
186//===----------------------------------------------------------------------===//
Ted Kremeneka6b87932010-02-14 19:09:13 +0000187// CHECK: Clang AST nodes should not have fields that can allocate
188// memory.
189//===----------------------------------------------------------------------===//
190
191static bool AllocatesMemory(QualType T) {
192 return IsStdVector(T) || IsStdString(T) || IsSmallVector(T);
193}
194
195// This type checking could be sped up via dynamic programming.
196static bool IsPartOfAST(const CXXRecordDecl *R) {
Nick Lewycky31b5c4b2010-05-30 18:05:23 +0000197 if (IsClangStmt(R) || IsClangType(R) || IsClangDecl(R) || IsClangAttr(R))
Ted Kremeneka6b87932010-02-14 19:09:13 +0000198 return true;
199
200 for (CXXRecordDecl::base_class_const_iterator I = R->bases_begin(),
201 E = R->bases_end(); I!=E; ++I) {
202 CXXBaseSpecifier BS = *I;
203 QualType T = BS.getType();
204 if (const RecordType *baseT = T->getAs<RecordType>()) {
205 CXXRecordDecl *baseD = cast<CXXRecordDecl>(baseT->getDecl());
206 if (IsPartOfAST(baseD))
207 return true;
208 }
209 }
210
211 return false;
212}
213
214namespace {
215class ASTFieldVisitor {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000216 SmallVector<FieldDecl*, 10> FieldChain;
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000217 const CXXRecordDecl *Root;
Ted Kremeneka6b87932010-02-14 19:09:13 +0000218 BugReporter &BR;
219public:
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000220 ASTFieldVisitor(const CXXRecordDecl *root, BugReporter &br)
Ted Kremeneka6b87932010-02-14 19:09:13 +0000221 : Root(root), BR(br) {}
222
223 void Visit(FieldDecl *D);
224 void ReportError(QualType T);
225};
226} // end anonymous namespace
227
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000228static void CheckASTMemory(const CXXRecordDecl *R, BugReporter &BR) {
Ted Kremeneka6b87932010-02-14 19:09:13 +0000229 if (!IsPartOfAST(R))
230 return;
231
232 for (RecordDecl::field_iterator I = R->field_begin(), E = R->field_end();
233 I != E; ++I) {
234 ASTFieldVisitor walker(R, BR);
David Blaikie581deb32012-06-06 20:45:41 +0000235 walker.Visit(*I);
Ted Kremeneka6b87932010-02-14 19:09:13 +0000236 }
237}
238
239void ASTFieldVisitor::Visit(FieldDecl *D) {
240 FieldChain.push_back(D);
241
242 QualType T = D->getType();
243
244 if (AllocatesMemory(T))
245 ReportError(T);
246
247 if (const RecordType *RT = T->getAs<RecordType>()) {
248 const RecordDecl *RD = RT->getDecl()->getDefinition();
249 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
250 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +0000251 Visit(*I);
Ted Kremeneka6b87932010-02-14 19:09:13 +0000252 }
253
254 FieldChain.pop_back();
255}
256
257void ASTFieldVisitor::ReportError(QualType T) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000258 SmallString<1024> buf;
Ted Kremeneka6b87932010-02-14 19:09:13 +0000259 llvm::raw_svector_ostream os(buf);
260
261 os << "AST class '" << Root->getName() << "' has a field '"
262 << FieldChain.front()->getName() << "' that allocates heap memory";
263 if (FieldChain.size() > 1) {
264 os << " via the following chain: ";
265 bool isFirst = true;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000266 for (SmallVectorImpl<FieldDecl*>::iterator I=FieldChain.begin(),
Ted Kremeneka6b87932010-02-14 19:09:13 +0000267 E=FieldChain.end(); I!=E; ++I) {
268 if (!isFirst)
269 os << '.';
270 else
271 isFirst = false;
272 os << (*I)->getName();
273 }
274 }
275 os << " (type " << FieldChain.back()->getType().getAsString() << ")";
276 os.flush();
277
278 // Note that this will fire for every translation unit that uses this
279 // class. This is suboptimal, but at least scan-build will merge
280 // duplicate HTML reports. In the future we need a unified way of merging
281 // duplicate reports across translation units. For C++ classes we cannot
282 // just report warnings when we see an out-of-line method definition for a
283 // class, as that heuristic doesn't always work (the complete definition of
284 // the class may be in the header file, for example).
Anna Zaks590dd8e2011-09-20 21:38:35 +0000285 PathDiagnosticLocation L = PathDiagnosticLocation::createBegin(
286 FieldChain.front(), BR.getSourceManager());
Ted Kremenek07189522012-04-04 18:11:35 +0000287 BR.EmitBasicReport(Root, "AST node allocates heap memory", "LLVM Conventions",
Anna Zaks590dd8e2011-09-20 21:38:35 +0000288 os.str(), L);
Ted Kremeneka6b87932010-02-14 19:09:13 +0000289}
290
291//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000292// LLVMConventionsChecker
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000293//===----------------------------------------------------------------------===//
294
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000295namespace {
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +0000296class LLVMConventionsChecker : public Checker<
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000297 check::ASTDecl<CXXRecordDecl>,
298 check::ASTCodeBody > {
299public:
300 void checkASTDecl(const CXXRecordDecl *R, AnalysisManager& mgr,
301 BugReporter &BR) const {
John McCall5e1cdac2011-10-07 06:10:15 +0000302 if (R->isCompleteDefinition())
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000303 CheckASTMemory(R, BR);
Ted Kremenek676ca152010-02-14 19:09:05 +0000304 }
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000305
306 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
307 BugReporter &BR) const {
308 CheckStringRefAssignedTemporary(D, BR);
309 }
310};
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000311}
Ted Kremenek676ca152010-02-14 19:09:05 +0000312
Argyrios Kyrtzidis9fb94742011-02-17 21:39:24 +0000313void ento::registerLLVMConventionsChecker(CheckerManager &mgr) {
314 mgr.registerChecker<LLVMConventionsChecker>();
Ted Kremenek676ca152010-02-14 19:09:05 +0000315}