Skip to content

Commit

Permalink
Integrate IWYU (include what you use) tool, format cmake, formatting …
Browse files Browse the repository at this point in the history
…- put system includes above project includes
  • Loading branch information
shravanrn committed Jun 29, 2019
1 parent 51dca6d commit d64ec50
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 38 deletions.
6 changes: 3 additions & 3 deletions .clang-format
Expand Up @@ -62,10 +62,10 @@ ForEachMacros:
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
- Regex: '^"'
Priority: 3
- Regex: '^<'
Priority: 2
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
Expand Down
98 changes: 63 additions & 35 deletions CMakeLists.txt
@@ -1,8 +1,10 @@
cmake_minimum_required(VERSION 3.11)

project(rlbox-cpp17 VERSION 0.1 DESCRIPTION "RLBox safe sandboxing API in C++17")
project(rlbox-cpp17
VERSION 0.1
DESCRIPTION "RLBox safe sandboxing API in C++17")

########## Project Settings ##########
# Project Settings ###################

set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -12,67 +14,93 @@ set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC)
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -pedantic -Werror)
add_compile_options(-Wall
-Wextra
-pedantic
-Werror)
endif()

########## Dev Tools ##########
# Dev Tools ###################

find_program(CLANG_TIDY "clang-tidy")
if(CLANG_TIDY)
# Config in .clang-tidy
set(CMAKE_CXX_CLANG_TIDY clang-tidy)
# Config in .clang-tidy
set(CMAKE_CXX_CLANG_TIDY clang-tidy)
endif()

file(GLOB_RECURSE
ALL_CXX_SOURCE_FILES
code/*.[chi]pp
code/*.[chi]xx
code/*.cc
code/*.hh
code/*.ii
code/*.[CHI])

find_program(CLANG_FORMAT "clang-format")
if(CLANG_FORMAT)
file(GLOB_RECURSE
ALL_CXX_SOURCE_FILES
code/*.[chi]pp code/*.[chi]xx code/*.cc code/*.hh code/*.ii code/*.[CHI]
)

# Config in .clang-format
add_custom_target(
format-source
COMMAND clang-format
-i
-style=file
${ALL_CXX_SOURCE_FILES}
)
# Config in .clang-format
add_custom_target(format-source
COMMAND clang-format
-i
-style=file
${ALL_CXX_SOURCE_FILES})
endif()

find_program(IWYU "iwyu")
if(IWYU)
# TODO: Still have to ensure this checks .hpp files, which it doesn't right
# now. See bug below about "check_also"... which doesn't seem to work
# https://github.com/include-what-you-use/include-what-you-use/issues/633
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE
iwyu
-Xiwyu
--transitive_includes_only
-Xiwyu
--check_also="*.hpp"
-Xiwyu
--verbose=4
-Xiwyu
--mapping_file=${CMAKE_SOURCE_DIR}/iwyu.imp)
endif()

########## Dependencies ##########
# Dependencies ###################

include(FetchContent)
FetchContent_Declare(catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.9.1
)
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.9.1)

FetchContent_GetProperties(catch2)
if (NOT catch2_POLULATED)
FetchContent_Populate(catch2)
if(NOT catch2_POLULATED)
FetchContent_Populate(catch2)
endif()

add_subdirectory("${catch2_SOURCE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${catch2_SOURCE_DIR}/contrib")

########## Targets ##########
# Targets ###################

add_library(rlbox-cpp17-lib INTERFACE)
target_include_directories(rlbox-cpp17-lib INTERFACE code/include)

########## Tests ##########
# Tests ###################

add_library(testing-lib INTERFACE)
target_include_directories(testing-lib INTERFACE code/tests)

add_executable(testing
code/tests/test_main.cpp
code/tests/test_conversion.cpp
code/tests/test_tainted_sizes.cpp
code/tests/test_tainted_assignment.cpp
code/tests/test_sandbox_ptr_conversion.cpp
)
target_link_libraries(testing Catch2::Catch2 rlbox-cpp17-lib)
code/tests/test_main.cpp
code/tests/test_conversion.cpp
code/tests/test_tainted_sizes.cpp
code/tests/test_tainted_assignment.cpp
code/tests/test_sandbox_ptr_conversion.cpp)

target_link_libraries(testing
Catch2::Catch2
rlbox-cpp17-lib
testing-lib)

include(CTest)
include(Catch)
catch_discover_tests(testing)

2 changes: 2 additions & 0 deletions code/include/rlbox_assign.hpp
@@ -1,4 +1,6 @@
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

#include "rlbox_helpers.hpp"
#include "rlbox_types.hpp"
Expand Down
2 changes: 2 additions & 0 deletions code/include/rlbox_conversion.hpp
@@ -1,4 +1,6 @@
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

#include <functional>
#include <numeric>
Expand Down
2 changes: 2 additions & 0 deletions code/include/rlbox_helpers.hpp
@@ -1,4 +1,6 @@
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

#include <iostream>
#include <stdexcept>
Expand Down
2 changes: 2 additions & 0 deletions code/include/rlbox_sandbox.hpp
@@ -1,4 +1,6 @@
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

#include <type_traits>

Expand Down
2 changes: 2 additions & 0 deletions code/include/rlbox_types.hpp
@@ -1,4 +1,6 @@
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

namespace rlbox {

Expand Down
2 changes: 2 additions & 0 deletions code/include/rlbox_typetraits.hpp
@@ -1,4 +1,6 @@
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

#include <type_traits>

Expand Down
4 changes: 4 additions & 0 deletions code/tests/test_conversion.cpp
@@ -1,3 +1,7 @@
#include <cstdint>
#include <limits>
#include <type_traits>

#include "test_include.hpp"

struct Foo
Expand Down
5 changes: 5 additions & 0 deletions code/tests/test_include.hpp
@@ -1,13 +1,18 @@
#pragma once

#include <cstdint>
#include <limits>
#include <type_traits>

// IWYU pragma: begin_exports
#include "catch2/catch.hpp"

#define RLBOX_NO_COMPILE_CHECKS
#define RLBOX_USE_EXCEPTIONS
#include "rlbox.hpp"

// IWYU pragma: end_exports

#define UNUSED(varName) (void)varName

const uint32_t SandboxMemoryBase = 0xAF00;
Expand Down
2 changes: 2 additions & 0 deletions code/tests/test_sandbox_ptr_conversion.cpp
@@ -1,3 +1,5 @@
#include <cstdint>

#include "test_include.hpp"

// NOLINTNEXTLINE
Expand Down
3 changes: 3 additions & 0 deletions code/tests/test_tainted_sizes.cpp
@@ -1,3 +1,6 @@
#include <array>
#include <cstdint>

#include "test_include.hpp"

using CallbackType = int (*)(uint32_t, const char*, std::array<uint32_t, 1>);
Expand Down
5 changes: 5 additions & 0 deletions iwyu.imp
@@ -0,0 +1,5 @@
[
{ include: ["<bits/stdint-intn.h>", "private", "<cstdint>", "public"] },
{ include: ["<bits/stdint-uintn.h>", "private", "<cstdint>", "public"] },
{ include: ["<cstdint.h>", "private", "<cstdint>", "public"] }
]

0 comments on commit d64ec50

Please sign in to comment.