Skip to content

SR-7165. Excise <iostream> from reflection #27081

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

Merged
merged 4 commits into from
Oct 16, 2019
Merged

SR-7165. Excise <iostream> from reflection #27081

merged 4 commits into from
Oct 16, 2019

Conversation

ismetanin
Copy link
Contributor

Replace iostream with raw_ostream to follow LLVM Coding Standards.

Related to closed PR of @nafu

Resolves SR-7165.

@ismetanin ismetanin changed the title [SR-7165] Excise iostream from reflection SR-7165. Excise iostream from reflection Sep 8, 2019
@ismetanin ismetanin changed the title SR-7165. Excise iostream from reflection SR-7165. Excise <iostream> from reflection Sep 8, 2019
@compnerd
Copy link
Member

compnerd commented Sep 8, 2019

This is reasonable. Removing the header usage isn't a bad idea. It would be nice if the two changes were squashed into a single change.

@compnerd
Copy link
Member

compnerd commented Sep 8, 2019

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Sep 8, 2019

Build failed
Swift Test Linux Platform
Git Sha - 683c7564e43e6a22fae845a68ceb93587a683dc7

@swift-ci
Copy link
Contributor

swift-ci commented Sep 8, 2019

Build failed
Swift Test OS X Platform
Git Sha - 683c7564e43e6a22fae845a68ceb93587a683dc7

@gottesmm
Copy link
Contributor

gottesmm commented Sep 8, 2019

I haven't read this PR (don't treat this as a review), but I want to give a big plus one to the effort. I wanted to look into this some time ago but had other pressing things to look at.

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

Looks overall pretty reasonable. Two things:

  1. I did a git grep with your branch and found there are still iostream includes. Can you remove any that are unnecessary as a result of this patch? Seems like some are in the locations you have been touching.

  2. Is there a reason you changed that CMakeLists.txt line I pointed out? Can you remove?

