]> granicus.if.org Git - clang/commitdiff
Modify some deleted function methods to better reflect reality:
authorSean Hunt <scshunt@csclub.uwaterloo.ca>
Fri, 6 May 2011 20:44:56 +0000 (20:44 +0000)
committerSean Hunt <scshunt@csclub.uwaterloo.ca>
Fri, 6 May 2011 20:44:56 +0000 (20:44 +0000)
 - New isDefined() function checks for deletedness
 - isThisDeclarationADefinition checks for deletedness
 - New doesThisDeclarationHaveABody() does what
   isThisDeclarationADefinition() used to do
 - The IsDeleted bit is not propagated across redeclarations
 - isDeleted() now checks the canoncial declaration
 - New isDeletedAsWritten() does what it says on the tin.
 - isUserProvided() now correct (thanks Richard!)

This fixes the bug that we weren't catching

void foo() = delete;
void foo() {}

as being a redefinition.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@131013 91177308-0d34-0410-b5e6-96231b3b80d8

21 files changed:
include/clang/AST/Decl.h
include/clang/AST/DeclCXX.h
lib/AST/ASTContext.cpp
lib/AST/Decl.cpp
lib/AST/DeclPrinter.cpp
lib/AST/DumpXML.cpp
lib/CodeGen/CodeGenModule.cpp
lib/CodeGen/ModuleBuilder.cpp
lib/Frontend/ASTConsumers.cpp
lib/Index/CallGraph.cpp
lib/Index/Indexer.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclCXX.cpp
lib/Sema/SemaTemplateInstantiate.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp
lib/Serialization/ASTReaderDecl.cpp
lib/Serialization/ASTWriterDecl.cpp
lib/StaticAnalyzer/Checkers/NSErrorChecker.cpp
lib/StaticAnalyzer/Core/CXXExprEngine.cpp
test/SemaCXX/deleted-function.cpp
tools/libclang/CIndex.cpp

index 2cf26184913cf305ec097da3f874d049702b5e3d..0a081e30fbe5ee636a821bde22e468704b04223b 100644 (file)
@@ -1514,6 +1514,16 @@ public:
     return hasBody(Definition);
   }
 
+  /// isDefined - Returns true if the function is defined at all, including
+  /// a deleted definition. Except for the behavior when the function is
+  /// deleted, behaves like hasBody.
+  bool isDefined(const FunctionDecl *&Definition) const;
+
+  virtual bool isDefined() const {
+    const FunctionDecl* Definition;
+    return isDefined(Definition);
+  }
+
   /// getBody - Retrieve the body (definition) of the function. The
   /// function body might be in any of the (re-)declarations of this
   /// function. The variant that accepts a FunctionDecl pointer will
@@ -1531,10 +1541,17 @@ public:
   /// isThisDeclarationADefinition - Returns whether this specific
   /// declaration of the function is also a definition. This does not
   /// determine whether the function has been defined (e.g., in a
-  /// previous definition); for that information, use getBody.
-  /// FIXME: Should return true if function is deleted or defaulted. However,
-  /// CodeGenModule.cpp uses it, and I don't know if this would break it.
+  /// previous definition); for that information, use isDefined. Note
+  /// that this returns false for a defaulted function unless that function
+  /// has been implicitly defined (possibly as deleted).
   bool isThisDeclarationADefinition() const {
+    return IsDeleted || Body || IsLateTemplateParsed;
+  }
+
+  /// doesThisDeclarationHaveABody - Returns whether this specific
+  /// declaration of the function has a body - that is, if it is a non-
+  /// deleted definition.
+  bool doesThisDeclarationHaveABody() const {
     return Body || IsLateTemplateParsed;
   }
 
@@ -1617,8 +1634,10 @@ public:
   ///   Integer(long double) = delete; // no construction from long double
   /// };
   /// @endcode
-  bool isDeleted() const { return IsDeleted; }
-  void setDeleted(bool D = true) { IsDeleted = D; }
+  // If a function is deleted, its first declaration must be.
+  bool isDeleted() const { return getCanonicalDecl()->IsDeleted; }
+  bool isDeletedAsWritten() const { return IsDeleted && !IsDefaulted; }
+  void setDeletedAsWritten(bool D = true) { IsDeleted = D; }
 
   /// \brief Determines whether this is a function "main", which is
   /// the entry point into an executable program.
