Skip to content

Commit

Permalink
Add support for a clang-tidy linter. Add a files utility function.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Nov 17, 2023
1 parent ca2342e commit 43278c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
3 changes: 0 additions & 3 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ google-*,
FormatStyle: file
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
UseColor: true
CheckOptions:
- { key: misc-const-correctness.AnalyzeValues, value: false }
Expand All @@ -33,6 +32,4 @@ CheckOptions:
- { key: readability-identifier-naming.GlobalConstantCase, value: UPPER_CASE }
- { key: readability-identifier-naming.StaticConstantCase, value: UPPER_CASE }
- { key: readability-identifier-naming.StaticVariableCase, value: UPPER_CASE }
ExtraArgs:
- '--std=c++17'
...
45 changes: 41 additions & 4 deletions cmake/Linters.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ file(
tools/*.h
)
# Filter some folders from the CI checks.
list(FILTER P4C_LINT_LIST EXCLUDE REGEX "backends/p4tools/submodules")
list(FILTER P4C_LINT_LIST EXCLUDE REGEX "backends/ebpf/runtime")
list(FILTER P4C_LINT_LIST EXCLUDE REGEX "backends/ubpf/runtime")
list(FILTER P4C_LINT_LIST EXCLUDE REGEX "control-plane/p4runtime")
Expand All @@ -29,7 +28,7 @@ list(FILTER P4C_LINT_LIST EXCLUDE REGEX "test/frameworks")
add_cpplint_files(${P4C_SOURCE_DIR} "${P4C_LINT_LIST}")

# Retrieve the global cpplint property.
get_property(CPPLINT_FILES GLOBAL PROPERTY cpplint-files)
get_property(CPPLINT_FILES GLOBAL PROPERTY CPPLINT-files)

if(DEFINED CPPLINT_FILES)
# Write the list to a file. We need to do this as too many files will reach
Expand Down Expand Up @@ -61,7 +60,7 @@ find_program(CLANG_FORMAT_CMD clang-format)

if(NOT ${CLANG_FORMAT_CMD})
# Retrieve the global clang-format property.
get_property(CLANG_FORMAT_FILES GLOBAL PROPERTY clang-format-files)
get_property(CLANG_FORMAT_FILES GLOBAL PROPERTY CLANG_FORMAT-files)
if(DEFINED CLANG_FORMAT_FILES)
# Write the list to a file.
# We need to do this as too many files will reach the shell argument limit.
Expand All @@ -87,6 +86,44 @@ else()
endif()


#################### CLANG-TIDY

# TODO: Add files here once clang-tidy is fast enough.
# file(
# GLOB_RECURSE P4C_CLANG_TIDY_LINT_LIST FOLLOW_SYMLINKS
# )
# add_clang_tidy_files(${P4C_SOURCE_DIR} "${P4C_CLANG_TIDY_LINT_LIST}")

find_program(CLANG_TIDY_CMD clang-tidy)

if(NOT ${CLANG_TIDY_CMD})
# Retrieve the global clang-tidy property.
get_property(CLANG_TIDY_FILES GLOBAL PROPERTY CLANG_TIDY-files)
if(DEFINED CLANG_TIDY_FILES)
# Write the list to a file.
# We need to do this as too many files will reach the shell argument limit.
set(CLANG_TIDY_TXT_FILE ${P4C_BINARY_DIR}/clang_tidy_files.txt)
list(SORT CLANG_TIDY_FILES)
file(WRITE ${CLANG_TIDY_TXT_FILE} "${CLANG_TIDY_FILES}")
set(CLANG_TIDY_CMD clang-tidy)
add_custom_target(
clang-tidy
COMMAND xargs -a ${CLANG_TIDY_TXT_FILE} -r -d '\;' ${CLANG_TIDY_CMD} -extra-arg=-std=c++17 -p ${CMAKE_BINARY_DIR}
WORKING_DIRECTORY ${P4C_SOURCE_DIR}
COMMENT "Applying clang-tidy analysis."
)
add_custom_target(
clang-tidy-fix-errors
COMMAND xargs -a ${CLANG_TIDY_TXT_FILE} -r -d '\;' ${CLANG_TIDY_CMD} -extra-arg=-std=c++17 -p ${CMAKE_BINARY_DIR} --fix
WORKING_DIRECTORY ${P4C_SOURCE_DIR}
COMMENT "Applying clang-tidy fix-its."
)
endif()
else()
message(WARNING "clang-tidy executable not found. Disabling clang-tidy checks. clang-tidy can be installed with \"pip3 install --user --upgrade clang-tidy\"")
endif()


#################### BLACK

file(
Expand All @@ -106,7 +143,7 @@ find_program(ISORT_CMD isort)
# Black and isort share the same files and should be run together.
if(NOT ${BLACK_CMD} OR NOT (NOT ${ISORT_CMD}))
# Retrieve the global black property.
get_property(BLACK_FILES GLOBAL PROPERTY black-files)
get_property(BLACK_FILES GLOBAL PROPERTY BLACK-files)
if(DEFINED BLACK_FILES)
# Write the list to a file.
# We need to do this as too many files will reach the shell argument limit.
Expand Down
72 changes: 22 additions & 50 deletions cmake/P4CUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,76 +48,48 @@ macro (p4c_add_library name symbol var)
endif()
endmacro(p4c_add_library)

# Add files with the appropriate path to the list of cpplint-linted files.
function(add_cpplint_files dir filelist)
# Utility function which adds @param filelist to a global list of ${label}-files
function(add_files dir filelist label)
if (NOT filelist)
message(WARNING "Input file list is empty. Returning.")
return()
endif()
# Initialize an empty list.
set (__cpplintFileList "")
set (__FileList "")
foreach(__f ${filelist})
string(REGEX MATCH "^/.*" abs_path "${__f}")
if (NOT ${abs_path} EQUAL "")
list (APPEND __cpplintFileList "${__f}")
list (APPEND __FileList "${__f}")
else()
list (APPEND __cpplintFileList "${dir}/${__f}")
list (APPEND __FileList "${dir}/${__f}")
endif()
endforeach(__f)

# Get the global cpplint property and append to it.
get_property(CPPLINT_FILES GLOBAL PROPERTY cpplint-files)
list (APPEND CPPLINT_FILES "${__cpplintFileList}")
list(REMOVE_DUPLICATES CPPLINT_FILES)
set_property(GLOBAL PROPERTY cpplint-files "${CPPLINT_FILES}")
# Get the global label property and append to it.
get_property(${label}_FILES GLOBAL PROPERTY ${label}-files)
list (APPEND ${label}_FILES "${__FileList}")
list(REMOVE_DUPLICATES ${label}_FILES)
set_property(GLOBAL PROPERTY ${label}-files "${${label}_FILES}")
endfunction(add_files)

# Add files with the appropriate path to the list of cpplint-linted files.
function(add_cpplint_files dir filelist)
add_files(${dir} "${filelist}" CPPLINT)
endfunction(add_cpplint_files)

# Add files with the appropriate path to the list of clang-format-linted files.
function(add_clang_format_files dir filelist)
if (NOT filelist)
message(WARNING "Input file list is empty. Returning.")
return()
endif()
# Initialize an empty list.
set (__clangFormatFileList "")
foreach(__f ${filelist})
string(REGEX MATCH "^/.*" abs_path "${__f}")
if (NOT ${abs_path} EQUAL "")
list (APPEND __clangFormatFileList "${__f}")
else()
list (APPEND __clangFormatFileList "${dir}/${__f}")
endif()
endforeach(__f)

# Get the global clang-format property and append to it.
get_property(CLANG_FORMAT_FILES GLOBAL PROPERTY clang-format-files)
list (APPEND CLANG_FORMAT_FILES "${__clangFormatFileList}")
list(REMOVE_DUPLICATES CLANG_FORMAT_FILES)
set_property(GLOBAL PROPERTY clang-format-files "${CLANG_FORMAT_FILES}")
add_files(${dir} "${filelist}" CLANG_FORMAT)
endfunction(add_clang_format_files)

# Add files with the appropriate path to the list of clang-tidy-linted files.
function(add_clang_tidy_files dir filelist)
add_files(${dir} "${filelist}" CLANG_TIDY)
endfunction(add_clang_tidy_files)

# Add files with the appropriate path to the list of black-linted files.
function(add_black_files dir filelist)
if (NOT filelist)
message(WARNING "Input file list is empty. Returning.")
return()
endif()
# Initialize an empty list.
set (__blackFileList "")
foreach(__f ${filelist})
string(REGEX MATCH "^/.*" abs_path "${__f}")
if (NOT ${abs_path} EQUAL "")
list (APPEND __blackFileList "${__f}")
else()
list (APPEND __blackFileList "${dir}/${__f}")
endif()
endforeach(__f)

# Get the global clang-format property and append to it.
get_property(BLACK_FILES GLOBAL PROPERTY black-files)
list (APPEND BLACK_FILES "${__blackFileList}")
list(REMOVE_DUPLICATES BLACK_FILES)
set_property(GLOBAL PROPERTY black-files "${BLACK_FILES}")
add_files(${dir} "${filelist}" BLACK)
endfunction(add_black_files)

macro(p4c_test_set_name name tag alias)
Expand Down

0 comments on commit 43278c0

Please sign in to comment.