@@ -1,3 +1,5 @@
append("-undefined dynamic_lookup" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Copy link
Contributor

Choose a reason for hiding this comment

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

Whats with this part of the change here?

Copy link
Member

Choose a reason for hiding this comment

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

Ugh, somehow I missed this part of the change. This seems wrong; I think that it is trying to work around the missing LLVMSupport linkage. Thank you @gottesmm for catching this oversight.

@gottesmm
Copy link
Contributor

gottesmm commented Sep 8, 2019

Specifically:

include/swift/Reflection/TypeRefBuilder.h:#include <iostream>
tools/swift-demangle-yamldump/swift-demangle-yamldump.cpp:#include <iostream>
tools/swift-demangle/swift-demangle.cpp:#include <iostream>
tools/swift-reflection-dump/swift-reflection-dump.cpp:#include <iostream>
unittests/Parse/SyntaxParsingCacheTests.cpp:#include <iostream>

@ismetanin
Copy link
Contributor Author

@compnerd

This is reasonable. Removing the header usage isn't a bad idea. It would be nice if the two changes were squashed into a single change.

Okay, I'll squash my changes but at first, can someone help me resolve linking error on Linux platform? I just thought that -undefined dynamic_lookup is OSX-specific and Linux shared libraries behave as if -undefined dynamic_lookup was enabled all the time

@ismetanin
Copy link
Contributor Author

Looks overall pretty reasonable. Two things:

  1. I did a git grep with your branch and found there are still iostream includes. Can you remove any that are unnecessary as a result of this patch? Seems like some are in the locations you have been touching.
  2. Is there a reason you changed that CMakeLists.txt line I pointed out? Can you remove?

@gottesmm

  1. I'll definitely look there and will try to remove, thanks

  2. It resolves Undefined symbols for architecture x86_64: error, you can find more details in that commit from the previous PR of @nafu

@compnerd
Copy link
Member

compnerd commented Sep 8, 2019

@ismetanin, I think that we are missing a link against LLVMSupport. Letting this be undefined is a bad idea, we don't want to end up in the case where the symbol is unresolved at load time.

@ismetanin
Copy link
Contributor Author

@compnerd
So how we should fix missing LLVMSupport linkage in the correct way?

@jrose-apple
Copy link
Contributor

jrose-apple commented Sep 9, 2019

Sorry, the intent of SR-7165 is to replace iostreams with printf or similar, not raw_ostream. Reflection libraries should not link in LLVMSupport because the client may have their own copy of LLVM. See the discussion in the previous #15677.

@ismetanin
Copy link
Contributor Author

Sorry, the intent of SR-7165 is to replace iostreams with printf or similar, not raw_ostream. Reflection libraries should not link in LLVMSupport because the client may have their own copy of LLVM. See the discussion in the previous #15677.

@jrose-apple oh, okay, I’ll try to fix it

Sent with GitHawk

@ismetanin
Copy link
Contributor Author

@jrose-apple Hey, I'm here again and I have a question.
Should I replace iostreams in this way?

Instead llvm::errs() << indent() << "## New NodeFactory\n";
use fprintf(stderr, "%s%s", indent(), "## New NodeFactory\n");

Instead std::cout << "<null type info>\n";
use printf("<null type info>\n")

And should I replace the use of std::ostream? If yes, with what?

@jrose-apple
Copy link
Contributor

That looks reasonable to me. I think the use of plain std::ostream is fine (via the <ostream> header rather than <iostream>); it is the global constructors of std::cin, std::cout, and std::cerr that we are trying to avoid.

@ismetanin
Copy link
Contributor Author

@jrose-apple I reasearced a bit and found that in some places we use std::ostream as an input parameter in this way: dump(std::cerr, 0); but std::cerr declared in <iostream> and I don't know what to use instead std::cerr. For example, we can change the parameter to C-style FILE* and do something like dump(stderr, 0) but I'm not sure that it is ok

@jrose-apple
Copy link
Contributor

Hm. @jckarter, what do you think? (Too bad C++ doesn't have a built-in std::ostream subclass that takes a FILE * or a file descriptor.)

@jckarter
Copy link
Contributor

jckarter commented Oct 1, 2019

I would personally just use FILE* instead of trying to use ostream.

@ismetanin
Copy link
Contributor Author

ismetanin commented Oct 14, 2019

@jrose-apple @jckarter I've just replaced iostreams with fprintf and now PR is ready to review 👾

@@ -24,7 +24,7 @@
//#define NODE_FACTORY_DEBUGGING

#ifdef NODE_FACTORY_DEBUGGING
#include <iostream>
#include <ostream>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can <ostream> be removed as well?

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.

Thanks!

@jckarter
Copy link
Contributor

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 683c7564e43e6a22fae845a68ceb93587a683dc7

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 683c7564e43e6a22fae845a68ceb93587a683dc7

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

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

Thanks for sticking with this! A few comments.

@@ -141,8 +133,7 @@ class NodeFactory {
size_t ObjectSize = NumObjects * sizeof(T);
CurPtr = align(CurPtr, alignof(T));
#ifdef NODE_FACTORY_DEBUGGING
std::cerr << indent() << "alloc " << ObjectSize << ", CurPtr = "
<< (void *)CurPtr << "\n";
fprintf(stderr, "%salloc%zu, CurPtr = %p\n", indent().c_str(), ObjectSize, (void *)CurPtr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: space after "alloc" got lost 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.

Fixed

unsigned Indent;

std::ostream &indent(unsigned Amount) {
FILE * &indent(unsigned Amount) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These should all drop the &. (They probably shouldn't return anything at this point since they're not going to be chained.)

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

printField("alignment", std::to_string(TI.getAlignment()));
printField("stride", std::to_string(TI.getStride()));
printField("num_extra_inhabitants", std::to_string(TI.getNumExtraInhabitants()));
printField("bitwise_takable", TI.isBitwiseTakable() ? "1" : "0");
Copy link
Contributor

Choose a reason for hiding this comment

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

Might as well go with "true" and "false" if you're changing it anyway.

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

for (auto &fieldRef : *descriptor.getLocalBuffer()) {
auto field = descriptor.getField(fieldRef);
auto fieldName = getTypeRefString(readTypeRef(field, field->FieldName));
OS << std::string(fieldName.begin(), fieldName.end());
fprintf(file, "%s", std::string(fieldName.begin(), fieldName.end()).c_str());
Copy link
Contributor

Choose a reason for hiding this comment

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

@jckarter Is this necessary or is the field name going to be null-terminated?

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to use fprintf(file, "%*s", (int)fieldName.size(), fieldName.data()).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed ✅

fprintf(file, "Size: %s\n", std::to_string(descriptor->Size).c_str());
fprintf(file, "Alignment: %s:\n", std::to_string(descriptor->getAlignment()).c_str());
fprintf(file, "Stride: %s:\n", std::to_string(descriptor->Stride).c_str());
fprintf(file, "NumExtraInhabitants: %s:\n", std::to_string(descriptor->NumExtraInhabitants).c_str());
Copy link
Contributor

Choose a reason for hiding this comment

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

These are all numeric values, why go through %s?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, my bad 🧐
I'll fix it

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

@ismetanin
Copy link
Contributor Author

Can someone help me with fails on @swift-ci? I'm confused with error 20:44:51 ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job. (the same on Linux and macOS)
Probably it's because I force pushed to the repo (Now I'm just pushing new commits thought)

@theblixguy
Copy link
Collaborator

theblixguy commented Oct 14, 2019

You can ignore it - the job gets restarted if you look at the status checks. I don't know why it fails initially most of the time; might have something to do with force pushes/rebase or maybe something's wrong with the bot.

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - dd7d173

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - dd7d173

@jrose-apple jrose-apple self-assigned this Oct 15, 2019
@jckarter
Copy link
Contributor

@swift-ci Please test

@jrose-apple
Copy link
Contributor

10:44:46 
/home/buildnode/jenkins/workspace/swift-PR-Linux@2/branch-master/swift/test/Reflection/typeref_lowering.swift:13:19: error: CHECK-64-NEXT: expected string not found in input
10:44:46 // CHECK-64-NEXT: (struct size=16 alignment=4 stride=16 num_extra_inhabitants=0 bitwise_takable=1
10:44:46                   ^
10:44:46 <stdin>:2:1: note: scanning from here
10:44:46 (struct size=16 alignment=4 stride=16 num_extra_inhabitants=0 bitwise_takable=true
10:44:46 ^

Oops, that's my fault for wanting it to say "true".

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 8036edd

@jckarter
Copy link
Contributor

Sorry, looks like some of the tests need to be updated for the change in the way we present bool output:

/home/buildnode/jenkins/workspace/swift-PR-Linux/branch-master/swift/test/Reflection/typeref_lowering.swift:13:19: error: CHECK-64-NEXT: expected string not found in input
// CHECK-64-NEXT: (struct size=16 alignment=4 stride=16 num_extra_inhabitants=0 bitwise_takable=1
                  ^
<stdin>:2:1: note: scanning from here
(struct size=16 alignment=4 stride=16 num_extra_inhabitants=0 bitwise_takable=true
^

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 8036edd

@ismetanin
Copy link
Contributor Author

Sorry, I didn't retest after PR fixes, now everything should be ok 👌

Could someone help me with the development process?
I mean I changed all the tests, rebuild with ninja swift and then ninja swift-stdlib but tests are still failing with CHECK-NEXT: expected string not found in input ... bitwise_takable=1 but everywhere in tests bitwise_takable=true. Looks like I should rebuild the entire project shouldn't I?

@jckarter
Copy link
Contributor

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 8036edd

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 8036edd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants