[cfe-commits] r106213 - in /cfe/trunk: include/clang/Parse/Parser.h lib/Parse/ParseDecl.cpp lib/Parse/ParseStmt.cpp lib/Parse/Parser.cpp lib/Parse/RAIIObjectsForParser.h

Argiris Kirtzidis akyrtzi at gmail.com
Thu Jun 17 03:52:18 PDT 2010


Author: akirtzidis
Date: Thu Jun 17 05:52:18 2010
New Revision: 106213

URL: http://llvm.org/viewvc/llvm-project?rev=106213&view=rev
Log:
Make sure parens/braces/brackets are correctly balanced.

In a line like:

(;

the semicolon leaves Parser:ParenCount unbalanced (it's 1 even though we stopped looking for a right paren).
This may affect later parsing and result in bad recovery for parsing errors.

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

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=106213&r1=106212&r2=106213&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Thu Jun 17 05:52:18 2010
@@ -72,6 +72,7 @@
 class Parser {
   friend class PragmaUnusedHandler;
   friend class ColonProtectionRAIIObject;
+  friend class ParenBraceBracketBalancer;
   PrettyStackTraceParserEntry CrashInfo;
 
   Preprocessor &PP;

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=106213&r1=106212&r2=106213&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Thu Jun 17 05:52:18 2010
@@ -309,6 +309,8 @@
 Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
                                                 SourceLocation &DeclEnd,
                                                 CXX0XAttributeList Attr) {
+  ParenBraceBracketBalancer BalancerRAIIObj(*this);
+  
   DeclPtrTy SingleDecl;
   switch (Tok.getKind()) {
   case tok::kw_template:

Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=106213&r1=106212&r2=106213&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Thu Jun 17 05:52:18 2010
@@ -77,6 +77,8 @@
 Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
   const char *SemiError = 0;
   OwningStmtResult Res(Actions);
+  
+  ParenBraceBracketBalancer BalancerRAIIObj(*this);
 
   CXX0XAttributeList Attr;
   if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier())

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=106213&r1=106212&r2=106213&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Thu Jun 17 05:52:18 2010
@@ -127,7 +127,16 @@
   }
   Diag(Tok, DID);
   Diag(LHSLoc, diag::note_matching) << LHSName;
-  SkipUntil(RHSTok);
+  if (!SkipUntil(RHSTok)) {
+    // We stopped before finding a RHS token, e.g. we encountered a ';'.
+    // Balance Paren/Brace/Bracket counting. 
+    switch (RHSTok) {
+    default: break;
+    case tok::r_paren : assert(ParenCount > 0); --ParenCount; break;
+    case tok::r_brace : assert(BraceCount > 0); --BraceCount; break;
+    case tok::r_square: assert(BracketCount > 0); --BracketCount; break;
+    }
+  }
   return R;
 }
 
@@ -401,6 +410,8 @@
 ///
 /// [C++0x/GNU] 'extern' 'template' declaration
 Parser::DeclGroupPtrTy Parser::ParseExternalDeclaration(CXX0XAttributeList Attr) {
+  ParenBraceBracketBalancer BalancerRAIIObj(*this);
+  
   DeclPtrTy SingleDecl;
   switch (Tok.getKind()) {
   case tok::semi:

Modified: cfe/trunk/lib/Parse/RAIIObjectsForParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/RAIIObjectsForParser.h?rev=106213&r1=106212&r2=106213&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/RAIIObjectsForParser.h (original)
+++ cfe/trunk/lib/Parse/RAIIObjectsForParser.h Thu Jun 17 05:52:18 2010
@@ -80,6 +80,23 @@
     }
   };
   
+  /// \brief RAII object that makes sure paren/bracket/brace count is correct
+  /// after declaration/statement parsing, even when there's a parsing error.
+  class ParenBraceBracketBalancer {
+    Parser &P;
+    unsigned short ParenCount, BracketCount, BraceCount;
+  public:
+    ParenBraceBracketBalancer(Parser &p)
+      : P(p), ParenCount(p.ParenCount), BracketCount(p.BracketCount),
+        BraceCount(p.BraceCount) { }
+    
+    ~ParenBraceBracketBalancer() {
+      P.ParenCount = ParenCount;
+      P.BracketCount = BracketCount;
+      P.BraceCount = BraceCount;
+    }
+  };
+  
 } // end namespace clang
 
 #endif





More information about the cfe-commits mailing list