[libcxxabi] r351474 - NFC: Make the copies of the demangler byte-for-byte identical

Erik Pilkington erik.pilkington at gmail.com
Thu Jan 17 12:37:52 PST 2019


Author: epilk
Date: Thu Jan 17 12:37:51 2019
New Revision: 351474

URL: http://llvm.org/viewvc/llvm-project?rev=351474&view=rev
Log:
NFC: Make the copies of the demangler byte-for-byte identical

With this patch, the copies of the files ItaniumDemangle.h,
StringView.h, and Utility.h are kept byte-for-byte in sync between
libcxxabi and llvm. All differences (namespaces, fallthrough, and
unreachable macros) are defined in each copies' DemanglerConfig.h.

This patch also adds a script to copy changes from libcxxabi
(cp-to-llvm.sh), and a README.txt explaining the situation.

Differential revision: https://reviews.llvm.org/D53538

Added:
    libcxxabi/trunk/src/demangle/DemangleConfig.h
    libcxxabi/trunk/src/demangle/README.txt
    libcxxabi/trunk/src/demangle/cp-to-llvm.sh   (with props)
Removed:
    libcxxabi/trunk/src/demangle/Compiler.h
Modified:
    libcxxabi/trunk/src/cxa_demangle.cpp
    libcxxabi/trunk/src/demangle/ItaniumDemangle.h
    libcxxabi/trunk/src/demangle/StringView.h
    libcxxabi/trunk/src/demangle/Utility.h

Modified: libcxxabi/trunk/src/cxa_demangle.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/cxa_demangle.cpp?rev=351474&r1=351473&r2=351474&view=diff
==============================================================================
--- libcxxabi/trunk/src/cxa_demangle.cpp (original)
+++ libcxxabi/trunk/src/cxa_demangle.cpp Thu Jan 17 12:37:51 2019
@@ -11,12 +11,8 @@
 // file does not yet support:
 //   - C++ modules TS
 
-#define _LIBCPP_NO_EXCEPTIONS
-
-#include "__cxxabi_config.h"
-
 #include "demangle/ItaniumDemangle.h"
-
+#include "__cxxabi_config.h"
 #include <cassert>
 #include <cctype>
 #include <cstdio>
@@ -25,7 +21,6 @@
 #include <functional>
 #include <numeric>
 #include <utility>
-#include <vector>
 
 using namespace itanium_demangle;
 

Removed: libcxxabi/trunk/src/demangle/Compiler.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/Compiler.h?rev=351473&view=auto
==============================================================================
--- libcxxabi/trunk/src/demangle/Compiler.h (original)
+++ libcxxabi/trunk/src/demangle/Compiler.h (removed)
@@ -1,34 +0,0 @@
-//===--- Compiler.h ---------------------------------------------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-// This file is contains a subset of macros copied from
-// llvm/lib/Demangle/Compiler.h.
-//===----------------------------------------------------------------------===//
-
-#ifndef LIBCXX_DEMANGLE_COMPILER_H
-#define LIBCXX_DEMANGLE_COMPILER_H
-
-#ifdef _MSC_VER
-// snprintf is implemented in VS 2015
-#if _MSC_VER < 1900
-#define snprintf _snprintf_s
-#endif
-#endif
-
-#ifndef __has_attribute
-#define __has_attribute(x) 0
-#endif
-
-#ifndef NDEBUG
-#if __has_attribute(noinline) && __has_attribute(used)
-#define DUMP_METHOD __attribute__((noinline, used))
-#else
-#define DUMP_METHOD
-#endif
-#endif
-
-#endif

