Skip to content

Commit

Permalink
Merge pull request #2290 from DARMA-tasking/2288-header-guards-genera…
Browse files Browse the repository at this point in the history
…tion

#2288: Add new script to generate/fix header guards and license
  • Loading branch information
lifflander authored Jun 6, 2024
2 parents 4e8275f + 4a6968d commit 70cbb0d
Show file tree
Hide file tree
Showing 78 changed files with 250 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Check header guards
name: Check header guards and license

on: pull_request

Expand All @@ -15,8 +15,5 @@ jobs:
with:
python-version: '3.7'

- name: Install checkguard
run: pip install guardonce

- name: Check that include guards are properly named
- name: Check that for correct include guards and license
run: ./scripts/check_guards.sh $(pwd)
16 changes: 0 additions & 16 deletions .github/workflows/check-license.yml

This file was deleted.

16 changes: 9 additions & 7 deletions scripts/check_guards.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env bash

path_to_vt=${1}
wrong_files=""

cd "$path_to_vt" || exit 1

for sub_dir in "src" "tests/unit" "tests/perf"
for sub_dir in "src" "tests" "tutorial" "examples" "tools"
do
wrong_files+=$(checkguard -r "$sub_dir" -p "path -1 | prepend INCLUDED_ | upper")
python3 "${path_to_vt}/scripts/generate_header_guards_and_license.py" -s=${sub_dir} -l="${path_to_vt}/scripts/license-template"
done

if [[ $wrong_files ]]; then
echo "Files with wrong header guard:"
printf "%s\n" "$wrong_files"
exit 1
# Check for modified files
modified_files=$(git ls-files -m)

if [ -n "$modified_files" ]; then
echo "The following files have been modified by generate_header_guards_and_license.py:"
echo "$modified_files"
echo "Please run path_to_vt/scripts/generate_header_guards_and_license.py -s=your_sub_dir -l=path_to_vt/scripts/license-template to fix them"
fi
135 changes: 135 additions & 0 deletions scripts/generate_header_guards_and_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os
import re
import argparse as ap

def has_license_header(lines):
"""
Check if the first two lines contain the license header start.
"""
if len(lines) >= 2 and lines[0] == "/*\n" and lines[1] == "//@HEADER\n":
return True
return False

def find_existing_guards(lines):
"""
Find the range of lines containing existing header guards.
"""
ifndef_pattern = re.compile(r'#(?:if !defined|ifndef)\s+([A-Z0-9_]+)')
define_pattern = re.compile(r'#define\s+([A-Z0-9_]+)')
endif_pattern = re.compile(r'#endif')

ifndef_start = None
define_line = None
endif_line = None

for i, line in enumerate(lines):
if ifndef_start is None:
match = ifndef_pattern.match(line)
if match:
ifndef_start = i
if ifndef_start is not None and define_line is None:
match = define_pattern.match(line)
if match:
define_line = i
if ifndef_start is not None and define_line is not None:
match = endif_pattern.match(line)
if match:
endif_line = i

if ifndef_start is not None and define_line is not None and endif_line is not None:
return ifndef_start, endif_line
return None, None


def generate_license(file_path, license_path):
with open(file_path, 'r') as file:
lines = file.readlines()

with open(license_path, 'r') as license_file:
license_file = license_file.readlines()

file_name = os.path.basename(file_path)
lenright = int((80 - 2 - len(file_name))/2)
license_file[3] = f"//{' ' :<{lenright}}{file_name}\n"

# Remove leading empty lines
non_empty_lines_start = 0
for i, line in enumerate(lines):
if line.strip() != '':
non_empty_lines_start = i
break

trimmed_lines = lines[non_empty_lines_start:]

existing_license_start = -1
existing_license_end = -1
for i, line in enumerate(trimmed_lines):
if line.startswith("//@HEADER"):
if existing_license_start == -1:
existing_license_start = i
elif existing_license_end == -1:
existing_license_end = i
break

if existing_license_start != -1:
updated_file = trimmed_lines[:existing_license_start] + license_file + trimmed_lines[existing_license_end + 1:]
else:
updated_file = ['/*\n'] + license_file + ['*/\n'] + trimmed_lines

