blob: db4fbca36deb8505d54be6a8c82f692e6b65ce7d [file] [log] [blame]
Ted Kremenek184b3382010-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 Kyrtzidis24ffc082011-02-17 21:39:24 +000015#include "ClangSACheckers.h"
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +000016#include "clang/AST/DeclTemplate.h"
17#include "clang/AST/StmtVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
19#include "clang/StaticAnalyzer/Core/Checker.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000020#include "llvm/ADT/SmallString.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000021#include "llvm/Support/raw_ostream.h"
Ted Kremenek184b3382010-02-14 02:45:18 +000022
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek184b3382010-02-14 02:45:18 +000025
26//===----------------------------------------------------------------------===//
Ted Kremenek4806a832010-02-14 19:08:36 +000027// Generic type checking routines.
28//===----------------------------------------------------------------------===//
29
Ted Kremenek48531312010-02-14 19:09:13 +000030static bool IsLLVMStringRef(QualType T) {
Ted Kremenek4806a832010-02-14 19:08:36 +000031 const RecordType *RT = T->getAs<RecordType>();
32 if (!RT)
33 return false;
34
Chris Lattner0e62c1c2011-07-23 10:55:15 +000035 return StringRef(QualType(RT, 0).getAsString()) ==
36 "class StringRef";
Ted Kremenek48531312010-02-14 19:09:13 +000037}
38
Nick Lewycky6cbc3f72010-05-30 18:05:23 +000039/// Check whether the declaration is semantically inside the top-level
40/// namespace named by ns.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000041static bool InNamespace(const Decl *D, StringRef NS) {
Ted Kremenek48531312010-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 Lewycky6cbc3f72010-05-30 18:05:23 +000046 if (!II || !II->getName().equals(NS))
Ted Kremenek48531312010-02-14 19:09:13 +000047 return false;
Ted Kremenek8cb349d2011-01-14 22:31:41 +000048 return isa<TranslationUnitDecl>(ND->getDeclContext());
Ted Kremenek4806a832010-02-14 19:08:36 +000049}
50
51static bool IsStdString(QualType T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +000052 if (const ElaboratedType *QT = T->getAs<ElaboratedType>())
Ted Kremenek4806a832010-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 Smithdda56e42011-04-15 14:24:37 +000059 const TypedefNameDecl *TD = TT->getDecl();
Ted Kremenek48531312010-02-14 19:09:13 +000060
Richard Trieuc771d5d2014-05-28 02:16:01 +000061 if (!TD->isInStdNamespace())
Ted Kremenek48531312010-02-14 19:09:13 +000062 return false;
63
64 return TD->getName() == "string";
65}
66
Ted Kremenek48531312010-02-14 19:09:13 +000067static bool IsClangType(const RecordDecl *RD) {
Nick Lewycky6cbc3f72010-05-30 18:05:23 +000068 return RD->getName() == "Type" && InNamespace(RD, "clang");
Ted Kremenek48531312010-02-14 19:09:13 +000069}
70
71static bool IsClangDecl(const RecordDecl *RD) {
Nick Lewycky6cbc3f72010-05-30 18:05:23 +000072 return RD->getName() == "Decl" && InNamespace(RD, "clang");
Ted Kremenek48531312010-02-14 19:09:13 +000073}
74
75static bool IsClangStmt(const RecordDecl *RD) {
Nick Lewycky6cbc3f72010-05-30 18:05:23 +000076 return RD->getName() == "Stmt" && InNamespace(RD, "clang");
Ted Kremenek48531312010-02-14 19:09:13 +000077}
78
Nick Lewycky6cbc3f72010-05-30 18:05:23 +000079static bool IsClangAttr(const RecordDecl *RD) {
80 return RD->getName() == "Attr" && InNamespace(RD, "clang");
Ted Kremenek4ba99be2010-02-14 22:58:16 +000081}
82
Ted Kremenek48531312010-02-14 19:09:13 +000083static bool IsStdVector(QualType T) {
84 const TemplateSpecializationType *TS = T->getAs<TemplateSpecializationType>();
85 if (!TS)
Ted Kremenek4806a832010-02-14 19:08:36 +000086 return false;
87
Ted Kremenek48531312010-02-14 19:09:13 +000088 TemplateName TM = TS->getTemplateName();
89 TemplateDecl *TD = TM.getAsTemplateDecl();
90
Nick Lewycky6cbc3f72010-05-30 18:05:23 +000091 if (!TD || !InNamespace(TD, "std"))
Ted Kremenek48531312010-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 Lewycky6cbc3f72010-05-30 18:05:23 +0000105 if (!TD || !InNamespace(TD, "llvm"))
Ted Kremenek48531312010-02-14 19:09:13 +0000106 return false;
107
108 return TD->getName() == "SmallVector";
Ted Kremenek4806a832010-02-14 19:08:36 +0000109}
110
111//===----------------------------------------------------------------------===//
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000112// CHECK: a StringRef should not be bound to a temporary std::string whose
Ted Kremenek4806a832010-02-14 19:08:36 +0000113// lifetime is shorter than the StringRef's.
Ted Kremenek184b3382010-02-14 02:45:18 +0000114//===----------------------------------------------------------------------===//
115
116namespace {
117class StringRefCheckerVisitor : public StmtVisitor<StringRefCheckerVisitor> {
Ted Kremenek5a10f082012-04-04 18:11:35 +0000118 const Decl *DeclWithIssue;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000119 BugReporter &BR;
120 const CheckerBase *Checker;
121
Ted Kremenek184b3382010-02-14 02:45:18 +0000122public:
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000123 StringRefCheckerVisitor(const Decl *declWithIssue, BugReporter &br,
124 const CheckerBase *checker)
125 : DeclWithIssue(declWithIssue), BR(br), Checker(checker) {}
Ted Kremenek184b3382010-02-14 02:45:18 +0000126 void VisitChildren(Stmt *S) {
Benjamin Kramer973431b2015-07-03 15:12:24 +0000127 for (Stmt *Child : S->children())
128 if (Child)
129 Visit(Child);
Ted Kremenek184b3382010-02-14 02:45:18 +0000130 }
131 void VisitStmt(Stmt *S) { VisitChildren(S); }
132 void VisitDeclStmt(DeclStmt *DS);
133private:
134 void VisitVarDecl(VarDecl *VD);
135};
136} // end anonymous namespace
137
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000138static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR,
139 const CheckerBase *Checker) {
140 StringRefCheckerVisitor walker(D, BR, Checker);
Ted Kremenek184b3382010-02-14 02:45:18 +0000141 walker.Visit(D->getBody());
142}
143
144void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) {
Ted Kremenekc968e5e2010-02-14 19:08:43 +0000145 VisitChildren(S);
146
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000147 for (auto *I : S->decls())
148 if (VarDecl *VD = dyn_cast<VarDecl>(I))
Ted Kremenek184b3382010-02-14 02:45:18 +0000149 VisitVarDecl(VD);
150}
151
Ted Kremenek184b3382010-02-14 02:45:18 +0000152void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) {
153 Expr *Init = VD->getInit();
154 if (!Init)
Ted Kremenek988805c2010-02-14 19:09:05 +0000155 return;
Ted Kremenek4806a832010-02-14 19:08:36 +0000156
Ted Kremenek184b3382010-02-14 02:45:18 +0000157 // Pattern match for:
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000158 // StringRef x = call() (where call returns std::string)
Ted Kremenek48531312010-02-14 19:09:13 +0000159 if (!IsLLVMStringRef(VD->getType()))
Ted Kremenek184b3382010-02-14 02:45:18 +0000160 return;
John McCall5d413782010-12-06 08:20:24 +0000161 ExprWithCleanups *Ex1 = dyn_cast<ExprWithCleanups>(Init);
Ted Kremenek184b3382010-02-14 02:45:18 +0000162 if (!Ex1)
163 return;
164 CXXConstructExpr *Ex2 = dyn_cast<CXXConstructExpr>(Ex1->getSubExpr());
165 if (!Ex2 || Ex2->getNumArgs() != 1)
166 return;
167 ImplicitCastExpr *Ex3 = dyn_cast<ImplicitCastExpr>(Ex2->getArg(0));
168 if (!Ex3)
169 return;
170 CXXConstructExpr *Ex4 = dyn_cast<CXXConstructExpr>(Ex3->getSubExpr());
171 if (!Ex4 || Ex4->getNumArgs() != 1)
172 return;
173 ImplicitCastExpr *Ex5 = dyn_cast<ImplicitCastExpr>(Ex4->getArg(0));
174 if (!Ex5)
175 return;
176 CXXBindTemporaryExpr *Ex6 = dyn_cast<CXXBindTemporaryExpr>(Ex5->getSubExpr());
177 if (!Ex6 || !IsStdString(Ex6->getType()))
178 return;
Ted Kremenek4806a832010-02-14 19:08:36 +0000179
Ted Kremenek184b3382010-02-14 02:45:18 +0000180 // Okay, badness! Report an error.
Ted Kremenek48531312010-02-14 19:09:13 +0000181 const char *desc = "StringRef should not be bound to temporary "
182 "std::string that it outlives";
Anna Zaksc29bed32011-09-20 21:38:35 +0000183 PathDiagnosticLocation VDLoc =
184 PathDiagnosticLocation::createBegin(VD, BR.getSourceManager());
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000185 BR.EmitBasicReport(DeclWithIssue, Checker, desc, "LLVM Conventions", desc,
Anna Zaksc29bed32011-09-20 21:38:35 +0000186 VDLoc, Init->getSourceRange());
Ted Kremenek184b3382010-02-14 02:45:18 +0000187}
188
189//===----------------------------------------------------------------------===//
Ted Kremenek48531312010-02-14 19:09:13 +0000190// CHECK: Clang AST nodes should not have fields that can allocate
191// memory.
192//===----------------------------------------------------------------------===//
193
194static bool AllocatesMemory(QualType T) {
195 return IsStdVector(T) || IsStdString(T) || IsSmallVector(T);
196}
197
198// This type checking could be sped up via dynamic programming.
199static bool IsPartOfAST(const CXXRecordDecl *R) {
Nick Lewycky6cbc3f72010-05-30 18:05:23 +0000200 if (IsClangStmt(R) || IsClangType(R) || IsClangDecl(R) || IsClangAttr(R))
Ted Kremenek48531312010-02-14 19:09:13 +0000201 return true;
202
Aaron Ballman574705e2014-03-13 15:41:46 +0000203 for (const auto &BS : R->bases()) {
Ted Kremenek48531312010-02-14 19:09:13 +0000204 QualType T = BS.getType();
205 if (const RecordType *baseT = T->getAs<RecordType>()) {
206 CXXRecordDecl *baseD = cast<CXXRecordDecl>(baseT->getDecl());
207 if (IsPartOfAST(baseD))
208 return true;
209 }
210 }
211
212 return false;
213}
214
215namespace {
216class ASTFieldVisitor {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000217 SmallVector<FieldDecl*, 10> FieldChain;
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000218 const CXXRecordDecl *Root;
Ted Kremenek48531312010-02-14 19:09:13 +0000219 BugReporter &BR;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000220 const CheckerBase *Checker;
221
Ted Kremenek48531312010-02-14 19:09:13 +0000222public:
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000223 ASTFieldVisitor(const CXXRecordDecl *root, BugReporter &br,
224 const CheckerBase *checker)
225 : Root(root), BR(br), Checker(checker) {}
Ted Kremenek48531312010-02-14 19:09:13 +0000226
227 void Visit(FieldDecl *D);
228 void ReportError(QualType T);
229};
230} // end anonymous namespace
231
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000232static void CheckASTMemory(const CXXRecordDecl *R, BugReporter &BR,
233 const CheckerBase *Checker) {
Ted Kremenek48531312010-02-14 19:09:13 +0000234 if (!IsPartOfAST(R))
235 return;
236
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000237 for (auto *I : R->fields()) {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000238 ASTFieldVisitor walker(R, BR, Checker);
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000239 walker.Visit(I);
Ted Kremenek48531312010-02-14 19:09:13 +0000240 }
241}
242
243void ASTFieldVisitor::Visit(FieldDecl *D) {
244 FieldChain.push_back(D);
245
246 QualType T = D->getType();
247
248 if (AllocatesMemory(T))
249 ReportError(T);
250
251 if (const RecordType *RT = T->getAs<RecordType>()) {
252 const RecordDecl *RD = RT->getDecl()->getDefinition();
Aaron Ballmane8a8bae2014-03-08 20:12:42 +0000253 for (auto *I : RD->fields())
254 Visit(I);
Ted Kremenek48531312010-02-14 19:09:13 +0000255 }
256
257 FieldChain.pop_back();
258}
259
260void ASTFieldVisitor::ReportError(QualType T) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000261 SmallString<1024> buf;
Ted Kremenek48531312010-02-14 19:09:13 +0000262 llvm::raw_svector_ostream os(buf);
263
264 os << "AST class '" << Root->getName() << "' has a field '"
265 << FieldChain.front()->getName() << "' that allocates heap memory";
266 if (FieldChain.size() > 1) {
267 os << " via the following chain: ";
268 bool isFirst = true;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000269 for (SmallVectorImpl<FieldDecl*>::iterator I=FieldChain.begin(),
Ted Kremenek48531312010-02-14 19:09:13 +0000270 E=FieldChain.end(); I!=E; ++I) {
271 if (!isFirst)
272 os << '.';
273 else
274 isFirst = false;
275 os << (*I)->getName();
276 }
277 }
278 os << " (type " << FieldChain.back()->getType().getAsString() << ")";
Ted Kremenek48531312010-02-14 19:09:13 +0000279
280 // Note that this will fire for every translation unit that uses this
281 // class. This is suboptimal, but at least scan-build will merge
282 // duplicate HTML reports. In the future we need a unified way of merging
283 // duplicate reports across translation units. For C++ classes we cannot
284 // just report warnings when we see an out-of-line method definition for a
285 // class, as that heuristic doesn't always work (the complete definition of
286 // the class may be in the header file, for example).
Anna Zaksc29bed32011-09-20 21:38:35 +0000287 PathDiagnosticLocation L = PathDiagnosticLocation::createBegin(
288 FieldChain.front(), BR.getSourceManager());
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000289 BR.EmitBasicReport(Root, Checker, "AST node allocates heap memory",
290 "LLVM Conventions", os.str(), L);
Ted Kremenek48531312010-02-14 19:09:13 +0000291}
292
293//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000294// LLVMConventionsChecker
Ted Kremenek184b3382010-02-14 02:45:18 +0000295//===----------------------------------------------------------------------===//
296
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000297namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +0000298class LLVMConventionsChecker : public Checker<
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000299 check::ASTDecl<CXXRecordDecl>,
300 check::ASTCodeBody > {
301public:
302 void checkASTDecl(const CXXRecordDecl *R, AnalysisManager& mgr,
303 BugReporter &BR) const {
John McCallf937c022011-10-07 06:10:15 +0000304 if (R->isCompleteDefinition())
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000305 CheckASTMemory(R, BR, this);
Ted Kremenek988805c2010-02-14 19:09:05 +0000306 }
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000307
308 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
309 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000310 CheckStringRefAssignedTemporary(D, BR, this);
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000311 }
312};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000313}
Ted Kremenek988805c2010-02-14 19:09:05 +0000314
Argyrios Kyrtzidis24ffc082011-02-17 21:39:24 +0000315void ento::registerLLVMConventionsChecker(CheckerManager &mgr) {
316 mgr.registerChecker<LLVMConventionsChecker>();
Ted Kremenek988805c2010-02-14 19:09:05 +0000317}