Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up order of includes #2015

Merged
merged 18 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,48 @@ BinPackParameters: false
# Open brace goes on new line only when starting a new struct, enum, or func.
BreakBeforeBraces: Mozilla
#
# Don't sort includes in alphabetical order because Windows headers are odd.
SortIncludes: false
# Special include file sort ordering rules.
# Priority indicates the "group" (where groups are separated by a blank line).
# SortPriority indicates the overall order when we need to override alphabetical
# order due to Windows header dependencies.
SortIncludes: 'true'
IncludeBlocks: Regroup
IncludeIsMainRegex: "UNUSED$"
IncludeCategories:
# winsock2.h must be before in6addr.h or windows.h
- Regex: '^<[Ww]insock2.h>'
Priority: 2
SortPriority: 2
- Regex: '^<in6addr.h>'
Priority: 2
SortPriority: 3
# windows.h must be before ElfWrapper.h or netsh.h
- Regex: '^<[Ww]indows.h>'
Priority: 2
SortPriority: 3
# ws2def.h and ws2ipdef.h must be before iphlpapi.h
- Regex: '^<(ws2def|ws2ipdef).h>'
Priority: 2
SortPriority: 3
# ws2tcpip.h must be before mstcpip.h
- Regex: '^<ws2tcpip.h>'
Priority: 2
SortPriority: 3
- Regex: '^(<|")ElfWrapper.h'
Priority: 2
SortPriority: 4
# ntifs.h must be before ntddk.h
- Regex: '^<ntifs.h'
Priority: 2
SortPriority: 4
# ndis.h must be before fwpsk.h
- Regex: '^<ndis.h'
Priority: 2
SortPriority: 4
- Regex: '\.c"'
Priority: 6
- Regex: '^"'
Priority: 1
- Regex: '^<'
Priority: 2
SortPriority: 5
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**/.[Gg]radle/**
/**/[Rr]elease/**
/**/[Dd]ebug/**
/**/FuzzerDebug/**
/**/[Tt]arget/**

# All kinds of output and temporary files that should not be checked in.
Expand All @@ -24,6 +25,7 @@
/**/*.beam
/**/*.trx
/**/*.log
/**/*.tlog
/**/.vs/
/**/*.bak
/**/*.suo
Expand Down
8 changes: 6 additions & 2 deletions docs/DevelopmentGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ Header Files
headers to be included first. That is, any dependencies should be included within
the header file itself.

* **DO** include system headers (with `<>`) before local headers (with `""`), and list them
in alphabetical order where possible. This helps ensure there are not duplicate includes,
* **DO** include local headers (with `""`) before system headers (with `<>`). This
helps ensure that local headers don't have dependencies on other things being
included first, and is also consistent with the use of a local header for precompiled
headers.

* **DO** list headers in alphabetical order where possible. This helps ensure there are not duplicate includes,
and also helps ensure that headers are usable directly.

* **DO** use `#pragma once` in all header files, rather than using ifdefs to test for duplicate inclusion.
Expand Down
1 change: 0 additions & 1 deletion ebpfapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
add_library("EbpfApi" SHARED
Source.def

pch.h
resource.h

dllmain.cpp
Expand Down
7 changes: 5 additions & 2 deletions ebpfapi/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

// dllmain.cpp : Defines the entry point for the DLL application.
#include "pch.h"
/**
* @file
* @brief Defines the entry point for the DLL application.
*/
#include "api_internal.h"
#include "framework.h"

bool use_ebpf_store = true;

Expand Down
1 change: 0 additions & 1 deletion ebpfapi/ebpfapi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
<ClInclude Include="..\libs\api\platform.h">
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</DeploymentContent>
</ClInclude>
<ClInclude Include="pch.h" />
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 0 additions & 3 deletions ebpfapi/ebpfapi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\libs\api\platform.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
18 changes: 0 additions & 18 deletions ebpfapi/pch.h

This file was deleted.

11 changes: 6 additions & 5 deletions ebpfapi/rpc_client.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#include "ebpf_api.h"
#include "ebpf_platform.h"

#include "rpc_interface_c.c"

// Windows.h needs to be included before other headers.
// It has a #define for WINAPI_FAMILY_PARTITION among others that control
// the behavior of other Windows headers.
#include <winsock2.h>
#include <windows.h>

#include <ctype.h>
#include <iostream>
#include <sddl.h>
#include <stdio.h>
#include <stdlib.h>
#include "ebpf_api.h"
#include "ebpf_platform.h"
#include "rpc_interface_c.c"

#pragma comment(lib, "Rpcrt4.lib")

Expand Down Expand Up @@ -136,4 +137,4 @@ void __RPC_USER
MIDL_user_free(_Pre_maybenull_ _Post_invalid_ void* p)
{
ebpf_free(p);
}
}
26 changes: 8 additions & 18 deletions ebpfcore/ebpf_drv.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

/*++

Abstract:
WDF based driver that does the following:
1. Initializes the eBPF execution context.
2. Opens an IOCTL surface that forwards commands to ebpf_core.

Environment:

Kernel mode

--*/

// ntddk.h needs to be included first due to inter header dependencies on Windows.
#include <ntddk.h>

#include <netiodef.h>
#include <wdf.h>
/**
* @file
* WDF based driver that does the following:
* 1. Initializes the eBPF execution context.
* 2. Opens an IOCTL surface that forwards commands to ebpf_core.
*/

