[cfe-commits] r72555 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Parse/Action.h include/clang/Parse/DeclSpec.h lib/Frontend/PrintParserCallbacks.cpp lib/Parse/DeclSpec.cpp lib/Parse/ParseDecl.cpp lib/Parse/ParseDeclCXX.cpp lib/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaTemplate.cpp lib/Sema/SemaType.cpp test/SemaCXX/type-definition-in-specifier.cpp

Douglas Gregor dgregor at apple.com
Thu May 28 16:31:59 PDT 2009


Author: dgregor
Date: Thu May 28 18:31:59 2009
New Revision: 72555

URL: http://llvm.org/viewvc/llvm-project?rev=72555&view=rev
Log:
When we parse a tag specifier, keep track of whether that tag
specifier resulted in the creation of a new TagDecl node, which
happens either when the tag specifier was a definition or when the tag
specifier was the first declaration of that tag type. This information
has several uses, the first of which is implemented in this commit:

  1) In C++, one is not allowed to define tag types within a type
  specifier (e.g., static_cast<struct S { int x; } *>(0) is
  ill-formed) or within the result or parameter types of a
  function. We now diagnose this.

  2) We can extend DeclGroups to contain information about any tags
  that are declared/defined within the declaration specifiers of a
  variable, e.g.,

    struct Point { int x, y, z; } p;

  This will help improve AST printing and template instantiation,
  among other things.

  3) For C99, we can keep track of whether a tag type is defined
  within the type of a parameter, to properly cope with cases like,
  e.g.,

    int bar(struct T2 { int x; } y) {
      struct T2 z;
    }

  We can also do similar things wherever there is a type specifier,
  e.g., to keep track of where the definition of S occurs in this
  legal C99 code:

    (struct S { int x, y; } *)0

  


Added:
    cfe/trunk/test/SemaCXX/type-definition-in-specifier.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/include/clang/Parse/DeclSpec.h
    cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp
    cfe/trunk/lib/Parse/DeclSpec.cpp
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/lib/Parse/ParseDeclCXX.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/lib/Sema/SemaType.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu May 28 18:31:59 2009
@@ -253,6 +253,13 @@
 def err_allocation_of_abstract_type : Error<
   "allocation of an object of abstract type %0">;
   
+def err_type_defined_in_type_specifier : Error<
+  "%0 can not be defined in a type specifier">;
+def err_type_defined_in_result_type : Error<
+  "%0 can not be defined in the result type of a function">;
+def err_type_defined_in_param_type : Error<
+  "%0 can not be defined in a parameter type">;
+
 def note_pure_virtual_function : Note<
   "pure virtual function %0">;
 

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Thu May 28 18:31:59 2009
@@ -397,7 +397,8 @@
   virtual DeclPtrTy ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
                              SourceLocation KWLoc, const CXXScopeSpec &SS,
                              IdentifierInfo *Name, SourceLocation NameLoc,
-                             AttributeList *Attr, AccessSpecifier AS) {
+                             AttributeList *Attr, AccessSpecifier AS,
+                             bool &OwnedDecl) {
     // TagType is an instance of DeclSpec::TST, indicating what kind of tag this
     // is (struct/union/enum/class).
     return DeclPtrTy();

Modified: cfe/trunk/include/clang/Parse/DeclSpec.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/DeclSpec.h?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/DeclSpec.h (original)
+++ cfe/trunk/include/clang/Parse/DeclSpec.h Thu May 28 18:31:59 2009
@@ -114,7 +114,8 @@
   /*TSC*/unsigned TypeSpecComplex : 2;
   /*TSS*/unsigned TypeSpecSign : 2;
   /*TST*/unsigned TypeSpecType : 5;
-  
+  bool TypeSpecOwned : 1;
+
   // type-qualifiers
   unsigned TypeQualifiers : 3;  // Bitwise OR of TQ.
   
@@ -129,7 +130,7 @@
   /// TypeRep - This contains action-specific information about a specific TST.
   /// For example, for a typedef or struct, it might contain the declaration for
   /// these.
-  ActionBase::TypeTy *TypeRep;  
+  void *TypeRep;  
   
   // attributes.
   AttributeList *AttrList;
@@ -168,6 +169,7 @@
       TypeSpecComplex(TSC_unspecified),
       TypeSpecSign(TSS_unspecified),
       TypeSpecType(TST_unspecified),
+      TypeSpecOwned(false),
       TypeQualifiers(TSS_unspecified),
       FS_inline_specified(false),
       FS_virtual_specified(false),
@@ -201,6 +203,7 @@
   TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
   TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
   TST getTypeSpecType() const { return (TST)TypeSpecType; }
+  bool isTypeSpecOwned() const { return TypeSpecOwned; }
   void *getTypeRep() const { return TypeRep; }
   
   const SourceRange &getSourceRange() const { return Range; }
@@ -272,7 +275,7 @@
   bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec);
   bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec);
   bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
