njames93 created this revision. njames93 added reviewers: rsmith, aaron.ballman. njames93 requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Lookup the map using a string ref and store indexed diag info using a StringRef to the map. It may be worth using DenseMap to store this, but I'll leave that option on the table for now. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D97400 Files: clang/lib/Basic/DiagnosticIDs.cpp Index: clang/lib/Basic/DiagnosticIDs.cpp =================================================================== --- clang/lib/Basic/DiagnosticIDs.cpp +++ clang/lib/Basic/DiagnosticIDs.cpp @@ -356,42 +356,56 @@ namespace clang { namespace diag { - class CustomDiagInfo { - typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc; - std::vector<DiagDesc> DiagInfo; - std::map<DiagDesc, unsigned> DiagIDs; - public: - - /// getDescription - Return the description of the specified custom - /// diagnostic. - StringRef getDescription(unsigned DiagID) const { - assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && - "Invalid diagnostic ID"); - return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; - } - - /// getLevel - Return the level of the specified custom diagnostic. - DiagnosticIDs::Level getLevel(unsigned DiagID) const { - assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && - "Invalid diagnostic ID"); - return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; - } - - unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, - DiagnosticIDs &Diags) { - DiagDesc D(L, std::string(Message)); - // Check to see if it already exists. - std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); - if (I != DiagIDs.end() && I->first == D) - return I->second; - - // If not, assign a new ID. - unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; - DiagIDs.insert(std::make_pair(D, ID)); - DiagInfo.push_back(D); - return ID; - } - }; + using DiagDesc = std::pair<DiagnosticIDs::Level, std::string>; + struct DiagDescRef { + DiagDescRef(DiagnosticIDs::Level Level, StringRef FormatString) + : Level(Level), FormatString(FormatString) {} + DiagDescRef(const DiagDesc &D) : DiagDescRef(D.first, D.second) {} + DiagnosticIDs::Level Level; + StringRef FormatString; + }; + static bool operator<(const DiagDesc &LHS, const DiagDescRef &RHS) { + if (LHS.first == RHS.Level) + return LHS.second < RHS.FormatString; + return LHS.first < RHS.Level; + } + class CustomDiagInfo { + std::vector<DiagDescRef> DiagInfo; + std::map<DiagDesc, unsigned, std::less<>> DiagIDs; + + public: + /// getDescription - Return the description of the specified custom + /// diagnostic. + StringRef getDescription(unsigned DiagID) const { + assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && + "Invalid diagnostic ID"); + return DiagInfo[DiagID - DIAG_UPPER_LIMIT].FormatString; + } + + /// getLevel - Return the level of the specified custom diagnostic. + DiagnosticIDs::Level getLevel(unsigned DiagID) const { + assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && + "Invalid diagnostic ID"); + return DiagInfo[DiagID - DIAG_UPPER_LIMIT].Level; + } + + unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, + DiagnosticIDs &Diags) { + // Check to see if it already exists. + + auto I = DiagIDs.lower_bound(DiagDescRef(L, Message)); + if (I != DiagIDs.end() && I->first.first == L && + I->first.second == Message) + return I->second; + + // If not, assign a new ID. + unsigned ID = DiagInfo.size() + DIAG_UPPER_LIMIT; + auto InsertRet = + DiagIDs.insert(std::make_pair(DiagDesc{L, Message.str()}, ID)); + DiagInfo.emplace_back(InsertRet.first->first); + return ID; + } + }; } // end diag namespace } // end clang namespace
Index: clang/lib/Basic/DiagnosticIDs.cpp =================================================================== --- clang/lib/Basic/DiagnosticIDs.cpp +++ clang/lib/Basic/DiagnosticIDs.cpp @@ -356,42 +356,56 @@ namespace clang { namespace diag { - class CustomDiagInfo { - typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc; - std::vector<DiagDesc> DiagInfo; - std::map<DiagDesc, unsigned> DiagIDs; - public: - - /// getDescription - Return the description of the specified custom - /// diagnostic. - StringRef getDescription(unsigned DiagID) const { - assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && - "Invalid diagnostic ID"); - return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second; - } - - /// getLevel - Return the level of the specified custom diagnostic. - DiagnosticIDs::Level getLevel(unsigned DiagID) const { - assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && - "Invalid diagnostic ID"); - return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; - } - - unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, - DiagnosticIDs &Diags) { - DiagDesc D(L, std::string(Message)); - // Check to see if it already exists. - std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); - if (I != DiagIDs.end() && I->first == D) - return I->second; - - // If not, assign a new ID. - unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; - DiagIDs.insert(std::make_pair(D, ID)); - DiagInfo.push_back(D); - return ID; - } - }; + using DiagDesc = std::pair<DiagnosticIDs::Level, std::string>; + struct DiagDescRef { + DiagDescRef(DiagnosticIDs::Level Level, StringRef FormatString) + : Level(Level), FormatString(FormatString) {} + DiagDescRef(const DiagDesc &D) : DiagDescRef(D.first, D.second) {} + DiagnosticIDs::Level Level; + StringRef FormatString; + }; + static bool operator<(const DiagDesc &LHS, const DiagDescRef &RHS) { + if (LHS.first == RHS.Level) + return LHS.second < RHS.FormatString; + return LHS.first < RHS.Level; + } + class CustomDiagInfo { + std::vector<DiagDescRef> DiagInfo; + std::map<DiagDesc, unsigned, std::less<>> DiagIDs; + + public: + /// getDescription - Return the description of the specified custom + /// diagnostic. + StringRef getDescription(unsigned DiagID) const { + assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && + "Invalid diagnostic ID"); + return DiagInfo[DiagID - DIAG_UPPER_LIMIT].FormatString; + } + + /// getLevel - Return the level of the specified custom diagnostic. + DiagnosticIDs::Level getLevel(unsigned DiagID) const { + assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() && + "Invalid diagnostic ID"); + return DiagInfo[DiagID - DIAG_UPPER_LIMIT].Level; + } + + unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message, + DiagnosticIDs &Diags) { + // Check to see if it already exists. + + auto I = DiagIDs.lower_bound(DiagDescRef(L, Message)); + if (I != DiagIDs.end() && I->first.first == L && + I->first.second == Message) + return I->second; + + // If not, assign a new ID. + unsigned ID = DiagInfo.size() + DIAG_UPPER_LIMIT; + auto InsertRet = + DiagIDs.insert(std::make_pair(DiagDesc{L, Message.str()}, ID)); + DiagInfo.emplace_back(InsertRet.first->first); + return ID; + } + }; } // end diag namespace } // end clang namespace
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits