Skip to content

Commit

Permalink
Reformat C++ and add clang-format CI (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
externl authored May 21, 2024
1 parent 3551805 commit e8ccf38
Show file tree
Hide file tree
Showing 183 changed files with 4,792 additions and 5,451 deletions.
28 changes: 28 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# See https://clang.llvm.org/docs/ClangFormatStyleOptions.html
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
AccessModifierOffset: -4
AlignAfterOpenBracket: AlwaysBreak
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowShortIfStatementsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: TopLevel
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Allman
ColumnLimit: 120
DerivePointerAlignment: false
FixNamespaceComments: false
IndentCaseLabels: true
IndentPPDirectives: AfterHash
KeepEmptyLinesAtTheStartOfBlocks: false
NamespaceIndentation: All
PackConstructorInitializers: CurrentLine
PointerAlignment: Left
SpaceAfterTemplateKeyword: false
TabWidth: 4
UseTab: Never
30 changes: 30 additions & 0 deletions .github/workflows/clang_format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Clang Format

on:
workflow_dispatch:
push:
branches: ["main"]
pull_request:
# The branches below must be a subset of the branches above
branches: ["main"]

jobs:
clang-format:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Clang Format
run: |
# This LLVM script will add the relevant LLVM PPA: https://apt.llvm.org/
wget https://apt.llvm.org/llvm.sh -O /tmp/llvm.sh
chmod +x /tmp/llvm.sh
sudo /tmp/llvm.sh 18
sudo apt-get install -y clang-format-18
rm /tmp/llvm.sh
clang-format-18 --version
- name: Run Clang Format
run: |
find . -name "*.h" -o -name "*.cpp" | xargs clang-format-18 --style=file --fallback-style=none --Werror --dry-run
16 changes: 16 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "0.2",
"language": "en",
"words": [
"icegrid",
"zeroc"
],
"dictionaries": [
"csharp",
"softwareTerms",
"en_US"
],
"enableFiletypes": [
"ice"
]
}
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"ZeroCInc.slice"
]
}
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"C_Cpp.autoAddFileAssociations": false,
"files.associations": {
"Make.*": "makefile"
},
"C_Cpp.default.cppStandard": "c++17",
"gradle.nestedProjects": true
}
24 changes: 11 additions & 13 deletions cpp/Chat/client/ChatUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <ChatUtils.h>
#include <Chat.h>
#include <ChatUtils.h>

using namespace std;

using HtmlEntity = pair<const string, const string>;

static const HtmlEntity htmlEntities[] = { HtmlEntity("&quot;", "\""),
HtmlEntity("&#39;", "'"),
HtmlEntity("&lt;", "<"),
HtmlEntity("&gt;", ">"),
HtmlEntity("&amp;", "&")
};
static const HtmlEntity htmlEntities[] = {
HtmlEntity("&quot;", "\""),
HtmlEntity("&#39;", "'"),
HtmlEntity("&lt;", "<"),
HtmlEntity("&gt;", ">"),
HtmlEntity("&amp;", "&")};

