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

clang-format #882

Closed
wants to merge 10 commits into from
Closed

clang-format #882

wants to merge 10 commits into from

Conversation

srajangarg
Copy link
Contributor

Adresses #866 . Also fixes the trailing whitespace problem @Sumith1896 .

I will soon link a wiki page for the workflow to be followed.
Edit : Here it is https://github.com/symengine/symengine/wiki/Development-workflow-for-SymEngine

@srajangarg
Copy link
Contributor Author

@certik @isuruf I think the codecov failure can be ignored for this PR.

@Sumith1896
Copy link
Contributor

@srajangarg Could you also update this https://github.com/symengine/symengine/blob/master/doc/style_guide.md (design guide)? Will also help in reviewing this PR. Lot's of changes here.

@Sumith1896
Copy link
Contributor

@srajangarg I'm going through this PR slowly. Could you tell me tell me if there is maximum character limit for a line? Is it this ColumnLimit: 115? I found a longer line!

return Mul::from_dict(p->second, std::move(d2));
}
}
if (is_a<Pow>(*p->first)) {
insert(m, rcp_static_cast<const Pow>(p->first)->get_base(),
rcp_static_cast<const Pow>(p->first)->get_exp());
rcp_static_cast<const Pow>(p->first)->get_exp());
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this suggested by clang-format? What rule is this, so that I can document it.

Copy link
Contributor

Choose a reason for hiding this comment

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

