diff options
author | Ted Kremenek <kremenek@apple.com> | 2010-02-14 19:08:36 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2010-02-14 19:08:36 +0000 |
commit | 55abf16c7aec8c3063c368707857795afd06e21d (patch) | |
tree | 8ab7f65cb7d5318a59c84c346d5a13b014c779e3 /lib/Checker/LLVMConventionsChecker.cpp | |
parent | 94baf7fc056f271d1be27c31265431d2e3e5b6c3 (diff) |
Place type-checking static methods at type of file (where they will congregate).
No functionality change.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96180 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Checker/LLVMConventionsChecker.cpp')
-rw-r--r-- | lib/Checker/LLVMConventionsChecker.cpp | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/lib/Checker/LLVMConventionsChecker.cpp b/lib/Checker/LLVMConventionsChecker.cpp index 82f6d2c7bd..4e69f2916f 100644 --- a/lib/Checker/LLVMConventionsChecker.cpp +++ b/lib/Checker/LLVMConventionsChecker.cpp @@ -22,8 +22,41 @@ using namespace clang; //===----------------------------------------------------------------------===// -// Check if an llvm::StringRef is bound to temporary std::string whose lifetime -// is shorter than the StringRef's. +// Generic type checking routines. +//===----------------------------------------------------------------------===// + +static bool IsStringRef(QualType T) { + const RecordType *RT = T->getAs<RecordType>(); + if (!RT) + return false; + + return llvm::StringRef(QualType(RT, 0).getAsString()) == + "class llvm::StringRef"; +} + +static bool IsStdString(QualType T) { + if (const QualifiedNameType *QT = T->getAs<QualifiedNameType>()) + T = QT->getNamedType(); + + const TypedefType *TT = T->getAs<TypedefType>(); + if (!TT) + return false; + + const TypedefDecl *TD = TT->getDecl(); + const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(TD->getDeclContext()); + if (!ND) + return false; + const IdentifierInfo *II = ND->getIdentifier(); + if (!II || II->getName() != "std") + return false; + + DeclarationName N = TD->getDeclName(); + return llvm::StringRef(N.getAsString()) == "string"; +} + +//===----------------------------------------------------------------------===// +// CHECK: a llvm::StringRef should not be bound to a temporary std::string whose +// lifetime is shorter than the StringRef's. //===----------------------------------------------------------------------===// namespace { @@ -55,40 +88,11 @@ void StringRefCheckerVisitor::VisitDeclStmt(DeclStmt *S) { VisitVarDecl(VD); } -static bool IsStringRef(QualType T) { - const RecordType *RT = T->getAs<RecordType>(); - if (!RT) - return false; - - return llvm::StringRef(QualType(RT, 0).getAsString()) == - "class llvm::StringRef"; -} - -static bool IsStdString(QualType T) { - if (const QualifiedNameType *QT = T->getAs<QualifiedNameType>()) - T = QT->getNamedType(); - - const TypedefType *TT = T->getAs<TypedefType>(); - if (!TT) - return false; - - const TypedefDecl *TD = TT->getDecl(); - const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(TD->getDeclContext()); - if (!ND) - return false; - const IdentifierInfo *II = ND->getIdentifier(); - if (!II || II->getName() != "std") - return false; - - DeclarationName N = TD->getDeclName(); - return llvm::StringRef(N.getAsString()) == "string"; -} - void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) { Expr *Init = VD->getInit(); if (!Init) return; - + // Pattern match for: // llvm::StringRef x = call() (where call returns std::string) if (!IsStringRef(VD->getType())) @@ -111,7 +115,7 @@ void StringRefCheckerVisitor::VisitVarDecl(VarDecl *VD) { CXXBindTemporaryExpr *Ex6 = dyn_cast<CXXBindTemporaryExpr>(Ex5->getSubExpr()); if (!Ex6 || !IsStdString(Ex6->getType())) return; - + // Okay, badness! Report an error. BR.EmitBasicReport("StringRef should not be bound to temporary " "std::string that it outlives", "LLVM Conventions", |