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

Reformat code (and add make target to do so) #326

Merged
merged 1 commit into from
Aug 3, 2017

Conversation

sbarzowski
Copy link
Collaborator

No description provided.

@sbarzowski
Copy link
Collaborator Author

it's a massive change, even though I tried to match the settings with the existing style. There were no manual changes (it's a bad idea to mix automatic reformatting with other restructuring imo).

Sometimes it goes from bad to worse, for example when there are plenty of arguments to a function. But I think it's still a net win, especially if we enforce it automatically later on.

@lwander, @sparkprime

Copy link

@lwander lwander left a comment

Choose a reason for hiding this comment

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

Looks largely good to me, my only gripe is the comment doc reformatting - I think that's something to fix before merging.

Also, this might be the right time to introduce a check in our Travis CI build to require code to match the output of make reformat.

Makefile Outdated
@@ -101,6 +101,9 @@ test: jsonnet libjsonnet.so libjsonnet_test_snippet libjsonnet_test_file
cd test_suite ; ./run_tests.sh
cd test_suite ; ./run_fmt_tests.sh

reformat:
Copy link

Choose a reason for hiding this comment

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

all should probably depend on this.

also I think you should declare this as .PHONY

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm worried that reformatting on every build (== adding to all) may be annoying.

👍 for .PHONY I forgot about it.

Also I guess I'll add a separate test-formatting target for use in TravisCI etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

test-formatting added, ..PHONY added

My concern with all is not really valid, there is a separate default target. But still I interpret all as build all as it doesn't run tests etc., so I think reformatting doesn't really belong there. @sparkprime?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think make all should change source code. However it can run the test-formatting.

* \param vars The variables defined within lexical scope of ast_.
* \returns The free variables in ast_.
* \param in_object Whether or not ast_ is within the lexical scope of an object
* AST. \param vars The variables defined within lexical scope of ast_. \returns
Copy link

Choose a reason for hiding this comment

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

This kind of doc reformatting seems wrong

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I can fix it through CommentPragma option, thanks for noticing

.clang-format Outdated
@@ -0,0 +1,108 @@
---
Language: Cpp
# BasedOnStyle: Google
Copy link

Choose a reason for hiding this comment

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

Why commented out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The real reason is that it was autogenerated this way. But I guess I can either remove it completely or leave it active. I'll take another look at this config.

@sparkprime
Copy link
Contributor

#143

@davidzchen will be happy about this

SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Auto
TabWidth: 8
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be 4?

Copy link
Collaborator Author

@sbarzowski sbarzowski Jul 20, 2017

Choose a reason for hiding this comment

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

No, this is not the indentation, but how we interpret actual tab characters (we change them to this many spaces then). For example Google style, that I started with, uses 8 here, even though the Indentation is only 2 chars wide.

@sbarzowski sbarzowski changed the title Refromat code (and add make target to do so) Reformat code (and add make target to do so) Jul 20, 2017
@sbarzowski
Copy link
Collaborator Author

sbarzowski commented Jul 21, 2017

BTW if we want to enforce it automatically there are some workflow issues to discuss. Most importantly the problems with having different versions of clang-format on different machines which may disagree with each other and the "official" version on TravisCI. The most proper solution I think is to pin down the version of clang-format somehow, locally for the project. I was using a workflow like this very successfully in Python and it was very easy with virtualenv. Some people do it for C++, but it's quite complex (https://engineering.mongodb.com/post/succeeding-with-clangformat-part-2-the-big-reformat). I suppose typically C++ projects use autoformatting in a more ad-hoc way.

But probably it's a topic for another change/issue.

@sparkprime
Copy link
Contributor

Did you try it with 100 line width yet?

@sparkprime
Copy link
Contributor

This doesn't seem too onerous:

The Wrapper Script

In any non-trivial use of clang-format, you will want a wrapper around it. Our script, clang_format.py:

Fetches clang-format binaries, from LLVM when it can, and from a MongoDB cache when it cannot
Validates clang-format is at the right version when binaries are already present
Skips files that we shouldn’t bother formatting, i.e. 3rd party library code, like boost
Offers modes for both validating and updating
Manages parallel invocations of clang-format to increase throughput

