From: Richard Trieu Date: Wed, 28 May 2014 02:16:01 +0000 (+0000) Subject: Move the logic for testing for namespace std into one location. This check can X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=62c949d4812cea796cc50db15ae95ad22fbeed06;p=clang Move the logic for testing for namespace std into one location. This check can be performed by using Decl::isInStdNamespace or DeclContext::isStdNamespace git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209708 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index b832123719..c77b2b60df 100644 --- a/include/clang/AST/DeclBase.h +++ b/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/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index a783364c67..1c3a5384d3 100644 --- a/lib/AST/Decl.cpp +++ b/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/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index 1743b91454..2b1506d191 100644 --- a/lib/AST/DeclBase.cpp +++ b/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/lib/Analysis/Consumed.cpp b/lib/Analysis/Consumed.cpp index 2027e77bbf..2b2da2c69a 100644 --- a/lib/Analysis/Consumed.cpp +++ b/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/lib/Sema/SemaExceptionSpec.cpp b/lib/Sema/SemaExceptionSpec.cpp index 40112a0521..24d82224dd 100644 --- a/lib/Sema/SemaExceptionSpec.cpp +++ b/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/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 60ec5bbad9..be2db41efd 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/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/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp b/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp index 17ebf9edb6..0b7375a4b6 100644 --- a/lib/StaticAnalyzer/Checkers/LLVMConventionsChecker.cpp +++ b/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/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp b/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp index 5679569e1e..b415d5b53f 100644 --- a/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp +++ b/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/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp b/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp index 6983178056..4619f62bd2 100644 --- a/lib/StaticAnalyzer/Core/ExprEngineCallAndReturn.cpp +++ b/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