index 25a75b9ea0c8a3114a72d2134a4f9f0d1ae7ffd3..5298745312f54468b64f45ad32968b2d337da35d 100644 (file)
@@ -1193,7 +1193,7 @@ public:
   /// isUserProvided - True if it is either an implicit constructor or
   /// if it was defaulted or deleted on first declaration.
   bool isUserProvided() const {
-    return getCanonicalDecl()->isDeleted() || getCanonicalDecl()->isDefaulted();
+    return !(isDeleted() || getCanonicalDecl()->isDefaulted());
   }
   
   ///
index a5ff66444907edfa77fa9305bde2a622fda55521..07823e04081adeae3c618b3ceffd6ea460e6bb20 100644 (file)
@@ -6100,7 +6100,7 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
 
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     // Forward declarations aren't required.
-    if (!FD->isThisDeclarationADefinition())
+    if (!FD->doesThisDeclarationHaveABody())
       return false;
 
     // Constructors and destructors are required.
index e336dd771a046504a17d21e304ac8c8c51666ccd..b57c6ab7d76df854bcf88225e5295aa377fe61a1 100644 (file)
@@ -1422,6 +1422,17 @@ bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
   return false;
 }
 
+bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
+  for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
+    if (I->IsDeleted || I->Body || I->IsLateTemplateParsed) {
+      Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
+      return true;
+    }
+  }
+
+  return false;
+}
+
 Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
   for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
     if (I->Body) {
@@ -1690,7 +1701,7 @@ bool FunctionDecl::isInlined() const {
 /// an externally visible symbol, but "extern inline" will not create an 
 /// externally visible symbol.
 bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
-  assert(isThisDeclarationADefinition() && "Must have the function definition");
+  assert(doesThisDeclarationHaveABody() && "Must have the function definition");
   assert(isInlined() && "Function must be inline");
   ASTContext &Context = getASTContext();
   
index 2fd88d7c808d8355b541b09597616e0e9685a5dc..49f27234c0436b65d887c9f78f9acc1f3f9ee44c 100644 (file)
@@ -400,7 +400,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
         if (D->getNumParams()) POut << ", ";
         POut << "...";
       }
-    } else if (D->isThisDeclarationADefinition() && !D->hasPrototype()) {
+    } else if (D->doesThisDeclarationHaveABody() && !D->hasPrototype()) {
       for (unsigned i = 0, e = D->getNumParams(); i != e; ++i) {
         if (i)
           Proto += ", ";
@@ -518,9 +518,9 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) {
 
   if (D->isPure())
     Out << " = 0";
-  else if (D->isDeleted())
+  else if (D->isDeletedAsWritten())
     Out << " = delete";
-  else if (D->isThisDeclarationADefinition()) {
+  else if (D->doesThisDeclarationHaveABody()) {
     if (!D->hasPrototype() && D->getNumParams()) {
       // This is a K&R function definition, so we need to print the
       // parameters.
index 8bb39ba470f924cf986804fd0d18e7a985eaba37..9bb3807c1d9239cbbf6bca5e85fa494858732389 100644 (file)
@@ -482,7 +482,7 @@ struct XMLDumper : public XMLDeclVisitor<XMLDumper>,
     setFlag("trivial", D->isTrivial());
     setFlag("returnzero", D->hasImplicitReturnZero());
     setFlag("prototype", D->hasWrittenPrototype());
-    setFlag("deleted", D->isDeleted());
+    setFlag("deleted", D->isDeletedAsWritten());
     if (D->getStorageClass() != SC_None)
       set("storage",
           VarDecl::getStorageClassSpecifierString(D->getStorageClass()));
@@ -493,7 +493,7 @@ struct XMLDumper : public XMLDeclVisitor<XMLDumper>,
     for (FunctionDecl::param_iterator
            I = D->param_begin(), E = D->param_end(); I != E; ++I)
       dispatch(*I);
-    if (D->isThisDeclarationADefinition())
+    if (D->doesThisDeclarationHaveABody())
       dispatch(D->getBody());
   }
 
index 96aa725f56f653648ef98e397fa2d132e6d5d88b..2ccd6a782c28559ce8e6f71f22660110a2e894f2 100644 (file)
@@ -727,7 +727,7 @@ void CodeGenModule::EmitGlobal(GlobalDecl GD) {
     }
 
     // Forward declarations are emitted lazily on first use.
-    if (!FD->isThisDeclarationADefinition())
+    if (!FD->doesThisDeclarationHaveABody())
       return;
   } else {
     const VarDecl *VD = cast<VarDecl>(Global);
@@ -897,7 +897,7 @@ CodeGenModule::GetOrCreateLLVMFunction(llvm::StringRef MangledName,
           assert(FD->isUsed() && "Sema didn't mark implicit function as used!");
           DeferredDeclsToEmit.push_back(D.getWithDecl(FD));
           break;
-        } else if (FD->isThisDeclarationADefinition()) {
+        } else if (FD->doesThisDeclarationHaveABody()) {
           DeferredDeclsToEmit.push_back(D.getWithDecl(FD));
           break;
         }
index 894502864464c073451834d4884a00e4fa96e1d6..4a2c4abbeb03c097be5783fd2ada53c481f654f1 100644 (file)
@@ -79,7 +79,7 @@ namespace {
                                      MEnd = D->decls_end();
              M != MEnd; ++M)
           if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*M))
-            if (Method->isThisDeclarationADefinition() &&
+            if (Method->doesThisDeclarationHaveABody() &&
                 (Method->hasAttr<UsedAttr>() || 
                  Method->hasAttr<ConstructorAttr>()))
               Builder->EmitTopLevelDecl(Method);
index ecd6ef442aea96c6e0082fc1919abdbb7bd5fd95..28d312a2219b4817e7fd645172b80531a12e6ce4 100644 (file)
@@ -173,7 +173,7 @@ void DeclContextPrinter::PrintDeclContext(const DeclContext* DC,
     break;
   case Decl::Function: {
     const FunctionDecl* FD = cast<FunctionDecl>(DC);
-    if (FD->isThisDeclarationADefinition())
+    if (FD->doesThisDeclarationHaveABody())
       Out << "[function] ";
     else
       Out << "<function> ";
index bf3f5a8a8d248547df10dc6be6343d7c7471c2d9..94790b8fbc17f0b59a28d74f924a6fca01abf460 100644 (file)
@@ -74,7 +74,7 @@ void CallGraph::addTU(ASTContext& Ctx) {
        I != E; ++I) {
 
     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
-      if (FD->isThisDeclarationADefinition()) {
+      if (FD->doesThisDeclarationHaveABody()) {
         // Set caller's ASTContext.
         Entity Ent = Entity::get(FD, Prog);
         CallGraphNode *Node = getOrInsertFunction(Ent);
index 7f21c4f3035a07086b48a3364f8f0ab333a35299..ebba43c4747e0a2fd38dc917728c9d1dd2b66b79 100644 (file)
@@ -39,7 +39,7 @@ public:
 
     Decl *D = Ent.getDecl(TU->getASTContext());
     if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
-      if (FD->isThisDeclarationADefinition())
+      if (FD->doesThisDeclarationHaveABody())
         DefMap[Ent] = std::make_pair(FD, TU);
   }
 };
index 0bf984d40a540778f1b372b8397dff9ca446a6b8..46b38e165d91e01a8007968c26d7b9da834495f6 100644 (file)
@@ -938,7 +938,7 @@ static void RemoveUsingDecls(LookupResult &R) {
 static bool IsDisallowedCopyOrAssign(const CXXMethodDecl *D) {
   // FIXME: Should check for private access too but access is set after we get
   // the decl here.
-  if (D->isThisDeclarationADefinition())
+  if (D->doesThisDeclarationHaveABody())
     return false;
 
   if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(D))
@@ -973,10 +973,9 @@ bool Sema::ShouldWarnIfUnusedFileScopedDecl(const DeclaratorDecl *D) const {
         return false;
     }
 
-    if (FD->isThisDeclarationADefinition() &&
+    if (FD->doesThisDeclarationHaveABody() &&
         Context.DeclMustBeEmitted(FD))
       return false;
-
   } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
     if (!VD->isFileVarDecl() ||
         VD->getType().isConstant(Context) ||
@@ -1907,10 +1906,6 @@ bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old) {
   if (Old->isPure())
     New->setPure();
 
-  // Merge the "deleted" flag.
-  if (Old->isDeleted())
-    New->setDeleted();
-
   // Merge attributes from the parameters.  These can mismatch with K&R
   // declarations.
   if (New->getNumParams() == Old->getNumParams())
@@ -4723,7 +4718,7 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
   if (Redeclaration && Previous.isSingleResult()) {
     const FunctionDecl *Def;
     FunctionDecl *PrevFD = dyn_cast<FunctionDecl>(Previous.getFoundDecl());
-    if (PrevFD && PrevFD->hasBody(Def) && D.hasAttributes()) {
+    if (PrevFD && PrevFD->isDefined(Def) && D.hasAttributes()) {
       Diag(NewFD->getLocation(), diag::warn_attribute_precede_definition);
       Diag(Def->getLocation(), diag::note_previous_definition);
     }
@@ -6118,7 +6113,7 @@ void Sema::CheckForFunctionRedefinition(FunctionDecl *FD) {
   // Don't complain if we're in GNU89 mode and the previous definition
   // was an extern inline function.
   const FunctionDecl *Definition;
-  if (FD->hasBody(Definition) &&
+  if (FD->isDefined(Definition) &&
       !canRedefineFunction(Definition, getLangOptions())) {
     if (getLangOptions().GNUMode && Definition->isInlineSpecified() &&
         Definition->getStorageClass() == SC_Extern)
index ca5fdd1fdd457dcfd14a6ae01c4321fe9c2683d8..41b09df9ca7d2a4eb1c12a1aa57e89ed07508c61 100644 (file)
@@ -2805,7 +2805,7 @@ static void CheckAbstractClassUsage(AbstractUsageInfo &Info,
                                     CXXMethodDecl *MD) {
   // No need to do the check on definitions, which require that
   // the return/param types be complete.
-  if (MD->isThisDeclarationADefinition())
+  if (MD->doesThisDeclarationHaveABody())
     return;
 
   // For safety's sake, just ignore it if we don't have type source
@@ -7646,7 +7646,7 @@ void Sema::SetDeclDeleted(Decl *Dcl, SourceLocation DelLoc) {
     // If the declaration wasn't the first, we delete the function anyway for
     // recovery.
   }
-  Fn->setDeleted();
+  Fn->setDeletedAsWritten();
 }
 
 static void SearchForReturnInStmt(Sema &Self, Stmt *S) {
index 3e1e735c85b5f4654991b5907ce19c477c7d37ef..03c2befbc560b5c6753f5d6e4bb7b430249454af 100644 (file)
@@ -2013,7 +2013,7 @@ Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
             SuppressNew)
           continue;
         
-        if (Function->hasBody())
+        if (Function->isDefined())
           continue;
 
         if (TSK == TSK_ExplicitInstantiationDefinition) {
@@ -2023,7 +2023,7 @@ Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation,
           //   specialization and is only an explicit instantiation definition 
           //   of members whose definition is visible at the point of 
           //   instantiation.
-          if (!Pattern->hasBody())
+          if (!Pattern->isDefined())
             continue;
         
           Function->setTemplateSpecializationKind(TSK, PointOfInstantiation);
index f6cbe267b4e2e4ce67e93047da0a96efb1873320..1f0bd4cac21015aaf6bc9c1df003439fed5e4d41 100644 (file)
@@ -1216,7 +1216,7 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
         D->isThisDeclarationADefinition()) {
       // Check for a function body.
       const FunctionDecl *Definition = 0;
-      if (Function->hasBody(Definition) &&
+      if (Function->isDefined(Definition) &&
           Definition->getTemplateSpecializationKind() == TSK_Undeclared) {
         SemaRef.Diag(Function->getLocation(), diag::err_redefinition) 
           << Function->getDeclName();
@@ -1248,7 +1248,7 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
         default:
           if (const FunctionDecl *RPattern
               = R->getTemplateInstantiationPattern())
-            if (RPattern->hasBody(RPattern)) {
+            if (RPattern->isDefined(RPattern)) {
               SemaRef.Diag(Function->getLocation(), diag::err_redefinition) 
                 << Function->getDeclName();
               SemaRef.Diag(R->getLocation(), diag::note_previous_definition);
@@ -2124,8 +2124,8 @@ TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D,
 bool
 TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
                                                     FunctionDecl *Tmpl) {
-  if (Tmpl->isDeleted())
-    New->setDeleted();
+  if (Tmpl->isDeletedAsWritten())
+    New->setDeletedAsWritten();
 
   // If we are performing substituting explicitly-specified template arguments
   // or deduced template arguments into a function template and we reach this
@@ -2300,7 +2300,7 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
                                          FunctionDecl *Function,
                                          bool Recursive,
                                          bool DefinitionRequired) {
-  if (Function->isInvalidDecl() || Function->hasBody())
+  if (Function->isInvalidDecl() || Function->isDefined())
     return;
 
   // Never instantiate an explicit specialization.
index 2b82f90ae316265c0d4b54ea3166e29cc08bf775..44b8c6a4a5734e0d516cc6615d9f9ac7d7cebdb8 100644 (file)
@@ -1385,7 +1385,7 @@ static bool isConsumerInterestedIn(Decl *D) {
     return Var->isFileVarDecl() &&
            Var->isThisDeclarationADefinition() == VarDecl::Definition;
   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
-    return Func->isThisDeclarationADefinition();
+    return Func->doesThisDeclarationHaveABody();
   return isa<ObjCProtocolDecl>(D) || isa<ObjCImplementationDecl>(D);
 }
 
index dbcbadb6864510f1305c1972cbbeb7a3ba55f933..8c1774a0302f3636275d4a2ee453c9763b2ebc3f 100644 (file)
@@ -128,8 +128,8 @@ void ASTDeclWriter::Visit(Decl *D) {
   // have been written. We want it last because we will not read it back when
   // retrieving it from the AST, we'll just lazily set the offset. 
   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-    Record.push_back(FD->isThisDeclarationADefinition());
-    if (FD->isThisDeclarationADefinition())
+    Record.push_back(FD->doesThisDeclarationHaveABody());
+    if (FD->doesThisDeclarationHaveABody())
       Writer.AddStmt(FD->getBody());
   }
 }
@@ -322,7 +322,7 @@ void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
   Record.push_back(D->isPure());
   Record.push_back(D->hasInheritedPrototype());
   Record.push_back(D->hasWrittenPrototype());
-  Record.push_back(D->isDeleted());
+  Record.push_back(D->isDeletedAsWritten());
   Record.push_back(D->isTrivial());
   Record.push_back(D->hasImplicitReturnZero());
   Writer.AddSourceLocation(D->getLocEnd(), Record);
index 63a591729944e27e7f994e9070ca771fd3fe58e9..a51d8e0d19aeeb849d659eaa8853d12279bc05a9 100644 (file)
@@ -97,7 +97,7 @@ public:
 void CFErrorFunctionChecker::checkASTDecl(const FunctionDecl *D,
                                         AnalysisManager &mgr,
                                         BugReporter &BR) const {
-  if (!D->isThisDeclarationADefinition())
+  if (!D->doesThisDeclarationHaveABody())
     return;
   if (!D->getResultType()->isVoidType())
     return;
index 54cbca08b981bc260206aaa0e8a1d8cc1b2ea352..ef7bc2016cdff009c61ac83646b786495b4d57dd 100644 (file)
@@ -132,7 +132,7 @@ void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *E,
   assert(CD);
   
 #if 0
-  if (!(CD->isThisDeclarationADefinition() && AMgr.shouldInlineCall()))
+  if (!(CD->doesThisDeclarationHaveABody() && AMgr.shouldInlineCall()))
     // FIXME: invalidate the object.
     return;
 #endif
@@ -246,7 +246,7 @@ void ExprEngine::VisitCXXDestructor(const CXXDestructorDecl *DD,
                                       const Stmt *S,
                                       ExplodedNode *Pred, 
                                       ExplodedNodeSet &Dst) {
-  if (!(DD->isThisDeclarationADefinition() && AMgr.shouldInlineCall()))
+  if (!(DD->doesThisDeclarationHaveABody() && AMgr.shouldInlineCall()))
     return;
   // Create the context for 'this' region.
   const StackFrameContext *SFC = AMgr.getStackFrame(DD,
index b3e12960042199124e2620b9be160ada11f21dd7..6a8965ceb5786d964754cb4bb9ab2f99d82fe69a 100644 (file)
@@ -7,9 +7,8 @@ void fn() = delete; // expected-note {{candidate function has been explicitly de
 void fn2(); // expected-note {{previous declaration is here}}
 void fn2() = delete; // expected-error {{deleted definition must be first declaration}}
 
-void fn3() = delete;
-void fn3() {
-  // FIXME: This definition should be invalid.
+void fn3() = delete; // expected-note {{previous definition is here}}
+void fn3() { // expected-error {{redefinition}}
 }
 
 void ov(int) {} // expected-note {{candidate function}}
index f17a415f44b2b3b44d5df6b9d43ff24440657093..eea078dd47fe7886352ac573e462fdd34af4ffa5 100644 (file)
@@ -790,7 +790,7 @@ bool CursorVisitor::VisitFunctionDecl(FunctionDecl *ND) {
     // FIXME: Attributes?
   }
   
-  if (ND->isThisDeclarationADefinition() && !ND->isLateTemplateParsed()) {
+  if (ND->doesThisDeclarationHaveABody() && !ND->isLateTemplateParsed()) {
     if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(ND)) {
       // Find the initializers that were written in the source.
       llvm::SmallVector<CXXCtorInitializer *, 4> WrittenInits;