Skip to content

Commit 1866d3f

Browse files
committed
lint refactor
1 parent b8f7550 commit 1866d3f

File tree

8 files changed

+31
-20
lines changed

8 files changed

+31
-20
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Demonstrate linking of
1919
* C and C++ program calling Fortran libraries
2020
* Fortran program calling C and C++ libraries
2121

22-
We assume the compilers are C++11 and Fortran 2018 capable.
22+
We assume the compilers are C++20 and Fortran 2018 capable.
2323

2424
This repo's examples are also known to work with:
2525

@@ -155,6 +155,7 @@ For arrays of struct, compiler
155155
[pragma may be needed](https://stackoverflow.com/questions/53161673/data-alignment-inside-a-structure-in-intel-fortran).
156156

157157
Valgrind needed suppression to avoid memory alignment (uninitialized memory) warnings for C struct input to nanosleep.
158+
158159
### MacOS
159160

160161
For MacOS with Apple's Clang and Homebrew GCC,

src/bool/logbool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "my_bool.h"
44

5-
bool logical_not(bool a)
5+
bool logical_not(const bool a)
66
{
77
std::cout << "c++ input boolean: " << a << " size " << sizeof(a) << "\n";
88

src/bool/my_bool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
extern "C" {
33
#endif
44

5-
bool logical_not(bool);
5+
bool logical_not(const bool);
66

77
bool bool_true();
88
bool bool_false();

test/allocate/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
Deallocating arrays in a Fortran procedure that were allocated in another Fortran procedure, both called from a C/C++ main program is defined in a compiler vendor-dependent way.
44
The Intel oneAPI compiler works with the "fancy_allocate" demo, while GCC Gfortran works with all the allocate examples.
5-
Cray Fortran ftn compiler currently doesn't work with the allocate in Fortran from C/C++ main program examples.
65

7-
To be more compiler vendor independent, the general recommendation when using a C/C++ main program is to allocate the memory in C/C++ e.g. via `malloc()` or in C++ via `new`, `vector` or `array` and then pass back and forth to Fortran.
6+
7+
To be more compiler vendor independent, the general recommendation when using a C/C++ main program is to allocate the memory in:
8+
9+
* C `malloc()`
10+
* C++ `std::vector`
11+
* C++ `std::array`
12+
* C++ `std::make_unique`
13+
14+
and then pass back and forth to Fortran.
15+
816
This allows memory to be managed fully, particularly with regard to freeing memory when no longer used to avoid memory leaks in long-running programs.

test/iso_fortran_binding/sampling.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,17 @@ namespace stdr = std::ranges;
9191
// [in] n: the number of samples
9292
// [out] samples: an array of (x,y) values, shape [n,2]
9393
//
94-
extern "C" void draw_random_samples(int n, CFI_cdesc_t *samples);
94+
extern "C" void draw_random_samples(const int n, CFI_cdesc_t *samples);
9595

9696
// Same procedure as the one below, but implemented in Fortran
97-
extern "C" double f_estimate_pi(int n);
97+
extern "C" double f_estimate_pi(const int n);
9898

9999
/* Calculate pi using the Monte-Carlo method.
100100
*
101101
* Random numbers are generated in Fortran just for the
102102
* sake of testing the F2018 enhanced C interoperability.
103103
*/
104-
double estimate_pi(int n)
104+
double estimate_pi(const int n)
105105
{
106106
CFI_CDESC_T(2) samples_;
107107
const auto samples = (CFI_cdesc_t *) &samples_;
@@ -111,7 +111,7 @@ double estimate_pi(int n)
111111
CFI_attribute_allocatable,
112112
CFI_type_double,
113113
0 /* ignored */,
114-
(CFI_rank_t) 2,
114+
static_cast<CFI_rank_t>(2),
115115
nullptr /* ignored */) );
116116

117117
// Make sure we don't forget to deallocate
@@ -127,8 +127,8 @@ double estimate_pi(int n)
127127
{
128128
// <!> Pointer arithmetic <!>
129129

130-
const double x = *( (double *) samples->base_addr + i);
131-
const double y = *( (double *) samples->base_addr + i + n);
130+
const double x = *( static_cast<double*>(samples->base_addr) + i);
131+
const double y = *( static_cast<double*>(samples->base_addr) + i + n);
132132

133133
return x*x + y*y < 1;
134134
};
@@ -151,16 +151,14 @@ double estimate_pi(int n)
151151
int ncircle = count_if( iota(0,n-1), inside_of_circle );
152152
#endif
153153

154-
return 4.0 * ((double) ncircle) / n;
154+
return 4.0 * (static_cast<double>(ncircle)) / n;
155155

156156
} // dealloc called here
157157

158158
int main(int argc, char const *argv[])
159159
{
160160

161-
int N = 1000;
162-
if (argc > 1)
163-
N = atoi(argv[1]);
161+
const int N = (argc > 1) ? atoi(argv[1]) : 1000;
164162

165163
std::cout << "pi = " << estimate_pi( N ) << '\n';
166164
std::cout << "pi = " << f_estimate_pi( N ) << '\n';

test/iso_fortran_binding/string_array.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ int main()
2323
// Use macro from ISO_Fortran_binding to set aside an address to "description" of string data
2424
CFI_CDESC_T(1) names;
2525

26-
CFI_establish((CFI_cdesc_t *)&names, nullptr,
26+
CFI_establish(static_cast<CFI_cdesc_t*>(&names), nullptr,
2727
CFI_attribute_pointer,
2828
CFI_type_char, 0, (CFI_rank_t)1, nullptr);
2929

3030
// Call the Fortran procedure for string manipulation
31-
get_names((CFI_cdesc_t *)&names);
31+
get_names(static_cast<CFI_cdesc_t*>(&names));
3232

3333
for (int i = 0; i < names.dim[0].extent; i++) {
34-
vs.push_back(std::string_view((char *)names.base_addr).substr(i * names.elem_len, names.elem_len));
34+
vs.push_back(std::string_view(
35+
static_cast<char*>(names.base_addr)).substr(
36+
i * names.elem_len, names.elem_len));
3537
}
3638
for (int i = 0; i < names.dim[0].extent; i++) {
3739
std::cout << vs[i] << "\n";

test/iso_fortran_binding/string_view.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
extern "C" {
99
void echo_c( const CFI_cdesc_t* Fstr )
1010
{
11-
auto view = std::string_view((char *)Fstr->base_addr).substr(0, Fstr->elem_len);
11+
auto view = std::string_view(
12+
static_cast<char*>(Fstr->base_addr)).substr(
13+
0, Fstr->elem_len);
1214
std::cout << view << "\n";
1315
}
1416
}

test/malloc/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ int main(void)
2828
free(x);
2929
free(x2);
3030

31-
printf("OK: C malloc new\n");
31+
printf("OK: C malloc\n");
3232

3333
return EXIT_SUCCESS;
3434
}

0 commit comments

Comments
 (0)