blob: 4e69f2916fdc3abc92064d5075ea2961dfc85988 [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
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/StmtVisitor.h"
17#include "clang/Checker/Checkers/LocalCheckers.h"
18#include "clang/Checker/BugReporter/BugReporter.h"
19#include <string>
20#include <llvm/ADT/StringRef.h>
21
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
Ted Kremenek55abf162010-02-14 19:08:36 +000025// Generic type checking routines.
26//===----------------------------------------------------------------------===//
27
28static bool IsStringRef(QualType T) {
29 const RecordType *RT = T->getAs<RecordType>();
30 if (!RT)
31 return false;
32
33 return llvm::StringRef(QualType(RT, 0).getAsString()) ==
34 "class llvm::StringRef";
35}
36
37static bool IsStdString(QualType T) {
38 if (const QualifiedNameType *QT = T->getAs<QualifiedNameType>())
39 T = QT->getNamedType();
40
41 const TypedefType *TT = T->getAs<TypedefType>();
42 if (!TT)
43 return false;
44
45 const TypedefDecl *TD = TT->getDecl();
46 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(TD->getDeclContext());
47 if (!ND)
48 return false;
49 const IdentifierInfo *II = ND->getIdentifier();
50 if (!II || II->getName() != "std")
51 return false;
52
53 DeclarationName N = TD->getDeclName();
54 return llvm::StringRef(N.getAsString()) == "string";
55}
56
57//===----------------------------------------------------------------------===//
58// CHECK: a llvm::StringRef should not be bound to a temporary std::string whose
59// lifetime is shorter than the StringRef's.
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000060//===----------------------------------------------------------------------===//
61
62namespace {
63class StringRefCheckerVisitor : public StmtVisitor<StringRefCheckerVisitor> {
64 BugReporter &BR;
65public:
66 StringRefCheckerVisitor(BugReporter &br) : BR(br) {}
67 void VisitChildren(Stmt *S) {
68 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end() ;
69 I != E; ++I)
70 if (Stmt *child = *I)
71 Visit(child);
72 }
73 void VisitStmt(Stmt *S) { VisitChildren(S); }
74 void VisitDeclStmt(DeclStmt *DS);
75private:
76 void VisitVarDecl(VarDecl *VD);
77};
78} // end anonymous namespace
79
80static void CheckStringRefAssignedTemporary(const Decl *D, BugReporter &BR) {
81 StringRefCheckerVisitor walker(BR);
82 walker.Visit(D->getBody());
83}
84
85void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) {
86 for (DeclStmt::decl_iterator I = S->decl_begin(), E = S->decl_end();I!=E; ++I)
87 if (VarDecl *VD = dyn_cast<VarDecl>(*I))
88 VisitVarDecl(VD);
89}
90
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000091void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) {
92 Expr *Init = VD->getInit();
93 if (!Init)
94 return;
Ted Kremenek55abf162010-02-14 19:08:36 +000095
Ted Kremenek6dd66ed2010-02-14 02:45:18 +000096 // Pattern match for:
97 // llvm::StringRef x = call() (where call returns std::string)
98 if (!IsStringRef(VD->getType()))
99 return;
100 CXXExprWithTemporaries *Ex1 = dyn_cast<CXXExprWithTemporaries>(Init);
101 if (!Ex1)
102 return;
103 CXXConstructExpr *Ex2 = dyn_cast<CXXConstructExpr>(Ex1->getSubExpr());
104 if (!Ex2 || Ex2->getNumArgs() != 1)
105 return;
106 ImplicitCastExpr *Ex3 = dyn_cast<ImplicitCastExpr>(Ex2->getArg(0));
107 if (!Ex3)
108 return;
109 CXXConstructExpr *Ex4 = dyn_cast<CXXConstructExpr>(Ex3->getSubExpr());
110 if (!Ex4 || Ex4->getNumArgs() != 1)
111 return;
112 ImplicitCastExpr *Ex5 = dyn_cast<ImplicitCastExpr>(Ex4->getArg(0));
113 if (!Ex5)
114 return;
115 CXXBindTemporaryExpr *Ex6 = dyn_cast<CXXBindTemporaryExpr>(Ex5->getSubExpr());
116 if (!Ex6 || !IsStdString(Ex6->getType()))
117 return;
Ted Kremenek55abf162010-02-14 19:08:36 +0000118
Ted Kremenek6dd66ed2010-02-14 02:45:18 +0000119 // Okay, badness! Report an error.
120 BR.EmitBasicReport("StringRef should not be bound to temporary "
121 "std::string that it outlives", "LLVM Conventions",
122 VD->getLocStart(), Init->getSourceRange());
123}
124
125//===----------------------------------------------------------------------===//
126// Entry point for all checks.
127//===----------------------------------------------------------------------===//
128
129void clang::CheckLLVMConventions(const Decl *D, BugReporter &BR) {
130 CheckStringRefAssignedTemporary(D, BR);
131}