From: Argyrios Kyrtzidis Date: Fri, 4 Jan 2013 18:30:04 +0000 (+0000) Subject: Move the common source locations of CastStmt & DefaultStmt into their base class... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=81cc2f1ecd31d8e3bca2c936fb9538a9d9c39695;p=clang Move the common source locations of CastStmt & DefaultStmt into their base class, SwitchCase. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171483 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index ad3889185f..e475c3859c 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -619,8 +619,14 @@ protected: // A pointer to the following CaseStmt or DefaultStmt class, // used by SwitchStmt. SwitchCase *NextSwitchCase; + SourceLocation KeywordLoc; + SourceLocation ColonLoc; + + SwitchCase(StmtClass SC, SourceLocation KWLoc, SourceLocation ColonLoc) + : Stmt(SC), NextSwitchCase(0), KeywordLoc(KWLoc), ColonLoc(ColonLoc) {} - SwitchCase(StmtClass SC) : Stmt(SC), NextSwitchCase(0) {} + SwitchCase(StmtClass SC, EmptyShell) + : Stmt(SC), NextSwitchCase(0) {} public: const SwitchCase *getNextSwitchCase() const { return NextSwitchCase; } @@ -629,13 +635,18 @@ public: void setNextSwitchCase(SwitchCase *SC) { NextSwitchCase = SC; } + SourceLocation getKeywordLoc() const { return KeywordLoc; } + void setKeywordLoc(SourceLocation L) { KeywordLoc = L; } + SourceLocation getColonLoc() const { return ColonLoc; } + void setColonLoc(SourceLocation L) { ColonLoc = L; } + Stmt *getSubStmt(); const Stmt *getSubStmt() const { return const_cast(this)->getSubStmt(); } - SourceLocation getLocStart() const LLVM_READONLY { return SourceLocation(); } - SourceLocation getLocEnd() const LLVM_READONLY { return SourceLocation(); } + SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } + SourceLocation getLocEnd() const LLVM_READONLY; static bool classof(const Stmt *T) { return T->getStmtClass() == CaseStmtClass || @@ -647,26 +658,22 @@ class CaseStmt : public SwitchCase { enum { LHS, RHS, SUBSTMT, END_EXPR }; Stmt* SubExprs[END_EXPR]; // The expression for the RHS is Non-null for // GNU "case 1 ... 4" extension - SourceLocation CaseLoc; SourceLocation EllipsisLoc; - SourceLocation ColonLoc; public: CaseStmt(Expr *lhs, Expr *rhs, SourceLocation caseLoc, SourceLocation ellipsisLoc, SourceLocation colonLoc) - : SwitchCase(CaseStmtClass) { + : SwitchCase(CaseStmtClass, caseLoc, colonLoc) { SubExprs[SUBSTMT] = 0; SubExprs[LHS] = reinterpret_cast(lhs); SubExprs[RHS] = reinterpret_cast(rhs); - CaseLoc = caseLoc; EllipsisLoc = ellipsisLoc; - ColonLoc = colonLoc; } /// \brief Build an empty switch case statement. - explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass) { } + explicit CaseStmt(EmptyShell Empty) : SwitchCase(CaseStmtClass, Empty) { } - SourceLocation getCaseLoc() const { return CaseLoc; } - void setCaseLoc(SourceLocation L) { CaseLoc = L; } + SourceLocation getCaseLoc() const { return KeywordLoc; } + void setCaseLoc(SourceLocation L) { KeywordLoc = L; } SourceLocation getEllipsisLoc() const { return EllipsisLoc; } void setEllipsisLoc(SourceLocation L) { EllipsisLoc = L; } SourceLocation getColonLoc() const { return ColonLoc; } @@ -688,7 +695,7 @@ public: void setLHS(Expr *Val) { SubExprs[LHS] = reinterpret_cast(Val); } void setRHS(Expr *Val) { SubExprs[RHS] = reinterpret_cast(Val); } - SourceLocation getLocStart() const LLVM_READONLY { return CaseLoc; } + SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } SourceLocation getLocEnd() const LLVM_READONLY { // Handle deeply nested case statements with iteration instead of recursion. const CaseStmt *CS = this; @@ -710,26 +717,24 @@ public: class DefaultStmt : public SwitchCase { Stmt* SubStmt; - SourceLocation DefaultLoc; - SourceLocation ColonLoc; public: DefaultStmt(SourceLocation DL, SourceLocation CL, Stmt *substmt) : - SwitchCase(DefaultStmtClass), SubStmt(substmt), DefaultLoc(DL), - ColonLoc(CL) {} + SwitchCase(DefaultStmtClass, DL, CL), SubStmt(substmt) {} /// \brief Build an empty default statement. - explicit DefaultStmt(EmptyShell) : SwitchCase(DefaultStmtClass) { } + explicit DefaultStmt(EmptyShell Empty) + : SwitchCase(DefaultStmtClass, Empty) { } Stmt *getSubStmt() { return SubStmt; } const Stmt *getSubStmt() const { return SubStmt; } void setSubStmt(Stmt *S) { SubStmt = S; } - SourceLocation getDefaultLoc() const { return DefaultLoc; } - void setDefaultLoc(SourceLocation L) { DefaultLoc = L; } + SourceLocation getDefaultLoc() const { return KeywordLoc; } + void setDefaultLoc(SourceLocation L) { KeywordLoc = L; } SourceLocation getColonLoc() const { return ColonLoc; } void setColonLoc(SourceLocation L) { ColonLoc = L; } - SourceLocation getLocStart() const LLVM_READONLY { return DefaultLoc; } + SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; } SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} static bool classof(const Stmt *T) { @@ -740,6 +745,11 @@ public: child_range children() { return child_range(&SubStmt, &SubStmt+1); } }; +inline SourceLocation SwitchCase::getLocEnd() const { + if (const CaseStmt *CS = dyn_cast(this)) + return CS->getLocEnd(); + return cast(this)->getLocEnd(); +} /// LabelStmt - Represents a label, which has a substatement. For example: /// foo: return; diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp index fe29ee652a..c509d99bcf 100644 --- a/lib/Serialization/ASTReaderStmt.cpp +++ b/lib/Serialization/ASTReaderStmt.cpp @@ -132,6 +132,8 @@ void ASTStmtReader::VisitCompoundStmt(CompoundStmt *S) { void ASTStmtReader::VisitSwitchCase(SwitchCase *S) { VisitStmt(S); Reader.RecordSwitchCaseID(S, Record[Idx++]); + S->setKeywordLoc(ReadSourceLocation(Record, Idx)); + S->setColonLoc(ReadSourceLocation(Record, Idx)); } void ASTStmtReader::VisitCaseStmt(CaseStmt *S) { @@ -139,16 +141,12 @@ void ASTStmtReader::VisitCaseStmt(CaseStmt *S) { S->setLHS(Reader.ReadSubExpr()); S->setRHS(Reader.ReadSubExpr()); S->setSubStmt(Reader.ReadSubStmt()); - S->setCaseLoc(ReadSourceLocation(Record, Idx)); S->setEllipsisLoc(ReadSourceLocation(Record, Idx)); - S->setColonLoc(ReadSourceLocation(Record, Idx)); } void ASTStmtReader::VisitDefaultStmt(DefaultStmt *S) { VisitSwitchCase(S); S->setSubStmt(Reader.ReadSubStmt()); - S->setDefaultLoc(ReadSourceLocation(Record, Idx)); - S->setColonLoc(ReadSourceLocation(Record, Idx)); } void ASTStmtReader::VisitLabelStmt(LabelStmt *S) { diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp index 1591c6c86a..0c6dc5dba0 100644 --- a/lib/Serialization/ASTWriterStmt.cpp +++ b/lib/Serialization/ASTWriterStmt.cpp @@ -78,6 +78,8 @@ void ASTStmtWriter::VisitCompoundStmt(CompoundStmt *S) { void ASTStmtWriter::VisitSwitchCase(SwitchCase *S) { VisitStmt(S); Record.push_back(Writer.getSwitchCaseID(S)); + Writer.AddSourceLocation(S->getKeywordLoc(), Record); + Writer.AddSourceLocation(S->getColonLoc(), Record); } void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) { @@ -85,17 +87,13 @@ void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) { Writer.AddStmt(S->getLHS()); Writer.AddStmt(S->getRHS()); Writer.AddStmt(S->getSubStmt()); - Writer.AddSourceLocation(S->getCaseLoc(), Record); Writer.AddSourceLocation(S->getEllipsisLoc(), Record); - Writer.AddSourceLocation(S->getColonLoc(), Record); Code = serialization::STMT_CASE; } void ASTStmtWriter::VisitDefaultStmt(DefaultStmt *S) { VisitSwitchCase(S); Writer.AddStmt(S->getSubStmt()); - Writer.AddSourceLocation(S->getDefaultLoc(), Record); - Writer.AddSourceLocation(S->getColonLoc(), Record); Code = serialization::STMT_DEFAULT; }