with open(file_path, 'w') as file:
file.writelines(updated_file)



def generate_header_guard(file_path, root):
with open(file_path, 'r') as file:
lines = file.readlines()

has_license = has_license_header(lines)

# Create a header guard macro name based on the file name
base_name = os.path.relpath(file_path, root).upper().replace('/', '_').replace('.', '_').replace('-', '_')
guard_name = f'INCLUDED_{base_name}'

# Add header guards
ifndef_guard = f'#if !defined {guard_name}\n#define {guard_name}\n'
endif_guard = f'#endif /*{guard_name}*/\n'

# Detect existing guards
start_line, end_line = find_existing_guards(lines)

if has_license:
license_content = ''.join(lines[:42])
file_content = ''.join(lines[42:])
else:
license_content = ''
file_content = ''.join(lines)

if start_line is not None and end_line is not None:
# Replace existing guards
new_content = license_content + "\n" + ifndef_guard + ''.join(lines[start_line+2:end_line]) + endif_guard
else:
# Insert new guards
new_content = license_content + "\n" + ifndef_guard + file_content + endif_guard

with open(file_path, 'w') as file:
file.write(new_content)

def main():
parser = ap.ArgumentParser()
parser.add_argument("--src_dir", "-s", dest='src_dir', required=True)
parser.add_argument("--license", "-l", dest='license_path', required=True)
args = parser.parse_args()

src_dir_abs = os.path.abspath(os.path.expanduser(args.src_dir))
license_path_abs = os.path.abspath(os.path.expanduser(args.license_path))
for root, _, files in os.walk(src_dir_abs):
if "extern" not in root.split(os.sep): # skip googletest
for file in files:
if file.endswith('.h') or file.endswith('.cc'):
generate_license(os.path.join(root, file), license_path_abs)
if file.endswith('.h'):
generate_header_guard(os.path.join(root, file), src_dir_abs)

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion src/vt/activefn/activefn.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ using ActiveTypedRDMAPutFnType = void(

} // end namespace vt

#endif /*INCLUDED_VT_ACTIVEFN_ACTIVEFN_H*/
#endif /*INCLUDED_VT_ACTIVEFN_ACTIVEFN_H*/
1 change: 0 additions & 1 deletion src/vt/collective/reduce/reduce.fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
//@HEADER
*/


#if !defined INCLUDED_VT_COLLECTIVE_REDUCE_REDUCE_FWD_H
#define INCLUDED_VT_COLLECTIVE_REDUCE_REDUCE_FWD_H

Expand Down
2 changes: 1 addition & 1 deletion src/vt/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
#include "vt/configs/types/types_headers.h"
#include "vt/configs/error/error_headers.h"

#endif /*INCLUDED_VT_CONFIG_H*/
#endif /*INCLUDED_VT_CONFIG_H*/
2 changes: 1 addition & 1 deletion src/vt/configs/types/types_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
#include "vt/configs/types/types_sentinels.h"
#include "vt/epoch/epoch_type.h"

#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_HEADERS_H*/
#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_HEADERS_H*/
2 changes: 1 addition & 1 deletion src/vt/configs/types/types_rdma.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ using RDMA_PutSerialize = std::function<RDMA_PutRetType(RDMA_PutRetType)>;

} // end namespace vt

#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_RDMA_H*/
#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_RDMA_H*/
2 changes: 1 addition & 1 deletion src/vt/configs/types/types_sentinels.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ static constexpr ThreadIDType const no_thread_id = 0;

} // end namespace vt

#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_SENTINELS_H*/
#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_SENTINELS_H*/
2 changes: 1 addition & 1 deletion src/vt/configs/types/types_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ static constexpr BitCountType const

} // end namespace vt

#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_SIZE_H*/
#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_SIZE_H*/
2 changes: 1 addition & 1 deletion src/vt/configs/types/types_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,4 @@ using ActionNodeType = std::function<void(NodeType)>;

} // end namespace vt

#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_TYPE_H*/
#endif /*INCLUDED_VT_CONFIGS_TYPES_TYPES_TYPE_H*/
1 change: 0 additions & 1 deletion src/vt/context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,3 @@ extern ctx::Context* theContext();
} // end namespace vt