@sbarzowski
Copy link
Collaborator Author

sbarzowski commented Jul 21, 2017

Did you try it with 100 line width yet?

Yes, current version uses 100 as a target width.

core/lexer.h Outdated
@@ -56,14 +56,14 @@ static inline std::ostream &operator<<(std::ostream &o, const FodderElement &f)
{
switch (f.kind) {
case FodderElement::LINE_END:
o << "END(" << f.blanks << ", " << f.indent << ", " << f.comment[0] << ")";
break;
o << "END(" << f.blanks << ", " << f.indent << ", " << f.comment[0] << ")";
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a moderate preference for keeping them on the same line, because they're essentially just indirect goto labels. However if there's no supported option in clang for that then I can live with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I checked that and I couldn't find anything to control that.

core/lexer.h Outdated
case TRUE: return "true";

case END_OF_FILE: return "end of file";
case BRACE_L:
Copy link
Contributor

Choose a reason for hiding this comment

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

Any way to encourage it to put the return on the same line? This is less readable than before.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes I experimented with that, I think there were fewer changes if I set it this way, but I might be wrong. Anyway, I'll change it.

void testLex(const char* name,
const char* input,
const std::list<Token>& tokens,
namespace
Copy link
Contributor

Choose a reason for hiding this comment

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

I've never seen anyone put the { after a namespace before

Copy link
Contributor

Choose a reason for hiding this comment

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

(on the line after)

abort(); \
#define CATCH(func) \
} \
catch (const std::bad_alloc &) \
Copy link
Contributor

Choose a reason for hiding this comment

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

I think try { } catch should have the same rules as if { } else { }

Copy link
Collaborator Author

@sbarzowski sbarzowski Jul 21, 2017

Choose a reason for hiding this comment

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

It does. But here we have crazy macros, so it gets a little confused.

@@ -277,16 +284,14 @@ const char *jsonnet_version(void)

JsonnetVm *jsonnet_make(void)
{
TRY
return new JsonnetVm();
TRY return new JsonnetVm();
Copy link
Contributor

Choose a reason for hiding this comment

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

uh-oh.

Copy link
Contributor

Choose a reason for hiding this comment

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

we could change this to

TRY {
    return new JsonnetVm();
} CATCH("jsonnet_make")

It'll probably behavior better then.

Copy link
Contributor

Choose a reason for hiding this comment

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

(move the { } out of the macro, that is)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was able to tell clang-format that TRY ... CATCH is a block (as it is, no changes to the macro).

std::string input;
input.assign(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>());
TRY std::ifstream f;
Copy link
Contributor

Choose a reason for hiding this comment

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

TRY { would fix this indentation too

cmd/jsonnet.cpp Outdated
// TODO(dcunnin): Remove deprecated --var, --env and -E
} else if (arg == "-V" || arg == "--ext-str"
|| arg == "--var" || arg == "--env" || arg == "-E") {
// TODO(dcunnin): Remove deprecated --var, --env and -E
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should do this: #327

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, that will help.

case '\t':
indent += 8;
break;
case '\t': indent += 8; break;
Copy link
Contributor

Choose a reason for hiding this comment

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

weird that it put it on the one line for this case but not the ' ' case above

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

clang-format can sometimes produce weird results. Some quick experimentation shows that the reason why the ' ' case is not put in one line is the comment below it. But I have no idea why.

case '6':
case '7':
case '8':
case '9': state = AFTER_DIGIT; break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Any way to get it to binpack the cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't find any.

@sparkprime
Copy link
Contributor

Ok I read the whole thing. Generally this is a big net improvement. The majority of changes are actually good, the rest are things that don't make it better / worse but were not consistent across the whole codebase originally. Probably I'd just do something random in each case. The only thing that's still worth looking into is the bin packing of cases in that floating point number lexing rule. However that's not a deal breaker by any means. Thanks for setting this up!

@sbarzowski
Copy link
Collaborator Author

(rebased)

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

Successfully merging this pull request may close these issues.

4 participants