[PATCH] D97400: [clang][NFC] Remove unnecessary string copies in CustomDiagInfo
Nathan James via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 24 10:10:00 PST 2021
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D97400.326136.patch
Type: text/x-patch
Size: 3677 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210224/73c05c8a/attachment-0001.bin>
More information about the cfe-commits
mailing list