-
Notifications
You must be signed in to change notification settings - Fork 116
Initial refactor for layer group implementation #288
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
Conversation
So, we need to merge #278 first to fix CI's clang-format version... |
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.
@lan496, great. Thanks!
#include <gtest/gtest.h> | ||
|
||
extern "C" { | ||
#include <math.h> |
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.
Don't use extern "C"
on std libraries. Let the cpp compiler optimize itself.
sym_free_symmetry(symmetry); | ||
cel_free_cell(cell); | ||
symmetry = NULL; | ||
cell = NULL; |
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.
NULL
-> nullptr
. Also you mean to assert it not assign it here.
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.
At least for me, I am not familiar with C++. If we want to write in C, is there any good way to do 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 would say that writing in C++, especially for these tests it is more advantageous because:
- you get a clear trace of how/what fails
- you decouple potential C memory issues, e.g. not pre-allocating pointers from tests result errors
In these case I only have 2 simple suggestions:
- Use
std::array
and continue to use the C interface as you would.
The reason for this is that you allow in the future to use a wider range of built-in tools like range-for loops similar to python, that make the code much easier to read and detangle. - Check with the IDE's suggestion.
The NULL
-> nullptr
is suggested by CLion and that is because the NULL
definition in C is different then C++'s nullptr
and C++ uses the latter for enforcing that a pointer is truely null and not a pointer assigned to (void*)0
. Think of the issues of comparing addresses of empty strings (&"" == &""
). That is not guaranteed to give true
and depends on the compiler's optimization.
Other than that there is a genuine error here in that it is supposed to be ASSERT_EQ(symmetry == nullptr)
and not an assignment symmetry = nullptr
.
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 see your point. But I want to confine this PR about the algorithmic side for the layer-group implementation for now. After we adopt C11 in develop branch, the corresponding refactoring would be awesome.
} | ||
|
||
TEST(test_delaunay, test_delaunay_reduce_layer) { | ||
double min_lattice[3][3]; |
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 always paranoid with C code. Fo you need to explicitly deallocate these? I prefer to use std::vector
to be safe and pass c_pointer to C functions and use reinterpret_cast when needed
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.
Do you comment in general? Or some specific point?
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.
Both. In this case with fixed sized arrays like that, you don't need to use de-allocator?
And in general I am paranoid. E.g. if there is thread_local/static pointer and if for example someone uses the python api and the pointer is replaced without proper de-allocation, then that's a sure way to get memory leaks. If we impose and fix the issues specified by the compiler warnings in -Wall
, clang-tidy
inspection, and use the necessary sanitizations, then I would be less paranoid about such hypothetical issues.
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 see your point. It is uneasy to take a balance of feature developments and fixing holes. I want to merge this PR without going far from the original aim of PR.
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 understand, but at the very least new code should not introduce new holes. Whenever you are touching a part of a code that your IDE/clang-tidy and any other tools tells you to fix, then do a quick fix for that, otherwise the holes are never addressed.
See for example the warnings already displayed by github in the Files changed
tab. (Amazed that github is able to do that 😮)
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.
Can invalid use of pointers to arrays with different qualifiers in ISO C before C2X [-Wpedantic]
and pointers to arrays with different qualifiers are incompatible in ISO C [-Wpedantic]
be postponed to fix? Or just removing const
can be a solution?
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 did a bit of experimentation and research and tl;dr: we don't need to address "invalid * with different qualifiers".
There aren't concerning warnings in this PR now and I did a clang-tidy
test
$ pre-commit run --hook-stage manual clang-tidy --from-ref origin/develop --to-ref lan496/layer-group-refactor
clang-tidy...............................................................Passed
All seem well for this PR at least on this front. There are still the conversations that I have not marked as resolved which should be addressed and I'm all 👍 for now.
Actually concerning warnings
The actual concerning warnings are not in this
src/refinement.c:1451:9: warning: 'mat_multiply_matrix_di3' accessing 72 bytes in a region of size 24
src/spacegroup.c:1150:51: warning: 'j' may be used uninitialized
About "invalid * with different qualifiers"
According to the wording in the C standard pre C23, it is interpreted that:
const double matrix[2][3]
which is a pointer to a (double-typed) pointer has the const such that it is a pointer to a (double-typed) const pointer, instead of the more natural pointer to a (const double-typed) pointer, meaning that in the former you cannot change where a slice is pointing to and in the latter you cannot change the values of the matrix array.
But in reality no compiler is following the logic of the former and instead they all follow the latter which is consistent with C++ standard as well. And in C23 they have changed the wording so that it is correctly interpreted as the latter (values are constant, and not pointers to the array slice is constant). Indeed if you set CMAKE_C_STANDARD=23
these warnings go away.
Here is a more detailed example of my tests:
#include <stdio.h>
void test_const_matrix(const int matrix[][2]){
int vector[2] = {2,3};
// matrix[0][0] = 2;
matrix = vector;
matrix = &vector;
}
void test_const_vector(const int vector[2]){
// vector[0] = 2;
}
void test_matrix(int matrix[][2]){
int vector[2] = {2,3};
matrix[0][0] = 2;
matrix = vector;
matrix = &vector;
}
void test_vector(int vector[2]){
vector[0] = 2;
}
int main() {
int t_matrix[2][2] = {0};
int t_vector[2] = {0};
test_const_matrix(t_matrix);
test_const_vector(t_vector);
test_matrix(t_matrix);
test_vector(t_vector);
printf("Hello, World!\n");
return 0;
}
Here test_const_matrix
would fail as expected with matrix[0][0]=2
even though the wording would indicate otherwise even when I bump down the standard. Although I did not test with antiquated gcc
compilers to confirm this behaviour is true in old compilers as well. (Note that in the example given there are additional warnings about invalid operations that should be considered).
I'll leave this conversation unresolved for now, just for visibility.
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.
Thanks @LecrisUT.
For warning: 'mat_multiply_matrix_di3' accessing 72 bytes in a region of size 24
, I have really no idea.
For warning: 'j' may be used uninitialized
, this is OK, which is confirmed by assert.
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.
For
warning: 'j' may be used uninitialized
, this is OK, which is confirmed by assert.
Not really, I will explain in a PR where I address the clang-tidy issues.
Codecov ReportPatch coverage:
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. Additional details and impacted files@@ Coverage Diff @@
## develop #288 +/- ##
===========================================
+ Coverage 85.92% 86.09% +0.16%
===========================================
Files 23 23
Lines 6082 6069 -13
===========================================
- Hits 5226 5225 -1
+ Misses 856 844 -12
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry. |
db667c9
to
1de4f12
Compare
Now I can not follow the current status. To merge this PR, what is left to do and what will be postponed? |
1de4f12
to
724e786
Compare
After the all tests pass and @LecrisUT approves, this PR will be ready to be merged. |
This PR addresses the following as a by-product of my code reading for the layer group implementation.
match_hall_symbol_db