From: Chris Lattner Date: Thu, 16 Apr 2009 06:13:46 +0000 (+0000) Subject: optimize and comment GetDiagInfo. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=87d854ebedb43a9ff0f55591821dc5600984e479;p=clang optimize and comment GetDiagInfo. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69273 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp index 7242026b24..bf11a13dc2 100644 --- a/lib/Basic/Diagnostic.cpp +++ b/lib/Basic/Diagnostic.cpp @@ -48,6 +48,13 @@ struct StaticDiagInfoRec { 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 @@ static const StaticDiagInfoRec StaticDiagInfo[] = { }; #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) {