-                       void *Rep = 0);
+                       void *Rep = 0, bool Owned = false);
   bool SetTypeSpecError();
 
   bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,

Modified: cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintParserCallbacks.cpp Thu May 28 18:31:59 2009
@@ -195,7 +195,8 @@
     virtual DeclPtrTy ActOnTag(Scope *S, unsigned TagType, TagKind TK,
                                SourceLocation KWLoc, const CXXScopeSpec &SS,
                                IdentifierInfo *Name, SourceLocation NameLoc,
-                               AttributeList *Attr, AccessSpecifier AS) {
+                               AttributeList *Attr, AccessSpecifier AS,
+                               bool &Owned) {
       // TagType is an instance of DeclSpec::TST, indicating what kind of tag this
       // is (struct/union/enum/class).
       Out << __FUNCTION__ << "\n";

Modified: cfe/trunk/lib/Parse/DeclSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/DeclSpec.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/DeclSpec.cpp (original)
+++ cfe/trunk/lib/Parse/DeclSpec.cpp Thu May 28 18:31:59 2009
@@ -241,12 +241,14 @@
 }
 
 bool DeclSpec::SetTypeSpecType(TST T, SourceLocation Loc,
-                               const char *&PrevSpec, void *Rep) {
+                               const char *&PrevSpec, void *Rep,
+                               bool Owned) {
   if (TypeSpecType != TST_unspecified)
     return BadSpecifier((TST)TypeSpecType, PrevSpec);
   TypeSpecType = T;
   TypeRep = Rep;
   TSTLoc = Loc;
+  TypeSpecOwned = Owned;
   return false;
 }
 

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Thu May 28 18:31:59 2009
@@ -1460,8 +1460,10 @@
     TK = Action::TK_Declaration;
   else
     TK = Action::TK_Reference;
+  bool Owned = false;
   DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TK,
-                                       StartLoc, SS, Name, NameLoc, Attr, AS);
+                                       StartLoc, SS, Name, NameLoc, Attr, AS,
+                                       Owned);
   
   if (Tok.is(tok::l_brace))
     ParseEnumBody(StartLoc, TagDecl);
@@ -1469,7 +1471,7 @@
   // TODO: semantic analysis on the declspec for enums.
   const char *PrevSpec = 0;
   if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec,
-                         TagDecl.getAs<void>()))
+                         TagDecl.getAs<void>(), Owned))
     Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
 }
 

Modified: cfe/trunk/lib/Parse/ParseDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDeclCXX.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Thu May 28 18:31:59 2009
@@ -479,6 +479,7 @@
   // FIXME: When TK == TK_Reference and we have a template-id, we need
   // to turn that template-id into a type.
 
+  bool Owned = false;
   if (TemplateId && TK != Action::TK_Reference) {
     // Explicit specialization, class template partial specialization,
     // or explicit instantiation.
@@ -582,7 +583,7 @@
 
     // Declaration or definition of a class type
     TagOrTempResult = Actions.ActOnTag(CurScope, TagType, TK, StartLoc, SS, 
-                                       Name, NameLoc, Attr, AS);
+                                       Name, NameLoc, Attr, AS, Owned);
   }
 
   // Parse the optional base clause (C++ only).
