[cfe-commits] r39281 - in /cfe/cfe/trunk: Parse/ParseDecl.cpp include/clang/Parse/Parser.h

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:42:26 PDT 2007


Author: sabre
Date: Wed Jul 11 11:42:25 2007
New Revision: 39281

URL: http://llvm.org/viewvc/llvm-project?rev=39281&view=rev
Log:
split the code for parsing a struct/union body out into its own method.

Modified:
    cfe/cfe/trunk/Parse/ParseDecl.cpp
    cfe/cfe/trunk/include/clang/Parse/Parser.h

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

==============================================================================
--- cfe/cfe/trunk/Parse/ParseDecl.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseDecl.cpp Wed Jul 11 11:42:25 2007
@@ -404,34 +404,13 @@
 ///       struct-or-union:
 ///         'struct'
 ///         'union'
-///       struct-contents:
-///         struct-declaration-list
-/// [EXT]   empty
-/// [GNU]   "struct-declaration-list" without terminatoring ';'   [TODO]
-///       struct-declaration-list:
-///         struct-declaration
-///         struct-declaration-list struct-declaration
-/// [OBC]   '@' 'defs' '(' class-name ')'                         [TODO]
-///       struct-declaration:
-///         specifier-qualifier-list struct-declarator-list ';'
-/// [GNU]   __extension__ struct-declaration                       [TODO]
-/// [GNU]   specifier-qualifier-list ';'                           [TODO]
-///       struct-declarator-list:
-///         struct-declarator
-///         struct-declarator-list ',' struct-declarator
-/// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
-///       struct-declarator:
-///         declarator
-/// [GNU]   declarator attributes[opt]
-///         declarator[opt] ':' constant-expression
-/// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
 ///
 void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
   assert((Tok.getKind() == tok::kw_struct ||
           Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
-  bool isUnion = Tok.getKind() == tok::kw_union;
   SourceLocation StartLoc = ConsumeToken();
-  DeclSpec::TST TagType = isUnion ? DeclSpec::TST_union : DeclSpec::TST_struct;
+  DeclSpec::TST TagType =
+    Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
 
   // If attributes exist after tag, parse them.
   if (Tok.getKind() == tok::kw___attribute)
@@ -467,88 +446,117 @@
   DeclTy *TagDecl =
     Actions.ParseTag(CurScope, TagType, isUse, StartLoc, Name, NameLoc);
   
-  if (Tok.getKind() == tok::l_brace) {
-    SourceLocation LBraceLoc = ConsumeBrace();
-
-    if (Tok.getKind() == tok::r_brace)
-      Diag(Tok, diag::ext_empty_struct_union_enum, isUnion ? "union":"struct");
+  // If there is a body, parse it and inform the actions module.
+  if (Tok.getKind() == tok::l_brace)
+    ParseStructUnionBody(TagType, TagDecl);
 
-    while (Tok.getKind() != tok::r_brace && 
-           Tok.getKind() != tok::eof) {
-      // Each iteration of this loop reads one struct-declaration.
+  const char *PrevSpec = 0;
+  if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec))
+    Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
+}
 
-      // Parse the common specifier-qualifiers-list piece.
-      DeclSpec DS;
-      SourceLocation SpecQualLoc = Tok.getLocation();
-      ParseSpecifierQualifierList(DS);
-      // TODO: Does specifier-qualifier list correctly check that *something* is
-      // specified?
-      
-      Declarator DeclaratorInfo(DS, Declarator::MemberContext);
-
-      // If there are no declarators, issue a warning.
-      if (Tok.getKind() == tok::semi) {
-        Diag(SpecQualLoc, diag::w_no_declarators);
-      } else {
-        // Read struct-declarators until we find the semicolon.
-        while (1) {
-          /// struct-declarator: declarator
-          /// struct-declarator: declarator[opt] ':' constant-expression
-          if (Tok.getKind() != tok::colon)
-            ParseDeclarator(DeclaratorInfo);
-          
-          if (Tok.getKind() == tok::colon) {
-            ConsumeToken();
-            ExprResult Res = ParseConstantExpression();
-            if (Res.isInvalid) {
-              SkipUntil(tok::semi, true, true);
-            } else {
-              // Process it.
-            }
-          }
-          
-          // If attributes exist after the declarator, parse them.
-          if (Tok.getKind() == tok::kw___attribute)
-            ParseAttributes();
 
-          // TODO: install declarator.
-          
-          // If we don't have a comma, it is either the end of the list (a ';')
-          // or an error, bail out.
-          if (Tok.getKind() != tok::comma)
-            break;
-          
-          // Consume the comma.
+/// ParseStructUnionBody
+///       struct-contents:
+///         struct-declaration-list
+/// [EXT]   empty
+/// [GNU]   "struct-declaration-list" without terminatoring ';'   [TODO]
+///       struct-declaration-list:
+///         struct-declaration
+///         struct-declaration-list struct-declaration
+/// [OBC]   '@' 'defs' '(' class-name ')'                         [TODO]
+///       struct-declaration:
+///         specifier-qualifier-list struct-declarator-list ';'
+/// [GNU]   __extension__ struct-declaration                       [TODO]
+/// [GNU]   specifier-qualifier-list ';'                           [TODO]
+///       struct-declarator-list:
+///         struct-declarator
+///         struct-declarator-list ',' struct-declarator
+/// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
+///       struct-declarator:
+///         declarator
+/// [GNU]   declarator attributes[opt]
+///         declarator[opt] ':' constant-expression
+/// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
+///
+void Parser::ParseStructUnionBody(unsigned TagType, DeclTy *TagDecl) {
+  SourceLocation LBraceLoc = ConsumeBrace();
+  
+  if (Tok.getKind() == tok::r_brace)
+    Diag(Tok, diag::ext_empty_struct_union_enum, 
+         DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
+  
+  while (Tok.getKind() != tok::r_brace && 
+         Tok.getKind() != tok::eof) {
+    // Each iteration of this loop reads one struct-declaration.
+    
+    // Parse the common specifier-qualifiers-list piece.
+    DeclSpec DS;
+    SourceLocation SpecQualLoc = Tok.getLocation();
+    ParseSpecifierQualifierList(DS);
+    // TODO: Does specifier-qualifier list correctly check that *something* is
+    // specified?
+    
+    Declarator DeclaratorInfo(DS, Declarator::MemberContext);
+    
+    // If there are no declarators, issue a warning.
+    if (Tok.getKind() == tok::semi) {
+      Diag(SpecQualLoc, diag::w_no_declarators);
+    } else {
+      // Read struct-declarators until we find the semicolon.
+      while (1) {
+        /// struct-declarator: declarator
+        /// struct-declarator: declarator[opt] ':' constant-expression
+        if (Tok.getKind() != tok::colon)
+          ParseDeclarator(DeclaratorInfo);
+        
+        if (Tok.getKind() == tok::colon) {
           ConsumeToken();
-          
-          // Parse the next declarator.
-          DeclaratorInfo.clear();
-
-          // Attributes are only allowed on the second declarator.
-          if (Tok.getKind() == tok::kw___attribute)
-            ParseAttributes();
+          ExprResult Res = ParseConstantExpression();
+          if (Res.isInvalid) {
+            SkipUntil(tok::semi, true, true);
+          } else {
+            // Process it.
+          }
         }
-      }
-      
-      if (Tok.getKind() == tok::semi) {
+        
+        // If attributes exist after the declarator, parse them.
+        if (Tok.getKind() == tok::kw___attribute)
+          ParseAttributes();
+        
+        // TODO: install declarator.
+        
+        // If we don't have a comma, it is either the end of the list (a ';')
+        // or an error, bail out.
+        if (Tok.getKind() != tok::comma)
+          break;
+        
+        // Consume the comma.
         ConsumeToken();
-      } else {
-        Diag(Tok, diag::err_expected_semi_decl_list);
-        // Skip to end of block or statement
-        SkipUntil(tok::r_brace, true, true);
+        
+        // Parse the next declarator.
+        DeclaratorInfo.clear();
+        
+        // Attributes are only allowed on the second declarator.
+        if (Tok.getKind() == tok::kw___attribute)
+          ParseAttributes();
       }
     }
-
-    MatchRHSPunctuation(tok::r_brace, LBraceLoc);
     
-    // If attributes exist after struct contents, parse them.
-    if (Tok.getKind() == tok::kw___attribute)
-      ParseAttributes();
+    if (Tok.getKind() == tok::semi) {
+      ConsumeToken();
+    } else {
+      Diag(Tok, diag::err_expected_semi_decl_list);
+      // Skip to end of block or statement
+      SkipUntil(tok::r_brace, true, true);
+    }
   }
-
-  const char *PrevSpec = 0;
-  if (DS.SetTypeSpecType(TagType, StartLoc, PrevSpec))
-    Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
+  
+  MatchRHSPunctuation(tok::r_brace, LBraceLoc);
+  
+  // If attributes exist after struct contents, parse them.
+  if (Tok.getKind() == tok::kw___attribute)
+    ParseAttributes();
 }
 
 

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

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:42:25 2007
@@ -333,6 +333,7 @@
 
   void ParseEnumSpecifier(DeclSpec &DS);
   void ParseStructUnionSpecifier(DeclSpec &DS);
+  void ParseStructUnionBody(unsigned TagType, DeclTy *TagDecl);
 
   bool isDeclarationSpecifier() const;
   bool isTypeSpecifierQualifier() const;





More information about the cfe-commits mailing list