Skip to content

Commit a729c15

Browse files
authored
preparation for clang format (#5422)
* Clang-format excludes * Add .clang-format * Don't clang-format in external * Missing includes and forward declarations * Replace wonky include-once macro name * neaten include naming * Add clang-format to formatting script * Add xargs and diff to required binaries * add clang-format to ci formatting check * Add max version check to formatting script * temporarily disable checking formatting for cpp files
1 parent a15d770 commit a729c15

Some content is hidden

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

53 files changed

+208
-114
lines changed

.clang-format

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
BasedOnStyle: LLVM
2+
3+
# Whitespace
4+
MaxEmptyLinesToKeep: 2
5+
IndentWidth: 4
6+
ColumnLimit: 100
7+
8+
# Preprocessor
9+
AlignEscapedNewlines: Left
10+
IncludeBlocks: Regroup
11+
12+
# Functions
13+
AllowAllArgumentsOnNextLine: false
14+
BinPackArguments: false
15+
BinPackParameters: false
16+
AllowAllParametersOfDeclarationOnNextLine: false
17+
PenaltyReturnTypeOnItsOwnLine: 9999999
18+
PenaltyBreakBeforeFirstCallParameter: 6
19+
AllowShortFunctionsOnASingleLine: Inline
20+
21+
# Brackets and braces
22+
AlignAfterOpenBracket: AlwaysBreak
23+
BreakBeforeBraces: Allman
24+
25+
# Pointers
26+
PointerAlignment: Left
27+
28+
# Classes
29+
PackConstructorInitializers: NextLineOnly
30+
BreakConstructorInitializers: BeforeComma
31+
AccessModifierOffset: -4
32+
33+
# Switch statements
34+
IndentCaseBlocks: true
35+
AllowShortCaseLabelsOnASingleLine: true
36+
AlignConsecutiveShortCaseStatements:
37+
Enabled: true
38+
39+
# Templates
40+
SpaceAfterTemplateKeyword: false
41+
AlwaysBreakTemplateDeclarations: true
42+
43+
# Misc
44+
SortUsingDeclarations: false

.github/actions/format-setup/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ runs:
1616
tmpdir=$(mktemp -d)
1717
curl -L -H "Authorization: token ${{github.token}}" \
1818
-o "$tmpdir/clang-format" \
19-
https://github.com/shader-slang/slang-binaries/raw/4e88845ec51641b4c92e68027e359090bdb219e0/clang-format/x86_64-linux/bin/clang-format
19+
https://github.com/shader-slang/slang-binaries/raw/306d22efc0f5f72c7230b0b6b7c99f03c46995bd/clang-format/x86_64-linux/bin/clang-format
2020
chmod +x "$tmpdir/clang-format"
2121
echo "$tmpdir" >> $GITHUB_PATH

.github/workflows/check-formatting.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,5 @@ jobs:
1010
runs-on: ubuntu-latest
1111
steps:
1212
- uses: actions/checkout@v4
13-
- name: Setup
14-
uses: ./.github/actions/format-setup
13+
- uses: ./.github/actions/format-setup
1514
- run: ./extras/formatting.sh --check-only
16-

examples/example-base/test-base.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include "test-base.h"
22

33
#ifdef _WIN32
4-
#include <windows.h>
5-
#include <shellapi.h>
4+
// clang-format off
5+
// include ordering sensitive
6+
# include <windows.h>
7+
# include <shellapi.h>
8+
// clang-format on
69
#endif
710

811
int TestBase::parseOption(int argc, char** argv)

examples/hello-world/vulkan-api.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// the Vulkan API.
88

99
// The Vulkan function pointers we will use in this example.
10+
// clang-format off
1011
#define VK_API_GLOBAL_PROCS(x) \
1112
x(vkGetInstanceProcAddr) \
1213
x(vkCreateInstance) \
@@ -87,6 +88,7 @@
8788
/* */
8889

8990
#define VK_API_DECLARE_PROC(NAME) PFN_##NAME NAME = nullptr;
91+
// clang-format on
9092

9193
struct VulkanAPI
9294
{

external/.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DisableFormat: true

extras/.clang-format

-48
This file was deleted.

extras/formatting.sh

+45-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ cd "$source_dir" || exit 1
4040

4141
require_bin() {
4242
local name="$1"
43-
local required="$2"
43+
local min_version="$2"
44+
local max_version="${3:-}"
4445
local version
4546

4647
if ! command -v "$name" &>/dev/null; then
@@ -50,16 +51,29 @@ require_bin() {
5051
fi
5152

5253
if [ "$no_version_check" -eq 0 ]; then
53-
version=$("$name" --version | grep -oP "$name(?:\s+version)?\s+\K\d+\.\d+\.?\d*")
54-
if ! printf '%s\n%s\n' "$required" "$version" | sort -V -C; then
55-
echo "$name version $version is too old. Version $required or newer is required."
54+
version=$("$name" --version | grep -oP "\d+\.\d+\.?\d*" | head -n1)
55+
56+
if ! printf '%s\n%s\n' "$min_version" "$version" | sort -V -C; then
57+
echo "$name version $version is too old. Version $min_version or newer is required."
5658
missing_bin=1
59+
return
60+
fi
61+
62+
if [ -n "$max_version" ]; then
63+
if ! printf '%s\n%s\n' "$version" "$max_version" | sort -V -C; then
64+
echo "$name version $version is too new. Version less than $max_version is required."
65+
missing_bin=1
66+
return
67+
fi
5768
fi
5869
fi
5970
}
6071

6172
require_bin "git" "1.8"
6273
require_bin "gersemi" "0.17"
74+
require_bin "xargs" "3"
75+
require_bin "diff" "2"
76+
require_bin "clang-format" "17" "18"
6377

6478
if [ "$missing_bin" ]; then
6579
exit 1
@@ -84,7 +98,33 @@ cmake_formatting() {
8498
fi
8599
}
86100

101+
cpp_formatting() {
102+
readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h')
103+
104+
if [ "$check_only" -eq 1 ]; then
105+
local tmpdir
106+
tmpdir=$(mktemp -d)
107+
trap 'rm -rf "$tmpdir"' EXIT
108+
109+
printf '%s\n' "${files[@]}" | xargs -P "$(nproc)" -I{} bash -c "
110+
mkdir -p \"\$(dirname \"$tmpdir/{}\")\"
111+
diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
112+
:
113+
"
114+
115+
for file in "${files[@]}"; do
116+
if [ -s "$tmpdir/$file" ]; then
117+
cat "$tmpdir/$file"
118+
exit_code=1
119+
fi
120+
done
121+
else
122+
printf '%s\n' "${files[@]}" | xargs -n1 -P "$(nproc)" clang-format -i
123+
fi
124+
}
125+
87126
cmake_formatting
127+
# Disable until we've formatted the code
128+
# cpp_formatting
88129

89130
exit $exit_code
90-

include/.clang-format

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BasedOnStyle: InheritParentConfig
2+
IndentPPDirectives: BeforeHash

include/slang-gfx.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ class IShaderProgram: public ISlangUnknown
199199
// TODO: Confirm with Yong that we really want this naming convention
200200
// TODO: Rename to what?
201201
// Dont' change without keeping in sync with Format
202+
// clang-format off
202203
#define GFX_FORMAT(x) \
203204
x( Unknown, 0, 0) \
204205
\
@@ -304,8 +305,9 @@ class IShaderProgram: public ISlangUnknown
304305
\
305306
x(R64_UINT, 8, 1) \
306307
\
307-
x(R64_SINT, 8, 1) \
308-
\
308+
x(R64_SINT, 8, 1)
309+
// clang-format on
310+
309311
// TODO: This should be generated from above
310312
// TODO: enum class should be explicitly uint32_t or whatever's appropriate
311313
/// Different formats of things like pixels or elements of vertices

source/compiler-core/slang-artifact-desc-util.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ bool isDerivedFrom(ENUM_TYPE kind, ENUM_TYPE base) { return g_table##ENUM_TYPE.i
154154

155155
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! ArtifactKind !!!!!!!!!!!!!!!!!!!!!!! */
156156

157+
// clang-format off
157158
#define SLANG_ARTIFACT_KIND(x) \
158159
x(Invalid, Invalid) \
159160
x(Base, Invalid) \
@@ -245,6 +246,7 @@ SLANG_HIERARCHICAL_ENUM(ArtifactPayload, SLANG_ARTIFACT_PAYLOAD, SLANG_ARTIFACT_
245246
x(Kernel, CodeLike) \
246247
x(Host, CodeLike) \
247248
x(Obfuscated, Base)
249+
// clang-format on
248250

249251
#define SLANG_ARTIFACT_STYLE_ENTRY(TYPE, PARENT) { Index(ArtifactStyle::TYPE), Index(ArtifactStyle::PARENT), #TYPE },
250252

source/compiler-core/slang-dxc-compiler.cpp

+27-20
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,33 @@
3737
// generate code on Windows.
3838
#if SLANG_ENABLE_DXIL_SUPPORT
3939

40-
# ifdef _WIN32
41-
# include <windows.h>
42-
# include <unknwn.h>
43-
# include "../../external/dxc/dxcapi.h"
44-
# else
45-
# include "../../external/dxc/dxcapi.h"
46-
47-
# ifdef __uuidof
48-
// DXC's WinAdapter.h defines __uuidof(T) over types, but the existing
49-
// usage in this file is over values (both are accepted on MSVC.)
50-
// We also need to decay through Slang::ComPtr, hence the helper struct
51-
template <typename T>
52-
struct StripSlangComPtr { using type = T; };
53-
template <typename T>
54-
struct StripSlangComPtr<Slang::ComPtr<T>> { using type = T; };
55-
# undef __uuidof
56-
# define __uuidof(x) __emulated_uuidof<StripSlangComPtr<std::decay_t<decltype(x)>>::type>()
57-
# endif
58-
# endif
59-
40+
# ifdef _WIN32
41+
# include <unknwn.h>
42+
# include <windows.h>
43+
# endif
44+
45+
# include "../../external/dxc/dxcapi.h"
46+
47+
# ifndef _WIN32
48+
# ifdef __uuidof
49+
// DXC's WinAdapter.h defines __uuidof(T) over types, but the existing
50+
// usage in this file is over values (both are accepted on MSVC.)
51+
// We also need to decay through Slang::ComPtr, hence the helper struct
52+
template<typename T>
53+
struct StripSlangComPtr
54+
{
55+
using type = T;
56+
};
57+
template<typename T>
58+
struct StripSlangComPtr<Slang::ComPtr<T>>
59+
{
60+
using type = T;
61+
};
62+
# undef __uuidof
63+
# define __uuidof(x) \
64+
__emulated_uuidof<StripSlangComPtr<std::decay_t<decltype(x)>>::type>()
65+
# endif
66+
# endif
6067
#endif
6168

6269
namespace Slang

source/compiler-core/slang-nvrtc-compiler.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef enum {
4141

4242
typedef struct _nvrtcProgram *nvrtcProgram;
4343

44+
// clang-format off
4445
#define SLANG_NVRTC_FUNCS(x) \
4546
x(const char*, nvrtcGetErrorString, (nvrtcResult result)) \
4647
x(nvrtcResult, nvrtcVersion, (int *major, int *minor)) \
@@ -53,6 +54,7 @@ typedef struct _nvrtcProgram *nvrtcProgram;
5354
x(nvrtcResult, nvrtcGetProgramLog, (nvrtcProgram prog, char *log))\
5455
x(nvrtcResult, nvrtcAddNameExpression, (nvrtcProgram prog, const char * const name_expression)) \
5556
x(nvrtcResult, nvrtcGetLoweredName, (nvrtcProgram prog, const char *const name_expression, const char** lowered_name))
57+
// clang-format on
5658

5759
} // namespace nvrtc
5860

source/core/slang-blob.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SLANG_CORE_BLOB_H
22
#define SLANG_CORE_BLOB_H
33

4-
#include "../../include/slang.h"
4+
#include "slang.h"
55

66
#include "slang-string.h"
77
#include "slang-list.h"

source/core/slang-chunked-list.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SLANG_CORE_CHUNKED_LIST_H
22
#define SLANG_CORE_CHUNKED_LIST_H
33

4-
#include "../../include/slang.h"
4+
#include "slang.h"
55

66
#include "slang-allocator.h"
77
#include "slang-array-view.h"

source/core/slang-common.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#ifndef SLANG_CORE_COMMON_H
2-
#define SLANG_CORE_COMMON_H
1+
#pragma once
32

4-
#include "../../include/slang.h"
3+
#include "slang.h"
54

65
#include <assert.h>
76

@@ -237,7 +236,6 @@ template<typename T> void slang_use_obj(T&) {}
237236

238237
#define SLANG_UNREFERENCED_PARAMETER(P) slang_use_obj(P)
239238
#define SLANG_UNREFERENCED_VARIABLE(P) slang_use_obj(P)
240-
#endif
241239

242240
#if defined(SLANG_RT_DYNAMIC)
243241
#if defined(_MSC_VER)

source/core/slang-crypto.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "../../include/slang.h"
2+
#include "slang.h"
33
#include "../core/slang-string.h"
44
#include "../core/slang-blob.h"
55
#include "../core/slang-list.h"

source/core/slang-file-system.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SLANG_FILE_SYSTEM_H_INCLUDED
22
#define SLANG_FILE_SYSTEM_H_INCLUDED
33

4-
#include "../../include/slang.h"
4+
#include "slang.h"
55
#include "slang-com-helper.h"
66
#include "slang-com-ptr.h"
77

source/core/slang-free-list.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SLANG_CORE_FREE_LIST_H
22
#define SLANG_CORE_FREE_LIST_H
33

4-
#include "../../include/slang.h"
4+
#include "slang.h"
55

66
#include "slang-common.h"
77

0 commit comments

Comments
 (0)