Skip to content

Commit

Permalink
Merge pull request TrenchBroom#3907 from TrenchBroom/3058-clang-format
Browse files Browse the repository at this point in the history
3058: Introduce source code formatting
  • Loading branch information
kduske authored Nov 13, 2021
2 parents db45dd2 + 807c7e2 commit 031adaf
Show file tree
Hide file tree
Showing 1,209 changed files with 174,205 additions and 162,501 deletions.
37 changes: 37 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
BasedOnStyle: Microsoft

AlignAfterOpenBracket: AlwaysBreak
AlignOperands: Align
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortLambdasOnASingleLine: Empty
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyNamespace: false
SplitEmptyRecord: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
ColumnLimit: 100
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
IndentCaseLabels: true
IndentWidth: 2
PointerAlignment: Left
20 changes: 20 additions & 0 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: clang-format Check
on: [pull_request]
jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
strategy:
matrix:
path:
- 'app'
- 'common'
- 'dump-shortcuts'
- 'lib/kdl'
steps:
- uses: actions/checkout@v2
- name: Run clang-format style check for C/C++ programs.
uses: jidicula/[email protected]
with:
clang-format-version: '13'
check-path: ${{ matrix.path }}
69 changes: 35 additions & 34 deletions app/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,59 @@
along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
*/

#include "Model/GameFactory.h"
#include "PreferenceManager.h"
#include "TrenchBroomApp.h"
#include "Model/GameFactory.h"
#include "View/MapDocument.h"
#include "View/MapDocumentCommandFacade.h"
#include "View/MapFrame.h"

#include <QApplication>
#include <QSurfaceFormat>
#include <QSettings>
#include <QSurfaceFormat>
#include <QtGlobal>

extern void qt_set_sequence_auto_mnemonic(bool b);

