Skip to content

Commit b2f42b2

Browse files
robertodrilfreddy
authored andcommitted
Use #pragma once instead of preprocessor guards (#93)
Other tasks achieved by this commit: - Fix hooks for shebang and Py3 compatibility. - Reformat some header files to comply with code style.
1 parent 6af5237 commit b2f42b2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+511
-678
lines changed

.githooks/license_maintainer.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,39 @@
2323

2424
from datetime import date
2525
import glob
26-
import mmap
2726
import os
2827
import re
2928
import shutil
3029
import tempfile
3130

31+
3232
def add_header(filepath, header, YEAR, AUTHORS):
3333
"""
3434
Add or update header in source file
3535
"""
3636
tmpdir = tempfile.gettempdir()
3737
tmpfil = os.path.join(tmpdir, os.path.basename(filepath) + '.bak')
3838
shutil.copy2(filepath, tmpfil)
39-
with open(tmpfil, 'r+b') as tmp:
39+
with open(tmpfil, 'r') as tmp:
4040
inpt = tmp.readlines()
4141
output = []
4242

4343
# Check if header is already present
4444
present = re.compile('PCMSolver, an API for the Polarizable Continuum Model')
45-
if filter(present.search, inpt):
45+
if list(filter(present.search, inpt)):
4646
# Check if year and authors in current file are up to date
4747
toupdate = re.compile(r'{0} (?!{1} {2}).*\n'.format('Copyright \(C\)', YEAR, AUTHORS))
48-
if filter(toupdate.search, inpt):
49-
print('Updating header in {}'.format(filepath))
48+
if list(filter(toupdate.search, inpt)):
49+
print(('Updating header in {}'.format(filepath)))
5050
# Check to preserve '#!' at the top of the file
5151
if len(inpt) > 0 and inpt[0].startswith('#!'):
5252
output.append(inpt[0] + '\n')
5353
inpt = inpt[1:]
5454
regex = re.compile(r'Copyright \(C\).*\n')
5555
repl = r'Copyright (C) ' + YEAR + ' ' + AUTHORS + '\n'
56-
output.extend(map(lambda x: re.sub(regex, repl, x), inpt))
56+
output.extend([re.sub(regex, repl, x) for x in inpt])
5757
else:
58-
print('Adding header in {}'.format(filepath))
58+
print(('Adding header in {}'.format(filepath)))
5959
# Check to preserve '#!' at the top of the file
6060
if len(inpt) > 0 and inpt[0].startswith('#!'):
6161
output.append(inpt[0] + '\n')
@@ -68,8 +68,8 @@ def add_header(filepath, header, YEAR, AUTHORS):
6868
try:
6969
f = open(filepath, 'w')
7070
f.writelines(output)
71-
except IOError, err:
72-
print('Something went wrong trying to add header to {}: {}'.format(filepath, err))
71+
except IOError as err:
72+
print(('Something went wrong trying to add header to {}: {}'.format(filepath, err)))
7373
finally:
7474
f.close()
7575
os.remove(tmpfil)
@@ -79,12 +79,12 @@ def prepare_header(stub, YEAR, AUTHORS):
7979
"""
8080
Update year and author information in license header template
8181
"""
82-
with open(stub, 'r+b') as l:
82+
with open(stub, 'r') as l:
8383
header = l.read()
8484
# Insert correct YEAR and AUTHORS in stub
8585
rep = {'YEAR' : YEAR, 'AUTHORS' : AUTHORS}
86-
rep = dict((re.escape(k), v) for k, v in rep.iteritems())
87-
pattern = re.compile("|".join(rep.keys()))
86+
rep = dict((re.escape(k), v) for k, v in rep.items())
87+
pattern = re.compile("|".join(list(rep.keys())))
8888
header = pattern.sub(lambda m: rep[re.escape(m.group(0))], header)
8989
return header
9090

