[cfe-commits] r91014 - in /cfe/trunk: include/clang/Parse/Parser.h lib/Parse/ParseDeclCXX.cpp lib/Parse/ParseExprCXX.cpp lib/Parse/Parser.cpp lib/Parse/RAIIObjectsForParser.h

Chris Lattner sabre at nondot.org
Wed Dec 9 16:32:41 PST 2009


Author: lattner
Date: Wed Dec  9 18:32:41 2009
New Revision: 91014

URL: http://llvm.org/viewvc/llvm-project?rev=91014&view=rev
Log:
refactor the 'ColonIsSacred' argument to ParseOptionalCXXScopeSpecifier
to be a bool in Parser that is twiddled by the ColonProtectionRAIIObject
class.  No functionality change.

Modified:
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/ParseDeclCXX.cpp
    cfe/trunk/lib/Parse/ParseExprCXX.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=91014&r1=91013&r2=91014&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Wed Dec  9 18:32:41 2009
@@ -30,6 +30,7 @@
   class DiagnosticBuilder;
   class Parser;
   class PragmaUnusedHandler;
+  class ColonProtectionRAIIObject;
 
 /// PrettyStackTraceParserEntry - If a crash happens while the parser is active,
 /// an entry is printed for it.
@@ -47,6 +48,7 @@
 ///
 class Parser {
   friend class PragmaUnusedHandler;
+  friend class ColonProtectionRAIIObject;
   PrettyStackTraceParserEntry CrashInfo;
 
   Preprocessor &PP;
@@ -90,6 +92,12 @@
   /// template argument list, where the '>' closes the template
   /// argument list.
   bool GreaterThanIsOperator;
+  
+  /// ColonIsSacred - When this is false, we aggressively try to recover from
+  /// code like "foo : bar" as if it were a typo for "foo :: bar".  This is not
+  /// safe in case statements and a few other things.  This is managed by the
+  /// ColonProtectionRAIIObject RAII object.
+  bool ColonIsSacred;
 
   /// The "depth" of the template parameters currently being parsed.
   unsigned TemplateParameterDepth;
@@ -890,8 +898,7 @@
 
   bool ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
                                       TypeTy *ObjectType,
-                                      bool EnteringContext,
-                                      bool ColonIsSacred = false);
+                                      bool EnteringContext);
 
   //===--------------------------------------------------------------------===//
   // C++ 5.2p1: C++ Casts

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseDeclCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDeclCXX.cpp Wed Dec  9 18:32:41 2009
@@ -601,10 +601,14 @@
 
   // Parse the (optional) nested-name-specifier.
   CXXScopeSpec SS;
-  if (getLang().CPlusPlus &&
-      ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true, true))
-    if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
-      Diag(Tok, diag::err_expected_ident);
+  if (getLang().CPlusPlus) {
+    // "FOO : BAR" is not a potential typo for "FOO::BAR".
+    ColonProtectionRAIIObject X(*this);
+    
+    if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true))
+      if (Tok.isNot(tok::identifier) && Tok.isNot(tok::annot_template_id))
+        Diag(Tok, diag::err_expected_ident);
+  }
 
   TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
 

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Wed Dec  9 18:32:41 2009
@@ -45,14 +45,10 @@
 /// \param EnteringContext whether we will be entering into the context of
 /// the nested-name-specifier after parsing it.
 ///
-/// \param ColonIsSacred - If this is true, then a colon is valid after the
-/// specifier, so we should not try to recover from colons aggressively.
-///
 /// \returns true if a scope specifier was parsed.
 bool Parser::ParseOptionalCXXScopeSpecifier(CXXScopeSpec &SS,
                                             Action::TypeTy *ObjectType,
-                                            bool EnteringContext,
-                                            bool ColonIsSacred) {
+                                            bool EnteringContext) {
   assert(getLang().CPlusPlus &&
          "Call sites of this function should be guarded by checking for C++");
 

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

==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Wed Dec  9 18:32:41 2009
@@ -36,7 +36,8 @@
 
 Parser::Parser(Preprocessor &pp, Action &actions)
   : CrashInfo(*this), PP(pp), Actions(actions), Diags(PP.getDiagnostics()),
-    GreaterThanIsOperator(true), TemplateParameterDepth(0) {
+    GreaterThanIsOperator(true), ColonIsSacred(false),
+    TemplateParameterDepth(0) {
   Tok.setKind(tok::eof);
   CurScope = 0;
   NumCachedScopes = 0;

Modified: cfe/trunk/lib/Parse/RAIIObjectsForParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/RAIIObjectsForParser.h?rev=91014&r1=91013&r2=91014&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/RAIIObjectsForParser.h (original)
+++ cfe/trunk/lib/Parse/RAIIObjectsForParser.h Wed Dec  9 18:32:41 2009
@@ -36,6 +36,25 @@
       Diags.DecrementAllExtensionsSilenced();
     }
   };
-}
+  
+  // TODO: move GreaterThanIsOperatorScope here.
+  
+  /// ColonProtectionRAIIObject - This sets the Parser::ColonIsSacred bool and
+  /// restores it when destroyed.  This says that "foo:" should not be
+  /// considered a possible typo for "foo::" for error recovery purposes.
+  class ColonProtectionRAIIObject {
+    Parser &P;
+    bool OldVal;
+  public:
+    ColonProtectionRAIIObject(Parser &p) : P(p), OldVal(P.ColonIsSacred) {
+      P.ColonIsSacred = true;
+    }
+    
+    ~ColonProtectionRAIIObject() {
+      P.ColonIsSacred = OldVal;
+    }
+  };
+  
+} // end namespace clang
 
 #endif





More information about the cfe-commits mailing list