Added: libcxxabi/trunk/src/demangle/DemangleConfig.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/DemangleConfig.h?rev=351474&view=auto
==============================================================================
--- libcxxabi/trunk/src/demangle/DemangleConfig.h (added)
+++ libcxxabi/trunk/src/demangle/DemangleConfig.h Thu Jan 17 12:37:51 2019
@@ -0,0 +1,42 @@
+//===--- Compiler.h ---------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+// This file is contains a subset of macros copied from
+// llvm/lib/Demangle/Compiler.h.
+//===----------------------------------------------------------------------===//
+
+#ifndef LIBCXX_DEMANGLE_COMPILER_H
+#define LIBCXX_DEMANGLE_COMPILER_H
+
+#include "__config"
+
+#ifdef _MSC_VER
+// snprintf is implemented in VS 2015
+#if _MSC_VER < 1900
+#define snprintf _snprintf_s
+#endif
+#endif
+
+#ifndef __has_attribute
+#define __has_attribute(x) 0
+#endif
+
+#ifndef NDEBUG
+#if __has_attribute(noinline) && __has_attribute(used)
+#define DEMANGLE_DUMP_METHOD __attribute__((noinline, used))
+#else
+#define DEMANGLE_DUMP_METHOD
+#endif
+#endif
+
+#define DEMANGLE_FALLTHROUGH _LIBCPP_FALLTHROUGH()
+#define DEMANGLE_UNREACHABLE _LIBCPP_UNREACHABLE()
+
+#define DEMANGLE_NAMESPACE_BEGIN namespace { namespace itanium_demangle {
+#define DEMANGLE_NAMESPACE_END } }
+
+#endif

Modified: libcxxabi/trunk/src/demangle/ItaniumDemangle.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/ItaniumDemangle.h?rev=351474&r1=351473&r2=351474&view=diff
==============================================================================
--- libcxxabi/trunk/src/demangle/ItaniumDemangle.h (original)
+++ libcxxabi/trunk/src/demangle/ItaniumDemangle.h Thu Jan 17 12:37:51 2019
@@ -7,22 +7,21 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// WARNING: This file defines its contents within an anonymous namespace. It
-// should not be included anywhere other than cxa_demangle.h.
+// Generic itanium demangler library. This file has two byte-per-byte identical
+// copies in the source tree, one in libcxxabi, and the other in llvm.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LIBCXX_DEMANGLE_ITANIUMDEMANGLE_H
-#define LIBCXX_DEMANGLE_ITANIUMDEMANGLE_H
+#ifndef DEMANGLE_ITANIUMDEMANGLE_H
+#define DEMANGLE_ITANIUMDEMANGLE_H
 
 // FIXME: (possibly) incomplete list of features that clang mangles that this
 // file does not yet support:
 //   - C++ modules TS
 
-#include "Compiler.h"
+#include "DemangleConfig.h"
 #include "StringView.h"
 #include "Utility.h"
-
 #include <cassert>
 #include <cctype>
 #include <cstdio>
@@ -100,8 +99,8 @@
     X(BracedExpr) \
     X(BracedRangeExpr)
 
-namespace {
-namespace itanium_demangle {
+DEMANGLE_NAMESPACE_BEGIN
+
 // Base class of all AST nodes. The AST is built by the parser, then is
 // traversed by the printLeft/Right functions to produce a demangled string.
 class Node {
@@ -199,7 +198,7 @@ public:
   virtual ~Node() = default;
 
 #ifndef NDEBUG
-  DUMP_METHOD void dump() const;
+  DEMANGLE_DUMP_METHOD void dump() const;
 #endif
 };
 
@@ -1283,7 +1282,7 @@ public:
     case SpecialSubKind::iostream:
       return StringView("basic_iostream");
     }
-    _LIBCPP_UNREACHABLE();
+    DEMANGLE_UNREACHABLE;
   }
 
   void printLeft(OutputStream &S) const override {
@@ -1335,7 +1334,7 @@ public:
     case SpecialSubKind::iostream:
       return StringView("iostream");
     }
-    _LIBCPP_UNREACHABLE();
+    DEMANGLE_UNREACHABLE;
   }
 
   void printLeft(OutputStream &S) const override {
@@ -3472,7 +3471,7 @@ Node *AbstractManglingParser<Derived, Al
       Result = getDerived().parseFunctionType();
       break;
     }
-    _LIBCPP_FALLTHROUGH();
+    DEMANGLE_FALLTHROUGH;
   }
   case 'U': {
     Result = getDerived().parseQualifiedType();
@@ -3759,7 +3758,7 @@ Node *AbstractManglingParser<Derived, Al
       // substitution table.
       return Sub;
     }
-    _LIBCPP_FALLTHROUGH();
+    DEMANGLE_FALLTHROUGH;
   }
   //        ::= <class-enum-type>
   default: {
@@ -5183,7 +5182,6 @@ struct ManglingParser : AbstractMangling
                                Alloc>::AbstractManglingParser;
 };
 