string
ChatUtils::unstripHtml(const string& s)
{
string out = s;
for(const HtmlEntity& entity : htmlEntities)
for (const HtmlEntity& entity : htmlEntities)
{
for(string::size_type pos = out.find(entity.first);
pos != string::npos;
pos = out.find(entity.first, pos))
for (string::size_type pos = out.find(entity.first); pos != string::npos; pos = out.find(entity.first, pos))
{
out.replace(pos, entity.first.size(), entity.second);
}
Expand All @@ -37,9 +35,9 @@ ChatUtils::trim(const string& s)
{
static const string delims = "\t\r\n ";
const string::size_type last = s.find_last_not_of(delims);
if(last != string::npos)
if (last != string::npos)
{
return s.substr(s.find_first_not_of(delims), last+1);
return s.substr(s.find_first_not_of(delims), last + 1);
}
return s;
}
4 changes: 2 additions & 2 deletions cpp/Chat/client/ChatUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace ChatUtils
{

std::string unstripHtml(const std::string&);
std::string trim(const std::string&);
std::string unstripHtml(const std::string&);
std::string trim(const std::string&);

}

Expand Down
85 changes: 36 additions & 49 deletions cpp/Chat/client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <Ice/Ice.h>
#include <Glacier2/Glacier2.h>
#include <Ice/Ice.h>

#include <ChatSession.h>
#include <ChatUtils.h>
Expand All @@ -15,48 +15,43 @@ static const unsigned int maxMessageSize = 1024;
namespace
{

// mutex to prevent intertwined cout output
mutex coutMutex;
// mutex to prevent intertwined cout output
mutex coutMutex;

}

class ChatRoomCallbackI : public Chat::ChatRoomCallback
{
public:

virtual void
init(Ice::StringSeq names, const Ice::Current&) override
virtual void init(Ice::StringSeq names, const Ice::Current&) override
{
const lock_guard<mutex> lock(coutMutex);
cout << "Users: ";
for(auto it = names.begin(); it != names.end();)
for (auto it = names.begin(); it != names.end();)
{
cout << *it;
it++;
if(it != names.end())
if (it != names.end())
{
cout << ", ";
}
}
cout << endl;
}

virtual void
join(long long, string name, const Ice::Current&) override
virtual void join(long long, string name, const Ice::Current&) override
{
const lock_guard<mutex> lock(coutMutex);
cout << ">>>> " << name << " joined." << endl;
}

virtual void
leave(long long, string name, const Ice::Current&) override
virtual void leave(long long, string name, const Ice::Current&) override
{
const lock_guard<mutex> lock(coutMutex);
cout << "<<<< " << name << " left." << endl;
}

virtual void
send(long long, string name, string message, const Ice::Current&) override
virtual void send(long long, string name, string message, const Ice::Current&) override
{
const lock_guard<mutex> lock(coutMutex);
cout << name << " > " << ChatUtils::unstripHtml(message) << endl;
Expand All @@ -66,21 +61,19 @@ class ChatRoomCallbackI : public Chat::ChatRoomCallback
class ChatClient : public Glacier2::Application
{
public:

ChatClient() :
//
// Since this is an interactive demo we don't want any signal
// handling.
//
Application(Ice::SignalPolicy::NoSignalHandling)
ChatClient()
: //
// Since this is an interactive demo we don't want any signal
// handling.
//
Application(Ice::SignalPolicy::NoSignalHandling)
{
}

virtual shared_ptr<Glacier2::SessionPrx>
createSession() override
virtual shared_ptr<Glacier2::SessionPrx> createSession() override
{
shared_ptr<Glacier2::SessionPrx> sessionPrx;
while(!sessionPrx)
while (!sessionPrx)
{
cout << "This demo accepts any user ID and password.\n";

Expand All @@ -99,39 +92,35 @@ class ChatClient : public Glacier2::Application
sessionPrx = router()->createSession(id, pw);
break;
}
catch(const Glacier2::CannotCreateSessionException& ex)
catch (const Glacier2::CannotCreateSessionException& ex)
{
cout << "Login failed:\n" << ex.reason << endl;
}
catch(const Glacier2::PermissionDeniedException& ex)
catch (const Glacier2::PermissionDeniedException& ex)
{
cout << "Login failed:\n" << ex.reason << endl;
}
catch(const Ice::LocalException& ex)
catch (const Ice::LocalException& ex)
{
cerr << "Communication with the server failed:\n" << ex << endl;
}
}
return sessionPrx;
}

virtual void
sessionDestroyed() override
{
cerr << "Session destroyed " << endl;
}
virtual void sessionDestroyed() override { cerr << "Session destroyed " << endl; }

virtual int
runWithSession(int argc, char*[]) override
virtual int runWithSession(int argc, char*[]) override
{
if(argc > 1)
if (argc > 1)
{
const string message = "usage: ";
throw runtime_error(message + appName());
}

auto sessionPrx = Ice::uncheckedCast<Chat::ChatSessionPrx>(this->session());
sessionPrx->setCallback(Ice::uncheckedCast<Chat::ChatRoomCallbackPrx>(addWithUUID(make_shared<ChatRoomCallbackI>())));
sessionPrx->setCallback(
Ice::uncheckedCast<Chat::ChatRoomCallbackPrx>(addWithUUID(make_shared<ChatRoomCallbackI>())));

menu();

Expand All @@ -142,19 +131,19 @@ class ChatClient : public Glacier2::Application
getline(cin, s);
s = ChatUtils::trim(s);

if(!s.empty())
if (!s.empty())
{
if(s[0] == '/')
if (s[0] == '/')
{
if(s == "/quit")
if (s == "/quit")
{
break;
}
menu();
}
else
{
if(s.size() > maxMessageSize)
if (s.size() > maxMessageSize)
{
const lock_guard<mutex> lock(coutMutex);
cout << "Message length exceeded, maximum length is " << maxMessageSize << " characters.";
Expand All @@ -165,15 +154,12 @@ class ChatClient : public Glacier2::Application
}
}
}
}
while(cin.good());
} while (cin.good());
return 0;
}

private:

void
menu()
void menu()
{
const lock_guard<mutex> lock(coutMutex);
cout << "enter /quit to exit." << endl;
Expand All @@ -193,14 +179,15 @@ main(int argc, char* argv[])
//
// Set Ice.Default.Router if not set
//
if(initData.properties->getProperty("Ice.Default.Router").empty())
if (initData.properties->getProperty("Ice.Default.Router").empty())
{
initData.properties->setProperty("Ice.Plugin.IceSSL","IceSSL:createIceSSL");
initData.properties->setProperty("Ice.Plugin.IceSSL", "IceSSL:createIceSSL");
initData.properties->setProperty("IceSSL.UsePlatformCAs", "1");
initData.properties->setProperty("IceSSL.CheckCertName", "2");
initData.properties->setProperty("IceSSL.VerifyDepthMax", "5");
initData.properties->setProperty("Ice.Default.Router",
"Glacier2/router:wss -p 443 -h zeroc.com -r /demo-proxy/chat/glacier2");
initData.properties->setProperty(
"Ice.Default.Router",
"Glacier2/router:wss -p 443 -h zeroc.com -r /demo-proxy/chat/glacier2");
}

ChatClient app;
Expand Down
Loading

0 comments on commit e8ccf38

Please sign in to comment.