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

Add .clang-format file with default Google style #357

Closed
wants to merge 2 commits into from

Conversation

phcerdan
Copy link
Collaborator

@phcerdan phcerdan commented May 15, 2020

A lot of IDE's, and editors plugins interface well with clang-tools.
This adds the file .clang-format in the top folder (the expected place).
It is populated with the default google style.

It was generated with the command:

clang-format -style=google -dump-config > .clang-format

With

clang-format --version
clang-format version 10.0.0

This should conform with the same style using cpplint.

See #317


This change is Reviewable

A lot of IDE's, and editors plugins interface well with clang-tools.
This adds the file .clang-format in the top folder (the expected place).
It is populated with the default google style.

It was generated with the command:

```
clang-format -style=google -dump-config > .clang-format
```

With

```
clang-format --version
clang-format version 10.0.0
```

This should conform with the same style using `cpplint`.
@maxitg maxitg requested review from maxitg and aokellermann May 15, 2020 13:41
@maxitg maxitg self-assigned this May 15, 2020
@maxitg maxitg added feature New functionality, or change in existing functionality infrastructure Has to do with changes to the development process, e.g., build scripts, CI, testing utilities labels May 15, 2020
Copy link
Owner

@maxitg maxitg left a comment

Choose a reason for hiding this comment

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

This is great! But keep in mind, we have exceptions described here. Specifically, we at least need the equivalent of cpplint.py --linelength=120 --filter=-legal/copyright. It does not look like clang-format checks for legal/copyright, but we need lines, and check other comments below.

Reviewed 1 of 1 files at r1.
Reviewable status: all files reviewed, 17 unresolved discussions (waiting on @aokellermann and @phcerdan)


.clang-format, line 15 at r1 (raw file):

AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never

Should we set this to Empty? @aokellermann?


.clang-format, line 17 at r1 (raw file):

AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All

Should we set this to Empty as well? Not sure, but sounds like it should be consistent with AllowShortBlocksOnASingleLine.


.clang-format, line 19 at r1 (raw file):

AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse

I think we should set this to Always because it's dangerous otherwise. We don't want code like this:

if (a) return;
else
  return;

because it could easily become before anybody notices:

if (a) return;
else
  a = false;
  return;

.clang-format, line 21 at r1 (raw file):

AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None

The docs say it's deprecated, so we should probably remove it.


.clang-format, line 26 at r1 (raw file):

AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true

Should we set these two to false to make arguments more obvious? Not sure.


.clang-format, line 27 at r1 (raw file):

BinPackArguments: true
BinPackParameters: true
BraceWrapping:

This is ignored because BreakBeforeBraces is not set to BS_Custom


.clang-format, line 53 at r1 (raw file):

BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit:     80

This should be 120.


.clang-format, line 54 at r1 (raw file):

BreakStringLiterals: true
ColumnLimit:     80
CommentPragmas:  '^ IWYU pragma:'

If I understand how this works, I think we should set this to '^ NO LINT' to be consistent with cpplint.py.


.clang-format, line 61 at r1 (raw file):

Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: true

I think we should set this to false and make the alignment consistent everywhere.


.clang-format, line 65 at r1 (raw file):

ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:

I don't think we need this since we are not using any of those.


.clang-format, line 91 at r1 (raw file):

IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true

I think we should remove the JavaScript* configs since we don't plan to have any. We can decide on the style if/when we do.


.clang-format, line 100 at r1 (raw file):

ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true

Same for ObjC*.


.clang-format, line 122 at r1 (raw file):

    CanonicalDelimiter: ''
    BasedOnStyle:    google
  - Language:        TextProto

Same here.


.clang-format, line 155 at r1 (raw file):

SpacesInAngles:  false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true

I don't think this applies to C++, but if it does, it should be false.


.clang-format, line 160 at r1 (raw file):

SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard:        Auto

Should we say c++17 explicitly to avoid any potential incorrect detection?


.clang-format, line 161 at r1 (raw file):

SpaceBeforeSquareBrackets: false
Standard:        Auto
StatementMacros:

I don't think we need any of these either.


.clang-format, line 164 at r1 (raw file):

  - Q_UNUSED
  - QT_REQUIRE_VERSION
TabWidth:        8

We don't use tabs, so we should either remove it, or at least set it to 2.

Copy link
Collaborator Author

@phcerdan phcerdan left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 18 unresolved discussions (waiting on @aokellermann and @phcerdan)

a discussion (no related file):
All those options are used. It would be somehow cleaner to only write the changes that differ from those.
And uncomment BasedOnStyle: Google.


Copy link
Owner

@maxitg maxitg left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @aokellermann and @phcerdan)

a discussion (no related file):

Previously, phcerdan (Pablo Hernandez-Cerdan) wrote…

All those options are used. It would be somehow cleaner to only write the changes that differ from those.
And uncomment BasedOnStyle: Google.

I agree, we should do that.


@phcerdan phcerdan force-pushed the add_clang_format_google branch from 08967db to c2ff67b Compare May 15, 2020 17:56
@phcerdan phcerdan force-pushed the add_clang_format_google branch from c2ff67b to 510d947 Compare May 15, 2020 18:03
Copy link
Collaborator Author

@phcerdan phcerdan left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 files reviewed, 6 unresolved discussions (waiting on @aokellermann, @maxitg, and @phcerdan)

a discussion (no related file):

Previously, maxitg (Max Piskunov) wrote…

I agree, we should do that.

When you completely decide about option, we can commit and apply the clang-format to all files. Right now check an example on these options applied to Expressions.cpp:

image.png



.clang-format, line 53 at r1 (raw file):

Previously, maxitg (Max Piskunov) wrote…

This should be 120.

Done.


.clang-format, line 54 at r1 (raw file):

Previously, maxitg (Max Piskunov) wrote…

If I understand how this works, I think we should set this to '^ NO LINT' to be consistent with cpplint.py.

It seems to be NOLINT:.*

@maxitg maxitg mentioned this pull request May 18, 2020
@maxitg maxitg closed this in #361 May 19, 2020
maxitg added a commit that referenced this pull request May 19, 2020
## Changes
* This closes the C++ Style Refactor project.
* Resolves #316.
* Resolves #317.
* Resolves #319.
* Closes #357.
* Reformats all C++ code according to [Google C++ Style Guide].
* Adds `.clang-format` file specifying the exceptions to the Google style guide.
* Adds lint.sh script which prints a diff generated by `clang-format` (if any) and the errors generated by `cpplint`, and allows one to apply formatting in place with `./lint.sh -i`.
* Adds Lint step to CI before Build.
* Updates .gitbub/CONTRIBUTING.md with instructions on how to use lint.sh.
* Adds the missing test file to the Xcode project.

## Comments
* Linting in CI is not parallelized and is using the same Docker image because it will eventually include linting of the Wolfram Language code as well as described in #247.
* Bin packing is now disallowed in addition to the existing style exceptions.
* All `cpplint` errors are fixed as well, not just the formatting.
* `#include <chrono>` is not allowed by `cpplint` for Chromium-specific reasons, so added `// NOLINT` there.
* Renamed `SetTest.cpp` -> `Set_test.cpp` because the suffix appears to be hard-coded to `cpplint`.
* Before the formatting, the code was mostly following this style (imperfectly):
```
---
Language: Cpp
BasedOnStyle: Google
ColumnLimit: 120
BinPackArguments: false
BinPackParameters: false
AllowShortFunctionsOnASingleLine: Empty
IndentWidth: 4
NamespaceIndentation: All
AccessModifierOffset: -4
FixNamespaceComments: false
SpaceAfterTemplateKeyword: false
BreakConstructorInitializers: AfterColon
SpacesBeforeTrailingComments: 1
...
```

## Examples
* Break formatting/code style somewhere in the code, and run `./lint.sh`. For example, we can remove `explicit` on line 22 of Match.cpp, and add a spurious space on the line below:
```
> ./lint.sh 
--- libSetReplace/Match.cpp
+++ formatted
@@ -24 +24 @@
-  bool  operator()(const MatchPtr& a, const MatchPtr& b) const {
+  bool operator()(const MatchPtr& a, const MatchPtr& b) const {

libSetReplace/Match.cpp:22:  Single-parameter constructors should be marked explicit.  [runtime/explicit] [5]
Done processing libSetReplace/Match.cpp
Total errors found: 1
```

* We can run `./lint.sh -i` to fix the space automatically:
```
> ./lint.sh -i
--- libSetReplace/Match.cpp
+++ formatted
@@ -24 +24 @@
-  bool  operator()(const MatchPtr& a, const MatchPtr& b) const {
+  bool operator()(const MatchPtr& a, const MatchPtr& b) const {

libSetReplace/Match.cpp:22:  Single-parameter constructors should be marked explicit.  [runtime/explicit] [5]
Done processing libSetReplace/Match.cpp
Total errors found: 1
```

* The output is the same, however, if we run `./lint.sh` again, there will be no diff:
```
> ./lint.sh   
libSetReplace/Match.cpp:22:  Single-parameter constructors should be marked explicit.  [runtime/explicit] [5]
Done processing libSetReplace/Match.cpp
Total errors found: 1
```

* Finally, if we put explicit back in, there will be no output, which means everything is ok:
```
> ./lint.sh
```
@maxitg maxitg mentioned this pull request Jun 4, 2020
maxitg added a commit that referenced this pull request Jun 4, 2020
## Changes
* Changes the order of headers in `Set_test.cpp`, so that `cpplint` does not fail an Mac.
* Adds the corresponding rules to `.clang-format` (taken from #357).

## Comments
* Apparently, the difference is due to version 1.5.0 that was released 4 days ago. Updating the CI now.

## Examples
* Before:
```
➜  SetReplace git:(master) ./lint.sh
libSetReplace/test/Set_test.cpp:5:  Found C++ system header after other system header.
  Should be: Set_test.h, c system, c++ system, other.  [build/include_order] [4]
Done processing libSetReplace/test/Set_test.cpp
Total errors found: 1
```
* After:
```
➜  SetReplace git:(fixTestHeaderOrder) ./lint.sh
```

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/maxitg/setreplace/373)
<!-- Reviewable:end -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality, or change in existing functionality infrastructure Has to do with changes to the development process, e.g., build scripts, CI, testing utilities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants