[cfe-dev] parse C++ throw

Chris Lattner clattner at apple.com
Mon Feb 25 14:45:57 PST 2008


On Feb 25, 2008, at 2:19 PM, Mike Stump wrote:
> I just copied CXXCastExpr, which didn't have it.  I'll submit a  
> patch for that next.

Thanks Mike!

Ok.  A couple new things:

--- ./Parse/ParseExprCXX.cpp.~1~	2008-02-25 13:35:37.000000000 -0800
+++ ./Parse/ParseExprCXX.cpp	2008-02-25 13:35:57.000000000 -0800
@@ -1,3 +1,4 @@
+/* -*-  indent-tabs-mode:nil;  -*- */

Please don't do this.


+  class CXXThrowExpr : public Expr {
...
+    virtual SourceRange getSourceRange() const {
+      return SourceRange(ThrowLoc, getSubExpr()- 
 >getSourceRange().getEnd());
+    }

If getSubExpr() is null, this should return SourceRange(ThrowLoc,  
ThrowLoc).


+++ ./AST/StmtPrinter.cpp	2008-02-25 13:35:57.000000000 -0800
@@ -780,6 +780,12 @@ void StmtPrinter::VisitCXXBoolLiteralExp
    OS << (Node->getValue() ? "true" : "false");
  }

+void StmtPrinter::VisitCXXThrowExpr(CXXThrowExpr *Node) {
+  OS << "throw ";
+  if (Node->getSubExpr())
+    PrintExpr(Node->getSubExpr());

It would be slightly nicer to not print the space after the throw if  
subexpr is null.


> I also added a FIXME for handling throw when not followed by a ';'  
> nor an assignment-expression.  Something parser generators do  
> automagically for us, but something we have to compute.  Do we have  
> a tentative parse system yet or some other easy way to do this?  [ I  
> think I know the answer, I bet not. ]

The fixme is fine for a first step.  However, it would be better to  
eventually add a predicate that determines whether a token is the  
start of an expression.  I don't think there are any cases where a  
declaration is allowed after a throw, so this predicate should be  
relatively straight-forward, something like  
Parser::isDeclarationSpecifier().

I agree this is somewhat ugly, but it could be worse

-Chris



More information about the cfe-dev mailing list