The next line should start from (?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

whenever braking a function call, clang-format will align the arguments properly, i.e. the arguments in the new line will match the start of the arguments in the above line.

GOOD
my_func(mqwe, pter, lolqw,
        acc, cccs)

BAD
my_func(mssad, pwqe, loadsl,
           adsa, casd)

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. But how do you decide where exactly to move to next line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think clang-format won't mind as long as they are aligned, and below the column limit. For our own sake, we can ask the users to fit as many arguments on the initial line before moving to the next.

@Sumith1896
Copy link
Contributor

@srajangarg Thank you for this patient work. I have left some comments above, could you address them?

From what I understand, what you have taken care of is

  1. Single line if condition
if(something)
    oneline
  1. for loop
for (const auto &p : dict) {

}
  1. Space before and after operators
  2. Single line functions written as
type function { single-line-function-here }
  1. Functions
type functions(args)
{

}

The same for namespaces and classes.

  1. We write type &arg and not type& arg
    Same goes for type &function

The code sure is more uniform now. I still have following doubts

  1. At places we use //! and at some // for comments. I think this was due to part doxygen effort.
  2. Sometimes we use std::atanh() some other times ::tanh(). Wouldn't just tanh() work for the current namespace?

@certik @isuruf Could you also go through this, atleast my comments to decide on decisions where there is doubt.

@Sumith1896 Sumith1896 mentioned this pull request Mar 26, 2016
@srajangarg
Copy link
Contributor Author

@Sumith1896

The longest limit of a line is indeed 115, but clang-format gives us another option of PenaltyExcessCharacter, which find the right way to "break" a line. Say a line has 118 characters, it will see that its is not feasible to break the line just because of 3 characters, hence doesn't.

It also checks for trailing whitespaces.

I'm still not sure about comments and how they are being handled here.
Yes I agree about the namespace thing, but that I feel doesn't qualify as a code formatting issue.

@srajangarg
Copy link
Contributor Author

I will address the remaining comments/modifications soon.

Standard: Cpp11
TabWidth: 4
UseTab: Never

Copy link
Contributor

Choose a reason for hiding this comment

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

This line is not needed.

@srajangarg
Copy link
Contributor Author

@Sumith1896 @certik @isuruf Can you review this quickly? Everytime master changes, merge conflicts arise!

@Sumith1896
Copy link
Contributor

@srajangarg We'll try to block other PRs before this gets merged. Don't worry.


public:
public:
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change it, if needed.

@@ -129,6 +131,7 @@ matrix:
install:
- source bin/install_travis.sh
script:
- bin/travis_clang.sh
Copy link
Member

Choose a reason for hiding this comment

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

This should be run only on one Travis job

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should I do that?

Copy link
Member

Choose a reason for hiding this comment

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

Set an environment variable (eg: TEST_CLANG_FORMAT) to true in one of the jobs and check that the variable is true before calling this script.

…l single line functions except empty functions'
@certik
Copy link
Contributor

certik commented Mar 30, 2016

@srajangarg thanks for adding this. Let me play with this as well. E.g. I don't like for the functions to have { on the same line as the declaration, only if it's a method in the header file (update: looks like it is doing the right thing, I must have been looking at a wrong method)

@certik
Copy link
Contributor

certik commented Mar 30, 2016

@srajangarg, @Sumith1896, here is a plan for this PR that I propose:

  • Forget about master, just keep working on your branch until we are satisfied. This is an important change, let's get it right.
  • Then just take the formatting scripts, create a new PR off top of the latest master. Then add a commit that formats the whole tree.

That way it should be painless.

I am now installing llvm-3.7 and clang-3.7 from source and will play with this myself.

if [ $? -ne 0 ]; then
echo "[!] INCORRECT FORMATTING! $FILE" >&2
RETURN=1
fi
Copy link
Contributor

Choose a reason for hiding this comment

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

You are mixing tabs and spaces here. Use spaces.

@certik
Copy link
Contributor

certik commented Mar 30, 2016

I would like the column limit to be 80. However, when I changed:

--- a/.clang-format
+++ b/.clang-format
@@ -20,7 +20,7 @@ BreakBeforeBinaryOperators: All
 BreakBeforeBraces: Linux
 BreakBeforeTernaryOperators: true
 BreakConstructorInitializersBeforeComma: false
-ColumnLimit:     130
+ColumnLimit:     80
 ConstructorInitializerAllOnOneLineOrOnePerLine: false
 ConstructorInitializerIndentWidth: 4
 ContinuationIndentWidth: 4

it still has longer lines than 80... I see, one also has to do the following change:

--- a/.clang-format
+++ b/.clang-format
@@ -43,7 +43,7 @@ PenaltyBreakBeforeFirstCallParameter: 19
 PenaltyBreakComment: 22312
 PenaltyBreakFirstLessLess: 120
 PenaltyBreakString: 2123
-PenaltyExcessCharacter: 2
+PenaltyExcessCharacter: 1000000
 PenaltyReturnTypeOnItsOwnLine: 60
 PointerAlignment: Right
 SpaceAfterCStyleCast: false

Now it works as expected.

@srajangarg I suggest to do these two changes.

@certik
Copy link
Contributor

certik commented Mar 30, 2016

Another issue that I found is minor, e.g. this in add.cpp:

                    // We can steal the dictionary:
                    // Cast away const'ness, so that we can move
                    // 'dict_', since
                    // 'p->first' will be destroyed when 'd' is at the
                    // end of
                    // this function, so we "steal" its dict_ to avoid
                    // an
                    // unnecessary copy. We know the refcount_ is one,
                    // so
                    // nobody else is using the Mul except us.

should be this:

                    // We can steal the dictionary: Cast away const'ness, so
                    // that we can move 'dict_', since 'p->first' will be
                    // destroyed when 'd' is at the end of this function, so we
                    // "steal" its dict_ to avoid an unnecessary copy. We know
                    // the refcount_ is one, so nobody else is using the Mul
                    // except us.

Possible workaround: use /* */ instead of //, then clang-format seems to let it be. We can do such changes once this PR is merged.

@certik
Copy link
Contributor

certik commented Mar 30, 2016

Besides the column limit, I don't have any other suggestions. The clang-format seems to do a very good job and I agree with all the changes that it made.

@certik
Copy link
Contributor

certik commented Mar 30, 2016

Another issue that I found is that when I install clang-format from source (hashdist/hashstack#941), it creates a clang-format binary by default. So then the test_format_local.sh script fails, as it expects clang-format-3.7 on linux.

@srajangarg
Copy link
Contributor Author

@certik Don't you feel 80 characters is a bit too less? I know it is the defacto standard, but still. Also what about lines which are just longer than 80 characters?

What do you suggest about the clang-format executable issue? I feel it's very minor, and we can ask developers to install it via their respective package managers.

@certik
Copy link
Contributor

certik commented Mar 31, 2016

I don't think 80 is too narrow. Here is the patch: srajangarg#2, the code looks great. I don't understand your question about a line longer than 80 characters. I don't think there is any. If you want some pointers why 80 is a good choice (in the past and today), see e.g.:

https://www.emacswiki.org/emacs/EightyColumnRule
https://www.reddit.com/r/programming/comments/2nkntp/does_column_width_80_make_sense_in_2014/

It really is a great choice.

As to clang-format, all we need to do is make the script robust so that it works in most cases. Most likely using the following algorithm:

  1. if clang-format-3.7 exists in the path, use it
  2. if clang-format exists, check that it is the 3.7 version using:
$ clang-format -version
clang-format version 3.7.1 (tags/RELEASE_371/final)

and if so, use it.

Otherwise fail. (We can possibly even skip the version detection in step 2.)

Use 80 characters column limit
@srajangarg
Copy link
Contributor Author

@certik, considering all the inputs on this, should I go ahead with a new PR?

@certik
Copy link
Contributor

certik commented Mar 31, 2016

@srajangarg looks like you addressed all the issues raised in the PR? If so, I would go ahead and create a nice, clean new PR off top of the latest master as I suggested above.

@srajangarg srajangarg mentioned this pull request Mar 31, 2016
@srajangarg srajangarg closed this Mar 31, 2016
@srajangarg srajangarg deleted the clang-format branch June 6, 2016 05:43
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.

None yet

5 participants