Skip to content

Commit

Permalink
style: add more const qualifiers to pointers and parentheses in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Sep 29, 2024
1 parent b116592 commit 3d78747
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 139 deletions.
15 changes: 12 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
---
BasedOnStyle: Google
UseTab: Never
IndentWidth: 4
ContinuationIndentWidth: 4
AccessModifierOffset: -3
AccessModifierOffset: -4
ColumnLimit: 100

Language: Cpp
Standard: c++17
AlignAfterOpenBracket: Align
AlignEscapedNewlines: Right
AllowAllArgumentsOnNextLine: false
Expand All @@ -12,6 +16,7 @@ AllowShortIfStatementsOnASingleLine: Never
BinPackArguments: false
BinPackParameters: false
BreakBeforeTernaryOperators: true
CommentPragmas: 'NOLINT(NEXTLINE|BEGIN|END)?\[.*\]'
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
Expand All @@ -34,13 +39,17 @@ IndentPPDirectives: None
InsertBraces: true
InsertTrailingCommas: Wrapped
LambdaBodyIndentation: Signature
MacroBlockBegin: '^Py_BEGIN_(ALLOW_THREADS|CRITICAL_SECTION(2)?(_MUT)?)$'
MacroBlockEnd: '^Py_END_(ALLOW_THREADS|CRITICAL_SECTION(2)?(_MUT)?)$'
PackConstructorInitializers: NextLine
PointerAlignment: Right
QualifierAlignment: Custom
QualifierOrder: [friend, static, inline, const, constexpr, volatile, type, restrict]
QualifierOrder:
[friend, static, inline, const, constexpr, volatile, type, restrict]
ReferenceAlignment: Right
RemoveParentheses: MultipleParentheses
RemoveParentheses: ReturnStatement
RemoveSemicolon: true
SeparateDefinitionBlocks: Leave
SkipMacroDefinitionBody: false
SortIncludes: CaseSensitive
SpaceAroundPointerQualifiers: Both
40 changes: 20 additions & 20 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ InheritParentConfig: true
FormatStyle: file
UseColor: true
WarningsAsErrors: '*'
Checks: '
bugprone-*,
-bugprone-easily-swappable-parameters,
clang-analyzer-*,
cppcoreguidelines-*,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-pro-type-reinterpret-cast,
hicpp-*,
misc-*,
modernize-*,
-modernize-use-trailing-return-type,
performance-*,
readability-*,
-readability-redundant-inline-specifier,
-readability-redundant-member-init,
-readability-identifier-length,
'
CheckOptions:
misc-include-cleaner.IgnoreHeaders: 'python.*/.*;pybind11/.*;include/.*'
HeaderFilterRegex: '^include/.*$'
...

Checks: |
bugprone-*,
-bugprone-easily-swappable-parameters,
clang-analyzer-*,
cppcoreguidelines-*,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-pro-type-reinterpret-cast,
hicpp-*,
misc-*,
modernize-*,
-modernize-use-trailing-return-type,
performance-*,
readability-*,
-readability-redundant-inline-specifier,
-readability-redundant-member-init,
-readability-identifier-length,
CheckOptions:
misc-include-cleaner.IgnoreHeaders: 'python.*/.*;pybind11/.*;include/.*'
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if(OPTREE_CXX_WERROR)
endif()

string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_PREFIX_SIZE)
add_definitions("-DSOURCE_PATH_PREFIX_SIZE=${SOURCE_PATH_PREFIX_SIZE}")
add_definitions("-DSOURCE_PATH_PREFIX_SIZE=(${SOURCE_PATH_PREFIX_SIZE})")

