Skip to content

Commit 1ee6e70

Browse files
committed
Tidier
1 parent a05ddec commit 1ee6e70

18 files changed

+291
-157
lines changed

.clang-format

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
BasedOnStyle: LLVM
2+
3+
IndentWidth: 4
4+
5+
BreakAfterReturnType: ExceptShortType
6+
BreakBeforeBraces: Custom
7+
BraceWrapping:
8+
AfterCaseLabel: true
9+
AfterFunction: true
10+
AfterClass: false
11+
AfterControlStatement: false
12+
AfterEnum: false
13+
AfterNamespace: false
14+
BeforeElse: true
15+
IndentBraces: false
16+
17+
# Don’t reflow or wrap lines
18+
ColumnLimit: 0
19+
20+
# Leave initializer lists alone
21+
Cpp11BracedListStyle: False
22+
23+
AllowShortCaseExpressionOnASingleLine: true
24+
AllowShortIfStatementsOnASingleLine: WithoutElse
25+
26+
# Switch/case
27+
IndentCaseLabels: true
28+
IndentCaseBlocks: true
29+
30+
IndentPPDirectives: BeforeHash
31+
32+
PointerAlignment: Left
33+
34+
SortIncludes: false
35+
SpaceInEmptyBlock: true

.clang-tidy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Checks: >
2+
-*,
3+
bugprone-*,
4+
modernize-use-override,
5+
clang-analyzer-*,
6+
-bugprone-easily-swappable-parameters,
7+
-clang-diagnostic-error,
8+
-clang-analyzer-cplusplus.NewDelete,
9+
10+
WarningsAsErrors: '' # keep warnings first, promote to errors later
11+
HeaderFilterRegex: 'src/.*'
12+
FormatStyle: none

ASTNode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _PYC_ASTNODE_H
2-
#define _PYC_ASTNODE_H
1+
#ifndef PYC_ASTNODE_H
2+
#define PYC_ASTNODE_H
33

44
#include "pyc_module.h"
55
#include <list>

