Skip to content

Commit eed45dd

Browse files
committed
Built "make format" target format target using clang-format based on arrow-cpp and clean up existing codebase
Change-Id: I06131ffe683e5d126cd7d95bf9525bcf818341bc
1 parent 7d2c28f commit eed45dd

Some content is hidden

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

43 files changed

+422
-334
lines changed

CMakeLists.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,26 @@ set(THIRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
2222
# Must be declared in the top-level CMakeLists.txt.
2323
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
2424

25-
# For thirdparty libs: googletest
26-
# include(toolchain)
27-
2825
set(CMAKE_MACOSX_RPATH 1)
2926
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
3027

28+
find_package(ClangTools)
29+
if ("$ENV{CMAKE_EXPORT_COMPILE_COMMANDS}" STREQUAL "1" OR CLANG_TIDY_FOUND)
30+
# Generate a Clang compile_commands.json "compilation database" file for use
31+
# with various development tools, such as Vim's YouCompleteMe plugin.
32+
# See http://clang.llvm.org/docs/JSONCompilationDatabase.html
33+
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
34+
endif()
35+
36+
find_program(CCACHE_FOUND ccache)
37+
if(CCACHE_FOUND)
38+
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
39+
set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
40+
endif(CCACHE_FOUND)
41+
42+
# ----------------------------------------------------------------------
43+
# CMake options
44+
3145
# Top level cmake dir
3246
if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
3347
option(PANDAS_BUILD_TESTS
@@ -578,6 +592,19 @@ if (UNIX)
578592
`find ${CMAKE_CURRENT_SOURCE_DIR}/src -name \\*.cc -or -name \\*.h`)
579593
endif (UNIX)
580594

595+
############################################################
596+
# "make format" and "make check-format" targets
597+
############################################################
598+
if (${CLANG_FORMAT_FOUND})
599+
# runs clang format and updates files in place.
600+
add_custom_target(format ${BUILD_SUPPORT_DIR}/run-clang-format.sh ${CMAKE_CURRENT_SOURCE_DIR} ${CLANG_FORMAT_BIN} 1
601+
`find ${CMAKE_CURRENT_SOURCE_DIR}/src -name \\*.cc -or -name \\*.h | sed -e '/_generated/g'`)
602+
603+
# runs clang format and exits with a non-zero exit code if any files need to be reformatted
604+
add_custom_target(check-format ${BUILD_SUPPORT_DIR}/run-clang-format.sh ${CMAKE_CURRENT_SOURCE_DIR} ${CLANG_FORMAT_BIN} 0
605+
`find ${CMAKE_CURRENT_SOURCE_DIR}/src -name \\*.cc -or -name \\*.h | sed -e '/_generated/g'`)
606+
endif()
607+
581608
#############################################################
582609
# Test linking
583610

build-support/run-clang-format.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# Runs clang format in the given directory
16+
# Arguments:
17+
# $1 - Path to the source tree
18+
# $2 - Path to the clang format binary
19+
# $3 - Apply fixes (will raise an error if false and not there where changes)
20+
# $ARGN - Files to run clang format on
21+
#
22+
SOURCE_DIR=$1
23+
shift
24+
CLANG_FORMAT=$1
25+
shift
26+
APPLY_FIXES=$1
27+
shift
28+
29+
# clang format will only find its configuration if we are in
30+
# the source tree or in a path relative to the source tree
31+
pushd $SOURCE_DIR
32+
if [ "$APPLY_FIXES" == "1" ]; then
33+
$CLANG_FORMAT -i $@
34+
else
35+
36+
NUM_CORRECTIONS=`$CLANG_FORMAT -output-replacements-xml $@ | grep offset | wc -l`
37+
if [ "$NUM_CORRECTIONS" -gt "0" ]; then
38+
echo "clang-format suggested changes, please run 'make format'!!!!"
39+
exit 1
40+
fi
41+
fi
42+
popd

cmake_modules/FindClangTools.cmake

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
# Tries to find the clang-tidy and clang-format modules
15+
#
16+
# Usage of this module as follows:
17+
#
18+
# find_package(ClangTools)
19+
#
20+
# Variables used by this module, they can change the default behaviour and need
21+
# to be set before calling find_package:
22+
#
23+
# ClangToolsBin_HOME -
24+
# When set, this path is inspected instead of standard library binary locations
25+
# to find clang-tidy and clang-format
26+
#
27+
# This module defines
28+
# CLANG_TIDY_BIN, The path to the clang tidy binary
29+
# CLANG_TIDY_FOUND, Whether clang tidy was found
30+
# CLANG_FORMAT_BIN, The path to the clang format binary
31+
# CLANG_TIDY_FOUND, Whether clang format was found
32+
33+
find_program(CLANG_TIDY_BIN
34+
NAMES clang-tidy-3.8 clang-tidy-3.7 clang-tidy-3.6 clang-tidy
35+
PATHS ${ClangTools_PATH} $ENV{CLANG_TOOLS_PATH} /usr/local/bin /usr/bin
36+
NO_DEFAULT_PATH
37+
)
38+
39+
if ( "${CLANG_TIDY_BIN}" STREQUAL "CLANG_TIDY_BIN-NOTFOUND" )
40+
set(CLANG_TIDY_FOUND 0)
41+
message("clang-tidy not found")
42+
else()
43+
set(CLANG_TIDY_FOUND 1)
44+
message("clang-tidy found at ${CLANG_TIDY_BIN}")
45+
endif()
46+
47+
find_program(CLANG_FORMAT_BIN
48+
NAMES clang-format-3.8 clang-format-3.7 clang-format-3.6 clang-format
49+
PATHS ${ClangTools_PATH} $ENV{CLANG_TOOLS_PATH} /usr/local/bin /usr/bin
50+
NO_DEFAULT_PATH
51+
)
52+
53+
if ( "${CLANG_FORMAT_BIN}" STREQUAL "CLANG_FORMAT_BIN-NOTFOUND" )
54+
set(CLANG_FORMAT_FOUND 0)
55+
message("clang-format not found")
56+
else()
57+
set(CLANG_FORMAT_FOUND 1)
58+
message("clang-format found at ${CLANG_FORMAT_BIN}")
59+
endif()

src/.clang-format

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
Language: Cpp
3+
# BasedOnStyle: Google
4+
AccessModifierOffset: -1
5+
AlignAfterOpenBracket: false
6+
AlignConsecutiveAssignments: false
7+
AlignEscapedNewlinesLeft: true
8+
AlignOperands: true
9+
AlignTrailingComments: true
10+
AllowAllParametersOfDeclarationOnNextLine: true
11+
AllowShortBlocksOnASingleLine: true
12+
AllowShortCaseLabelsOnASingleLine: false
13+
AllowShortFunctionsOnASingleLine: Inline
14+
AllowShortIfStatementsOnASingleLine: true
15+
AllowShortLoopsOnASingleLine: false
16+
AlwaysBreakAfterDefinitionReturnType: None
17+
AlwaysBreakBeforeMultilineStrings: true
18+
AlwaysBreakTemplateDeclarations: true
19+
BinPackArguments: true
20+
BinPackParameters: true
21+
BreakBeforeBinaryOperators: None
22+
BreakBeforeBraces: Attach
23+
BreakBeforeTernaryOperators: true
24+
BreakConstructorInitializersBeforeComma: false
25+
ColumnLimit: 90
26+
CommentPragmas: '^ IWYU pragma:'
27+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
28+
ConstructorInitializerIndentWidth: 4
29+
ContinuationIndentWidth: 4
30+
Cpp11BracedListStyle: true
31+
DerivePointerAlignment: false
32+
DisableFormat: false
33+
ExperimentalAutoDetectBinPacking: false
34+
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
35+
IndentCaseLabels: true
36+
IndentWidth: 2
37+
IndentWrappedFunctionNames: false
38+
KeepEmptyLinesAtTheStartOfBlocks: false
39+
MacroBlockBegin: ''
40+
MacroBlockEnd: ''
41+
MaxEmptyLinesToKeep: 1
42+
NamespaceIndentation: None
43+
ObjCBlockIndentWidth: 2
44+
ObjCSpaceAfterProperty: false
45+
ObjCSpaceBeforeProtocolList: false
46+
PenaltyBreakBeforeFirstCallParameter: 1000
47+
PenaltyBreakComment: 300
48+
PenaltyBreakFirstLessLess: 120
49+
PenaltyBreakString: 1000
50+
PenaltyExcessCharacter: 1000000
51+
PenaltyReturnTypeOnItsOwnLine: 200
52+
PointerAlignment: Left
53+
SpaceAfterCStyleCast: false
54+
SpaceBeforeAssignmentOperators: true
55+
SpaceBeforeParens: ControlStatements
56+
SpaceInEmptyParentheses: false
57+
SpacesBeforeTrailingComments: 2
58+
SpacesInAngles: false
59+
SpacesInContainerLiterals: true
60+
SpacesInCStyleCastParentheses: false
61+
SpacesInParentheses: false
62+
SpacesInSquareBrackets: false
63+
Standard: Cpp11
64+
TabWidth: 8
65+
UseTab: Never

src/pandas/api.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
#include "pandas/types/category.h"
1414
#include "pandas/types/numeric.h"
1515

16-
#endif // PANDAS_API_H
16+
#endif // PANDAS_API_H

src/pandas/array-test.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class TestArray : public ::testing::Test {
2626
void SetUp() {
2727
values_ = {0, 1, 2, 3, 4, 5, 6, 7};
2828

29-
auto buffer = std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(values_.data()),
30-
values_.size() * sizeof(double));
29+
auto buffer =
30+
std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(values_.data()),
31+
values_.size() * sizeof(double));
3132

3233
array_ = std::make_shared<DoubleArray>(values_.size(), buffer);
3334
}
@@ -55,8 +56,9 @@ class TestArrayView : public ::testing::Test {
5556
void SetUp() {
5657
values_ = {0, 1, 2, 3, 4, 5, 6, 7};
5758

58-
auto buffer = std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(values_.data()),
59-
values_.size() * sizeof(value_t));
59+
auto buffer =
60+
std::make_shared<Buffer>(reinterpret_cast<const uint8_t*>(values_.data()),
61+
values_.size() * sizeof(value_t));
6062

6163
auto arr = std::make_shared<DoubleArray>(values_.size(), buffer);
6264
view_ = ArrayView(arr);

src/pandas/array.cc

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ namespace pandas {
1212
// Array
1313

1414
Array::Array(const std::shared_ptr<DataType>& type, int64_t length)
15-
: type_(type),
16-
length_(length) {}
15+
: type_(type), length_(length) {}
1716

1817
Status Array::Copy(std::shared_ptr<Array>* out) const {
1918
return Copy(0, length_, out);
@@ -23,23 +22,17 @@ Status Array::Copy(std::shared_ptr<Array>* out) const {
2322
// ArrayView
2423

2524
ArrayView::ArrayView(const std::shared_ptr<Array>& data)
26-
: data_(data),
27-
offset_(0),
28-
length_(data->length()) {}
25+
: data_(data), offset_(0), length_(data->length()) {}
2926

3027
ArrayView::ArrayView(const std::shared_ptr<Array>& data, int64_t offset)
31-
: data_(data),
32-
offset_(offset),
33-
length_(data->length() - offset) {
28+
: data_(data), offset_(offset), length_(data->length() - offset) {
3429
// Debugging sanity checks
3530
PANDAS_DCHECK_GE(offset, 0);
3631
PANDAS_DCHECK_LT(offset, data->length());
3732
}
3833

3934
ArrayView::ArrayView(const std::shared_ptr<Array>& data, int64_t offset, int64_t length)
40-
: data_(data),
41-
offset_(offset),
42-
length_(length) {
35+
: data_(data), offset_(offset), length_(length) {
4336
// Debugging sanity checks
4437
PANDAS_DCHECK_GE(offset, 0);
4538
PANDAS_DCHECK_LT(offset, data->length());
@@ -49,15 +42,11 @@ ArrayView::ArrayView(const std::shared_ptr<Array>& data, int64_t offset, int64_t
4942

5043
// Copy ctor
5144
ArrayView::ArrayView(const ArrayView& other)
52-
: data_(other.data_),
53-
offset_(other.offset_),
54-
length_(other.length_) {}
45+
: data_(other.data_), offset_(other.offset_), length_(other.length_) {}
5546

5647
// Move ctor
5748
ArrayView::ArrayView(ArrayView&& other)
58-
: data_(std::move(other.data_)),
59-
offset_(other.offset_),
60-
length_(other.length_) {}
49+
: data_(std::move(other.data_)), offset_(other.offset_), length_(other.length_) {}
6150

6251
// Copy assignment
6352
ArrayView& ArrayView::operator=(const ArrayView& other) {
@@ -97,4 +86,4 @@ int64_t ArrayView::ref_count() const {
9786
return data_.use_count();
9887
}
9988

100-
} // namespace pandas
89+
} // namespace pandas

src/pandas/array.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ class Array {
2222
public:
2323
virtual ~Array() {}
2424

25-
int64_t length() const { return length_;}
26-
std::shared_ptr<DataType> type() const { return type_;}
27-
DataType::TypeId type_id() const { return type_->type();}
25+
int64_t length() const { return length_; }
26+
std::shared_ptr<DataType> type() const { return type_; }
27+
DataType::TypeId type_id() const { return type_->type(); }
2828

2929
// Copy a section of the array into a new output array
30-
virtual Status Copy(int64_t offset, int64_t length, std::shared_ptr<Array>* out) const = 0;
30+
virtual Status Copy(
31+
int64_t offset, int64_t length, std::shared_ptr<Array>* out) const = 0;
3132

3233
// Copy the entire array (using the virtual Copy function)
3334
Status Copy(std::shared_ptr<Array>* out) const;
@@ -93,4 +94,4 @@ class ArrayView {
9394
int64_t length_;
9495
};
9596

96-
} // namespace pandas
97+
} // namespace pandas

src/pandas/buffer-test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
#include "gtest/gtest.h"
1111

12-
#include "pandas/test-util.h"
1312
#include "pandas/buffer.h"
1413
#include "pandas/memory.h"
1514
#include "pandas/status.h"
15+
#include "pandas/test-util.h"
1616

1717
using std::string;
1818

src/pandas/buffer.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ Status Buffer::Copy(int64_t start, int64_t nbytes, std::shared_ptr<Buffer>* out)
3838
return Status::OK();
3939
}
4040

41-
std::shared_ptr<Buffer> SliceBuffer(const std::shared_ptr<Buffer>& buffer, int64_t offset,
42-
int64_t length) {
41+
std::shared_ptr<Buffer> SliceBuffer(
42+
const std::shared_ptr<Buffer>& buffer, int64_t offset, int64_t length) {
4343
PANDAS_DCHECK_LT(offset, buffer->size());
4444
PANDAS_DCHECK_LE(length, buffer->size() - offset);
4545
return std::make_shared<Buffer>(buffer, offset, length);

src/pandas/buffer.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ class Status;
3131
class PANDAS_EXPORT Buffer {
3232
public:
3333
Buffer(uint8_t* data, int64_t size)
34-
: is_mutable_(true),
35-
mutable_data_(data),
36-
data_(data),
37-
size_(size),
38-
capacity_(size) {}
34+
: is_mutable_(true),
35+
mutable_data_(data),
36+
data_(data),
37+
size_(size),
38+
capacity_(size) {}
3939

4040
Buffer(const uint8_t* data, int64_t size)
41-
: is_mutable_(false),
42-
mutable_data_(nullptr),
43-
data_(data),
44-
size_(size),
45-
capacity_(size) {}
41+
: is_mutable_(false),
42+
mutable_data_(nullptr),
43+
data_(data),
44+
size_(size),
45+
capacity_(size) {}
4646

4747
virtual ~Buffer();
4848

@@ -103,8 +103,8 @@ class PANDAS_EXPORT Buffer {
103103

104104
// Construct a view on passed buffer at the indicated offset and length. This
105105
// function cannot fail and does not error checking (except in debug builds)
106-
std::shared_ptr<Buffer> SliceBuffer(const std::shared_ptr<Buffer>& buffer, int64_t offset,
107-
int64_t length);
106+
std::shared_ptr<Buffer> SliceBuffer(
107+
const std::shared_ptr<Buffer>& buffer, int64_t offset, int64_t length);
108108

109109
class PANDAS_EXPORT ResizableBuffer : public Buffer {
110110
public:

0 commit comments

Comments
 (0)