]> granicus.if.org Git - clang/commitdiff
Make case sorting deterministic by not depending on pointer
authorChris Lattner <sabre@nondot.org>
Fri, 21 Sep 2007 18:15:22 +0000 (18:15 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 21 Sep 2007 18:15:22 +0000 (18:15 +0000)
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
clang.xcodeproj/project.pbxproj
include/clang/AST/Stmt.h

index 0522996a55e02113bf3da4d45b23e4a566cc1ec7..653a0ab5b9e3389830922edb6ebe538d827b9589 100644 (file)
@@ -259,8 +259,23 @@ namespace {
   };
 }
 
+/// CmpCaseVals - Comparison predicate for sorting case values.
+///
+static bool CmpCaseVals(const std::pair<llvm::APSInt, CaseStmt*>& lhs,
+                        const std::pair<llvm::APSInt, CaseStmt*>& 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) {
index 443e9245e0c93d6b5bd31b00df635087efc97d4d..5f0ee39dabebf6c32e24d5dd22df1473714177be 100644 (file)
                08FB7793FE84155DC02AAC07 /* Project object */ = {
                        isa = PBXProject;
                        buildConfigurationList = 1DEB923508733DC60010E9CD /* Build configuration list for PBXProject "clang" */;
-                       compatibilityVersion = "Xcode 2.4";
                        hasScannedForEncodings = 1;
                        mainGroup = 08FB7794FE84155DC02AAC07 /* clang */;
                        projectDirPath = "";
index cc1c1cf6e388538a6d8feee5a3e15373577ef762..a346fdc33fbbfc90caf487812fc96112e2668145 100644 (file)
@@ -258,6 +258,8 @@ public:
     CaseLoc = caseLoc;
   }
   
+  SourceLocation getCaseLoc() const { return CaseLoc; }
+  
   Expr *getLHS() { return reinterpret_cast<Expr*>(SubExprs[LHS]); }
   Expr *getRHS() { return reinterpret_cast<Expr*>(SubExprs[RHS]); }
   Stmt *getSubStmt() { return SubExprs[SUBSTMT]; }