function(system)
set(options STRIP)
Expand Down
2 changes: 2 additions & 0 deletions CPPLINT.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
linelength=100
filter=-readability/nolint
filter=-readability/braces
filter=-whitespace/indent
filter=-whitespace/newline
filter=-whitespace/parens
filter=-build/c++11
filter=-build/include_order
8 changes: 4 additions & 4 deletions include/critical_section.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ inline bool Py_IsConstant(PyObject* x) { return Py_IsNone(x) || Py_IsTrue(x) ||
#define Py_IsConstant(x) Py_IsConstant(x)

class scoped_critical_section {
public:
public:
scoped_critical_section() = delete;

#ifdef Py_GIL_DISABLED
Expand All @@ -65,15 +65,15 @@ class scoped_critical_section {
scoped_critical_section(scoped_critical_section&&) = delete;
scoped_critical_section& operator=(scoped_critical_section&&) = delete;

private:
private:
#ifdef Py_GIL_DISABLED
PyObject* m_ptr{nullptr};
PyCriticalSection m_critical_section{};
#endif
};

class scoped_critical_section2 {
public:
public:
scoped_critical_section2() = delete;

#ifdef Py_GIL_DISABLED
Expand Down Expand Up @@ -111,7 +111,7 @@ class scoped_critical_section2 {
scoped_critical_section2(scoped_critical_section2&&) = delete;
scoped_critical_section2& operator=(scoped_critical_section2&&) = delete;

private:
private:
#ifdef Py_GIL_DISABLED
PyObject* m_ptr1{nullptr};
PyObject* m_ptr2{nullptr};
Expand Down
45 changes: 20 additions & 25 deletions include/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ limitations under the License.
#include <string> // std::string

#ifndef SOURCE_PATH_PREFIX_SIZE
#define SOURCE_PATH_PREFIX_SIZE 0
#define SOURCE_PATH_PREFIX_SIZE (0)
#endif

#ifndef FILE_RELPATH
Expand All @@ -36,9 +36,11 @@ limitations under the License.
namespace optree {

class InternalError : public std::logic_error {
public:
public:
explicit InternalError(const std::string& msg) : std::logic_error{msg} {}
InternalError(const std::string& msg, const std::string& file, const std::size_t& lineno)
explicit InternalError(const std::string& msg,
const std::string& file,
const std::size_t& lineno)
: InternalError([&msg, &file, &lineno]() -> std::string {
std::ostringstream oss{};
oss << msg << " (at file " << file << ":" << lineno << ")\n\n"
Expand All @@ -49,32 +51,25 @@ class InternalError : public std::logic_error {

} // namespace optree

#define INTERNAL_ERROR1_(message) throw optree::InternalError(message, FILE_RELPATH, __LINE__)
#define INTERNAL_ERROR1_(message) throw optree::InternalError((message), FILE_RELPATH, __LINE__)
#define INTERNAL_ERROR0_() INTERNAL_ERROR1_("Unreachable code.")
#define INTERNAL_ERROR(...) /* NOLINTNEXTLINE[whitespace/parens] */ \
#define INTERNAL_ERROR(...) \
VA_FUNC2_(__0 __VA_OPT__(, ) __VA_ARGS__, INTERNAL_ERROR1_, INTERNAL_ERROR0_)(__VA_ARGS__)

#define EXPECT2_(condition, message) \
if (!(condition)) [[unlikely]] \
INTERNAL_ERROR1_(message)
if (!(condition)) [[unlikely]] { \
INTERNAL_ERROR1_(message); \
}
#define EXPECT0_() INTERNAL_ERROR0_()
#define EXPECT1_(condition) EXPECT2_(condition, "`" #condition "` failed.")
#define EXPECT_(...) /* NOLINTNEXTLINE[whitespace/parens] */ \
#define EXPECT1_(condition) EXPECT2_((condition), "`" #condition "` failed.")
#define EXPECT_(...) \
VA_FUNC3_(__0 __VA_OPT__(, ) __VA_ARGS__, EXPECT2_, EXPECT1_, EXPECT0_)(__VA_ARGS__)

#define EXPECT_TRUE(condition, ...) \
EXPECT_(condition __VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_FALSE(condition, ...) \
EXPECT_(!(condition)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_EQ(a, b, ...) \
EXPECT_((a) == (b)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_NE(a, b, ...) \
EXPECT_((a) != (b)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_LT(a, b, ...) \
EXPECT_((a) < (b)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_LE(a, b, ...) \
EXPECT_((a) <= (b)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_GT(a, b, ...) \
EXPECT_((a) > (b)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_GE(a, b, ...) \
EXPECT_((a) >= (b)__VA_OPT__(, ) __VA_ARGS__) // NOLINT[whitespace/parens]
#define EXPECT_TRUE(condition, ...) EXPECT_((condition)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_FALSE(condition, ...) EXPECT_(!(condition)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_EQ(a, b, ...) EXPECT_((a) == (b)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_NE(a, b, ...) EXPECT_((a) != (b)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_LT(a, b, ...) EXPECT_((a) < (b)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_LE(a, b, ...) EXPECT_((a) <= (b)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_GT(a, b, ...) EXPECT_((a) > (b)__VA_OPT__(, ) __VA_ARGS__)
#define EXPECT_GE(a, b, ...) EXPECT_((a) >= (b)__VA_OPT__(, ) __VA_ARGS__)
4 changes: 2 additions & 2 deletions include/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ limitations under the License.
#ifdef Py_GIL_DISABLED

class pymutex {
public:
public:
pymutex() = default;
~pymutex() = default;

Expand All @@ -36,7 +36,7 @@ class pymutex {
void lock() { PyMutex_Lock(&mutex); }
void unlock() { PyMutex_Unlock(&mutex); }

private:
private:
PyMutex mutex{0};
};

Expand Down
4 changes: 2 additions & 2 deletions include/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ constexpr PyTreeKind kStructSequence = PyTreeKind::StructSequence;

// Registry of custom node types.
class PyTreeTypeRegistry {
public:
public:
PyTreeTypeRegistry() = default;
~PyTreeTypeRegistry() = default;

Expand Down Expand Up @@ -116,7 +116,7 @@ class PyTreeTypeRegistry {
// Clear the registry on cleanup.
static void Clear();

private:
private:
template <bool NoneIsLeaf>
static PyTreeTypeRegistry *Singleton();

Expand Down
14 changes: 7 additions & 7 deletions include/treespec.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using ssize_t = py::ssize_t;

// The maximum depth of a pytree.
#ifndef Py_C_RECURSION_LIMIT
#define Py_C_RECURSION_LIMIT 1000
#define Py_C_RECURSION_LIMIT (1000)
#endif
#ifndef PYPY_VERSION
constexpr ssize_t MAX_RECURSION_DEPTH = std::min(1000, Py_C_RECURSION_LIMIT);
Expand Down Expand Up @@ -77,10 +77,10 @@ py::module_ GetCxxModule(const std::optional<py::module_> &module = std::nullopt
// the interior nodes are tuples, lists, dictionaries, or user-defined containers, and the leaves
// are other objects.
class PyTreeSpec {
private:
private:
struct Node;

public:
public:
PyTreeSpec() = default;
~PyTreeSpec() = default;

Expand Down Expand Up @@ -232,7 +232,7 @@ class PyTreeSpec {
// Used in tp_traverse for GC support.
static int PyTpTraverse(PyObject *self_base, visitproc visit, void *arg);

private:
private:
using RegistrationPtr = PyTreeTypeRegistry::RegistrationPtr;

struct Node {
Expand Down Expand Up @@ -358,7 +358,7 @@ class PyTreeSpec {
std::string registry_namespace);

class ThreadIndentTypeHash {
public:
public:
using is_transparent = void;
size_t operator()(const std::pair<const PyTreeSpec *, std::thread::id> &p) const;
};
Expand All @@ -370,7 +370,7 @@ class PyTreeSpec {
};

class PyTreeIter {
public:
public:
explicit PyTreeIter(const py::object &tree,
const std::optional<py::function> &leaf_predicate,
const bool &none_is_leaf,
Expand All @@ -397,7 +397,7 @@ class PyTreeIter {
// Used in tp_traverse for GC support.
static int PyTpTraverse(PyObject *self_base, visitproc visit, void *arg);

private:
private:
const py::object m_root;
std::vector<std::pair<py::object, ssize_t>> m_agenda;
const std::optional<py::function> m_leaf_predicate;
Expand Down
Loading

0 comments on commit 3d78747

Please sign in to comment.