@@ -94,26 +94,26 @@ def file_license(attributes):
9494
Obtain dictionary { file : license } from .gitattributes
9595
"""
9696
file_license = {}
97-
with open(attributes, 'r+b') as f:
97+
with open(attributes, 'r') as f:
9898
# Read in .gitattributes
99-
tmp_mm = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
99+
tmp = f.read()
100100
# Removing all comment lines and other attributes
101-
pattern = re.compile("|".join([r'(?m)^\#.*\n?', r'^((?!licensefile).)*$']))
102-
gitattributes = re.sub(pattern, '', tmp_mm).split()
101+
pattern = re.compile(r'(?m)^\#.*\n?|^((?!licensefile).)*$')
102+
gitattributes = re.sub(pattern, '', tmp).split()
103103
# Obtain list of files
104104
fil = [x for x in gitattributes if not 'licensefile' in x]
105105
# Remove licensefile= from strings
106106
lic = [re.sub(r'licensefile\=', '', x) for x in gitattributes if 'licensefile' in x]
107107
# Create list of blacklisted files
108-
blacklist = [fname for key, value in dict(zip(fil, lic)).items()
108+
blacklist = [fname for key, value in list(dict(list(zip(fil, lic))).items())
109109
if value == '!licensefile'
110110
for fname in glob.glob(key)]
111111
# Now create a dictionary with the files to be considered for
112112
# license header manipulation
113-
file_license = { key : value
114-
for k, value in dict(zip(fil, lic)).items()
115-
for key in glob.glob(k)
116-
if key not in blacklist}
113+
file_license = {key: value
114+
for k, value in list(dict(list(zip(fil, lic))).items())
115+
for key in glob.glob(k)
116+
if key not in blacklist}
117117
return file_license
118118

119119

@@ -129,7 +129,7 @@ def license_maintainer():
129129

130130
headerize = file_license(os.path.join(project_root_dir, '.gitattributes'))
131131

132-
for fname, license in headerize.items():
132+
for fname, license in list(headerize.items()):
133133
# Prepare header
134134
header = prepare_header(os.path.join(project_root_dir, license), YEAR, AUTHORS)
135135
add_header(fname, header, YEAR, AUTHORS)

.githooks/pre-commit-clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
# git pre-commit hook that runs an clang-format stylecheck.
44
# Features:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

33
python .githooks/license_maintainer.py

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
of the ASC.
1414
- Restored compilation for g++ < v5.1.
1515

16+
### Changed
17+
18+
- Use [`#pragma once`](https://en.wikipedia.org/wiki/Pragma_once) instead of
19+
`#ifndef, #define, #endif` to guard against multiple inclusion of header
20+
files.
21+
1622
## [Version 1.1.10] - 2017-03-27
1723

1824
### Changed

api/PCMInput.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
* PCMSolver API, see: <http://pcmsolver.readthedocs.io/>
2222
*/
2323

24-
#ifndef PCMINPUT_H
25-
#define PCMINPUT_H
24+
#pragma once
2625

2726
// To cope with the fact that C doesn't have bool as primitive type
2827
#ifndef pcmsolver_bool_t_DEFINED
@@ -78,5 +77,3 @@ typedef struct PCMInput {
7877
/// Type of Green's function requested outside the cavity.
7978
char outside_type[22];
8079
} PCMInput;
81-
82-
#endif // PCMINPUT_H

api/pcmsolver.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
* PCMSolver API, see: <http://pcmsolver.readthedocs.io/>
2222
*/
2323

24-
#ifndef PCMSOLVER_H_INCLUDED
25-
#define PCMSOLVER_H_INCLUDED
24+
#pragma once
2625

2726
#include <stddef.h>
27+
2828
#include "PCMInput.h"
2929
#include "PCMSolverExport.h"
3030

@@ -271,5 +271,3 @@ PCMSolver_API void pcmsolver_write_timings(pcmsolver_context_t * context);
271271
#ifdef __cplusplus
272272
}
273273
#endif
274-
275-
#endif /* PCMSOLVER_H_INCLUDED */

doc/programmers/coding-standards.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ Follow these guidelines to decide whether to include or forward declare:
3131

3232
.. code-block:: cpp
3333
34-
#ifndef MYCLASS_HPP
35-
#define MYCLASS_HPP
34+
#pragma once
3635
3736
//==============================
3837
// Forward declared dependencies
@@ -57,8 +56,6 @@ Follow these guidelines to decide whether to include or forward declare:
5756
// don't do anything about MyFriend
5857
};
5958
60-
#endif // MYCLASS_HPP
61-
6259
6360
Proper overloading of `operator<<`
6461
----------------------------------

include/Citation.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
* PCMSolver API, see: <http://pcmsolver.readthedocs.io/>
2222
*/
2323

24-
#ifndef CITATION_HPP
25-
#define CITATION_HPP
24+
#pragma once
2625

2726
#include <algorithm>
2827
#include <sstream>
@@ -45,7 +44,8 @@ inline std::string citation_message() {
4544
std::string version(TOSTRING(PROJECT_VERSION));
4645
rest << "\n" << std::endl;
4746
rest << " * PCMSolver, an API for the Polarizable Continuum Model electrostatic "
48-
"problem. Version " << version << std::endl;
47+
"problem. Version "
48+
<< version << std::endl;
4949
rest << " Main authors: R. Di Remigio, L. Frediani, K. Mozgawa" << std::endl;
5050
rest << " With contributions from:" << std::endl;
5151
rest << " R. Bast (CMake framework)" << std::endl;
@@ -54,9 +54,11 @@ inline std::string citation_message() {
5454
<< std::endl;
5555
rest << " Theory: - J. Tomasi, B. Mennucci and R. Cammi:" << std::endl;
5656
rest << " \"Quantum Mechanical Continuum Solvation Models\", Chem. "
57-
"Rev., 105 (2005) 2999" << std::endl;
57+
"Rev., 105 (2005) 2999"
58+
<< std::endl;
5859
rest << " PCMSolver is distributed under the terms of the GNU Lesser General "
59-
"Public License." << std::endl;
60+
"Public License."
61+
<< std::endl;
6062
return rest.str();
6163
}
6264

@@ -67,5 +69,3 @@ inline std::string version_info() {
6769
retval << " * Git last commit author : " << GIT_COMMIT_AUTHOR << std::endl;
6870
return retval.str();
6971
}
70-
71-
#endif // CITATION_HPP

include/Cxx11Workarounds.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
* PCMSolver API, see: <http://pcmsolver.readthedocs.io/>
2222
*/
2323

24-
#ifndef CXX11WORKAROUNDS_HPP
25-
#define CXX11WORKAROUNDS_HPP
24+
#pragma once
2625

2726
/*! \file Cxx11Workarounds.hpp
2827
* \brief Provide hacks and workarounds for C++11
@@ -73,8 +72,8 @@ using std::to_string;
7372
} /* end namespace pcm */
7473
#else /* HAS_CXX11*/
7574
/* Smart pointers workarounds */
76-
#include <boost/shared_ptr.hpp>
7775
#include <boost/make_shared.hpp>
76+
#include <boost/shared_ptr.hpp>
7877
namespace pcm {
7978
using boost::shared_ptr;
8079
using boost::make_shared;
@@ -105,8 +104,8 @@ namespace pcm {
105104
using boost::array;
106105
} /* end namespace pcm */
107106
/* std::to_string workarounds */
108-
#include <string>
109107
#include <boost/lexical_cast.hpp>
108+
#include <string>
110109
namespace pcm {
111110
template <typename Source> std::string to_string(const Source & arg) {
112111
return boost::lexical_cast<std::string>(arg);
@@ -153,5 +152,3 @@ template <typename Source> std::string to_string(const Source & arg) {
153152
#else /* HAS_CXX11_NORETURN */
154153
#define __noreturn
155154
#endif /* HAS_CXX11_NORETURN */
156-
157-
#endif /* CXX11WORKAROUNDS_HPP */

include/ErrorHandling.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
* PCMSolver API, see: <http://pcmsolver.readthedocs.io/>
2222
*/
2323

24-
#ifndef ERRORHANDLING_HPP
25-
#define ERRORHANDLING_HPP
24+
#pragma once
2625

2726
#include <cassert>
2827
#include <cstdio>
@@ -76,7 +75,8 @@
7675
std::ostringstream _err; \
7776
_err << "PCMSolver fatal error.\n" \
7877
<< " In function " << __func__ << " at line " << __LINE__ << " of file " \
79-
<< __FILE__ << "\n" << message << std::endl; \
78+
<< __FILE__ << "\n" \
79+
<< message << std::endl; \
8080
std::fprintf(stderr, "%s\n", _err.str().c_str()); \
8181
std::exit(EXIT_FAILURE); \
8282
}
@@ -91,5 +91,3 @@
9191
#include <boost/static_assert.hpp>
9292
#define PCMSOLVER_STATIC_ASSERT(arg, msg) BOOST_STATIC_ASSERT_MSG(arg, msg)
9393
#endif /* HAS_CXX11_STATIC_ASSERT */
94-
95-
#endif /* ERRORHANDLING_HPP */

0 commit comments

Comments
 (0)