int main(int argc, char *argv[])
{
// Set OpenGL defaults
// Needs to be done here before QApplication is created
// (see: https://doc.qt.io/qt-5/qsurfaceformat.html#setDefaultFormat)
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setSamples(4);
QSurfaceFormat::setDefaultFormat(format);
int main(int argc, char* argv[]) {
// Set OpenGL defaults
// Needs to be done here before QApplication is created
// (see: https://doc.qt.io/qt-5/qsurfaceformat.html#setDefaultFormat)
QSurfaceFormat format;
format.setDepthBufferSize(24);
format.setSamples(4);
QSurfaceFormat::setDefaultFormat(format);

// Makes all QOpenGLWidget in the application share a single context
// (default behaviour would be for QOpenGLWidget's in a single top-level window to share a context.)
// see: http://doc.qt.io/qt-5/qopenglwidget.html#context-sharing
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QSettings::setDefaultFormat(QSettings::IniFormat);
// Makes all QOpenGLWidget in the application share a single context
// (default behaviour would be for QOpenGLWidget's in a single top-level window to share a
// context.) see: http://doc.qt.io/qt-5/qopenglwidget.html#context-sharing
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QSettings::setDefaultFormat(QSettings::IniFormat);

// Set up Hi DPI scaling
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
// Set up Hi DPI scaling
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
// Enables non-integer scaling (e.g. 150% scaling on Windows)
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
// Enables non-integer scaling (e.g. 150% scaling on Windows)
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif

// Workaround bug in Qt's Ctrl+Click = RMB emulation (a macOS feature.)
// In Qt 5.13.0 / macOS 10.14.6, Ctrl+trackpad click+Drag produces no mouse events at all, but
// it should produce RMB down/move events.
// This environment variable disables Qt's emulation so we can implement it ourselves in InputEventRecorder::recordEvent
qputenv("QT_MAC_DONT_OVERRIDE_CTRL_LMB", "1");
// Workaround bug in Qt's Ctrl+Click = RMB emulation (a macOS feature.)
// In Qt 5.13.0 / macOS 10.14.6, Ctrl+trackpad click+Drag produces no mouse events at all, but
// it should produce RMB down/move events.
// This environment variable disables Qt's emulation so we can implement it ourselves in
// InputEventRecorder::recordEvent
qputenv("QT_MAC_DONT_OVERRIDE_CTRL_LMB", "1");

// Disable Qt OpenGL buglist; since we require desktop OpenGL 2.1 there's no point in
// having Qt disable it (also we've had reports of some Intel drivers being blocked that
// actually work with TB.)
qputenv("QT_OPENGL_BUGLIST", ":/opengl_buglist.json");
// Disable Qt OpenGL buglist; since we require desktop OpenGL 2.1 there's no point in
// having Qt disable it (also we've had reports of some Intel drivers being blocked that
// actually work with TB.)
qputenv("QT_OPENGL_BUGLIST", ":/opengl_buglist.json");

TrenchBroom::PreferenceManager::createInstance<TrenchBroom::AppPreferenceManager>();
TrenchBroom::View::TrenchBroomApp app(argc, argv);
TrenchBroom::PreferenceManager::createInstance<TrenchBroom::AppPreferenceManager>();
TrenchBroom::View::TrenchBroomApp app(argc, argv);

app.parseCommandLineAndShowFrame();
return app.exec();
app.parseCommandLineAndShowFrame();
return app.exec();
}
67 changes: 41 additions & 26 deletions common/benchmark/src/AABBTreeBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "IO/Reader.h"
#include "IO/TestParserStatus.h"
#include "IO/WorldReader.h"
#include "Model/BrushNode.h"
#include "Model/BrushFace.h"
#include "Model/BrushNode.h"
#include "Model/EntityNode.h"
#include "Model/EntityProperties.h"
#include "Model/GroupNode.h"
Expand All @@ -37,36 +37,51 @@

#include <vecmath/bbox.h>

#include "BenchmarkUtils.h"
#include "../../test/src/Catch2.h"
#include "BenchmarkUtils.h"

namespace TrenchBroom {
using AABB = AABBTree<double, 3, Model::Node*>;
using BOX = AABB::Box;
using AABB = AABBTree<double, 3, Model::Node*>;
using BOX = AABB::Box;

TEST_CASE("AABBTreeBenchmark.benchBuildTree", "[AABBTreeBenchmark]") {
const auto mapPath = IO::Disk::getCurrentWorkingDir() + IO::Path("fixture/benchmark/AABBTree/ne_ruins.map");
const auto file = IO::Disk::openFile(mapPath);
auto fileReader = file->reader().buffer();
TEST_CASE("AABBTreeBenchmark.benchBuildTree", "[AABBTreeBenchmark]") {
const auto mapPath =
IO::Disk::getCurrentWorkingDir() + IO::Path("fixture/benchmark/AABBTree/ne_ruins.map");
const auto file = IO::Disk::openFile(mapPath);
auto fileReader = file->reader().buffer();

IO::TestParserStatus status;
IO::WorldReader worldReader(fileReader.stringView(), Model::MapFormat::Standard, {});
IO::TestParserStatus status;
IO::WorldReader worldReader(fileReader.stringView(), Model::MapFormat::Standard, {});

const vm::bbox3 worldBounds(8192.0);
auto world = worldReader.read(worldBounds, status);
const vm::bbox3 worldBounds(8192.0);
auto world = worldReader.read(worldBounds, status);

std::vector<AABB> trees(100);
timeLambda([&world, &trees]() {
for (auto& tree : trees) {
world->accept(kdl::overload(
[] (auto&& thisLambda, Model::WorldNode* world_) { world_->visitChildren(thisLambda); },
[] (auto&& thisLambda, Model::LayerNode* layer) { layer->visitChildren(thisLambda); },
[] (auto&& thisLambda, Model::GroupNode* group) { group->visitChildren(thisLambda); },
[&](auto&& thisLambda, Model::EntityNode* entity) { entity->visitChildren(thisLambda); tree.insert(entity->physicalBounds(), entity); },
[&](Model::BrushNode* brush) { tree.insert(brush->physicalBounds(), brush); },
[&](Model::PatchNode* patch) { tree.insert(patch->physicalBounds(), patch); }
));
}
}, "Add objects to AABB tree");
}
std::vector<AABB> trees(100);
timeLambda(
[&world, &trees]() {
for (auto& tree : trees) {
world->accept(kdl::overload(
[](auto&& thisLambda, Model::WorldNode* world_) {
world_->visitChildren(thisLambda);
},
[](auto&& thisLambda, Model::LayerNode* layer) {
layer->visitChildren(thisLambda);
},
[](auto&& thisLambda, Model::GroupNode* group) {
group->visitChildren(thisLambda);
},
[&](auto&& thisLambda, Model::EntityNode* entity) {
entity->visitChildren(thisLambda);
tree.insert(entity->physicalBounds(), entity);
},
[&](Model::BrushNode* brush) {
tree.insert(brush->physicalBounds(), brush);
},
[&](Model::PatchNode* patch) {
tree.insert(patch->physicalBounds(), patch);
}));
}
},
"Add objects to AABB tree");
}
} // namespace TrenchBroom
18 changes: 8 additions & 10 deletions common/benchmark/src/BenchmarkUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@
#endif

// the noinline is so you can see the timeLambda when profiling
template<class L>
TB_NOINLINE static void timeLambda(L&& lambda, const std::string& message) {
const auto start = std::chrono::high_resolution_clock::now();
lambda();
const auto end = std::chrono::high_resolution_clock::now();

printf("Time elapsed for '%s': %fms\n", message.c_str(),
std::chrono::duration<double>(end - start).count() * 1000.0);
template <class L> TB_NOINLINE static void timeLambda(L&& lambda, const std::string& message) {
const auto start = std::chrono::high_resolution_clock::now();
lambda();
const auto end = std::chrono::high_resolution_clock::now();

printf(
"Time elapsed for '%s': %fms\n", message.c_str(),
std::chrono::duration<double>(end - start).count() * 1000.0);
}


30 changes: 16 additions & 14 deletions common/benchmark/src/IO/TestParserStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@
#include <string>

namespace TrenchBroom {
namespace IO {
NullLogger TestParserStatus::_logger;
namespace IO {
NullLogger TestParserStatus::_logger;

TestParserStatus::TestParserStatus() : ParserStatus(_logger, "") {}
TestParserStatus::TestParserStatus()
: ParserStatus(_logger, "") {}

size_t TestParserStatus::countStatus(LogLevel level) const {
const auto it = m_statusCounts.find(level);
if (it == std::end(m_statusCounts))
return 0;
return it->second;
}
size_t TestParserStatus::countStatus(LogLevel level) const {
const auto it = m_statusCounts.find(level);
if (it == std::end(m_statusCounts))
return 0;
return it->second;
}

void TestParserStatus::doProgress(const double) {}
void TestParserStatus::doProgress(const double) {}

void TestParserStatus::doLog(const LogLevel level, const std::string& /* str */) {
m_statusCounts[level]++; // unknown map values are value constructed, which initializes to 0 for size_t
}
}
void TestParserStatus::doLog(const LogLevel level, const std::string& /* str */) {
m_statusCounts[level]++; // unknown map values are value constructed, which initializes to 0 for
// size_t
}
} // namespace IO
} // namespace TrenchBroom
38 changes: 20 additions & 18 deletions common/benchmark/src/IO/TestParserStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,29 @@

#pragma once

#include "Logger.h"
#include "IO/ParserStatus.h"
#include "Logger.h"

#include <map>
#include <string>

namespace TrenchBroom {
namespace IO {
class TestParserStatus : public ParserStatus {
private:
static NullLogger _logger;
using StatusCounts = std::map<LogLevel, size_t>;
StatusCounts m_statusCounts;
public:
TestParserStatus();
public:
size_t countStatus(LogLevel level) const;
private:
void doProgress(double progress) override;
void doLog(LogLevel level, const std::string& str) override;
};
}
}

namespace IO {
class TestParserStatus : public ParserStatus {
private:
static NullLogger _logger;
using StatusCounts = std::map<LogLevel, size_t>;
StatusCounts m_statusCounts;

public:
TestParserStatus();

public:
size_t countStatus(LogLevel level) const;

private:
void doProgress(double progress) override;
void doLog(LogLevel level, const std::string& str) override;
};
} // namespace IO
} // namespace TrenchBroom
2 changes: 2 additions & 0 deletions common/benchmark/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
*/

// Hack to reuse the same main() function as the test suite
// clang-format off
#include "../../test/src/TestPreferenceManager.cpp"
#include "../../test/src/RunAllTests.cpp"
// clang-format on
Loading

0 comments on commit 031adaf

Please sign in to comment.