Convert static SourceReferenceFormatter functions to member ones#3135
Convert static SourceReferenceFormatter functions to member ones#3135chriseth merged 2 commits intoargotorg:developfrom
Conversation
| @@ -42,35 +42,37 @@ struct SourceReferenceFormatter | |||
| { | |||
There was a problem hiding this comment.
Can you change it to a class, please?
| _stream << line << endl; | ||
| m_stream << line << endl; | ||
|
|
||
| auto& stream = m_stream; |
There was a problem hiding this comment.
You can just capture this by value and then use m_value, no reason to create a local reference.
solc/CommandLineInterface.cpp
Outdated
| m_compiler.reset(new CompilerStack(fileReader)); | ||
|
|
||
| auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compiler->scanner(_sourceName); }; | ||
| auto formatter = SourceReferenceFormatter(cerr, scannerFromSourceName); |
There was a problem hiding this comment.
I would prefer the (actually shorter) and more explicit version:
SourceReferenceFormatter formatter(cerr, scannerFromSourceName);
| *error, | ||
| (error->type() == Error::Type::Warning) ? "Warning" : "Error", | ||
| scannerFromSourceName | ||
| (error->type() == Error::Type::Warning) ? "Warning" : "Error" |
There was a problem hiding this comment.
A next refactor could pull this expression into the formatter, I would say.
There was a problem hiding this comment.
Or actually just call typeName().
There was a problem hiding this comment.
Yes, I did not want to rock the boat too much with this one, just lay the foundation for better encapsulation and configuration.
chriseth
left a comment
There was a problem hiding this comment.
Great apart from minor details.
fb91b58 to
cac378a
Compare
|
@chriseth thanks! fixed. |
|
The test failure is weird, since this PR should not change that... |
| line.cbegin(), | ||
| line.cbegin() + startColumn, | ||
| [&_stream](char const& ch) { _stream << (ch == '\t' ? '\t' : ' '); } | ||
| [this](char const& ch) { m_stream << (ch == '\t' ? '\t' : ' '); } |
There was a problem hiding this comment.
Not sure which is faster for closures, giving the entire context or just the member variable?
There was a problem hiding this comment.
It's not a big object for copy, so I don't think it makes that much of a difference here.
|
@chriseth anything blocking this? |
|
I guess it is fine, but with time I'd like to use StandardCompiler in the CLI too (e.g. to reduce the number of public interfaces we have) and that would mean most of these changes aren't really utilised. |
|
The blocker here was as @chriseth mentioned the test failure: |
|
Retriggered tests, maybe that fixes it. |
|
@federicobond it is still failing the tests :( |
cac378a to
fa5177c
Compare
|
Tried this locally and cannot reproduce it :( |
|
Okay the problem is the different version of the shell, which emits the This is the code filtering out the |
fa5177c to
b87b359
Compare
|
Still failing :( |
|
@axic I did not understand your last comment fully. Is the shell not globbing one of the function arguments? |
|
@federicobond I had that hunch, but then I think it was something else but related, unfortunately had to leave the task and don't fully remember. I am sure it had something to do with the shell or the script. |
|
This is the output: The script removes any line containing |
|
Sounds like |
|
@federicobond can you try to fix it? Would be keen merging this. |
|
Let me try... (Travis being so slow today doesn't help much) |
|
Fails compilation on: |
328759a to
c7e4499
Compare
|
This is so frustrating, the change should have nothing to do with that test failure. |
c7e4499 to
d3d762a
Compare
|
Rebased again, perhaps it will work? |
d3d762a to
efba425
Compare
efba425 to
886d716
Compare
886d716 to
305d5f7
Compare
|
Ok, fingers crossed. I was not able to reproduce it, but I might have the fix. Explanation inline. |
| m_compiler.reset(new CompilerStack(fileReader)); | ||
|
|
||
| auto scannerFromSourceName = [&](string const& _sourceName) -> solidity::Scanner const& { return m_compiler->scanner(_sourceName); }; | ||
| SourceReferenceFormatter formatter(cerr, scannerFromSourceName); |
There was a problem hiding this comment.
The type of a lambda function is different from std::function, you can only implicitly convert into std::function. This means since auto is used above, the type of scannerFromSourceNam is not std::function. Since formatter takes a const&, the type is converted on the fly, creating a temporary copy of scannerFromSourceName, converted to std::function. Since SourceReferenceFormatter (used to) store a const&, it stores a const& to this temporary which is destroyed after this line and thus invalid. I changed it to storing a value instead of a reference.
There was a problem hiding this comment.
That’s awesome 👏 . Thanks for taking the time to review this Chris!
I should definitely improve my understanding of that part of the language.
There was a problem hiding this comment.
Yeah, that's the problem with C++: It does not warn you about ownership problems.
This cleans up the method signatures and also opens the door to configuring and even replacing the formatter implementations.