#endif /*INCLUDED_VT_CONTEXT_CONTEXT_H*/

1 change: 0 additions & 1 deletion src/vt/objgroup/type_registry/registry.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
//@HEADER
*/


#if !defined INCLUDED_VT_OBJGROUP_TYPE_REGISTRY_REGISTRY_IMPL_H
#define INCLUDED_VT_OBJGROUP_TYPE_REGISTRY_REGISTRY_IMPL_H

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
//@HEADER
*/


#if !defined INCLUDED_VT_PIPE_CALLBACK_OBJGROUP_BCAST_CALLBACK_OBJGROUP_BCAST_IMPL_H
#define INCLUDED_VT_PIPE_CALLBACK_OBJGROUP_BCAST_CALLBACK_OBJGROUP_BCAST_IMPL_H

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
//@HEADER
*/


#if !defined INCLUDED_VT_PIPE_CALLBACK_OBJGROUP_SEND_CALLBACK_OBJGROUP_SEND_H
#define INCLUDED_VT_PIPE_CALLBACK_OBJGROUP_SEND_CALLBACK_OBJGROUP_SEND_H

Expand Down
1 change: 0 additions & 1 deletion src/vt/pipe/pipe_callback_only.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
//@HEADER
*/


#if !defined INCLUDED_VT_PIPE_PIPE_CALLBACK_ONLY_H
#define INCLUDED_VT_PIPE_PIPE_CALLBACK_ONLY_H

Expand Down
1 change: 1 addition & 0 deletions src/vt/registry/auto/auto_registry_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_VT_REGISTRY_AUTO_AUTO_REGISTRY_GENERAL_H
#define INCLUDED_VT_REGISTRY_AUTO_AUTO_REGISTRY_GENERAL_H

Expand Down
1 change: 1 addition & 0 deletions src/vt/registry/auto/auto_registry_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_VT_REGISTRY_AUTO_AUTO_REGISTRY_IMPL_H
#define INCLUDED_VT_REGISTRY_AUTO_AUTO_REGISTRY_IMPL_H

Expand Down
1 change: 1 addition & 0 deletions src/vt/registry/auto/functor/auto_registry_functor_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_VT_REGISTRY_AUTO_FUNCTOR_AUTO_REGISTRY_FUNCTOR_IMPL_H
#define INCLUDED_VT_REGISTRY_AUTO_FUNCTOR_AUTO_REGISTRY_FUNCTOR_IMPL_H

Expand Down
1 change: 1 addition & 0 deletions src/vt/registry/auto/vc/auto_registry_vc_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_VT_REGISTRY_AUTO_VC_AUTO_REGISTRY_VC_IMPL_H
#define INCLUDED_VT_REGISTRY_AUTO_VC_AUTO_REGISTRY_VC_IMPL_H

Expand Down
2 changes: 1 addition & 1 deletion src/vt/runtime/mpi_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ namespace vt { namespace runtime {
}} // end namespace vt::runtime
#endif // vt_check_enabled(mpi_access_guards)

#endif /* INCLUDED_VT_RUNTIME_MPI_ACCESS_H */
#endif /*INCLUDED_VT_RUNTIME_MPI_ACCESS_H*/
2 changes: 1 addition & 1 deletion src/vt/serialization/messaging/serialized_data_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ using SerialEagerPayloadMsg = SerialPayloadMsg<

}} /* end namespace vt::serialization */

#endif /*INCLUDED_VT_SERIALIZATION_MESSAGING/SERIALIZED_DATA_MSG_H*/
#endif /*INCLUDED_VT_SERIALIZATION_MESSAGING_SERIALIZED_DATA_MSG_H*/
2 changes: 1 addition & 1 deletion src/vt/serialization/messaging/serialized_messenger.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ using SerializedMessenger = ::vt::serialization::SerializedMessenger;

#include "vt/serialization/messaging/serialized_messenger.impl.h"

#endif /*INCLUDED_VT_SERIALIZATION_MESSAGING/SERIALIZED_MESSENGER_H*/
#endif /*INCLUDED_VT_SERIALIZATION_MESSAGING_SERIALIZED_MESSENGER_H*/
2 changes: 1 addition & 1 deletion src/vt/topos/index/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ template <typename T> using IdxType3D = index::Index3D<T>;

#include "vt/topos/index/printer/print_index.h"

#endif /*INCLUDED_VT_TOPOS_INDEX_INDEX_H*/
#endif /*INCLUDED_VT_TOPOS_INDEX_INDEX_H*/
2 changes: 1 addition & 1 deletion src/vt/topos/index/index_example.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ static_assert(

}} // end namespace vt::index

#endif /*INCLUDED_VT_TOPOS_INDEX_INDEX_EXAMPLE_H*/
#endif /*INCLUDED_VT_TOPOS_INDEX_INDEX_EXAMPLE_H*/
2 changes: 1 addition & 1 deletion src/vt/topos/mapping/mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ using CoreSeedMapType = SeedMapType;

}} // end namespace vt::location

#endif /*INCLUDED_VT_TOPOS_MAPPING_MAPPING_H*/
#endif /*INCLUDED_VT_TOPOS_MAPPING_MAPPING_H*/
2 changes: 1 addition & 1 deletion src/vt/topos/mapping/mapping_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ using CollectionMapFnType = ActiveMapFnPtrType;

}} /* end namespace vt::mapping */

#endif /*INCLUDED_VT_TOPOS_MAPPING/MAPPING_FUNCTION_H*/
#endif /*INCLUDED_VT_TOPOS_MAPPING_MAPPING_FUNCTION_H*/
2 changes: 1 addition & 1 deletion src/vt/topos/mapping/mapping_headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
#include "vt/topos/mapping/dense/dense.h"
#include "vt/topos/mapping/mapping_function.h"

#endif /*INCLUDED_VT_TOPOS_MAPPING_MAPPING_HEADERS_H*/
#endif /*INCLUDED_VT_TOPOS_MAPPING_MAPPING_HEADERS_H*/
2 changes: 1 addition & 1 deletion src/vt/utils/bits/bits_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ using BitPackerType = utils::BitPacker;

} // end namespace vt

#endif /*INCLUDED_VT_UTILS_BITS_BITS_COMMON_H*/
#endif /*INCLUDED_VT_UTILS_BITS_BITS_COMMON_H*/
2 changes: 1 addition & 1 deletion src/vt/utils/bits/bits_counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ struct BitCounter {

}} // end namespace vt::utils

#endif /*INCLUDED_VT_UTILS_BITS_BITS_COUNTER_H*/
#endif /*INCLUDED_VT_UTILS_BITS_BITS_COUNTER_H*/
2 changes: 1 addition & 1 deletion src/vt/utils/bits/bits_packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ namespace vt {

#include "vt/utils/bits/bits_packer.impl.h"

#endif /*INCLUDED_VT_UTILS_BITS_BITS_PACKER_H*/
#endif /*INCLUDED_VT_UTILS_BITS_BITS_PACKER_H*/
1 change: 0 additions & 1 deletion src/vt/utils/container/concurrent_deque.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,3 @@ using ConcurrentDeque = ConcurrentDequeLocked<T>;
}}} //end namespace vt::util::container

#endif /*INCLUDED_VT_UTILS_CONTAINER_CONCURRENT_DEQUE_H*/

1 change: 0 additions & 1 deletion src/vt/utils/container/concurrent_deque_locked.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,3 @@ struct ConcurrentDequeLocked {
#include "vt/utils/container/concurrent_deque_locked.impl.h"

#endif /*INCLUDED_VT_UTILS_CONTAINER_CONCURRENT_DEQUE_LOCKED_H*/

2 changes: 1 addition & 1 deletion src/vt/utils/static_checks/all_true.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ using all_true = std::is_same<BoolPack<bs..., true>, BoolPack<true, bs...>>;

}} /* end namespace vt::util */

#endif /*INCLUDED_VT_UTILS_STATIC_CHECKS/ALL_TRUE_H*/
#endif /*INCLUDED_VT_UTILS_STATIC_CHECKS_ALL_TRUE_H*/
Loading

0 comments on commit 70cbb0d

Please sign in to comment.