#include "ebpf_core.h"
#include "ebpf_object.h"

#include <wdf.h>

// Driver global variables
static DEVICE_OBJECT* _ebpf_driver_device_object;
static BOOLEAN _ebpf_driver_unloading_flag = FALSE;
Expand Down
7 changes: 4 additions & 3 deletions ebpfsvc/rpc_api.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#include "api_service.h"
#include "rpc_interface_h.h"
#include "svc_common.h"

#include <mutex>
#include <stdexcept>
#include <stdio.h>
#include <vector>
#include "api_service.h"
#include "rpc_interface_h.h"
#include "svc_common.h"

bool use_ebpf_store = false;

Expand Down
10 changes: 4 additions & 6 deletions ebpfsvc/rpc_util.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

// Windows.h needs to be included before other headers.
// It has a #define for WINAPI_FAMILY_PARTITION among others that control
// the behavior of other Windows headers.
#include "svc_common.h"

#include <winsock2.h>
#include <windows.h>

#include <malloc.h>
#include <sddl.h>

#include "rpc_interface_s.c"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since we have an exception here (that rpc_interface_s.c is included after svc_common.h), should we add a comment in the file mentioning this is deliberate?

Copy link
Collaborator Author

@dthaler dthaler Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not an exception per se, since this is including a C file not a header. DevelopmentGuide.md says:

DO include local headers (with "") before system headers (with <>).

Line 60 of .clang-format explicitly puts C files after headers.

#include "svc_common.h"

#pragma comment(lib, "Rpcrt4.lib")

Expand Down Expand Up @@ -126,4 +124,4 @@ void __RPC_USER
MIDL_user_free(_Pre_maybenull_ _Post_invalid_ void* p)
{
free(p);
}
}
1 change: 1 addition & 0 deletions ebpfsvc/svc_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
#pragma once

#include "framework.h"

#include <Windows.h>
2 changes: 1 addition & 1 deletion include/bpf/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ bpf_prog_load(

#else
#pragma warning(push)
#include "libbpf/src/bpf.h"
#include "bpf_legacy.h"
#include "libbpf/src/bpf.h"
#pragma warning(pop)
#endif
2 changes: 2 additions & 0 deletions include/bpf/bpf_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: MIT
#pragma once

#include "libbpf.h"

#pragma warning(push)
#pragma warning(disable : 4201) // nonstandard extension used: nameless struct/union

Expand Down
6 changes: 3 additions & 3 deletions include/bpf2c.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#pragma once

#include "ebpf_structs.h"

#if defined(NO_CRT)
typedef signed char int8_t;
typedef short int16_t;
Expand All @@ -22,8 +24,6 @@ typedef unsigned long long uint64_t;
#include <stdint.h>
#endif

#include "ebpf_structs.h"

#ifdef __cplusplus
extern "C"
{
Expand Down
2 changes: 1 addition & 1 deletion include/bpf_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// this version of bpf_helpers.h is already cross-platform.

// Include platform-specific definitions.
#include "ebpf_structs.h"
#include "bpf_helpers_platform.h"
#include "ebpf_structs.h"

// If we're compiling an actual eBPF program, then include
// libbpf's bpf_helpers.h for the rest of the platform-agnostic
Expand Down
7 changes: 6 additions & 1 deletion include/bpf_helpers_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// SPDX-License-Identifier: MIT
#pragma once

// This file contains platform-specific defines used by eBPF programs.
/**
* @file
* @brief This file contains platform specific defines used by eBPF programs.
*/

#include <stdint.h>

// For eBPF programs, struct bpf_map means struct _ebpf_map_definition_in_file,
// since they use inner_map_idx and pass pointers to such structures to the various
Expand Down
7 changes: 4 additions & 3 deletions include/ebpf_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

#pragma once

#include <specstrings.h>
#include <stdbool.h>
#include <stdint.h>
#include "ebpf_core_structs.h"
#include "ebpf_execution_type.h"
#include "ebpf_program_attach_type_guids.h"
#include "ebpf_result.h"

#include <specstrings.h>
#include <stdbool.h>
#include <stdint.h>

#ifdef __cplusplus
#include <stdexcept>
#define EBPF_NO_EXCEPT noexcept
Expand Down
11 changes: 7 additions & 4 deletions include/ebpf_core_structs.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#pragma once

// This file contains eBPF definitions common to eBPF core libraries as well as
// the eBPF API library.
/**
* @file
* @brief This file contains eBPF definitions common to eBPF core libraries as well as
* the eBPF API library.
*/

#pragma once
#include "ebpf_structs.h"

#include <sal.h>
#include <stdint.h>
#include "ebpf_structs.h"

#define EBPF_MAX_PIN_PATH_LENGTH 256

Expand Down
4 changes: 2 additions & 2 deletions include/ebpf_extension_uuids.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#pragma once

#include <stdint.h>
#include "ebpf_windows.h"

#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
Expand Down
4 changes: 2 additions & 2 deletions include/ebpf_program_attach_type_guids.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT

#pragma once

#include "ebpf_windows.h"

#include <stdbool.h>
#include <stdint.h>
#include "ebpf_windows.h"

#ifdef __cplusplus
extern "C"
Expand Down
Loading