@@ -608,7 +609,7 @@
   }
   
   if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec, 
-                         TagOrTempResult.get().getAs<void>()))
+                         TagOrTempResult.get().getAs<void>(), Owned))
     Diag(StartLoc, diag::err_invalid_decl_spec_combination) << PrevSpec;
   
   if (DS.isFriendSpecified())

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Thu May 28 18:31:59 2009
@@ -346,7 +346,8 @@
                              QualType *ParamTypes, unsigned NumParamTypes,
                              bool Variadic, unsigned Quals,
                              SourceLocation Loc, DeclarationName Entity);
-  QualType GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip = 0);
+  QualType GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip = 0,
+                                TagDecl **OwnedDecl = 0);
   DeclarationName GetNameForDeclarator(Declarator &D);
 
   QualType ObjCGetTypeForMethodDefinition(DeclPtrTy D);
@@ -444,7 +445,8 @@
   virtual DeclPtrTy ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
                              SourceLocation KWLoc, const CXXScopeSpec &SS,
                              IdentifierInfo *Name, SourceLocation NameLoc,
-                             AttributeList *Attr, AccessSpecifier AS);
+                             AttributeList *Attr, AccessSpecifier AS,
+                             bool &OwnedDecl);
   
   virtual void ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
                          IdentifierInfo *ClassName,

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu May 28 18:31:59 2009
@@ -2835,8 +2835,16 @@
   if (getLangOptions().CPlusPlus)
     CheckExtraCXXDefaultArguments(D);
  
-  QualType parmDeclType = GetTypeForDeclarator(D, S);
+  TagDecl *OwnedDecl = 0;
+  QualType parmDeclType = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedDecl);
   
+  if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
+    // C++ [dcl.fct]p6:
+    //   Types shall not be defined in return or parameter types.
+    Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
+      << Context.getTypeDeclType(OwnedDecl);
+  }
+
   // TODO: CHECK FOR CONFLICTS, multiple decls with same name in one scope.
   // Can this happen for params?  We already checked that they don't conflict
   // among each other.  Here they can only shadow globals, which is ok.
@@ -3316,11 +3324,13 @@
 Sema::DeclPtrTy Sema::ActOnTag(Scope *S, unsigned TagSpec, TagKind TK,
                                SourceLocation KWLoc, const CXXScopeSpec &SS,
                                IdentifierInfo *Name, SourceLocation NameLoc,
-                               AttributeList *Attr, AccessSpecifier AS) {
+                               AttributeList *Attr, AccessSpecifier AS,
+                               bool &OwnedDecl) {
   // If this is not a definition, it must have a name.
   assert((Name != 0 || TK == TK_Definition) &&
          "Nameless record must be a definition!");
 
+  OwnedDecl = false;
   TagDecl::TagKind Kind;
   switch (TagSpec) {
   default: assert(0 && "Unknown tag type!");
@@ -3645,6 +3655,7 @@
     CurContext->addDecl(Context, New);
   }
 
+  OwnedDecl = true;
   return DeclPtrTy::make(New);
 }
 

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Thu May 28 18:31:59 2009
@@ -2357,8 +2357,9 @@
                                  SourceLocation NameLoc,
                                  AttributeList *Attr) {
 
+  bool Owned = false;
   DeclPtrTy TagD = ActOnTag(S, TagSpec, Action::TK_Reference,
-                            KWLoc, SS, Name, NameLoc, Attr, AS_none);
+                            KWLoc, SS, Name, NameLoc, Attr, AS_none, Owned);
   if (!TagD)
     return true;
 

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=72555&r1=72554&r2=72555&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Thu May 28 18:31:59 2009
@@ -604,7 +604,12 @@
 /// GetTypeForDeclarator - Convert the type for the specified
 /// declarator to Type instances. Skip the outermost Skip type
 /// objects.
-QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip) {
+///
+/// If OwnedDecl is non-NULL, and this declarator's decl-specifier-seq
+/// owns the declaration of a type (e.g., the definition of a struct
+/// type), then *OwnedDecl will receive the owned declaration.
+QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S, unsigned Skip,
+                                    TagDecl **OwnedDecl) {
   bool OmittedReturnType = false;
 
   if (D.getContext() == Declarator::BlockLiteralContext
@@ -637,6 +642,8 @@
       T = ConvertDeclSpecToType(DS, D.getIdentifierLoc(), isInvalid);
       if (isInvalid)
         D.setInvalidType(true);
+      else if (OwnedDecl && DS.isTypeSpecOwned())
+        *OwnedDecl = cast<TagDecl>((Decl *)DS.getTypeRep());
     }
     break;
   }
@@ -717,6 +724,15 @@
         D.setInvalidType(true);
       }
 
+      if (getLangOptions().CPlusPlus && D.getDeclSpec().isTypeSpecOwned()) {
+        // C++ [dcl.fct]p6:
+        //   Types shall not be defined in return or parameter types.
+        TagDecl *Tag = cast<TagDecl>((Decl *)D.getDeclSpec().getTypeRep());
+        if (Tag->isDefinition())
+          Diag(Tag->getLocation(), diag::err_type_defined_in_result_type)
+            << Context.getTypeDeclType(Tag);
+      }
+
       if (FTI.NumArgs == 0) {
         if (getLangOptions().CPlusPlus) {
           // C++ 8.3.5p2: If the parameter-declaration-clause is empty, the
@@ -971,14 +987,24 @@
   // the parser.
   assert(D.getIdentifier() == 0 && "Type name should have no identifier!");
   
-  QualType T = GetTypeForDeclarator(D, S);
+  TagDecl *OwnedTag = 0;
+  QualType T = GetTypeForDeclarator(D, S, /*Skip=*/0, &OwnedTag);
   if (D.isInvalidType())
     return true;
 
-  // Check that there are no default arguments (C++ only).
-  if (getLangOptions().CPlusPlus)
+  if (getLangOptions().CPlusPlus) {
+    // Check that there are no default arguments (C++ only).
     CheckExtraCXXDefaultArguments(D);
 
+    // C++0x [dcl.type]p3:
+    //   A type-specifier-seq shall not define a class or enumeration
+    //   unless it appears in the type-id of an alias-declaration
+    //   (7.1.3).
+    if (OwnedTag && OwnedTag->isDefinition())
+      Diag(OwnedTag->getLocation(), diag::err_type_defined_in_type_specifier)
+        << Context.getTypeDeclType(OwnedTag);
+  }
+
   return T.getAsOpaquePtr();
 }
 

Added: cfe/trunk/test/SemaCXX/type-definition-in-specifier.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/type-definition-in-specifier.cpp?rev=72555&view=auto

==============================================================================
--- cfe/trunk/test/SemaCXX/type-definition-in-specifier.cpp (added)
+++ cfe/trunk/test/SemaCXX/type-definition-in-specifier.cpp Thu May 28 18:31:59 2009
@@ -0,0 +1,25 @@
+// RUN: clang-cc -fsyntax-only -verify %s
+
+struct S0;
+struct S1;
+struct S2;
+struct S3;
+struct S4;
+struct S5;
+struct S6;
+
+struct S0 { int x; };
+
+void f0() {
+  typedef struct S1 { int x; } S1_typedef;
+
+  (void)((struct S2 { int x; }*)0); // expected-error{{can not be defined}}
+
+  struct S3 { int x; } s3;
+
+  (void)static_cast<struct S4 { int x; } *>(0); // expected-error{{can not be defined}}
+}
+
+struct S5 { int x; } f1() { return S5(); } // expected-error{{result type}}
+
+void f2(struct S6 { int x; } p); // expected-error{{parameter type}}





More information about the cfe-commits mailing list