Skip to content

Commit

Permalink
Linter Test 27
Browse files Browse the repository at this point in the history
  • Loading branch information
onurulgen committed Feb 13, 2024
1 parent 41c20a6 commit d9bc1a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
26 changes: 17 additions & 9 deletions .github/code_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Input variables from Github action
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
PR_NUM = os.getenv("PR_NUMBER", "-1")
WORK_DIR = os.getenv("GITHUB_WORKSPACE")
WORK_DIR = f'{os.getenv("GITHUB_WORKSPACE")}'
REPO_NAME = os.getenv("REPO")
TARGET_REPO_NAME = os.getenv("REPO", "")
SHA = os.getenv("GITHUB_SHA")
Expand Down Expand Up @@ -207,9 +207,10 @@ def is_excluded_dir(line):
if not exclude_dir:
return False

debug_print(f"{line} and {exclude_dir} with result {line.startswith(exclude_dir)}")
excluded_dir = f"{WORK_DIR}/{exclude_dir}"
debug_print(f"{line} and {excluded_dir} with result {line.startswith(excluded_dir)}")

return line.startswith(exclude_dir)
return line.startswith(excluded_dir)


def get_file_line_end(file_in, file_line_start_in):
Expand Down Expand Up @@ -354,12 +355,13 @@ def generate_output(is_note, file_path, file_line_start, file_line_end, descript
return new_line


def extract_info(line):
def extract_info(line, prefix):
"""
Extracts information from a given line containing file path, line number, and issue description.
Args:
- line (str): The input string containing file path, line number, and issue description.
- prefix (str): The prefix to remove from the start of the file path in the line.
- was_note (bool): Indicates if the previous issue was a note.
- output_string (str): The string containing previous output information.
Expand All @@ -372,6 +374,9 @@ def extract_info(line):
- file_line_end (int): The ending line number of the issue.
"""

# Clean up line
line = line.replace(prefix, "").lstrip("/")

# Get the line starting position /path/to/file:line and trim it
file_path_end_idx = line.index(":")
file_path = line[:file_path_end_idx]
Expand Down Expand Up @@ -425,12 +430,13 @@ def append_issue(is_note, per_issue_string, new_line, list_of_issues):
return per_issue_string


def create_comment_for_output(tool_output, files_changed_in_pr, output_to_console):
def create_comment_for_output(tool_output, prefix, files_changed_in_pr, output_to_console):
"""
Generates a comment for a GitHub pull request based on the tool output.
Parameters:
tool_output (str): The tool output to parse.
prefix (str): The prefix to look for in order to identify issues.
files_changed_in_pr (dict): A dictionary containing the files that were
changed in the pull request and the lines that were modified.
output_to_console (bool): Whether or not to output the results to the console.
Expand All @@ -443,14 +449,14 @@ def create_comment_for_output(tool_output, files_changed_in_pr, output_to_consol
was_note = False

for line in tool_output:
if not line[0].isspace() and not is_excluded_dir(line):
if line.startswith(prefix) and not is_excluded_dir(line):
(
file_path,
is_note,
file_line_start,
file_line_end,
issue_description,
) = extract_info(line)
) = extract_info(line, prefix)

# In case where we only output to console, skip the next part
if output_to_console:
Expand Down Expand Up @@ -523,14 +529,16 @@ def read_files_and_parse_results():
common_ancestor = parser.parse_args().common
feature_branch = parser.parse_args().head

debug_print(f"cppcheck result: \n {cppcheck_content} \n")
line_prefix = f"{WORK_DIR}"

debug_print(f"cppcheck result: \n {cppcheck_content} \n" f"line_prefix: {line_prefix} \n")

files_changed_in_pr = {}
if not output_to_console and (ONLY_PR_CHANGES == "true"):
files_changed_in_pr = get_changed_files(common_ancestor, feature_branch)

cppcheck_comment, cppcheck_issues_found = create_comment_for_output(
cppcheck_content, files_changed_in_pr, output_to_console
cppcheck_content, line_prefix, files_changed_in_pr, output_to_console
)

if output_to_console and cppcheck_issues_found:
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
git clone https://github.com/danmar/cppcheck.git
cd cppcheck
git checkout 2.13.x
# Disable color output of cppcheck
sed -i 's/ *bool *gDisableColors *= *false;/bool gDisableColors = true;/' lib/color.cpp
sudo make -j4 MATCHCOMPILER=yes FILESDIR=/usr/share/cppcheck HAVE_RULES=yes CXXFLAGS="-O2 -DNDEBUG -Wall -Wno-sign-compare -Wno-unused-function" install
- name: Install Python dependencies
Expand Down Expand Up @@ -52,9 +54,9 @@ jobs:
run: |
analysis_file="analysis.txt"
cppcheck_params="--enable=warning --check-level=exhaustive --suppress=internalError --suppress=internalAstError"
cppcheck -j4 $cppcheck_params --project=build/compile_commands.json --output-file=$analysis_file
cppcheck -j4 $cppcheck_params --project=$(pwd)/build/compile_commands.json --output-file=$analysis_file
# Since cppcheck does not support OpenCL and CUDA, we need to check these files separately
find reg-lib/cl/. -name "*.cl" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done
find reg-lib/cuda/. -name "*.cu" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done
find $(pwd)/reg-lib/cl/. -name "*.cl" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done
find $(pwd)/reg-lib/cuda/. -name "*.cu" -print0 | while IFS= read -r -d '' file; do cppcheck "$file" $cppcheck_params --language=c++ 2>> $analysis_file; done
cat $analysis_file
python3 ${{ github.workspace }}/.github/code_analysis.py -cc $analysis_file -o false -fk false
2 changes: 1 addition & 1 deletion niftyreg_build_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
449
450

0 comments on commit d9bc1a3

Please sign in to comment.