[clang-tools-extra] r318840 - [FindAllSymbols] Cache regexes, creating them is expensive

Benjamin Kramer via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 22 07:38:23 PST 2017


Author: d0k
Date: Wed Nov 22 07:38:23 2017
New Revision: 318840

URL: http://llvm.org/viewvc/llvm-project?rev=318840&view=rev
Log:
[FindAllSymbols] Cache regexes, creating them is expensive

This is a bit annoying because LLVM regexes are always mutable to store
errors. Assert that there are never errors and fix broken hardcoded
regexes.

Modified:
    clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.cpp
    clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.h
    clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp

Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.cpp?rev=318840&r1=318839&r2=318840&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.cpp Wed Nov 22 07:38:23 2017
@@ -13,6 +13,16 @@
 namespace clang {
 namespace find_all_symbols {
 
+HeaderMapCollector::HeaderMapCollector(
+    const RegexHeaderMap *RegexHeaderMappingTable) {
+  assert(RegexHeaderMappingTable);
+  this->RegexHeaderMappingTable.reserve(RegexHeaderMappingTable->size());
+  for (const auto &Entry : *RegexHeaderMappingTable) {
+    this->RegexHeaderMappingTable.emplace_back(llvm::Regex(Entry.first),
+                                               Entry.second);
+  }
+}
+
 llvm::StringRef
 HeaderMapCollector::getMappedHeader(llvm::StringRef Header) const {
   auto Iter = HeaderMappingTable.find(Header);
@@ -20,11 +30,13 @@ HeaderMapCollector::getMappedHeader(llvm
     return Iter->second;
   // If there is no complete header name mapping for this header, check the
   // regex header mapping.
-  if (RegexHeaderMappingTable) {
-    for (const auto &Entry : *RegexHeaderMappingTable) {
-      if (llvm::Regex(Entry.first).match(Header))
-        return Entry.second;
-    }
+  for (auto &Entry : RegexHeaderMappingTable) {
+#ifndef NDEBUG
+    std::string Dummy;
+    assert(Entry.first.isValid(Dummy) && "Regex should never be invalid!");
+#endif
+    if (Entry.first.match(Header))
+      return Entry.second;
   }
   return Header;
 }

Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.h?rev=318840&r1=318839&r2=318840&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.h (original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/HeaderMapCollector.h Wed Nov 22 07:38:23 2017
@@ -25,10 +25,8 @@ public:
   typedef llvm::StringMap<std::string> HeaderMap;
   typedef std::vector<std::pair<const char *, const char *>> RegexHeaderMap;
 
-  HeaderMapCollector() : RegexHeaderMappingTable(nullptr) {}
-
-  explicit HeaderMapCollector(const RegexHeaderMap *RegexHeaderMappingTable)
-      : RegexHeaderMappingTable(RegexHeaderMappingTable) {}
+  HeaderMapCollector() = default;
+  explicit HeaderMapCollector(const RegexHeaderMap *RegexHeaderMappingTable);
 
   void addHeaderMapping(llvm::StringRef OrignalHeaderPath,
                         llvm::StringRef MappingHeaderPath) {
@@ -47,8 +45,10 @@ private:
   HeaderMap HeaderMappingTable;
 
   // A map from header patterns to header names.
-  // This is a reference to a hard-coded map.
-  const RegexHeaderMap *const RegexHeaderMappingTable;
+  // The header names are not owned. This is only threadsafe because the regexes
+  // never fail.
+  mutable std::vector<std::pair<llvm::Regex, const char *>>
+      RegexHeaderMappingTable;
 };
 
 } // namespace find_all_symbols

Modified: clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp?rev=318840&r1=318839&r2=318840&view=diff
==============================================================================
--- clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp (original)
+++ clang-tools-extra/trunk/include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp Wed Nov 22 07:38:23 2017
@@ -314,10 +314,10 @@ const HeaderMapCollector::RegexHeaderMap
       {"include/xlocale.h$", "<cstring>"},
       {"bits/atomic_word.h$", "<memory>"},
       {"bits/basic_file.h$", "<fstream>"},
-      {"bits/c++allocator.h$", "<string>"},
-      {"bits/c++config.h$", "<iosfwd>"},
-      {"bits/c++io.h$", "<ios>"},
-      {"bits/c++locale.h$", "<locale>"},
+      {"bits/c\\+\\+allocator.h$", "<string>"},
+      {"bits/c\\+\\+config.h$", "<iosfwd>"},
+      {"bits/c\\+\\+io.h$", "<ios>"},
+      {"bits/c\\+\\+locale.h$", "<locale>"},
       {"bits/cpu_defines.h$", "<iosfwd>"},
       {"bits/ctype_base.h$", "<locale>"},
       {"bits/cxxabi_tweaks.h$", "<cxxabi.h>"},




More information about the cfe-commits mailing list