Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/scip-ruby/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ see the [Design Decisions doc][].
- [Writing a new snapshot test](#writing-a-new-snapshot-test)
- [Writing a new repo test](#writing-a-new-snapshot-test)
- [Debugging](#debugging)
- [More readable stack traces](#more-readable-stack-traces)
- [Debugging with print statements](#debugging-with-print-statements)
- [Debugging with LLDB](#debugging-with-lldb)
- [Debugging build issues](#debugging-build-issues)
Expand Down Expand Up @@ -205,6 +206,24 @@ file to include the relevant data.

## Debugging

### More readable stack traces

C++ stack traces can get pretty gnarly with text like:

```c++
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >
```

instead of `std::string`.

If you have a stack trace, run it through the simplification script.

```
./bazel-bin/main/scip-ruby <args> 2> >(./tools/scripts/simplify-cxx-fqns.py)
```

NOTE: The script requires [Comby](https://comby.dev/docs/get-started#install).

### Debugging with print statements

So far, I've mostly been using print debugging
Expand Down
28 changes: 28 additions & 0 deletions tools/scripts/comby-rewrites.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[sorbet]
match = 'sorbet:::[~(((core|realmain)::)?)]'
rewrite = ''

[std_string]
match = 'std::__1::basic_string<char,...>'
rewrite = 'string'

[std_string_view]
match = 'std::__1::basic_string_view<char,...>'
rewrite = 'string_view'

[std_utility]
match = 'std::__1:::[name~(pair|tuple|optional)]<:[a]>'
rewrite = 'name<:[a]>'

[std_ptr]
match = 'std::__1::shared_ptr'
rewrite = 'shared_ptr'

[std_vector]
match = 'std::__1::vector<:[a],...>'
rewrite = 'vector<:[a]>'

# CAUTION: Patterns are run in alphabetical order, so use zzz to deprioritize this
[std_zzz_1]
match = 'std::__1::'
rewrite = 'std::'
44 changes: 44 additions & 0 deletions tools/scripts/simplify-cxx-fqns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3

# Utility script to simplify fully-qualified symbol names in C++ stack traces.

import logging
import pathlib
import os
import subprocess as sp
import sys

# Why use comby instead of sed or backtracking regexes?
#
# 1. Comby handles < > balancing.
# 2. Comby takes care of whitespace automatically.
# 3. Comby patterns are quite readable

def simplify(input: bytes) -> bytes:
config_path = pathlib.Path(__file__).parent.joinpath('comby-rewrites.toml')
invocation = ['comby', '-lang', '.c', '-stdin', '-stdout', '-templates', str(config_path)]
comby_ret = sp.run(invocation, input=input, capture_output=True)
comby_ret.check_returncode()
return comby_ret.stdout

def simplify_until_fixpoint(input: bytes) -> bytes:
output = simplify(input)
if output == input:
return output
return simplify_until_fixpoint(output)

def default_main():
if sp.run(['comby', '--help'], stdout=sp.DEVNULL, stderr=sp.PIPE).returncode != 0:
logging.error('comby --help failed; is comby missing?')
sys.exit(1)

if len(sys.argv) > 1 and sys.argv[1] == '--help':
print('Usage: ./my-binary <args> 2> >(./tools/scripts/simplify-cxx-fqns.py)')
return

input = sys.stdin.buffer.read()
output = simplify_until_fixpoint(input)
sys.stdout.buffer.write(output)

if __name__ == '__main__':
default_main()