Skip to content

Commit

Permalink
[demangler] Preserve line numbering in copied demangler sources
Browse files Browse the repository at this point in the history
While prepending lines to the copied source files is functional, it
disturbs the line numbering between the original and the copy.  That
makes development more awkward than necessary, as it is the copy that
generally gets compiled first and emits compiler errors.

This uses sed to alter the first two lines, and also emits better
emacs mode setting, getting both C++ mode and read-only mode.

While here, also update and clarify documentation.

Reviewed By: ChuanqiXu

Differential Revision: https://reviews.llvm.org/D118135
  • Loading branch information
urnathan committed Feb 1, 2022
1 parent af8f1db commit fa7834a
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 84 deletions.
6 changes: 4 additions & 2 deletions libcxxabi/src/demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
//
//===----------------------------------------------------------------------===//
//
// 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.
// Generic itanium demangler library.
// There are two copies of this file in the source tree. The one under
// libcxxabi is the original and the one under llvm is the copy. Use
// cp-to-llvm.sh to update the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//

Expand Down
71 changes: 40 additions & 31 deletions libcxxabi/src/demangle/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,50 @@ 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.
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.
The canonical sources are in libcxxabi/src/demangle and some of the
files are copied to llvm/include/llvm/Demangle. The simple reason for
this comes from before the monorepo, and both [sub]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, and cannot rely on the
system's __cxa_demangle because it a) might not be available (i.e.,
on Windows), and b) may not be 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 optional argument the path to llvm, and 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.

FIXME: Now that LLVM is a monorepo, it should be possible to
de-duplicate this code, and have both LLVM and libcxxabi depend on a
shared demangler library.

Testing
-------
Expand Down
3 changes: 3 additions & 0 deletions libcxxabi/src/demangle/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//===----------------------------------------------------------------------===//
//
// FIXME: Use std::string_view instead when we support C++17.
// There are two copies of this file in the source tree. The one under
// libcxxabi is the original and the one under llvm is the copy. Use
// cp-to-llvm.sh to update the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//

Expand Down
5 changes: 4 additions & 1 deletion libcxxabi/src/demangle/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
//
//===----------------------------------------------------------------------===//
//
// Provide some utility classes for use in the demangler(s).
// Provide some utility classes for use in the demangler.
// There are two copies of this file in the source tree. The one in libcxxabi
// is the original and the one in llvm is the copy. Use cp-to-llvm.sh to update
// the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//

Expand Down
9 changes: 5 additions & 4 deletions libcxxabi/src/demangle/cp-to-llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ if [[ $ANSWER =~ ^[Yy]$ ]]; then
chmod -w $LLVM_DEMANGLE_DIR/README.txt
for I in $HDRS ; do
rm -f $LLVM_DEMANGLE_DIR/$I
cat - $I >$LLVM_DEMANGLE_DIR/$I <<EOF
// Do not edit! -*- read-only -*-
// See README.txt for instructions
EOF
dash=$(echo "$I---------------------------" | cut -c -27 |\
sed 's|[^-]*||')
sed -e '1s|^//=*-* .*\.h -*.*=*// *$|//===--- '"$I $dash"'-*- mode:c++;eval:(read-only-mode) -*-===//|' \
-e '2s|^// *$|// Do not edit! See README.txt.|' \
$I >$LLVM_DEMANGLE_DIR/$I
chmod -w $LLVM_DEMANGLE_DIR/$I
done
fi
12 changes: 6 additions & 6 deletions llvm/include/llvm/Demangle/ItaniumDemangle.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Do not edit! -*- read-only -*-
// See README.txt for instructions
//===------------------------- ItaniumDemangle.h ----------------*- C++ -*-===//
//
//===--- ItaniumDemangle.h -----------*- mode:c++;eval:(read-only-mode) -*-===//
// Do not edit! See README.txt.
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// 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.
// Generic itanium demangler library.
// There are two copies of this file in the source tree. The one under
// libcxxabi is the original and the one under llvm is the copy. Use
// cp-to-llvm.sh to update the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//

Expand Down
71 changes: 40 additions & 31 deletions llvm/include/llvm/Demangle/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,50 @@ 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.
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.
The canonical sources are in libcxxabi/src/demangle and some of the
files are copied to llvm/include/llvm/Demangle. The simple reason for
this comes from before the monorepo, and both [sub]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, and cannot rely on the
system's __cxa_demangle because it a) might not be available (i.e.,
on Windows), and b) may not be 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 optional argument the path to llvm, and 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.

FIXME: Now that LLVM is a monorepo, it should be possible to
de-duplicate this code, and have both LLVM and libcxxabi depend on a
shared demangler library.

Testing
-------
Expand Down
9 changes: 5 additions & 4 deletions llvm/include/llvm/Demangle/StringView.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Do not edit! -*- read-only -*-
// See README.txt for instructions
//===--- StringView.h -------------------------------------------*- C++ -*-===//
//
//===--- StringView.h ----------------*- mode:c++;eval:(read-only-mode) -*-===//
// Do not edit! See README.txt.
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// FIXME: Use std::string_view instead when we support C++17.
// There are two copies of this file in the source tree. The one under
// libcxxabi is the original and the one under llvm is the copy. Use
// cp-to-llvm.sh to update the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//

Expand Down
11 changes: 6 additions & 5 deletions llvm/include/llvm/Demangle/Utility.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Do not edit! -*- read-only -*-
// See README.txt for instructions
//===--- Utility.h ----------------------------------------------*- C++ -*-===//
//
//===--- Utility.h -------------------*- mode:c++;eval:(read-only-mode) -*-===//
// Do not edit! See README.txt.
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Provide some utility classes for use in the demangler(s).
// Provide some utility classes for use in the demangler.
// There are two copies of this file in the source tree. The one in libcxxabi
// is the original and the one in llvm is the copy. Use cp-to-llvm.sh to update
// the copy. See README.txt for more details.
//
//===----------------------------------------------------------------------===//

Expand Down

0 comments on commit fa7834a

Please sign in to comment.