-
Notifications
You must be signed in to change notification settings - Fork 440
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
Conversation
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. |
There was a problem hiding this 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: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
core/static_analysis.cpp
Outdated
* \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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why commented out?
There was a problem hiding this comment.
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.
@davidzchen will be happy about this |
SpacesInParentheses: false | ||
SpacesInSquareBrackets: false | ||
Standard: Auto | ||
TabWidth: 8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be 4?
There was a problem hiding this comment.
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.
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. |
Did you try it with 100 line width yet? |
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 |
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] << ")"; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
core/lexer_test.cpp
Outdated
void testLex(const char* name, | ||
const char* input, | ||
const std::list<Token>& tokens, | ||
namespace |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 &) \ |
There was a problem hiding this comment.
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 { }
There was a problem hiding this comment.
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.
core/libjsonnet.cpp
Outdated
@@ -277,16 +284,14 @@ const char *jsonnet_version(void) | |||
|
|||
JsonnetVm *jsonnet_make(void) | |||
{ | |||
TRY | |||
return new JsonnetVm(); | |||
TRY return new JsonnetVm(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uh-oh.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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).
core/libjsonnet.cpp
Outdated
std::string input; | ||
input.assign(std::istreambuf_iterator<char>(f), | ||
std::istreambuf_iterator<char>()); | ||
TRY std::ifstream f; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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! |
(rebased) |
No description provided.