-}  // namespace itanium_demangle
-}  // namespace
+DEMANGLE_NAMESPACE_END
 
-#endif // LIBCXX_DEMANGLE_ITANIUMDEMANGLE_H
+#endif // DEMANGLE_ITANIUMDEMANGLE_H

Added: libcxxabi/trunk/src/demangle/README.txt
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/README.txt?rev=351474&view=auto
==============================================================================
--- libcxxabi/trunk/src/demangle/README.txt (added)
+++ libcxxabi/trunk/src/demangle/README.txt Thu Jan 17 12:37:51 2019
@@ -0,0 +1,52 @@
+Itanium Name Demangler Library
+==============================
+
+Introduction
+------------
+
+This directory contains the generic itanium name demangler library. The main
+purpose of the library is to demangle C++ symbols, i.e. convert the string
+"_Z1fv" into "f()". You can also use the CRTP base ManglingParser to perform
+some simple analysis on the mangled name, or (in LLVM) use the opaque
+ItaniumPartialDemangler to query the demangled AST.
+
+Why are there multiple copies of the this library in the source tree?
+---------------------------------------------------------------------
+
+This directory is mirrored between libcxxabi/demangle and
+llvm/include/llvm/Demangle. The simple reason for this is that both projects
+need to demangle symbols, but neither can depend on each other. libcxxabi needs
+the demangler to implement __cxa_demangle, which is part of the itanium ABI
+spec. LLVM needs a copy for a bunch of places, but doesn't want to use the
+system's __cxa_demangle because it a) might not be available (i.e., on Windows),
+and b) probably isn't that up-to-date on the latest language features.
+
+The copy of the demangler in LLVM has some extra stuff that aren't needed in
+libcxxabi (ie, the MSVC demangler, ItaniumPartialDemangler), which depend on the
+shared generic components. Despite these differences, we want to keep the "core"
+generic demangling library identical between both copies to simplify development
+and testing.
+
+If you're working on the generic library, then do the work first in libcxxabi,
+then run the cp-to-llvm.sh script in src/demangle. This script takes as an
+argument the path to llvm, and re-copies the changes you made to libcxxabi over.
+Note that this script just blindly overwrites all changes to the generic library
+in llvm, so be careful.
+
+Because the core demangler needs to work in libcxxabi, everything needs to be
+declared in an anonymous namespace (see DEMANGLE_NAMESPACE_BEGIN), and you can't
+introduce any code that depends on the libcxx dylib.
+
+Hopefully, when LLVM becomes a monorepo, we can de-duplicate this code, and have
+both LLVM and libcxxabi depend on a shared demangler library.
+
+Testing
+-------
+
+The tests are split up between libcxxabi/test/{unit,}test_demangle.cpp, and
+llvm/unittest/Demangle. The llvm directory should only get tests for stuff not
+included in the core library. In the future though, we should probably move all
+the tests to LLVM.
+
+It is also a really good idea to run libFuzzer after non-trivial changes, see
+libcxxabi/fuzz/cxa_demangle_fuzzer.cpp and https://llvm.org/docs/LibFuzzer.html.

Modified: libcxxabi/trunk/src/demangle/StringView.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/StringView.h?rev=351474&r1=351473&r2=351474&view=diff
==============================================================================
--- libcxxabi/trunk/src/demangle/StringView.h (original)
+++ libcxxabi/trunk/src/demangle/StringView.h Thu Jan 17 12:37:51 2019
@@ -5,23 +5,29 @@
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
+//===----------------------------------------------------------------------===//
+//
+// FIXME: Use std::string_view instead when we support C++17.
 //
-// This file is copied from llvm/lib/Demangle/StringView.h.
 //===----------------------------------------------------------------------===//
 
-#ifndef LIBCXX_DEMANGLE_STRINGVIEW_H
-#define LIBCXX_DEMANGLE_STRINGVIEW_H
+#ifndef DEMANGLE_STRINGVIEW_H
+#define DEMANGLE_STRINGVIEW_H
 
+#include "DemangleConfig.h"
 #include <algorithm>
 #include <cassert>
 #include <cstring>
 
-namespace {
+DEMANGLE_NAMESPACE_BEGIN
+
 class StringView {
   const char *First;
   const char *Last;
 
 public:
+  static const size_t npos = ~size_t(0);
+
   template <size_t N>
   StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
   StringView(const char *First_, const char *Last_)
@@ -35,6 +41,17 @@ public:
     return StringView(begin() + From, size() - From);
   }
 
+  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()) {
+      // Just forward to memchr, which is faster than a hand-rolled loop.
+      if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
+        return size_t(static_cast<const char *>(P) - First);
+    }
+    return npos;
+  }
+
   StringView substr(size_t From, size_t To) const {
     if (To >= size())
       To = size() - 1;
@@ -45,15 +62,26 @@ public:
 
   StringView dropFront(size_t N = 1) const {
     if (N >= size())
-      N = size() - 1;
+      N = size();
     return StringView(First + N, Last);
   }
 
+  StringView dropBack(size_t N = 1) const {
+    if (N >= size())
+      N = size();
+    return StringView(First, Last - N);
+  }
+
   char front() const {
     assert(!empty());
     return *begin();
   }
 
+  char back() const {
+    assert(!empty());
+    return *(end() - 1);
+  }
+
   char popFront() {
     assert(!empty());
     return *First++;
@@ -93,6 +121,7 @@ inline bool operator==(const StringView
   return LHS.size() == RHS.size() &&
          std::equal(LHS.begin(), LHS.end(), RHS.begin());
 }
-} // namespace
+
+DEMANGLE_NAMESPACE_END
 
 #endif

Modified: libcxxabi/trunk/src/demangle/Utility.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/Utility.h?rev=351474&r1=351473&r2=351474&view=diff
==============================================================================
--- libcxxabi/trunk/src/demangle/Utility.h (original)
+++ libcxxabi/trunk/src/demangle/Utility.h Thu Jan 17 12:37:51 2019
@@ -5,22 +5,24 @@
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
+//===----------------------------------------------------------------------===//
+//
+// Provide some utility classes for use in the demangler(s).
 //
-// This file is copied from llvm/lib/Demangle/Utility.h.
 //===----------------------------------------------------------------------===//
 
-#ifndef LIBCXX_DEMANGLE_UTILITY_H
-#define LIBCXX_DEMANGLE_UTILITY_H
+#ifndef DEMANGLE_UTILITY_H
+#define DEMANGLE_UTILITY_H
 
 #include "StringView.h"
-
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
 #include <iterator>
 #include <limits>
 
-namespace {
+DEMANGLE_NAMESPACE_BEGIN
+
 // Stream that AST nodes write their string representation into after the AST
 // has been parsed.
 class OutputStream {
@@ -185,6 +187,6 @@ inline bool initializeOutputStream(char
   return true;
 }
 
-} // namespace
+DEMANGLE_NAMESPACE_END
 
 #endif

Added: libcxxabi/trunk/src/demangle/cp-to-llvm.sh
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/demangle/cp-to-llvm.sh?rev=351474&view=auto
==============================================================================
--- libcxxabi/trunk/src/demangle/cp-to-llvm.sh (added)
+++ libcxxabi/trunk/src/demangle/cp-to-llvm.sh Thu Jan 17 12:37:51 2019
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# Copies the 'demangle' library, excluding 'DemangleConfig.h', to llvm. If no
+# llvm directory is specified, then assume a monorepo layout.
+
+set -e
+
+FILES="ItaniumDemangle.h StringView.h Utility.h README.txt"
+LLVM_DEMANGLE_DIR=$1
+
+if [[ -z "$LLVM_DEMANGLE_DIR" ]]; then
+    LLVM_DEMANGLE_DIR="../../../llvm/include/llvm/Demangle"
+fi
+
+if [[ ! -d "$LLVM_DEMANGLE_DIR" ]]; then
+    echo "No such directory: $LLVM_DEMANGLE_DIR" >&2
+    exit 1
+fi
+
+read -p "This will overwrite the copies of $FILES in $LLVM_DEMANGLE_DIR; are you sure? [y/N]" -n 1 -r ANSWER
+echo
+
+if [[ $ANSWER =~ ^[Yy]$ ]]; then
+    for I in $FILES ; do
+        cp $I $LLVM_DEMANGLE_DIR/$I
+    done
+fi

Propchange: libcxxabi/trunk/src/demangle/cp-to-llvm.sh
------------------------------------------------------------------------------
    svn:executable = *




More information about the libcxx-commits mailing list