From 764a7ce5217f9569e100a3445f47496ee82daf86 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 21 Sep 2007 18:15:22 +0000 Subject: [PATCH] Make case sorting deterministic by not depending on pointer values. Patch mostly by Gabor Greif for PR1682. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@42203 91177308-0d34-0410-b5e6-96231b3b80d8 --- Sema/SemaStmt.cpp | 19 +++++++++++++++++-- clang.xcodeproj/project.pbxproj | 1 - include/clang/AST/Stmt.h | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Sema/SemaStmt.cpp b/Sema/SemaStmt.cpp index 0522996a55..653a0ab5b9 100644 --- a/Sema/SemaStmt.cpp +++ b/Sema/SemaStmt.cpp @@ -259,8 +259,23 @@ namespace { }; } +/// CmpCaseVals - Comparison predicate for sorting case values. +/// +static bool CmpCaseVals(const std::pair& lhs, + const std::pair& rhs) { + if (lhs.first < rhs.first) + return true; + + if (lhs.first == rhs.first && + lhs.second->getCaseLoc().getRawEncoding() + < rhs.second->getCaseLoc().getRawEncoding()) + return true; + return false; +} + Action::StmtResult -Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Body) { +Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, + ExprTy *Body) { Stmt *BodyStmt = (Stmt*)Body; SwitchStmt *SS = SwitchStack.back(); @@ -335,7 +350,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, StmtTy *Switch, ExprTy *Bo } // Sort all the scalar case values so we can easily detect duplicates. - std::stable_sort(CaseVals.begin(), CaseVals.end()); + std::stable_sort(CaseVals.begin(), CaseVals.end(), CmpCaseVals); if (!CaseVals.empty()) { for (unsigned i = 0, e = CaseVals.size()-1; i != e; ++i) { diff --git a/clang.xcodeproj/project.pbxproj b/clang.xcodeproj/project.pbxproj index 443e9245e0..5f0ee39dab 100644 --- a/clang.xcodeproj/project.pbxproj +++ b/clang.xcodeproj/project.pbxproj @@ -725,7 +725,6 @@ 08FB7793FE84155DC02AAC07 /* Project object */ = { isa = PBXProject; buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */; - compatibilityVersion = "Xcode 2.4"; hasScannedForEncodings = 1; mainGroup = 08FB7794FE84155DC02AAC07 /* clang */; projectDirPath = ""; diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index cc1c1cf6e3..a346fdc33f 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -258,6 +258,8 @@ public: CaseLoc = caseLoc; } + SourceLocation getCaseLoc() const { return CaseLoc; } + Expr *getLHS() { return reinterpret_cast(SubExprs[LHS]); } Expr *getRHS() { return reinterpret_cast(SubExprs[RHS]); } Stmt *getSubStmt() { return SubExprs[SUBSTMT]; } -- 2.40.0