Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Dump functions to Check, Parse, and Lex #4669

Merged
merged 22 commits into from
Dec 12, 2024

Conversation

jonmeow
Copy link
Contributor

@jonmeow jonmeow commented Dec 11, 2024

  • Provide Check::Dump(context, arg) and similar.
    • gdb and lldb should do contextual lookup, and call Dump(*this, Lex::TokenIndex::Invalid) has been tested with gdb.
    • Since this is only for debug, keeps the functions fully separated from code.
  • Uses alwayslink to ensure objects are correctly linked, even though there are no calls.
    • -Wno-missing-prototypes is needed when we don't have forward declarations.
  • Code is not linked in opt builds, using #ifndef NDEBUG.
    • This probably could be doing something in BUILD files with a select(), but the #ifndef seemed easier.

This is based on #4620, but uses free functions instead of member functions.

Co-authored-by: Dana Jansens [email protected]

danakj and others added 18 commits December 5, 2024 14:35
…ffer

These methods are useful for calling in a debugger, and will dump
verbose information from an id value.

Currently this is done in a pretty simple manner. The Lex and Parse
methods are each replicated onto the Check::Context class manually (and
same for Lex onto Parse::Tree).

The DumpId methods are implemented in this PR for:
- Lex::TokenIndex
- Parse::NodeId
- SemIR::LocId
@danakj
Copy link
Contributor

danakj commented Dec 12, 2024

This is based on #4620. Versus that, this is doing a few things differently:

Cool thanks for figuring out the free functions approach with bazel. If this merges would it just take the commit description and author from the first commit or will it apply co-authored correctly? We can reword the first commit to describe what the PR ended up doing?

@@ -93,6 +91,24 @@ cc_library(
"//toolchain/sem_ir:typed_insts",
"@llvm-project//llvm:Support",
],
alwayslink = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment why this is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this one was missed, I also wondered if it's actually needed since dump was moved to another cc_library.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I wasn't looking closely enough because I just assumed I'd already removed this set. Removed.

"//toolchain/parse:tree",
"//toolchain/sem_ir:file",
],
alwayslink = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment why this is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

cc_library(
name = "dump_id",
srcs = ["dump_id.cpp"],
copts = ["-Wno-missing-prototypes"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment why this is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

":tokenized_buffer",
"//common:ostream",
],
alwayslink = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment why this is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"//common:ostream",
"//toolchain/lex:dump_id",
],
alwayslink = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment why this is here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -26,4 +27,19 @@ auto DumpIdImpl(const Tree& tree, NodeId node_id) -> void {
llvm::errs() << ")";
}

// A set of DumpId() overloads that dump an object to stderr, useful for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Lex, this is moved to the header, should this comment move to the header here too? Or can we make it consistent either way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a more complete comment to each header.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also a mention in docs/


// A set of DumpId() overloads that dump an object to stderr, useful for
// calling inside a debugger.
LLVM_DUMP_METHOD auto DumpId(const Context& context, Lex::TokenIndex token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description says "this provides Check::Dump(context, arg) and similar" which is a typo (no Id) that we keep making. And we'll want to dump things other than Ids (like PendingBlock). So maybe we should drop the Id suffix. This is also fine for now, but I'm not happy with the naming forever yet. I was also talking with Geoff about maybe renaming "Dump" in Printable (Dump->Print and Print->PrintTo?) or something to avoid the name collisions.

Copy link
Contributor Author

@jonmeow jonmeow Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LLVM typically calls it dump, which is where the name Dump came from:

https://github.com/search?q=repo%3Allvm%2Fllvm-project%20LLVM_DUMP_METHOD&type=code

(ed: to be clear, I think there's significant value in consistency)

You're correct the PR description doesn't match, I'll change the call though.

Copy link
Contributor Author

@jonmeow jonmeow Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, removing Dump from Printable would seem fine to me (I only added it for debugging, for others), I'm just meaning that if there's going to be a function that does dumping, it should probably use the familiar name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right yeah I don't want to diverge from LLVM too much either. My full opinion state here is that I'd prefer Dump to be used for the place that includes as much info as possible. The dump() in LLVM does include comprehensive amount of info generally everything you need when debugging, similarly. I'd personally be fine with renaming or removing the Dump function from Printable. I haven't had a lot of use of calling Dump explicitly on Printable types, as they are already streamable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(to be sure, I'm assuming this'll be resolved separately)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, me too

@jonmeow
Copy link
Contributor Author

jonmeow commented Dec 12, 2024

This is based on #4620. Versus that, this is doing a few things differently:

Cool thanks for figuring out the free functions approach with bazel. If this merges would it just take the commit description and author from the first commit or will it apply co-authored correctly? We can reword the first commit to describe what the PR ended up doing?

Sure, and since it sounds like you're okay proceeding with this approach, would it make sense to close #4620?

AFAIU GitHub adds Co-authored-by lines automatically. You can find examples in PRs such as #4623 -- note that I have commits in that branch, and GitHub attributes me in daba2c7

@@ -93,6 +91,24 @@ cc_library(
"//toolchain/sem_ir:typed_insts",
"@llvm-project//llvm:Support",
],
alwayslink = 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this one was missed, I also wondered if it's actually needed since dump was moved to another cc_library.

@@ -6,6 +6,7 @@
#define CARBON_TOOLCHAIN_CHECK_CONTEXT_H_

#include "common/map.h"
#include "common/ostream.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer used in this PR and can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing

@@ -72,6 +72,7 @@ cc_library(
"//common:array_stack",
"//common:check",
"//common:map",
"//common:ostream",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer used in this PR and can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing

@danakj danakj changed the title Add DumpId functions to Check, Parse, and Lex Add Dump functions to Check, Parse, and Lex Dec 12, 2024
@jonmeow jonmeow enabled auto-merge December 12, 2024 20:39
@jonmeow jonmeow added this pull request to the merge queue Dec 12, 2024
Merged via the queue into carbon-language:trunk with commit 3ce0df6 Dec 12, 2024
8 checks passed
@jonmeow jonmeow deleted the dump-free-fn branch December 12, 2024 21:17
bricknerb pushed a commit to bricknerb/carbon-lang that referenced this pull request Dec 16, 2024
- Provide `Check::Dump(context, arg)` and similar.
- gdb and lldb should do contextual lookup, and `call Dump(*this,
Lex::TokenIndex::Invalid)` has been tested with gdb.
- Since this is only for debug, keeps the functions fully separated from
code.
- Uses alwayslink to ensure objects are correctly linked, even though
there are no calls.
- `-Wno-missing-prototypes` is needed when we don't have forward
declarations.
- Code is not linked in opt builds, using `#ifndef NDEBUG`.
- This probably could be doing something in BUILD files with a
`select()`, but the `#ifndef` seemed easier.

This is based on carbon-language#4620, but uses free functions instead of member
functions.

Co-authored-by: Dana Jansens <[email protected]>

---------

Co-authored-by: danakj <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Jan 6, 2025
Follow-on to #4669 . Did some manual testing, but may have not exercised
all code paths.

---------

Co-authored-by: Josh L <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants