Skip to content

Commit

Permalink
Add clang-format rules from 3.6 (#2360)
Browse files Browse the repository at this point in the history
  • Loading branch information
Teddy Reed committed Aug 15, 2016
1 parent 58fd284 commit a2017f6
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 66 deletions.
50 changes: 37 additions & 13 deletions .clang-format
@@ -1,41 +1,65 @@
---
Language: Cpp
# BasedOnStyle: Google
AccessModifierOffset: -1
ConstructorInitializerIndentWidth: 4
AlignEscapedNewlinesLeft: true
AlignTrailingComments: false
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: false # differs
AllowShortBlocksOnASingleLine: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false # differs
AllowShortLoopsOnASingleLine: false # differs
AllowShortFunctionsOnASingleLine: Empty
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: true
AlwaysBreakBeforeMultilineStrings: true
BreakBeforeBinaryOperators: false
BreakBeforeBinaryOperators: false # differs
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BinPackParameters: false
BinPackArguments: false
ColumnLimit: 80
ConstructorInitializerIndentWidth: 4
ConstructorInitializerAllOnOneLineOrOnePerLine: true
DerivePointerAlignment: false
DerivePointerBinding: true
ExperimentalAutoDetectBinPacking: true
IndentCaseLabels: false
IndentCaseLabels: false #differs
IndentWrappedFunctionNames: false
IndentFunctionDeclarationAfterType: false
MaxEmptyLinesToKeep: 1
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakComment: 60
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakString: 1000
PenaltyBreakFirstLessLess: 20
PenaltyBreakFirstLessLess: 120
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
PointerBindsToType: true
SpacesBeforeTrailingComments: 1
SpacesBeforeTrailingComments: 1 #differs
Cpp11BracedListStyle: true
Standard: Cpp11
IndentWidth: 2
TabWidth: 8
UseTab: Never
BreakBeforeBraces: Attach
IndentFunctionDeclarationAfterType: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SpaceAfterControlStatementKeyword: true
SpaceAfterCStyleCast: false
SpacesInContainerLiterals: true
SpaceBeforeAssignmentOperators: true
SpaceAfterControlStatementKeyword: true
ContinuationIndentWidth: 4
CommentPragmas: '^ IWYU pragma:'
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
SpaceBeforeParens: ControlStatements
...
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -507,7 +507,7 @@ add_custom_target(
# make format
add_custom_target(
format
"${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/formatting/git-clang-format.py"
"${PYTHON_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/tools/formatting/git-clang-format.py" "-f"
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
COMMENT "Formatting code staged code changes with clang-format" VERBATIM
)
Expand Down
6 changes: 2 additions & 4 deletions Doxyfile
Expand Up @@ -991,16 +991,14 @@ VERBATIM_HEADERS = YES
# Note: The availability of this option depends on whether or not doxygen was
# compiled with the --with-libclang option.
# The default value is: NO.

CLANG_ASSISTED_PARSING = NO
# CLANG_ASSISTED_PARSING = NO

# If clang assisted parsing is enabled you can provide the compiler with command
# line options that you would normally use when invoking the compiler. Note that
# the include paths will already be set by doxygen for the files and directories
# specified with INPUT and INCLUDE_PATH.
# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES.

CLANG_OPTIONS =
# CLANG_OPTIONS =

#---------------------------------------------------------------------------
# Configuration options related to the alphabetical class index
Expand Down
25 changes: 16 additions & 9 deletions Makefile
Expand Up @@ -30,10 +30,10 @@ ifneq ($(OSQUERY_DEPS),)
else
DEPS_DIR = /usr/local/osquery
endif
CMAKE := PATH="$(DEPS_DIR)/bin:/usr/local/bin:$(PATH)" \
CXXFLAGS="-L$(DEPS_DIR)/lib" cmake ../../
DOCS_CMAKE := PATH="$(DEPS_DIR)/bin:/usr/local/bin:$(PATH)" \
CXXFLAGS="-L$(DEPS_DIR)/lib" cmake ../
PATH_SET := PATH="$(DEPS_DIR)/bin:/usr/local/bin:$(PATH)"
CMAKE := $(PATH_SET) CXXFLAGS="-L$(DEPS_DIR)/lib" cmake ../../
DOCS_CMAKE := $(PATH_SET) CXXFLAGS="-L$(DEPS_DIR)/lib" cmake ../
FORMAT_COMMAND := python tools/formatting/git-clang-format.py "--commit" "master" "-f"

DEFINES := CTEST_OUTPUT_ON_FAILURE=1
.PHONY: docs build
Expand All @@ -46,6 +46,9 @@ docs: .setup
@cd build && DOCS=True $(DOCS_CMAKE) && \
$(DEFINES) $(MAKE) docs --no-print-directory $(MAKEFLAGS)

format_master:
@$(PATH_SET) $(FORMAT_COMMAND)

debug: .setup
@cd build/debug_$(BUILD_DIR) && DEBUG=True $(CMAKE) && \
$(DEFINES) $(MAKE) --no-print-directory $(MAKEFLAGS)
Expand Down Expand Up @@ -78,9 +81,13 @@ test_debug_sdk: .setup
@cd build/debug_$(BUILD_DIR) && SDK=True DEBUG=True $(CMAKE) && \
$(DEFINES) $(MAKE) test --no-print-directory $(MAKEFLAGS)

build:
cd build/$(BUILD_DIR) && \
$(DEFINES) $(MAKE) --no-print-directory $(MAKEFLAGS)
check:
@$(PATH_SET) cppcheck --quiet --enable=all --error-exitcode=0 \
-I ./include ./osquery
# We want check to produce an error if there are critical issues.
@echo ""
@$(PATH_SET) cppcheck --quiet --enable=warning --error-exitcode=1 \
-I ./include ./osquery

debug_build:
cd build/debug_$(BUILD_DIR) && \
Expand Down Expand Up @@ -141,15 +148,15 @@ endif
package: .setup
# Alias for packages (do not use CPack)
@cd build/$(BUILD_DIR) && PACKAGE=True $(CMAKE) && \
$(DEFINES) $(MAKE) packages --no-print-directory $(MAKEFLAGS)
$(DEFINES) $(MAKE) packages/fast --no-print-directory $(MAKEFLAGS)

debug_package: .setup
@cd build/debug_$(BUILD_DIR) && DEBUG=True PACKAGE=True $(CMAKE) && \
$(DEFINES) $(MAKE) packages --no-print-directory $(MAKEFLAGS)

packages: .setup
@cd build/$(BUILD_DIR) && PACKAGE=True $(CMAKE) && \
$(DEFINES) $(MAKE) packages --no-print-directory $(MAKEFLAGS)
$(DEFINES) $(MAKE) packages/fast --no-print-directory $(MAKEFLAGS)

debug_packages:
@cd build/debug_$(BUILD_DIR) && DEBUG=True PACKAGE=True $(CMAKE) && \
Expand Down
28 changes: 28 additions & 0 deletions docs/wiki/development/building.md
Expand Up @@ -71,6 +71,34 @@ The binaries are built to a distro-specific folder within *build* and symlinked
$ ls -la ./build/linux/osquery/
```

## Submitting Pull Requests

Once you have made changes you'll want to submit them to Github as a Pull Request. There are tons of wonderful guides and documentation around Pull Requests, and that is just out of scope for this wiki-- but consider the following workflow:

```
$ git checkout -b new_feature1
$ # write some code!
$ make -j 4
$ git commit -m "New Feature: do something wonderful"
$ git push
```

This assumes your remote `origin` is your osquery fork, and that you receive updates from an `upstream`. It is also common to use `origin` as `facebook/osquery` then add your fork as a target named after your Github username.

In that case the final push becomes `git push USERNAME`.

### Testing changes

Our Jenkins CI will test your changes in three steps.

1. A code audit is run using `./tools/audit.sh`.
2. The code is rebuilt, built again for release, then a package is generated using `./tools/build.sh` on various Linux and OS X versions.
3. The same step is run on Windows 10.

The audit step attempts to build the documentation, run code formatting checks, and a brief static code analysis. The formatting check is performed with `clang-format` (installed with `make deps` to your osquery dependencies directory). Your changes are compared against the local `master` branch. Within the build host this is always the TIP of `facebook/osquery`, but locally the branch may be behind.

To speed up the format auditing process please configure your code editor to run `clang-format` on files changed. Or periodically during your development run `make format`. Running `make check` is also helpful, but it will use `cppcheck` which is not installed by default.

## Dependencies and build internals

The `make deps` command is fairly intense and serves two purposes: (1) to communicate a standard set of environment setup instructions for our build and test nodes, (2) to provide an environment for reproducing errors. The are wonderful auxiliary benefits such as controlling the compiler and compile flags for almost all of our dependencies, controlling security-related features for dependencies, allowing a "mostly" universal build for Linux that makes deployment simple. To read more about the motivation and FAQ for our dependencies environment see the [Github Refererence #2253](https://github.com/facebook/osquery/issues/2253).
Expand Down
8 changes: 5 additions & 3 deletions osquery/dispatcher/scheduler.cpp
Expand Up @@ -50,8 +50,8 @@ inline SQL monitor(const std::string& name, const ScheduledQuery& query) {
}
}
// Always called while processes table is working.
Config::getInstance().recordQueryPerformance(name, t1 - t0, size, r0[0],
r1[0]);
Config::getInstance().recordQueryPerformance(
name, t1 - t0, size, r0[0], r1[0]);
}
return sql;
}
Expand Down Expand Up @@ -148,7 +148,9 @@ void SchedulerRunner::start() {
}
}

void startScheduler() { startScheduler(FLAGS_schedule_timeout, 1); }
void startScheduler() {
startScheduler(FLAGS_schedule_timeout, 1);
}

void startScheduler(unsigned long int timeout, size_t interval) {
Dispatcher::addService(std::make_shared<SchedulerRunner>(timeout, interval));
Expand Down
17 changes: 10 additions & 7 deletions osquery/remote/transports/tls.h
Expand Up @@ -38,12 +38,12 @@ void ERR_remove_state(unsigned long);
// On OS X these symbols are marked deprecated and clang will warn against
// us including them. We are squashing the noise for OS X's OpenSSL only.
// clang-format off
_Pragma("clang diagnostic push")
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"")
_Pragma("clang diagnostic ignored \"-Wunused-local-typedef\"")
_Pragma("clang diagnostic ignored \"-W#pragma-messages\"")
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma clang diagnostic ignored "-Wunused-local-typedef"
#pragma clang diagnostic ignored "-W#pragma-messages"
#include <boost/network/protocol/http/client.hpp>
_Pragma("clang diagnostic pop")
#pragma clang diagnostic pop
// clang-format on

#include <osquery/flags.h>
Expand All @@ -55,7 +55,8 @@ namespace osquery {
/// Path to optional TLS client secret key, used for enrollment/requests.
DECLARE_string(tls_client_key);

/// Path to optional TLS client certificate (PEM), used for enrollment/requests.
/// Path to optional TLS client certificate (PEM), used for
/// enrollment/requests.
DECLARE_string(tls_client_cert);

/// TLS server hostname.
Expand Down Expand Up @@ -106,7 +107,9 @@ class TLSTransport : public Transport {

private:
/// Testing-only, disable peer verification.
void disableVerifyPeer() { verify_peer_ = false; }
void disableVerifyPeer() {
verify_peer_ = false;
}

/// Set TLS-client authentication options.
void setClientCertificate(const std::string& certificate_file,
Expand Down
26 changes: 19 additions & 7 deletions osquery/sql/sql.cpp
Expand Up @@ -20,22 +20,34 @@ namespace osquery {

FLAG(int32, value_max, 512, "Maximum returned row value size");

SQL::SQL(const std::string& q) { status_ = query(q, results_); }
SQL::SQL(const std::string& q) {
status_ = query(q, results_);
}

const QueryData& SQL::rows() const { return results_; }
const QueryData& SQL::rows() const {
return results_;
}

bool SQL::ok() { return status_.ok(); }
bool SQL::ok() {
return status_.ok();
}

const Status& SQL::getStatus() const { return status_; }
const Status& SQL::getStatus() const {
return status_;
}

std::string SQL::getMessageString() { return status_.toString(); }
std::string SQL::getMessageString() {
return status_.toString();
}

static inline void escapeNonPrintableBytes(std::string& data) {
std::string escaped;
// clang-format off
char const hex_chars[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
};
// clang-format on

bool needs_replacement = false;
for (size_t i = 0; i < data.length(); i++) {
Expand Down

0 comments on commit a2017f6

Please sign in to comment.