From: Steve Naroff Date: Fri, 31 Aug 2007 23:28:33 +0000 (+0000) Subject: Move SourceRange protocol from Expr->Stmt. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b5a69586f1b8855ee4c1f0bb7a8f0ff4fe32ce09;p=clang Move SourceRange protocol from Expr->Stmt. Converted many of the statements over. Still a hanful left... git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41663 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Sema/SemaStmt.cpp b/Sema/SemaStmt.cpp index bec7f892df..1c26202c76 100644 --- a/Sema/SemaStmt.cpp +++ b/Sema/SemaStmt.cpp @@ -61,7 +61,6 @@ Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, Diag(D->getLocation(), diag::ext_mixed_decls_code); } } - // Warn about unused expressions in statements. for (unsigned i = 0; i != NumElts; ++i) { Expr *E = dyn_cast(Elts[i]); @@ -88,7 +87,7 @@ Sema::ParseCompoundStmt(SourceLocation L, SourceLocation R, Diag(E->getExprLoc(), diag::warn_unused_expr, E->getSourceRange()); } - return new CompoundStmt(Elts, NumElts); + return new CompoundStmt(Elts, NumElts, L, R); } Action::StmtResult @@ -119,7 +118,7 @@ Sema::ParseCaseStmt(SourceLocation CaseLoc, ExprTy *lhsval, return SubStmt; } - CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt); + CaseStmt *CS = new CaseStmt(LHSVal, RHSVal, SubStmt, CaseLoc); SwitchStack.back()->addSwitchCase(CS); return CS; } @@ -182,7 +181,7 @@ Sema::ParseIfStmt(SourceLocation IfLoc, ExprTy *CondVal, return Diag(IfLoc, diag::err_typecheck_statement_requires_scalar, condType.getAsString(), condExpr->getSourceRange()); - return new IfStmt(condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal); + return new IfStmt(IfLoc, condExpr, (Stmt*)ThenVal, (Stmt*)ElseVal); } Action::StmtResult @@ -445,7 +444,7 @@ Sema::ParseWhileStmt(SourceLocation WhileLoc, ExprTy *Cond, StmtTy *Body) { return Diag(WhileLoc, diag::err_typecheck_statement_requires_scalar, condType.getAsString(), condExpr->getSourceRange()); - return new WhileStmt(condExpr, (Stmt*)Body); + return new WhileStmt(condExpr, (Stmt*)Body, WhileLoc); } Action::StmtResult @@ -461,7 +460,7 @@ Sema::ParseDoStmt(SourceLocation DoLoc, StmtTy *Body, return Diag(DoLoc, diag::err_typecheck_statement_requires_scalar, condType.getAsString(), condExpr->getSourceRange()); - return new DoStmt((Stmt*)Body, condExpr); + return new DoStmt((Stmt*)Body, condExpr, DoLoc); } Action::StmtResult @@ -493,7 +492,7 @@ Sema::ParseForStmt(SourceLocation ForLoc, SourceLocation LParenLoc, return Diag(ForLoc, diag::err_typecheck_statement_requires_scalar, SecondType.getAsString(), Second->getSourceRange()); } - return new ForStmt(First, Second, Third, Body); + return new ForStmt(First, Second, Third, Body, ForLoc); } diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 44b97a942f..8f5247af18 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -40,8 +40,6 @@ public: /// value objects created/interpreted by SourceManager. We assume AST /// clients will have a pointer to the respective SourceManager. virtual SourceRange getSourceRange() const = 0; - SourceLocation getLocStart() const { return getSourceRange().Begin(); } - SourceLocation getLocEnd() const { return getSourceRange().End(); } /// getExprLoc - Return the preferred location for the arrow when diagnosing /// a problem with a generic expression. diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index de430a9193..35f4bf57e6 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -49,6 +49,13 @@ public: StmtClass getStmtClass() const { return sClass; } const char *getStmtClassName() const; + + /// SourceLocation tokens are not useful in isolation - they are low level + /// value objects created/interpreted by SourceManager. We assume AST + /// clients will have a pointer to the respective SourceManager. + virtual SourceRange getSourceRange() const = 0; + SourceLocation getLocStart() const { return getSourceRange().Begin(); } + SourceLocation getLocEnd() const { return getSourceRange().End(); } // global temp stats (until we have a per-module visitor) static void addStmtClass(const StmtClass s); @@ -124,6 +131,8 @@ public: const Decl *getDecl() const { return TheDecl; } Decl *getDecl() { return TheDecl; } + + virtual SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == DeclStmtClass; @@ -143,6 +152,8 @@ public: NullStmt(SourceLocation L) : Stmt(NullStmtClass), SemiLoc(L) {} SourceLocation getSemiLoc() const { return SemiLoc; } + + virtual SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == NullStmtClass; @@ -158,9 +169,12 @@ public: /// class CompoundStmt : public Stmt { llvm::SmallVector Body; + SourceLocation LBracLoc, RBracLoc; public: - CompoundStmt(Stmt **StmtStart, unsigned NumStmts) - : Stmt(CompoundStmtClass), Body(StmtStart, StmtStart+NumStmts) {} + CompoundStmt(Stmt **StmtStart, unsigned NumStmts, + SourceLocation LB, SourceLocation RB) + : Stmt(CompoundStmtClass), Body(StmtStart, StmtStart+NumStmts), + LBracLoc(LB), RBracLoc(RB) {} bool body_empty() const { return Body.empty(); } @@ -184,7 +198,10 @@ public: const_reverse_body_iterator body_rend() const { return Body.rend(); } void push_back(Stmt *S) { Body.push_back(S); } - + + virtual SourceRange getSourceRange() const { + return SourceRange(LBracLoc, RBracLoc); + } static bool classof(const Stmt *T) { return T->getStmtClass() == CompoundStmtClass; } @@ -212,6 +229,8 @@ public: virtual Stmt* v_getSubStmt() = 0; Stmt *getSubStmt() { return v_getSubStmt(); } + + virtual SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == CaseStmtClass || @@ -224,12 +243,14 @@ class CaseStmt : public SwitchCase { enum { SUBSTMT, LHS, RHS, END_EXPR }; Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for // GNU "case 1 ... 4" extension + SourceLocation CaseLoc; public: - CaseStmt(Expr *lhs, Expr *rhs, Stmt *substmt) + CaseStmt(Expr *lhs, Expr *rhs, Stmt *substmt, SourceLocation caseLoc) : SwitchCase(CaseStmtClass) { SubExprs[SUBSTMT] = substmt; SubExprs[LHS] = reinterpret_cast(lhs); SubExprs[RHS] = reinterpret_cast(rhs); + CaseLoc = caseLoc; } Expr *getLHS() { return reinterpret_cast(SubExprs[LHS]); } @@ -237,6 +258,9 @@ public: Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } virtual Stmt* v_getSubStmt() { return getSubStmt(); } + virtual SourceRange getSourceRange() const { + return SourceRange(CaseLoc, SubExprs[SUBSTMT]->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == CaseStmtClass; } @@ -253,12 +277,15 @@ class DefaultStmt : public SwitchCase { public: DefaultStmt(SourceLocation DL, Stmt *substmt) : SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL) {} - + Stmt *getSubStmt() { return SubStmt; } virtual Stmt* v_getSubStmt() { return getSubStmt(); } SourceLocation getDefaultLoc() const { return DefaultLoc; } + virtual SourceRange getSourceRange() const { + return SourceRange(DefaultLoc, SubStmt->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == DefaultStmtClass; } @@ -270,12 +297,13 @@ public: }; class LabelStmt : public Stmt { - SourceLocation IdentLoc; IdentifierInfo *Label; Stmt *SubStmt; + SourceLocation IdentLoc; public: - LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) - : Stmt(LabelStmtClass), IdentLoc(IL), Label(label), SubStmt(substmt) {} + LabelStmt(SourceLocation IL, IdentifierInfo *label, Stmt *substmt) + : Stmt(LabelStmtClass), Label(label), + SubStmt(substmt), IdentLoc(IL) {} SourceLocation getIdentLoc() const { return IdentLoc; } IdentifierInfo *getID() const { return Label; } @@ -285,7 +313,10 @@ public: void setIdentLoc(SourceLocation L) { IdentLoc = L; } void setSubStmt(Stmt *SS) { SubStmt = SS; } - + + virtual SourceRange getSourceRange() const { + return SourceRange(IdentLoc, SubStmt->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == LabelStmtClass; } @@ -302,11 +333,14 @@ public: class IfStmt : public Stmt { enum { COND, THEN, ELSE, END_EXPR }; Stmt* SubExprs[END_EXPR]; + SourceLocation IfLoc; public: - IfStmt(Expr *cond, Stmt *then, Stmt *elsev = 0) : Stmt(IfStmtClass) { + IfStmt(SourceLocation IL, Expr *cond, Stmt *then, Stmt *elsev = 0) + : Stmt(IfStmtClass) { SubExprs[COND] = reinterpret_cast(cond); SubExprs[THEN] = then; SubExprs[ELSE] = elsev; + IfLoc = IL; } const Expr *getCond() const { return reinterpret_cast(SubExprs[COND]);} @@ -316,6 +350,13 @@ public: Expr *getCond() { return reinterpret_cast(SubExprs[COND]); } Stmt *getThen() { return SubExprs[THEN]; } Stmt *getElse() { return SubExprs[ELSE]; } + + virtual SourceRange getSourceRange() const { + if (SubExprs[ELSE]) + return SourceRange(IfLoc, SubExprs[ELSE]->getLocEnd()); + else + return SourceRange(IfLoc, SubExprs[THEN]->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == IfStmtClass; @@ -357,6 +398,8 @@ public: FirstCase = SC; } + virtual SourceRange getSourceRange() const { return SourceRange(); } + static bool classof(const Stmt *T) { return T->getStmtClass() == SwitchStmtClass; } @@ -373,17 +416,22 @@ public: class WhileStmt : public Stmt { enum { COND, BODY, END_EXPR }; Stmt* SubExprs[END_EXPR]; + SourceLocation WhileLoc; public: - WhileStmt(Expr *cond, Stmt *body) : Stmt(WhileStmtClass) { + WhileStmt(Expr *cond, Stmt *body, SourceLocation WL) : Stmt(WhileStmtClass) { SubExprs[COND] = reinterpret_cast(cond); SubExprs[BODY] = body; + WhileLoc = WL; } Expr *getCond() { return reinterpret_cast(SubExprs[COND]); } const Expr *getCond() const { return reinterpret_cast(SubExprs[COND]);} Stmt *getBody() { return SubExprs[BODY]; } const Stmt *getBody() const { return SubExprs[BODY]; } - + + virtual SourceRange getSourceRange() const { + return SourceRange(WhileLoc, SubExprs[BODY]->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == WhileStmtClass; } @@ -399,17 +447,23 @@ public: class DoStmt : public Stmt { enum { COND, BODY, END_EXPR }; Stmt* SubExprs[END_EXPR]; + SourceLocation DoLoc; public: - DoStmt(Stmt *body, Expr *cond) : Stmt(DoStmtClass) { + DoStmt(Stmt *body, Expr *cond, SourceLocation DL) + : Stmt(DoStmtClass), DoLoc(DL) { SubExprs[COND] = reinterpret_cast(cond); SubExprs[BODY] = body; + DoLoc = DL; } Expr *getCond() { return reinterpret_cast(SubExprs[COND]); } const Expr *getCond() const { return reinterpret_cast(SubExprs[COND]);} Stmt *getBody() { return SubExprs[BODY]; } const Stmt *getBody() const { return SubExprs[BODY]; } - + + virtual SourceRange getSourceRange() const { + return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == DoStmtClass; } @@ -428,12 +482,15 @@ public: class ForStmt : public Stmt { enum { INIT, COND, INC, BODY, END_EXPR }; Stmt* SubExprs[END_EXPR]; // SubExprs[INIT] is an expression or declstmt. + SourceLocation ForLoc; public: - ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body) : Stmt(ForStmtClass) { + ForStmt(Stmt *Init, Expr *Cond, Expr *Inc, Stmt *Body, SourceLocation FL) + : Stmt(ForStmtClass) { SubExprs[INIT] = Init; SubExprs[COND] = reinterpret_cast(Cond); SubExprs[INC] = reinterpret_cast(Inc); SubExprs[BODY] = Body; + ForLoc = FL; } Stmt *getInit() { return SubExprs[INIT]; } @@ -445,7 +502,10 @@ public: const Expr *getCond() const { return reinterpret_cast(SubExprs[COND]);} const Expr *getInc() const { return reinterpret_cast(SubExprs[INC]); } const Stmt *getBody() const { return SubExprs[BODY]; } - + + virtual SourceRange getSourceRange() const { + return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); + } static bool classof(const Stmt *T) { return T->getStmtClass() == ForStmtClass; } @@ -464,6 +524,8 @@ public: GotoStmt(LabelStmt *label) : Stmt(GotoStmtClass), Label(label) {} LabelStmt *getLabel() const { return Label; } + + virtual SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == GotoStmtClass; @@ -484,6 +546,8 @@ public: Expr *getTarget() { return Target; } const Expr *getTarget() const { return Target; } + + virtual SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == IndirectGotoStmtClass; @@ -501,6 +565,9 @@ public: class ContinueStmt : public Stmt { public: ContinueStmt() : Stmt(ContinueStmtClass) {} + + virtual SourceRange getSourceRange() const { return SourceRange(); } + static bool classof(const Stmt *T) { return T->getStmtClass() == ContinueStmtClass; } @@ -516,6 +583,9 @@ public: class BreakStmt : public Stmt { public: BreakStmt() : Stmt(BreakStmtClass) {} + + virtual SourceRange getSourceRange() const { return SourceRange(); } + static bool classof(const Stmt *T) { return T->getStmtClass() == BreakStmtClass; } @@ -536,6 +606,8 @@ public: const Expr *getRetValue() const { return RetExpr; } Expr *getRetValue() { return RetExpr; } + + virtual SourceRange getSourceRange() const { return SourceRange(); } static bool classof(const Stmt *T) { return T->getStmtClass() == ReturnStmtClass;