From c771d5d79f6ad3a9996cc6125791cce9b1a8a8f4 Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Wed, 28 May 2014 02:16:01 +0000 Subject: [PATCH] Move the logic for testing for namespace std into one location. This check can be performed by using Decl::isInStdNamespace or DeclContext::isStdNamespace llvm-svn: 209708 --- clang/include/clang/AST/DeclBase.h | 4 ++++ clang/lib/AST/Decl.cpp | 9 +-------- clang/lib/AST/DeclBase.cpp | 20 ++++++++++++++++++++ clang/lib/Analysis/Consumed.cpp | 15 ++------------- clang/lib/Sema/SemaExceptionSpec.cpp | 11 ++--------- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 6 +----- .../Checkers/LLVMConventionsChecker.cpp | 2 +- .../lib/StaticAnalyzer/Core/BugReporterVisitors.cpp | 2 +- .../StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp | 4 ++-- 9 files changed, 34 insertions(+), 39 deletions(-) diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index b832123..c77b2b6 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -397,6 +397,8 @@ public: bool isInAnonymousNamespace() const; + bool isInStdNamespace() const; + ASTContext &getASTContext() const LLVM_READONLY; void setAccess(AccessSpecifier AS) { @@ -1156,6 +1158,8 @@ public: return DeclKind == Decl::Namespace; } + bool isStdNamespace() const; + bool isInlineNamespace() const; /// \brief Determines whether this context is dependent on a diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index a783364..1c3a5384 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -2328,12 +2328,6 @@ bool FunctionDecl::isReservedGlobalPlacementOperator() const { return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); } -static bool isNamespaceStd(const DeclContext *DC) { - const NamespaceDecl *ND = dyn_cast(DC->getRedeclContext()); - return ND && isNamed(ND, "std") && - ND->getParent()->getRedeclContext()->isTranslationUnit(); -} - bool FunctionDecl::isReplaceableGlobalAllocationFunction() const { if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) return false; @@ -2371,9 +2365,8 @@ bool FunctionDecl::isReplaceableGlobalAllocationFunction() const { Ty = Ty->getPointeeType(); if (Ty.getCVRQualifiers() != Qualifiers::Const) return false; - // FIXME: Recognise nothrow_t in an inline namespace inside std? const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); - return RD && isNamed(RD, "nothrow_t") && isNamespaceStd(RD->getDeclContext()); + return RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace(); } FunctionDecl * diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index 1743b91..2b1506d 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -251,6 +251,10 @@ bool Decl::isInAnonymousNamespace() const { return false; } +bool Decl::isInStdNamespace() const { + return getDeclContext()->isStdNamespace(); +} + TranslationUnitDecl *Decl::getTranslationUnitDecl() { if (TranslationUnitDecl *TUD = dyn_cast(this)) return TUD; @@ -795,6 +799,22 @@ bool DeclContext::isInlineNamespace() const { cast(this)->isInline(); } +bool DeclContext::isStdNamespace() const { + if (!isNamespace()) + return false; + + const NamespaceDecl *ND = cast(this); + if (ND->isInline()) { + return ND->getParent()->isStdNamespace(); + } + + if (!getParent()->getRedeclContext()->isTranslationUnit()) + return false; + + const IdentifierInfo *II = ND->getIdentifier(); + return II && II->isStr("std"); +} + bool DeclContext::isDependentContext() const { if (isFileContext()) return false; diff --git a/clang/lib/Analysis/Consumed.cpp b/clang/lib/Analysis/Consumed.cpp index 2027e77..2b2da2c 100644 --- a/clang/lib/Analysis/Consumed.cpp +++ b/clang/lib/Analysis/Consumed.cpp @@ -739,16 +739,6 @@ void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) { } } -static bool isStdNamespace(const DeclContext *DC) { - if (!DC->isNamespace()) return false; - while (DC->getParent()->isNamespace()) - DC = DC->getParent(); - const NamespaceDecl *ND = dyn_cast(DC); - - return ND && ND->getName() == "std" && - ND->getDeclContext()->isTranslationUnit(); -} - void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) { const FunctionDecl *FunDecl = Call->getDirectCallee(); if (!FunDecl) @@ -756,9 +746,8 @@ void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) { // Special case for the std::move function. // TODO: Make this more specific. (Deferred) - if (Call->getNumArgs() == 1 && - FunDecl->getNameAsString() == "move" && - isStdNamespace(FunDecl->getDeclContext())) { + if (Call->getNumArgs() == 1 && FunDecl->getNameAsString() == "move" && + FunDecl->isInStdNamespace()) { copyInfo(Call->getArg(0), Call, CS_Consumed); return; } diff --git a/clang/lib/Sema/SemaExceptionSpec.cpp b/clang/lib/Sema/SemaExceptionSpec.cpp index 40112a0..24d8222 100644 --- a/clang/lib/Sema/SemaExceptionSpec.cpp +++ b/clang/lib/Sema/SemaExceptionSpec.cpp @@ -468,15 +468,8 @@ bool Sema::CheckEquivalentExceptionSpec(const PartialDiagnostic &DiagID, IdentifierInfo* Name = ExRecord->getIdentifier(); if (Name && Name->getName() == "bad_alloc") { // It's called bad_alloc, but is it in std? - DeclContext* DC = ExRecord->getDeclContext(); - DC = DC->getEnclosingNamespaceContext(); - if (NamespaceDecl* NS = dyn_cast(DC)) { - IdentifierInfo* NSName = NS->getIdentifier(); - DC = DC->getParent(); - if (NSName && NSName->getName() == "std" && - DC->getEnclosingNamespaceContext()->isTranslationUnit()) { - return false; - } + if (ExRecord->isInStdNamespace()) { + return false; } } } diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 60ec5bb..be2db41 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -887,11 +887,7 @@ Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { if (DCParent->isNamespace() && cast(DCParent)->getIdentifier() && cast(DCParent)->getIdentifier()->isStr("tr1")) { - DeclContext *DCParent2 = DCParent->getParent(); - if (DCParent2->isNamespace() && - cast(DCParent2)->getIdentifier() && - cast(DCParent2)->getIdentifier()->isStr("std") && - DCParent2->getParent()->isTranslationUnit()) + if (cast(DCParent)->isInStdNamespace()) Complain = false; } } diff --git a/clang/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp index 17ebf9e..0b7375a 100644 --- a/clang/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp @@ -58,7 +58,7 @@ static bool IsStdString(QualType T) { const TypedefNameDecl *TD = TT->getDecl(); - if (!InNamespace(TD, "std")) + if (!TD->isInStdNamespace()) return false; return TD->getName() == "string"; diff --git a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp index 5679569..b415d5b 100644 --- a/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp +++ b/clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp @@ -1514,7 +1514,7 @@ static bool isInStdNamespace(const Decl *D) { while (const NamespaceDecl *Parent = dyn_cast(ND->getParent())) ND = Parent; - return ND->getName() == "std"; + return ND->isStdNamespace(); } diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp index 6983178..4619f62 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp @@ -387,14 +387,14 @@ static bool IsInStdNamespace(const FunctionDecl *FD) { const NamespaceDecl *ND = dyn_cast(DC); if (!ND) return false; - + while (const DeclContext *Parent = ND->getParent()) { if (!isa(Parent)) break; ND = cast(Parent); } - return ND->getName() == "std"; + return ND->isStdNamespace(); } // The GDM component containing the dynamic dispatch bifurcation info. When -- 2.7.4