Skip to content

Commit

Permalink
Setting up clang-format and clang-tidy
Browse files Browse the repository at this point in the history
- Enables MSBuild CLI to run clang-tidy
- Extending default MSBuild support was required.
- That's done with the combination of batch and python scripts under msbuild/ folder.
- Custom runner supports a .clang-ignore file similar to a .gitignore syntax.
  • Loading branch information
CarlosNihelton committed Jan 7, 2022
1 parent 7b0c719 commit 9113cf5
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/clang_lint/msbuild/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!--
Copyright (C) 2021 Canonical Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************************************
Copy this file to the project folder to get it linted by clang-tidy.
MSBuild command line must be ran from the solution directory, though.
msbuild -p:Platform=<PLATFORM> -t:ClangTidy
***********************************************************************************************
-->

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<RootDir Condition="'$(GITHUB_WORKSPACE)' != ''">$(GITHUB_WORKSPACE)\</RootDir>
<RootDir Condition="'$(GITHUB_WORKSPACE)' == ''">$(MSBuildProjectDirectory)\..\</RootDir>
<ClangTidyToolPath>$(RootDir).github\workflows\clang_lint\msbuild\</ClangTidyToolPath>
<ClangTidyToolExe>msbuild-clang-tidy-runner.bat</ClangTidyToolExe>
</PropertyGroup>
</Project>
26 changes: 26 additions & 0 deletions .github/workflows/clang_lint/msbuild/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# /usr/bin/python
""" Invoked by msbuild to run in behalf of the actual clang-tidy.exe
to accept the command lines --export-fixes and --line-filter. """
#
# Copyright (C) 2021 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


import sys
from . import clang_tidy_runner

if __name__ == "__main__":
if len(sys.argv) > 1:
clang_tidy_runner.run(*sys.argv[1:])
75 changes: 75 additions & 0 deletions .github/workflows/clang_lint/msbuild/clang_tidy_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# /usr/bin/python
""" Invoked by msbuild to run in behalf of the actual clang-tidy.exe
to accept the command lines --export-fixes and --line-filter. """
#
# Copyright (C) 2021 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Source code adapted from https://github.com/platisd/clang-tidy-pr-comments.


import subprocess
import json
import os

from clang_lint.clang_lint import files_to_lint


def run(*kwargs):
export_fixes = "fixes.yaml"
# Need to find rootdir in some way.
rootdir = os.getenv("GITHUB_WORKSPACE")
if rootdir is None:
gitProc = subprocess.run(["git", "rev-parse", "--show-toplevel"],
capture_output=True, text=True)
if gitProc.returncode != 0:
print("Could not determine repository root")
# Running from MSBuild my current directory will be child of
# the repository root. At least for WSL.
rootdir = os.path.abspath("../")
else:
rootdir = gitProc.stdout.strip()
project_dir = "DistroLauncher"
sources = files_to_lint.files_to_lint(rootdir, project_dir)
# line_filter will contain the non-excluded files.
line_filter = []
for s in sources:
line_filter += [{"name": os.path.basename(s)}]

line_filter_cli = json.dumps(line_filter)

# Clean up just in case:
if os.path.exists(export_fixes):
os.remove(export_fixes)

# Removing the files msbuild asked for linting but our ignore file says
# to igore.
lc_sources = [s.lower() for s in sources]
msbuild_cli = list(kwargs)
msbuild_cli = [c for c in msbuild_cli
if not os.path.exists(c) or
os.path.normpath(c.lower()) in lc_sources]
cli = ["clang-tidy", "--export-fixes", export_fixes, "--line-filter",
line_filter_cli] + msbuild_cli
print("==== Running clang-tidy with the following arguments:")
print(cli)

proc = subprocess.run(cli)

if proc.returncode != 0:
print("clang-tidy failed to complete its run.")

if not os.path.exists(export_fixes):
print("clang-tidy could not generate any output due failures.")
return None
23 changes: 23 additions & 0 deletions .github/workflows/clang_lint/msbuild/msbuild-clang-tidy-runner.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@echo off
REM
REM Copyright (C) 2021 Canonical Ltd
REM
REM This program is free software: you can redistribute it and/or modify
REM it under the terms of the GNU General Public License version 3 as
REM published by the Free Software Foundation.
REM
REM This program is distributed in the hope that it will be useful,
REM but WITHOUT ANY WARRANTY; without even the implied warranty of
REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
REM GNU General Public License for more details.
REM
REM You should have received a copy of the GNU General Public License
REM along with this program. If not, see <http://www.gnu.org/licenses/>.
REM
REM
REM Since this file will run as if it was clang-tidy, it's working directory will be
REM the project folder, which for WSL is right below the root directory.
@echo on

SET PYTHONPATH=../.github/workflows/
python3 -m clang_lint.msbuild %*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,4 @@ wsl-builder/prepare-build/prepare-build

# And dependencies
**/vcpkg_installed/
**/*fixes.yaml
54 changes: 54 additions & 0 deletions DistroLauncher/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Format Style Options - Created with Clang Power Tools
---
BasedOnStyle: Microsoft
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AllowAllParametersOfDeclarationOnNextLine: false
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignConsecutiveAssignments: None
AlignOperands: Align
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: Empty
AllowAllConstructorInitializersOnNextLine: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: true
AfterControlStatement: Never
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
AfterObjCDeclaration: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: false
SplitEmptyNamespace: true
BreakConstructorInitializers: AfterColon
ColumnLimit: 120
ContinuationIndentWidth: 2
ConstructorInitializerAllOnOneLineOrOnePerLine: false
Cpp11BracedListStyle: true
DerivePointerAlignment: true
ExperimentalAutoDetectBinPacking: true
FixNamespaceComments: false
IndentWrappedFunctionNames: true
NamespaceIndentation: All
SortIncludes: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeParens: ControlStatements
SpaceInEmptyBlock: true
StatementAttributeLikeMacros: []
TabWidth: 4
UseCRLF: false
UseTab: Never
...
11 changes: 11 additions & 0 deletions DistroLauncher/.clang-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Excluding upstream files relative to current directory.

messages.h
resource.h
targetver.h
stdafx.cpp
expected.hpp
Helpers.*
WslApiLoader.*
DistributionInfo.*
DistroLauncher.cpp
4 changes: 2 additions & 2 deletions DistroLauncher/.clang-tidy
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Checks: 'bugprone-*,performance-*,readability-*,google-readability-casting'
HeaderFilterRegex: .*
Checks: 'bugprone-*,performance-*,readability-*,google-readability-casting,modernize-use-nullptr,cppcoreguidelines-avoid-non-const-global-variables,misc-misplaced-const'
HeaderFilterRegex: DistroLauncher/.+\.h$

0 comments on commit 9113cf5

Please sign in to comment.