[llvm] Remove (some) LLVMDemangle header dependencies
authorserge-sans-paille <sguelton@redhat.com>
Thu, 20 Jan 2022 10:21:47 +0000 (11:21 +0100)
committerserge-sans-paille <sguelton@redhat.com>
Fri, 21 Jan 2022 09:48:09 +0000 (10:48 +0100)
- Avoid using <iterator> for std::end on a plain array (using <array> instead)
- Avoid using <algorithm> for std::min and std::equal (using alternate logic and std::strcmp instead)

As an hint to the impact of the cleanup, running

clang++ -E  -Iinclude -I../llvm/include ../llvm/lib/Demangle/*.cpp -std=c++14 -fno-rtti -fno-exceptions | wc -l

before:  203965 lines
after:   169704 lines

llvm/include/llvm/Demangle/ItaniumDemangle.h
llvm/include/llvm/Demangle/StringView.h
llvm/include/llvm/Demangle/Utility.h

index b25139d..01f414a 100644 (file)
 #include "DemangleConfig.h"
 #include "StringView.h"
 #include "Utility.h"
+#include <algorithm>
 #include <cassert>
 #include <cctype>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
-#include <numeric>
+#include <limits>
 #include <utility>
 
 #define FOR_EACH_NODE_KIND(X) \
index 1e4d380..7c8cb48 100644 (file)
@@ -14,7 +14,6 @@
 #define DEMANGLE_STRINGVIEW_H
 
 #include "DemangleConfig.h"
-#include <algorithm>
 #include <cassert>
 #include <cstring>
 
@@ -38,15 +37,16 @@ public:
 
   StringView substr(size_t Pos, size_t Len = npos) const {
     assert(Pos <= size());
-    return StringView(begin() + Pos, std::min(Len, size() - Pos));
+    if (Len > size() - Pos)
+      Len = size() - Pos;
+    return StringView(begin() + Pos, Len);
   }
 
   size_t find(char C, size_t From = 0) const {
-    size_t FindBegin = std::min(From, size());
     // Avoid calling memchr with nullptr.
-    if (FindBegin < size()) {
+    if (From < size()) {
       // Just forward to memchr, which is faster than a hand-rolled loop.
-      if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
+      if (const void *P = ::memchr(First + From, C, size() - From))
         return size_t(static_cast<const char *>(P) - First);
     }
     return npos;
@@ -98,7 +98,7 @@ public:
   bool startsWith(StringView Str) const {
     if (Str.size() > size())
       return false;
-    return std::equal(Str.begin(), Str.end(), begin());
+    return std::strncmp(Str.begin(), begin(), Str.size()) == 0;
   }
 
   const char &operator[](size_t Idx) const { return *(begin() + Idx); }
@@ -111,7 +111,7 @@ public:
 
 inline bool operator==(const StringView &LHS, const StringView &RHS) {
   return LHS.size() == RHS.size() &&
-         std::equal(LHS.begin(), LHS.end(), RHS.begin());
+         std::strncmp(LHS.begin(), RHS.begin(), LHS.size()) == 0;
 }
 
 DEMANGLE_NAMESPACE_END
index 733d83a..b816aa2 100644 (file)
 #define DEMANGLE_UTILITY_H
 
 #include "StringView.h"
+#include <array>
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
-#include <iterator>
+#include <exception>
 #include <limits>
 
 DEMANGLE_NAMESPACE_BEGIN
@@ -48,8 +49,8 @@ class OutputBuffer {
       return;
     }
 
-    char Temp[21];
-    char *TempPtr = std::end(Temp);
+    std::array<char, 21> Temp;
+    char *TempPtr = Temp.end();
 
     while (N) {
       *--TempPtr = char('0' + N % 10);
@@ -59,7 +60,7 @@ class OutputBuffer {
     // Add negative sign...
     if (isNeg)
       *--TempPtr = '-';
-    this->operator<<(StringView(TempPtr, std::end(Temp)));
+    this->operator<<(StringView(TempPtr, Temp.end()));
   }
 
 public: