[cfe-commits] r69273 - /cfe/trunk/lib/Basic/Diagnostic.cpp

Chris Lattner sabre at nondot.org
Wed Apr 15 23:13:46 PDT 2009


Author: lattner
Date: Thu Apr 16 01:13:46 2009
New Revision: 69273

URL: http://llvm.org/viewvc/llvm-project?rev=69273&view=rev
Log:
optimize and comment GetDiagInfo.

Modified:
    cfe/trunk/lib/Basic/Diagnostic.cpp

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=69273&r1=69272&r2=69273&view=diff

==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Thu Apr 16 01:13:46 2009
@@ -48,6 +48,13 @@
   unsigned Class : 3;
   const char *Description;
   const char *OptionGroup;
+  
+  bool operator<(const StaticDiagInfoRec &RHS) const {
+    return DiagID < RHS.DiagID;
+  }
+  bool operator>(const StaticDiagInfoRec &RHS) const {
+    return DiagID > RHS.DiagID;
+  }
 };
 
 static const StaticDiagInfoRec StaticDiagInfo[] = {
@@ -65,13 +72,32 @@
 };
 #undef DIAG
 
+/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
+/// or null if the ID is invalid.
 static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
-  // FIXME: Binary search.
-  for (unsigned i = 0, e = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0]);
-       i != e; ++i)
-    if (StaticDiagInfo[i].DiagID == DiagID)
-      return &StaticDiagInfo[i];
-  return 0;
+  unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
+
+  // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
+#ifndef NDEBUG
+  static bool IsFirst = true;
+  if (IsFirst) {
+    for (unsigned i = 1; i != NumDiagEntries; ++i)
+      assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
+             "Improperly sorted diag info");
+    IsFirst = false;
+  }
+#endif
+  
+  // Search the diagnostic table with a binary search.
+  StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0 };
+  
+  const StaticDiagInfoRec *Found =
+    std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find);
+  if (Found == StaticDiagInfo + NumDiagEntries ||
+      Found->DiagID != DiagID)
+    return 0;
+    
+  return Found;
 }
 
 static unsigned GetDefaultDiagMapping(unsigned DiagID) {





More information about the cfe-commits mailing list