[cfe-commits] r39298 - 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:41 PDT 2007


Author: sabre
Date: Wed Jul 11 11:42:40 2007
New Revision: 39298

URL: http://llvm.org/viewvc/llvm-project?rev=39298&view=rev
Log:
refactor tag processing into a new ParseTag method.  Share this between
structs and enums.

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=39298&r1=39297&r2=39298&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseDecl.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseDecl.cpp Wed Jul 11 11:42:40 2007
@@ -393,36 +393,21 @@
   }
 }
 
-
-/// ParseStructUnionSpecifier
-///       struct-or-union-specifier: [C99 6.7.2.1]
-///         struct-or-union identifier[opt] '{' struct-contents '}'
-///         struct-or-union identifier
-/// [GNU]   struct-or-union attributes[opt] identifier[opt] '{' struct-contents
-///                                                         '}' attributes[opt]
-/// [GNU]   struct-or-union attributes[opt] identifier
-///       struct-or-union:
-///         'struct'
-///         'union'
-///
-void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
-  assert((Tok.getKind() == tok::kw_struct ||
-          Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
-  DeclSpec::TST TagType =
-    Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
-
-  SourceLocation StartLoc = ConsumeToken();
-
+/// ParseTag - Parse "struct-or-union-or-class-or-enum identifier[opt]", where
+/// the first token has already been read and has been turned into an instance
+/// of DeclSpec::TST (TagType).  This returns true if there is an error parsing,
+/// otherwise it returns false and fills in Decl.
+bool Parser::ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc){
   // If attributes exist after tag, parse them.
   if (Tok.getKind() == tok::kw___attribute)
     ParseAttributes();
-
+  
   // Must have either 'struct name' or 'struct {...}'.
   if (Tok.getKind() != tok::identifier &&
       Tok.getKind() != tok::l_brace) {
     Diag(Tok, diag::err_expected_ident_lbrace);
     // TODO: better error recovery here.
-    return;
+    return true;
   }
   
   // If an identifier is present, consume and remember it.
@@ -448,8 +433,33 @@
     TK = Action::TK_Declaration;
   else
     TK = Action::TK_Reference;
-  DeclTy *TagDecl =
-    Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc);
+  Decl = Actions.ParseTag(CurScope, TagType, TK, StartLoc, Name, NameLoc);
+  return false;
+}
+
+
+/// ParseStructUnionSpecifier
+///       struct-or-union-specifier: [C99 6.7.2.1]
+///         struct-or-union identifier[opt] '{' struct-contents '}'
+///         struct-or-union identifier
+/// [GNU]   struct-or-union attributes[opt] identifier[opt] '{' struct-contents
+///                                                         '}' attributes[opt]
+/// [GNU]   struct-or-union attributes[opt] identifier
+///       struct-or-union:
+///         'struct'
+///         'union'
+///
+void Parser::ParseStructUnionSpecifier(DeclSpec &DS) {
+  assert((Tok.getKind() == tok::kw_struct ||
+          Tok.getKind() == tok::kw_union) && "Not a struct/union specifier");
+  DeclSpec::TST TagType =
+    Tok.getKind() == tok::kw_union ? DeclSpec::TST_union : DeclSpec::TST_struct;
+  SourceLocation StartLoc = ConsumeToken();
+
+  // Parse the tag portion of this.
+  DeclTy *TagDecl;
+  if (ParseTag(TagDecl, TagType, StartLoc))
+    return;
   
   // If there is a body, parse it and inform the actions module.
   if (Tok.getKind() == tok::l_brace)
@@ -600,18 +610,10 @@
   assert(Tok.getKind() == tok::kw_enum && "Not an enum specifier");
   SourceLocation StartLoc = ConsumeToken();
   
-  if (Tok.getKind() == tok::kw___attribute)
-    ParseAttributes();
-  
-  // Must have either 'enum name' or 'enum {...}'.
-  if (Tok.getKind() != tok::identifier &&
-      Tok.getKind() != tok::l_brace) {
-    Diag(Tok, diag::err_expected_ident_lbrace);
+  // Parse the tag portion of this.
+  DeclTy *TagDecl;
+  if (ParseTag(TagDecl, DeclSpec::TST_enum, StartLoc))
     return;
-  }
-  
-  if (Tok.getKind() == tok::identifier)
-    ConsumeToken();
   
   if (Tok.getKind() == tok::l_brace) {
     SourceLocation LBraceLoc = ConsumeBrace();
@@ -648,7 +650,7 @@
   
   
   const char *PrevSpec = 0;
-  if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec))
+  if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, TagDecl))
     Diag(StartLoc, diag::err_invalid_decl_spec_combination, PrevSpec);
 }
 

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=39298&r1=39297&r2=39298&view=diff

==============================================================================
--- cfe/cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/cfe/trunk/include/clang/Parse/Parser.h Wed Jul 11 11:42:40 2007
@@ -331,6 +331,7 @@
   void ParseDeclarationSpecifiers(DeclSpec &DS);
   void ParseSpecifierQualifierList(DeclSpec &DS);
 
+  bool ParseTag(DeclTy *&Decl, unsigned TagType, SourceLocation StartLoc);
   void ParseEnumSpecifier(DeclSpec &DS);
   void ParseStructUnionSpecifier(DeclSpec &DS);
   void ParseStructUnionBody(SourceLocation StartLoc, unsigned TagType,





More information about the cfe-commits mailing list