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

Minimal updated subset of changes from #2017 (clang-format) #2977

Merged
merged 5 commits into from
Jan 23, 2024

Conversation

Jakuje
Copy link
Member

@Jakuje Jakuje commented Jan 10, 2024

Based on last comment in #2017 from @popovec I pulled the .clang-format and some minimal changes to the separate branch, that can be merged if we will agree on that format.

I played with it a bit more, but I am still not completely satisfied with the current configuration as it sometimes formats some complicated constructs in non-intuitive way. But it is something we can try to tune later (or rewrite the code to avoid these constructs).

In any case, review here just to make sure the .clang-format, .editorconfig and description of the format in contributing.md matches would be welcomed. Similarly if we should add some more examples or some look odd.

Checklist
  • Documentation is added or updated

@Jakuje Jakuje changed the title Minimal updated subset of changes from #2017 Minimal updated subset of changes from #2017 (clang-format) Jan 10, 2024
Copy link
Member

@frankmorgner frankmorgner left a comment

Choose a reason for hiding this comment

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

If you fix the remark from the new formatting CI workflow, this is good to go

src/libopensc/card-openpgp.c Show resolved Hide resolved
@popovec
Copy link
Member

popovec commented Jan 11, 2024

I noticed that we currently have `TabWidth 4' defined. I don't have a problem with this setting, but I'll just remind you of the years-old debate about TAB in the Linux kernel.

https://public-inbox.org/git/[email protected]/

probably the most explanatory email from the mentioned debate:

https://public-inbox.org/git/[email protected]/#t

@Jakuje
Copy link
Member Author

Jakuje commented Jan 11, 2024

I noticed that we currently have `TabWidth 4' defined. I don't have a problem with this setting, but I'll just remind you of the years-old debate about TAB in the Linux kernel.

Oh, The editorconfig has tab width 8 and clang 4. This is clearly an inconsistency I wanted to catch before merging this. But it should not have any effect in the resulting formatting itself.

https://public-inbox.org/git/[email protected]/

probably the most explanatory email from the mentioned debate:

https://public-inbox.org/git/[email protected]/#t

The indentation debates will never be over. I read through this mail, but it really did does not bring much insights to what is the reason and what are advantages. There was already a lot of discussion in the #2017.

I was used to tab size 4 most of the time, but I agree that tab width 8 have some advantages such as avoiding too much indentation or better readability, especially if we use spaces for continuation indentation. So I do not have strong opinion.

While trying to reformat some code with current configuration, there are still things I do not like I mentioned in the previous PR, so I will try again here if there will be some other insights:

  • The ContinuationIndentWidth: 16 with IndentWidth: 8 and TabWidth: 8 indents the structures with two tabs making it needlessly deep. The alignment of "columns" looks off as it uses mixture of tabs and spaces without any sense (or at least I do not see it there). Example:
 	CK_ATTRIBUTE template[] = {
-		{ CKA_CLASS, &class, sizeof(class) },
-		{ CKA_KEY_TYPE, &keyType, sizeof(keyType) },
-		{ CKA_LABEL, label, sizeof(label)-1 },
-		{ CKA_ENCRYPT, &_true, sizeof(_true) },
-		{ CKA_DECRYPT, &_true, sizeof(_true) },
-		{ CKA_VALUE, key, keysize },
-		{ CKA_VALUE_LEN, &keylen, sizeof(keylen) },
+			{CKA_CLASS,	    &class,   sizeof(class)	 },
+			{CKA_KEY_TYPE,  &keyType, sizeof(keyType)	},
+			{CKA_LABEL,	    label,	   sizeof(label) - 1},
+			{CKA_ENCRYPT,   &_true,   sizeof(_true)	   },
+			{CKA_DECRYPT,   &_true,   sizeof(_true)	   },
+			{CKA_VALUE,	    key,	 keysize		},
+			{CKA_VALUE_LEN, &keylen,	 sizeof(keylen)   },
 	};
  • I do not like the continuation indentation of parameters of function call inside of if such as demonstrated here. Regardless the UseTab: Always it indents with a combination of 2 tabs and 4 spaces:
 	if (!find_object(session, CKO_PUBLIC_KEY, &hWrappingKey,
-			 opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
+			    opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
 		if (!find_object(session, CKO_SECRET_KEY, &hWrappingKey,
-				 opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
+				    opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
 			util_fatal("Public/secret key (wrapping key) not found");
  • With else if its 3 tabs and 3 spaces:
 		} else if (!find_object(session, CKO_SECRET_KEY, &object,
-				 opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
+					   opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
 			util_fatal("Secret key not found");
  • some continuation is still a mystery for me. The split string is indented with one tab and 3 spaces and the remaining argument with normal 2 tabs:
 				util_fatal("Unknown key pair type %s, valid key types for mechanism GOSTR3410 are GOSTR3410-2001:{A,B,C},"
-					" GOSTR3410-2012-256:{A,B,C,D}, GOSTR3410-2012-512:{A,B,C}", type);
+					   " GOSTR3410-2012-256:{A,B,C,D}, GOSTR3410-2012-512:{A,B,C}",
+						type);
 

(now it tries to reformat also the json files with p11test results, but thats something we can handle)

martinpaljak and others added 5 commits January 11, 2024 10:31
This allows for voluntary formatting with clang-format (or
IDE tools like clang-format plugin for VS Code).

Once a suitable formatting configuration is found, automatic formatting
can be configured

Tweaked by Jakub Jelen
Usage:

git config blame.ignoreRevsFile .ignoreRevsFile
@frankmorgner
Copy link
Member

I think most of the time we avoided a mixture of spaces and tabs by replacing 8 spaces with a tab so that IndentWidth and TabWidth should both be 8

The ContinuationIndentWidth: 16 with IndentWidth: 8 and TabWidth: 8 indents the structures with two tabs making it needlessly deep

I think ContinuationIndentWidth should be 8, making it one additional indent level.

  • I do not like the continuation indentation of parameters of function call inside of if
  • With else if its 3 tabs and 3 spaces:

Your examples above originally contain a mixture of some tabs and a single leading space. That single space is superfluous and may confuse clang-format. could you please try removing this space first and then apply clang-format?

  • some continuation is still a mystery for me

That particular example looks strange, indeed.

@Jakuje
Copy link
Member Author

Jakuje commented Jan 11, 2024

The ContinuationIndentWidth: 16 with IndentWidth: 8 and TabWidth: 8 indents the structures with two tabs making it needlessly deep

I think ContinuationIndentWidth should be 8, making it one additional indent level.

My reading of the documentation is the value as used in the clang-format is not additional indent, but the whole indent so we need 16 if we want to use two tabs for continuation:

https://clang.llvm.org/docs/ClangFormatStyleOptions.html#continuationindentwidth

The problem is that, I think, also the struct initialization is considered a continuation here. It should be solved with BracedInitializerIndentWidth: 8, but its only in clang 17 (i dont have it locally now to test).

  • I do not like the continuation indentation of parameters of function call inside of if
  • With else if its 3 tabs and 3 spaces:

Your examples above originally contain a mixture of some tabs and a single leading space. That single space is superfluous and may confuse clang-format. could you please try removing this space first and then apply clang-format?

It did not help. I think it tries to align something somewhere, but I do not see what and where.

I tried also different values for UseTab, but no improvement:

 		} else if (!find_object(session, CKO_PRIVATE_KEY, &object,
-				opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
+					   opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
 			if (!find_object(session, CKO_SECRET_KEY, &object,
-					opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
+					    opt_object_id_len ? opt_object_id : NULL, opt_object_id_len, 0))
 				util_fatal("Private/secret key not found");
  • some continuation is still a mystery for me

That particular example looks strange, indeed.

I though we could get around this with AlwaysBreakBeforeMultilineStrings by breaking before that long string, but it the output still looks the same -- broken string aligned with tab and spaces to the opening quote and the rest with the continuation indent ...

@frankmorgner
Copy link
Member

I don't see an easy way out of the formatting problems of clang-format. What about adding the configuration files as suggested right now, but allowing the workflow to fail?

@Jakuje
Copy link
Member Author

Jakuje commented Jan 15, 2024

I don't see an easy way out of the formatting problems of clang-format. What about adding the configuration files as suggested right now, but allowing the workflow to fail?

I am ok with that. Maybe adding also a note to the pipeline that its not mandatory to follow all the suggestions. Unfortunately, github does not have any way to make the result warning as githlab does so there is only a way to let it fail (drawing attention to that by PR contributor) or ignore the results altogether, making the results technically invisible for anyone searching for that. From this, I believe the first option (the current state in this PR) is more reasonable.

@frankmorgner
Copy link
Member

If I'm not mistaken, than a mandatory formatter workflow that doesn't work reliably will constantly show our project as "not building" on the main page. To avoid that, a succeeding job with somewhat invisible results should be better. Since we review all pull request, we can bring up the formatting issues by pointing to the results of this job if needed.

@Jakuje
Copy link
Member Author

Jakuje commented Jan 16, 2024

If I'm not mistaken, than a mandatory formatter workflow that doesn't work reliably will constantly show our project as "not building" on the main page.

If you mean the badges, in the readme, they are tied to the particular workflow file (now linux, osx, ...). This is separate workflow file that does not have to show up on the main page. And actually it does not even have to run on the branches/pushes, but only on the PRs as proposed right now.

Copy link
Member

@frankmorgner frankmorgner left a comment

Choose a reason for hiding this comment

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

OK, fine. Thanks for the clarification.

@frankmorgner frankmorgner merged commit 6f00246 into OpenSC:master Jan 23, 2024
36 of 37 checks passed
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

4 participants