Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 1 | //=== 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 | |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtVisitor.h" |
| 17 | #include "clang/Checker/Checkers/LocalCheckers.h" |
| 18 | #include "clang/Checker/BugReporter/BugReporter.h" |
| 19 | #include <string> |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 25 | // Generic type checking routines. |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 28 | static bool IsLLVMStringRef(QualType T) { |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 29 | const RecordType *RT = T->getAs<RecordType>(); |
| 30 | if (!RT) |
| 31 | return false; |
| 32 | |
| 33 | return llvm::StringRef(QualType(RT, 0).getAsString()) == |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 34 | "class llvm::StringRef"; |
| 35 | } |
| 36 | |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 37 | /// Check whether the declaration is semantically inside the top-level |
| 38 | /// namespace named by ns. |
Benjamin Kramer | ec1b1cc | 2010-07-14 23:19:41 +0000 | [diff] [blame] | 39 | static bool InNamespace(const Decl *D, llvm::StringRef NS) { |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 40 | const DeclContext *DC = D->getDeclContext(); |
| 41 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(D->getDeclContext()); |
| 42 | if (!ND) |
| 43 | return false; |
| 44 | const IdentifierInfo *II = ND->getIdentifier(); |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 45 | if (!II || !II->getName().equals(NS)) |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 46 | return false; |
| 47 | DC = ND->getDeclContext(); |
| 48 | return isa<TranslationUnitDecl>(DC); |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static bool IsStdString(QualType T) { |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 52 | if (const ElaboratedType *QT = T->getAs<ElaboratedType>()) |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 53 | T = QT->getNamedType(); |
| 54 | |
| 55 | const TypedefType *TT = T->getAs<TypedefType>(); |
| 56 | if (!TT) |
| 57 | return false; |
| 58 | |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 59 | const TypedefDecl *TD = TT->getDecl(); |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 60 | |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 61 | if (!InNamespace(TD, "std")) |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 62 | return false; |
| 63 | |
| 64 | return TD->getName() == "string"; |
| 65 | } |
| 66 | |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 67 | static bool IsClangType(const RecordDecl *RD) { |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 68 | return RD->getName() == "Type" && InNamespace(RD, "clang"); |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static bool IsClangDecl(const RecordDecl *RD) { |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 72 | return RD->getName() == "Decl" && InNamespace(RD, "clang"); |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static bool IsClangStmt(const RecordDecl *RD) { |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 76 | return RD->getName() == "Stmt" && InNamespace(RD, "clang"); |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 79 | static bool IsClangAttr(const RecordDecl *RD) { |
| 80 | return RD->getName() == "Attr" && InNamespace(RD, "clang"); |
Ted Kremenek | 1ed482b | 2010-02-14 22:58:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 83 | static bool IsStdVector(QualType T) { |
| 84 | const TemplateSpecializationType *TS = T->getAs<TemplateSpecializationType>(); |
| 85 | if (!TS) |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 86 | return false; |
| 87 | |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 88 | TemplateName TM = TS->getTemplateName(); |
| 89 | TemplateDecl *TD = TM.getAsTemplateDecl(); |
| 90 | |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 91 | if (!TD || !InNamespace(TD, "std")) |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 92 | return false; |
| 93 | |
| 94 | return TD->getName() == "vector"; |
| 95 | } |
| 96 | |
| 97 | static 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 Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 105 | if (!TD || !InNamespace(TD, "llvm")) |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 106 | return false; |
| 107 | |
| 108 | return TD->getName() == "SmallVector"; |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | //===----------------------------------------------------------------------===// |
| 112 | // CHECK: a llvm::StringRef should not be bound to a temporary std::string whose |
| 113 | // lifetime is shorter than the StringRef's. |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | namespace { |
| 117 | class StringRefCheckerVisitor : public StmtVisitor<StringRefCheckerVisitor> { |
| 118 | BugReporter &BR; |
| 119 | public: |
| 120 | StringRefCheckerVisitor(BugReporter &br) : BR(br) {} |
| 121 | void VisitChildren(Stmt *S) { |
| 122 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ; |
| 123 | I != E; ++I) |
| 124 | if (Stmt *child = *I) |
| 125 | Visit(child); |
| 126 | } |
| 127 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
| 128 | void VisitDeclStmt(DeclStmt *DS); |
| 129 | private: |
| 130 | void VisitVarDecl(VarDecl *VD); |
| 131 | }; |
| 132 | } // end anonymous namespace |
| 133 | |
| 134 | static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR) { |
| 135 | StringRefCheckerVisitor walker(BR); |
| 136 | walker.Visit(D->getBody()); |
| 137 | } |
| 138 | |
| 139 | void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) { |
Ted Kremenek | 005f09b | 2010-02-14 19:08:43 +0000 | [diff] [blame] | 140 | VisitChildren(S); |
| 141 | |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 142 | for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();I!=E; ++I) |
| 143 | if (VarDecl *VD = dyn_cast<VarDecl>(*I)) |
| 144 | VisitVarDecl(VD); |
| 145 | } |
| 146 | |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 147 | void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) { |
| 148 | Expr *Init = VD->getInit(); |
| 149 | if (!Init) |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 150 | return; |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 152 | // Pattern match for: |
| 153 | // llvm::StringRef x = call() (where call returns std::string) |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 154 | if (!IsLLVMStringRef(VD->getType())) |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 155 | return; |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame^] | 156 | ExprWithCleanups *Ex1 = dyn_cast<ExprWithCleanups>(Init); |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 157 | if (!Ex1) |
| 158 | return; |
| 159 | CXXConstructExpr *Ex2 = dyn_cast<CXXConstructExpr>(Ex1->getSubExpr()); |
| 160 | if (!Ex2 || Ex2->getNumArgs() != 1) |
| 161 | return; |
| 162 | ImplicitCastExpr *Ex3 = dyn_cast<ImplicitCastExpr>(Ex2->getArg(0)); |
| 163 | if (!Ex3) |
| 164 | return; |
| 165 | CXXConstructExpr *Ex4 = dyn_cast<CXXConstructExpr>(Ex3->getSubExpr()); |
| 166 | if (!Ex4 || Ex4->getNumArgs() != 1) |
| 167 | return; |
| 168 | ImplicitCastExpr *Ex5 = dyn_cast<ImplicitCastExpr>(Ex4->getArg(0)); |
| 169 | if (!Ex5) |
| 170 | return; |
| 171 | CXXBindTemporaryExpr *Ex6 = dyn_cast<CXXBindTemporaryExpr>(Ex5->getSubExpr()); |
| 172 | if (!Ex6 || !IsStdString(Ex6->getType())) |
| 173 | return; |
Ted Kremenek | 55abf16 | 2010-02-14 19:08:36 +0000 | [diff] [blame] | 174 | |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 175 | // Okay, badness! Report an error. |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 176 | const char *desc = "StringRef should not be bound to temporary " |
| 177 | "std::string that it outlives"; |
| 178 | |
| 179 | BR.EmitBasicReport(desc, "LLVM Conventions", desc, |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 180 | VD->getLocStart(), Init->getSourceRange()); |
| 181 | } |
| 182 | |
| 183 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 184 | // CHECK: Clang AST nodes should not have fields that can allocate |
| 185 | // memory. |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | |
| 188 | static bool AllocatesMemory(QualType T) { |
| 189 | return IsStdVector(T) || IsStdString(T) || IsSmallVector(T); |
| 190 | } |
| 191 | |
| 192 | // This type checking could be sped up via dynamic programming. |
| 193 | static bool IsPartOfAST(const CXXRecordDecl *R) { |
Nick Lewycky | 31b5c4b | 2010-05-30 18:05:23 +0000 | [diff] [blame] | 194 | if (IsClangStmt(R) || IsClangType(R) || IsClangDecl(R) || IsClangAttr(R)) |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 195 | return true; |
| 196 | |
| 197 | for (CXXRecordDecl::base_class_const_iterator I = R->bases_begin(), |
| 198 | E = R->bases_end(); I!=E; ++I) { |
| 199 | CXXBaseSpecifier BS = *I; |
| 200 | QualType T = BS.getType(); |
| 201 | if (const RecordType *baseT = T->getAs<RecordType>()) { |
| 202 | CXXRecordDecl *baseD = cast<CXXRecordDecl>(baseT->getDecl()); |
| 203 | if (IsPartOfAST(baseD)) |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | namespace { |
| 212 | class ASTFieldVisitor { |
| 213 | llvm::SmallVector<FieldDecl*, 10> FieldChain; |
| 214 | CXXRecordDecl *Root; |
| 215 | BugReporter &BR; |
| 216 | public: |
| 217 | ASTFieldVisitor(CXXRecordDecl *root, BugReporter &br) |
| 218 | : Root(root), BR(br) {} |
| 219 | |
| 220 | void Visit(FieldDecl *D); |
| 221 | void ReportError(QualType T); |
| 222 | }; |
| 223 | } // end anonymous namespace |
| 224 | |
| 225 | static void CheckASTMemory(CXXRecordDecl *R, BugReporter &BR) { |
| 226 | if (!IsPartOfAST(R)) |
| 227 | return; |
| 228 | |
| 229 | for (RecordDecl::field_iterator I = R->field_begin(), E = R->field_end(); |
| 230 | I != E; ++I) { |
| 231 | ASTFieldVisitor walker(R, BR); |
| 232 | walker.Visit(*I); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void ASTFieldVisitor::Visit(FieldDecl *D) { |
| 237 | FieldChain.push_back(D); |
| 238 | |
| 239 | QualType T = D->getType(); |
| 240 | |
| 241 | if (AllocatesMemory(T)) |
| 242 | ReportError(T); |
| 243 | |
| 244 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 245 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 246 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 247 | I != E; ++I) |
| 248 | Visit(*I); |
| 249 | } |
| 250 | |
| 251 | FieldChain.pop_back(); |
| 252 | } |
| 253 | |
| 254 | void ASTFieldVisitor::ReportError(QualType T) { |
| 255 | llvm::SmallString<1024> buf; |
| 256 | llvm::raw_svector_ostream os(buf); |
| 257 | |
| 258 | os << "AST class '" << Root->getName() << "' has a field '" |
| 259 | << FieldChain.front()->getName() << "' that allocates heap memory"; |
| 260 | if (FieldChain.size() > 1) { |
| 261 | os << " via the following chain: "; |
| 262 | bool isFirst = true; |
| 263 | for (llvm::SmallVectorImpl<FieldDecl*>::iterator I=FieldChain.begin(), |
| 264 | E=FieldChain.end(); I!=E; ++I) { |
| 265 | if (!isFirst) |
| 266 | os << '.'; |
| 267 | else |
| 268 | isFirst = false; |
| 269 | os << (*I)->getName(); |
| 270 | } |
| 271 | } |
| 272 | os << " (type " << FieldChain.back()->getType().getAsString() << ")"; |
| 273 | os.flush(); |
| 274 | |
| 275 | // Note that this will fire for every translation unit that uses this |
| 276 | // class. This is suboptimal, but at least scan-build will merge |
| 277 | // duplicate HTML reports. In the future we need a unified way of merging |
| 278 | // duplicate reports across translation units. For C++ classes we cannot |
| 279 | // just report warnings when we see an out-of-line method definition for a |
| 280 | // class, as that heuristic doesn't always work (the complete definition of |
| 281 | // the class may be in the header file, for example). |
| 282 | BR.EmitBasicReport("AST node allocates heap memory", "LLVM Conventions", |
| 283 | os.str(), FieldChain.front()->getLocStart()); |
| 284 | } |
| 285 | |
| 286 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 287 | // Entry point for all checks. |
| 288 | //===----------------------------------------------------------------------===// |
| 289 | |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 290 | static void ScanCodeDecls(DeclContext *DC, BugReporter &BR) { |
| 291 | for (DeclContext::decl_iterator I=DC->decls_begin(), E=DC->decls_end(); |
| 292 | I!=E ; ++I) { |
| 293 | |
| 294 | Decl *D = *I; |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 295 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 296 | if (D->hasBody()) |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 297 | CheckStringRefAssignedTemporary(D, BR); |
Ted Kremenek | a6b8793 | 2010-02-14 19:09:13 +0000 | [diff] [blame] | 298 | |
| 299 | if (CXXRecordDecl *R = dyn_cast<CXXRecordDecl>(D)) |
| 300 | if (R->isDefinition()) |
| 301 | CheckASTMemory(R, BR); |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 302 | |
| 303 | if (DeclContext *DC_child = dyn_cast<DeclContext>(D)) |
| 304 | ScanCodeDecls(DC_child, BR); |
| 305 | } |
Ted Kremenek | 6dd66ed | 2010-02-14 02:45:18 +0000 | [diff] [blame] | 306 | } |
Ted Kremenek | 676ca15 | 2010-02-14 19:09:05 +0000 | [diff] [blame] | 307 | |
| 308 | void clang::CheckLLVMConventions(TranslationUnitDecl &TU, |
| 309 | BugReporter &BR) { |
| 310 | ScanCodeDecls(&TU, BR); |
| 311 | } |
| 312 | |