ASTree.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
130130
&& opcode != Pyc::POP_JUMP_IF_TRUE_A
131131
&& opcode != Pyc::POP_JUMP_FORWARD_IF_TRUE_A
132132
&& opcode != Pyc::POP_BLOCK) {
133-
else_pop = false;
134-
135133
PycRef<ASTBlock> prev = curblock;
136134
while (prev->end() < pos
137135
&& prev->blktype() != ASTBlock::BLK_MAIN) {
@@ -2480,7 +2478,6 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
24802478
case Pyc::RESUME_A:
24812479
case Pyc::INSTRUMENTED_RESUME_A:
24822480
/* We just entirely ignore this / no-op */
2483-
break;
24842481
case Pyc::CACHE:
24852482
/* These "fake" opcodes are used as placeholders for optimizing
24862483
certain opcodes in Python 3.11+. Since we have no need for
@@ -2645,11 +2642,10 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
26452642
if (parent.type() == ASTNode::NODE_BINARY) {
26462643
PycRef<ASTBinary> binParent = parent.cast<ASTBinary>();
26472644
if (binParent->right() == child) {
2648-
if (binParent->op() == ASTBinary::BIN_SUBTRACT &&
2649-
binChild->op() == ASTBinary::BIN_ADD)
2650-
return 1;
2651-
else if (binParent->op() == ASTBinary::BIN_DIVIDE &&
2652-
binChild->op() == ASTBinary::BIN_MULTIPLY)
2645+
if ((binParent->op() == ASTBinary::BIN_SUBTRACT &&
2646+
binChild->op() == ASTBinary::BIN_ADD) ||
2647+
(binParent->op() == ASTBinary::BIN_DIVIDE &&
2648+
binChild->op() == ASTBinary::BIN_MULTIPLY))
26532649
return 1;
26542650
}
26552651
return binChild->op() - binParent->op();
@@ -2663,6 +2659,9 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
26632659
PycRef<ASTUnary> unChild = child.cast<ASTUnary>();
26642660
if (parent.type() == ASTNode::NODE_BINARY) {
26652661
PycRef<ASTBinary> binParent = parent.cast<ASTBinary>();
2662+
2663+
// NOLINTBEGIN(bugprone-branch-clone)f
2664+
// Not sure if reordering the branches could lead to some bugs
26662665
if (binParent->op() == ASTBinary::BIN_LOG_AND ||
26672666
binParent->op() == ASTBinary::BIN_LOG_OR)
26682667
return -1;
@@ -2672,6 +2671,7 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
26722671
return 1;
26732672
else
26742673
return -1;
2674+
// NOLINTEND(bugprone-branch-clone)
26752675
} else if (parent.type() == ASTNode::NODE_COMPARE) {
26762676
return (unChild->op() == ASTUnary::UN_NOT) ? 1 : -1;
26772677
} else if (parent.type() == ASTNode::NODE_UNARY) {
@@ -2696,23 +2696,18 @@ static void print_ordered(PycRef<ASTNode> parent, PycRef<ASTNode> child,
26962696
PycModule* mod, std::ostream& pyc_output)
26972697
{
26982698
if (child.type() == ASTNode::NODE_BINARY ||
2699-
child.type() == ASTNode::NODE_COMPARE) {
2699+
child.type() == ASTNode::NODE_COMPARE ||
2700+
child.type() == ASTNode::NODE_UNARY) {
27002701
if (cmp_prec(parent, child) > 0) {
27012702
pyc_output << "(";
27022703
print_src(child, mod, pyc_output);
27032704
pyc_output << ")";
2704-
} else {
2705-
print_src(child, mod, pyc_output);
27062705
}
2707-
} else if (child.type() == ASTNode::NODE_UNARY) {
2708-
if (cmp_prec(parent, child) > 0) {
2709-
pyc_output << "(";
2710-
print_src(child, mod, pyc_output);
2711-
pyc_output << ")";
2712-
} else {
2706+
else {
27132707
print_src(child, mod, pyc_output);
27142708
}
2715-
} else {
2709+
}
2710+
else {
27162711
print_src(child, mod, pyc_output);
27172712
}
27182713
}
@@ -2773,6 +2768,9 @@ void print_formatted_value(PycRef<ASTFormattedValue> formatted_value, PycModule*
27732768
case ASTFormattedValue::ASCII:
27742769
pyc_output << "!a";
27752770
break;
2771+
default:
2772+
// error case
2773+
break;
27762774
}
27772775
if (formatted_value->conversion() & ASTFormattedValue::HAVE_FMT_SPEC) {
27782776
pyc_output << ":" << formatted_value->format_spec().cast<ASTObject>()->object().cast<PycString>()->value();
@@ -2849,7 +2847,6 @@ void print_src(PycRef<ASTNode> node, PycModule* mod, std::ostream& pyc_output)
28492847
pyc_output << ", ";
28502848
pyc_output << "**";
28512849
print_src(call->kw(), mod, pyc_output);
2852-
first = false;
28532850
}
28542851
pyc_output << ")";
28552852
}

ASTree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _PYC_ASTREE_H
2-
#define _PYC_ASTREE_H
1+
#ifndef PYC_ASTREE_H
2+
#define PYC_ASTREE_H
33

44
#include "ASTNode.h"
55

CMakeLists.txt

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project(pycdc)
44
set(CMAKE_CXX_STANDARD 11)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7-
# Debug options.
7+
# Debug options
88
option(ENABLE_BLOCK_DEBUG "Enable block debugging" OFF)
99
option(ENABLE_STACK_DEBUG "Enable stack debugging" OFF)
1010

@@ -16,16 +16,15 @@ if (ENABLE_STACK_DEBUG)
1616
add_definitions(-DSTACK_DEBUG)
1717
endif()
1818

19-
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
20-
set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-error=shadow -Werror ${CMAKE_CXX_FLAGS}")
19+
# Compiler warnings
20+
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
21+
add_compile_options(-Wall -Wextra -Werror -Wno-error=shadow)
2122
elseif(MSVC)
22-
set(CMAKE_CXX_FLAGS "/WX ${CMAKE_CXX_FLAGS}")
23+
add_compile_options(/WX)
2324
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
2425
endif()
2526

26-
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
27-
28-
add_library(pycxx STATIC
27+
file(GLOB SRC_LIB_CPP CONFIGURE_DEPENDS
2928
bytecode.cpp
3029
data.cpp
3130
pyc_code.cpp
@@ -34,47 +33,91 @@ add_library(pycxx STATIC
3433
pyc_object.cpp
3534
pyc_sequence.cpp
3635
pyc_string.cpp
37-
bytes/python_1_0.cpp
38-
bytes/python_1_1.cpp
39-
bytes/python_1_3.cpp
40-
bytes/python_1_4.cpp
41-
bytes/python_1_5.cpp
42-
bytes/python_1_6.cpp
43-
bytes/python_2_0.cpp
44-
bytes/python_2_1.cpp
45-
bytes/python_2_2.cpp
46-
bytes/python_2_3.cpp
47-
bytes/python_2_4.cpp
48-
bytes/python_2_5.cpp
49-
bytes/python_2_6.cpp
50-
bytes/python_2_7.cpp
51-
bytes/python_3_0.cpp
52-
bytes/python_3_1.cpp
53-
bytes/python_3_2.cpp
54-
bytes/python_3_3.cpp
55-
bytes/python_3_4.cpp
56-
bytes/python_3_5.cpp
57-
bytes/python_3_6.cpp
58-
bytes/python_3_7.cpp
59-
bytes/python_3_8.cpp
60-
bytes/python_3_9.cpp
61-
bytes/python_3_10.cpp
62-
bytes/python_3_11.cpp
63-
bytes/python_3_12.cpp
64-
bytes/python_3_13.cpp
36+
bytes/*.cpp
37+
)
38+
39+
file(GLOB SRC_MAIN CONFIGURE_DEPENDS
40+
*.h
41+
bytes/*.h
42+
pycdas.cpp
43+
pycdc.cpp
44+
ASTree.cpp
45+
ASTNode.cpp
46+
${SRC_LIB_CPP})
47+
48+
file(GLOB SRC_ALL CONFIGURE_DEPENDS
49+
bytecode_ops.inl # This file does not contain compilable code
50+
${SRC_MAIN}
51+
)
52+
53+
# Enable/disable targets/tools
54+
option(ENABLE_CLANG_FORMAT "Provide `format` and `format-check` targets using clang-format" ON)
55+
option(ENABLE_CLANG_TIDY "Provide `tidy-all` target using clang-tidy" ON)
56+
57+
# Export compile commands for clang-tidy / language servers
58+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
59+
60+
if(ENABLE_CLANG_FORMAT)
61+
find_program(CLANG_FORMAT_EXECUTABLE
62+
NAMES clang-format
63+
)
64+
if(CLANG_FORMAT_EXECUTABLE)
65+
add_custom_target(format
66+
COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file -i ${SRC_ALL}
67+
COMMENT "Running clang-format on project sources"
68+
VERBATIM
69+
)
70+
71+
add_custom_target(format-check
72+
COMMAND ${CLANG_FORMAT_EXECUTABLE} -style=file --dry-run -Werror ${SRC_ALL}
73+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
74+
COMMENT "Checking clang-format (non-zero exit if formatting required)"
75+
VERBATIM
76+
USES_TERMINAL # Enables propagating error code
77+
)
78+
endif()
79+
endif()
80+
81+
if(ENABLE_CLANG_TIDY)
82+
find_program(CLANG_TIDY_EXECUTABLE
83+
NAMES clang-tidy
84+
)
85+
if(CLANG_TIDY_EXECUTABLE)
86+
# Minimal clang-tidy wrapper. Adjust checks in your .clang-tidy file.
87+
# Semicolon-separated list is required by CMake for the wrapper.
88+
set(CMAKE_CXX_CLANG_TIDY
89+
"${CLANG_TIDY_EXECUTABLE};-p=${CMAKE_BINARY_DIR}"
90+
)
91+
# If you prefer to apply clang-tidy to specific targets only, remove the
92+
# global set above and use set_target_properties(... CXX_CLANG_TIDY ...)
93+
# Create a custom target to run clang-tidy over main sources
94+
add_custom_target(tidy-all
95+
COMMAND ${CLANG_TIDY_EXECUTABLE} -p ${CMAKE_BINARY_DIR} ${SRC_MAIN}
96+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
97+
COMMENT "Running clang-tidy on project sources"
98+
VERBATIM
99+
)
100+
endif()
101+
endif()
102+
103+
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
104+
105+
# Library
106+
add_library(pycxx STATIC
107+
${SRC_LIB_CPP}
65108
)
66109

110+
# Executable: pycdas
67111
add_executable(pycdas pycdas.cpp)
68-
target_link_libraries(pycdas pycxx)
112+
target_link_libraries(pycdas PRIVATE pycxx)
69113

70-
install(TARGETS pycdas
71-
RUNTIME DESTINATION bin)
114+
install(TARGETS pycdas RUNTIME DESTINATION bin)
72115

116+
# Executable: pycdc
73117
add_executable(pycdc pycdc.cpp ASTree.cpp ASTNode.cpp)
74-
target_link_libraries(pycdc pycxx)
118+
target_link_libraries(pycdc PRIVATE pycxx)
75119

76-
install(TARGETS pycdc
77-
RUNTIME DESTINATION bin)
120+
install(TARGETS pycdc RUNTIME DESTINATION bin)
78121

79122
find_package(Python3 3.6 COMPONENTS Interpreter)
80123
if(Python3_FOUND)

FastStack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef _PYC_FASTSTACK_H
2-
#define _PYC_FASTSTACK_H
1+
#ifndef PYC_FASTSTACK_H
2+
#define PYC_FASTSTACK_H
33

44
#include "ASTNode.h"
55
#include <stack>

0 commit comments

Comments
 (0)