Skip to content

Commit

Permalink
Merge pull request #48739 from jbytheway/clang-tidy-to-llvm-11
Browse files Browse the repository at this point in the history
Switch clang-tidy plugin and workflow to LLVM 12
  • Loading branch information
ZhilkinSerg authored May 31, 2021
2 parents 26f1ac8 + 8216b65 commit 11f8b39
Show file tree
Hide file tree
Showing 46 changed files with 297 additions and 142 deletions.
29 changes: 29 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,47 @@ modernize-*,\
performance-*,\
readability-*,\
-readability-braces-around-statements,\
-bugprone-branch-clone,\
-bugprone-infinite-loop,\
-bugprone-misplaced-widening-cast,\
-bugprone-narrowing-conversions,\
-bugprone-redundant-branch-condition,\
-bugprone-reserved-identifier,\
-bugprone-signed-char-misuse,\
-bugprone-sizeof-expression,\
-bugprone-unhandled-self-assignment,\
-cata-combine-locals-into-point,\
-cert-dcl37-c,\
-cert-dcl51-cpp,\
-cert-oop54-cpp,\
-cert-str34-c,\
-clang-analyzer-core.CallAndMessage,\
-clang-analyzer-deadcode.DeadStores,\
-misc-misplaced-const,\
-misc-no-recursion,\
-misc-non-private-member-variables-in-classes,\
-misc-redundant-expression,\
-modernize-avoid-bind,\
-modernize-avoid-c-arrays,\
-modernize-loop-convert,\
-modernize-pass-by-value,\
-modernize-return-braced-init-list,\
-modernize-use-default-member-init,\
-modernize-use-override,\
-performance-no-automatic-move,\
-performance-trivially-destructible,\
-performance-unnecessary-value-param,\
-readability-avoid-const-params-in-decls,\
-readability-convert-member-functions-to-static,\
-readability-else-after-return,\
-readability-function-cognitive-complexity,\
-readability-implicit-bool-conversion,\
-readability-magic-numbers,\
-readability-make-member-function-const,\
-readability-named-parameter,\
-readability-qualified-auto,\
-readability-redundant-access-specifiers,\
-readability-use-anyofallof,\
"
WarningsAsErrors: '*'
HeaderFilterRegex: '(src|test|tools).*'
Expand Down
13 changes: 7 additions & 6 deletions .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Clang-tidy (clang-8, tiles)
name: Clang-tidy (clang-12, tiles)

on:
push:
Expand Down Expand Up @@ -28,11 +28,11 @@ on:

jobs:
build:
runs-on: ubuntu-16.04
runs-on: ubuntu-20.04
env:
CMAKE: 1
CLANG: clang++-8
COMPILER: clang++-8
CLANG: clang++-12
COMPILER: clang++-12
CATA_CLANG_TIDY: plugin
TILES: 1
SOUND: 1
Expand All @@ -43,9 +43,10 @@ jobs:
fetch-depth: 1
- name: install dependencies
run: |
#sudo apt-add-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-8 main"
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-add-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-12 main"
sudo apt-get update
sudo apt-get install libncursesw5-dev clang-8 libclang-8-dev llvm-8-dev llvm-8-tools \
sudo apt-get install libncursesw5-dev clang-12 libclang-12-dev llvm-12-dev llvm-12-tools \
libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev libsdl2-mixer-dev libpulse-dev ccache \
gettext
- name: prepare
Expand Down
8 changes: 4 additions & 4 deletions build-scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ then
cmake_extra_opts+=("-DCATA_CLANG_TIDY_PLUGIN=ON")
# Need to specify the particular LLVM / Clang versions to use, lest it
# use the llvm-7 that comes by default on the Travis Xenial image.
cmake_extra_opts+=("-DLLVM_DIR=/usr/lib/llvm-8/lib/cmake/llvm")
cmake_extra_opts+=("-DClang_DIR=/usr/lib/llvm-8/lib/cmake/clang")
cmake_extra_opts+=("-DLLVM_DIR=/usr/lib/llvm-12/lib/cmake/llvm")
cmake_extra_opts+=("-DClang_DIR=/usr/lib/llvm-12/lib/cmake/clang")
fi

if [ "$COMPILER" = "clang++-8" -a -n "$GITHUB_WORKFLOW" -a -n "$CATA_CLANG_TIDY" ]
if [ "$COMPILER" = "clang++-12" -a -n "$GITHUB_WORKFLOW" -a -n "$CATA_CLANG_TIDY" ]
then
# This is a hacky workaround for the fact that the custom clang-tidy we are
# using is built for Travis CI, so it's not using the correct include directories
# for GitHub workflows.
cmake_extra_opts+=("-DCMAKE_CXX_FLAGS=-isystem /usr/include/clang/8.0.0/include")
cmake_extra_opts+=("-DCMAKE_CXX_FLAGS=-isystem /usr/include/clang/12.0.0/include")
fi

mkdir build
Expand Down
113 changes: 64 additions & 49 deletions doc/DEVELOPER_TOOLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ In addition to the usual means of creating a `tags` file via e.g. [`ctags`](http
Cataclysm has a [clang-tidy configuration file](../.clang-tidy) and if you have
`clang-tidy` available you can run it to perform static analysis of the
codebase. We test with `clang-tidy` from LLVM 8.0.1 on Travis, so for the most
codebase. We test with `clang-tidy` from LLVM 12.0.0 with CI, so for the most
consistent results, you might want to use that version.
To run it you have a few options.
To run it, you have a few options.
* `clang-tidy` ships with a wrapper script `run-clang-tidy.py`.
Expand All @@ -92,12 +92,12 @@ We have written our own clang-tidy checks in a custom plugin. Unfortunately,
`clang-tidy` as distributed by LLVM doesn't support plugins, so making this
work requires some extra steps.
#### Ubuntu Xenial
#### Ubuntu Focal
If you are on Ubuntu Xenial then you might be able to get it working the same
way Travis does. Add the LLVM 8 Xenial source [listed
here](https://apt.llvm.org/) to your `sources.list`, install the `clang-8
libclang-8-dev llvm-8-dev llvm-8-tools` packages, and build Cataclysm with CMake,
If you are on Ubuntu Focal then you might be able to get it working the same
way our CI does. Add the LLVM 12 Focal source [listed
here](https://apt.llvm.org/) to your `sources.list`, install the needed packages (`clang-12
libclang-12-dev llvm-12-dev llvm-12-tools`), and build Cataclysm with CMake,
adding `-DCATA_CLANG_TIDY_PLUGIN=ON`.
On other distributions you will probably need to build `clang-tidy` yourself.
Expand Down Expand Up @@ -126,7 +126,7 @@ To run `clang-tidy` with this plugin enabled add the
to your `clang-tidy` command line.

If you wish to run the tests for the custom clang-tidy plugin you will also
need `lit`. This will be built as part of `llvm`, or you can install it via
need `lit`. This will be built as part of LLVM, or you can install it via
`pip` or your local package manager if you prefer.

Then, assuming `build` is your Cataclysm build directory, you can run the tests
Expand All @@ -139,34 +139,32 @@ lit -v build/tools/clang-tidy-plugin/test

##### Build LLVM

To build llvm on Windows, you'll first need to get some tools installed.
To build LLVM on Windows, you'll first need to get some tools installed.
- Cmake
- Python 3 (Python 2 may be still required to run the lit test,
which will be discussed in the next section.)
- Python 3
- MinGW-w64 (other compilers may or may not work. Clang itself does not seem to be
building llvm on Windows correctly.)
building LLVM on Windows correctly.)
- A shell environment
After the tools are installed, a patch still needs to be applied before building
llvm, since `clang-tidy` as distributed by LLVM doesn't support plugins.
LLVM, since `clang-tidy` as distributed by LLVM doesn't support plugins.

First, clone the llvm repo from for example [the official github repo](https://github.com/llvm/llvm-project.git).
Checkout the `release/8.x` branch, since that's where our patch was based on.
First, clone the LLVM repo from, for example, [the official github repo](https://github.com/llvm/llvm-project.git).
Checkout the `release/12.x` branch, since that's what our patch was based on.
On Windows, instead of applying the patch mentioned in the previous section, you
shoud apply `plugin-support.patch` from [this PR](https://github.com/jbytheway/clang-tidy-plugin-support/pull/1)
instead, if it's not merged yet. This is because the `-rdynamic` option is not
supported on Windows, so clang-tidy needs to be built as a static library instead.
(If you cloned the repo from the official github repo, replace `tools/extra` with
`clang-tools-extra` in the patch before applying it.)
On Windows, in addition to applying `plugin-support.patch` mentioned in the previous section, you
should also apply
[`clang-tidy-scripts.patch`](https://github.com/jbytheway/clang-tidy-plugin-support/blob/master/clang-tidy-scripts.patch)
so you can run the lit test with the custom clang-tidy executable and let
clang-tidy apply suggestions automatically.
After the patch is applied, you can then build the llvm code. Unfortunately, it
seems that clang itself cannot correctly compile the llvm code on Windows (gives
After the patch is applied, you can then build the LLVM code. Unfortunately, it
seems that clang itself cannot correctly compile the LLVM code on Windows (gives
some sort of relocation error). Luckily, MinGW-w64 can be used instead to compile
the code.
The first step to build the code is to run CMake to generate the makefile. On
the root dir of llvm, run the following script (substitute values inside `<>`
the root dir of LLVM, run the following script (substitute values inside `<>`
with the actual paths). Make sure CMake, python, and MinGW-w64 are on the path.
```sh
Expand All @@ -185,7 +183,7 @@ cmake \
The next step is to call `make` to actually build clang-tidy as a library.
When using MinGW-w64 to build, you should call `mingw32-make` instead.
Also, because `FileCheck` is not shipped with Windows, you'll also need to build
it youself using llvm sources by adding the `FileCheck` target to the make command.
it yourself using LLVM sources by adding the `FileCheck` target to the make command.

```sh
mkdir -p build
Expand All @@ -198,14 +196,14 @@ are needed to build our custom clang-tidy executable later.

##### Build clang-tidy with custom checks

After building clang-tidy as a library from the llvm source, the next step is to
After building clang-tidy as a library from the LLVM source, the next step is to
build clang-tidy as an executable, with the custom checks from the CDDA source.

In this step, the following tools are required.
- Python 3 (Python 2 may still be required to run the lit test for the custom checks)
- Python 3
- CMake
- MinGW-w64
- FileCheck (built from the llvm source)
- FileCheck (built from the LLVM source)
- A shell environment

You also need to install yaml for python 3 to work. Download the `.whl` installer
Expand All @@ -221,34 +219,53 @@ be applied before the custom checks can be built as an executable.

```patch
diff --git a/tools/clang-tidy-plugin/CMakeLists.txt b/tools/clang-tidy-plugin/CMakeLists.txt
index 553ef0ebe0..f591bc80d1 100644
index cf0c237645..540d3e29a5 100644
--- a/tools/clang-tidy-plugin/CMakeLists.txt
+++ b/tools/clang-tidy-plugin/CMakeLists.txt
@@ -3,8 +3,8 @@ include(ExternalProject)
@@ -4,7 +4,7 @@ include(ExternalProject)
find_package(LLVM REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG)
-add_library(
- CataAnalyzerPlugin MODULE
+add_executable(
+ CataAnalyzerPlugin
CataTidyModule.cpp
JsonTranslationInputCheck.cpp
NoLongCheck.cpp
@@ -51,6 +51,11 @@ else()
CataAnalyzerPlugin SYSTEM PRIVATE ${CATA_CLANG_TIDY_INCLUDE_DIR})
endif()
-add_library(CataAnalyzerPlugin MODULE
+add_executable(CataAnalyzerPlugin
AlmostNeverAutoCheck.cpp
AssertCheck.cpp
CataTidyModule.cpp
@@ -56,6 +56,11 @@ else ()
target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE ${CATA_CLANG_TIDY_INCLUDE_DIR})
endif ()
+target_link_libraries(
+ CataAnalyzerPlugin
+ clangTidyMain
+ )
+
target_compile_definitions(
CataAnalyzerPlugin PRIVATE ${LLVM_DEFINITIONS})
target_compile_definitions(CataAnalyzerPlugin PRIVATE ${LLVM_DEFINITIONS})
# We need to turn off exceptions and RTTI to match the LLVM build.
diff --git a/tools/clang-tidy-plugin/CataTidyModule.cpp b/tools/clang-tidy-plugin/CataTidyModule.cpp
index b7cb4df22c..a83db0c60e 100644
--- a/tools/clang-tidy-plugin/CataTidyModule.cpp
+++ b/tools/clang-tidy-plugin/CataTidyModule.cpp
@@ -18,6 +18,7 @@
#include "TestFilenameCheck.h"
#include "TestsMustRestoreGlobalStateCheck.h"
#include "TextStyleCheck.h"
+#include "tool/ClangTidyMain.h"
#include "TranslatorCommentsCheck.h"
#include "UnsequencedCallsCheck.h"
#include "UnusedStaticsCheck.h"
@@ -80,3 +81,8 @@ X( "cata-module", "Adds Cataclysm-DDA checks." );
} // namespace tidy
} // namespace clang
+
+int main( int argc, const char **argv )
+{
+ return clang::tidy::clangTidyMain( argc, argv );
+}
diff --git a/tools/clang-tidy-plugin/test/lit.cfg b/tools/clang-tidy-plugin/test/lit.cfg
index 4ab6e913a7..d1a4418ba6 100644
index 496804316a..43beb49653 100644
--- a/tools/clang-tidy-plugin/test/lit.cfg
+++ b/tools/clang-tidy-plugin/test/lit.cfg
@@ -17,11 +17,13 @@ else:
Expand All @@ -271,12 +288,10 @@ index 4ab6e913a7..d1a4418ba6 100644
The next step is to run CMake to generate the compilation database. The compilation
database contains compiler flags that clang-tidy uses to check the source files.
Make sure Python 3 (and Python 2 if it's still required), CMake, MinGW-w64, and FileCheck are on the path.
Make sure Python 3, CMake, MinGW-w64, and FileCheck are on the path.
Note that two `bin` directories of MinGW-w64 should be on the path: `<mingw-w64-root>/bin`,
and `<mingw-w64-root>/x86_64-w64-mingw32/bin`. FileCheck's path is `<llvm-source-root>/build/bin`,
if you built it with the instructions in the previous section. Python 2 should
precede Python 3 in the path, otherwise scripts that are intended to run with
Python 2 might not work.
if you built it with the instructions in the previous section.
Then add the following CMake options to generate the compilation database
(substitute values inside `<>` with the actual paths) and build the CDDA source
Expand All @@ -299,10 +314,10 @@ with Python 3 to fix some errors in the compilation database. Then the compilati
database should be usable by clang-tidy.
If you want to check if the custom checks are working correctly, run the following
script. Note that `python` here is the executable from Python 2.
script.
```sh
python <llvm-source-root>/llvm/utils/lit/lit.py -v build/tools/clang-tidy-plugin/test
python3 <llvm-source-root>/llvm/utils/lit/lit.py -v build/tools/clang-tidy-plugin/test
```
Finally, use the following command to run clang-tidy with the custom checks.
Expand Down
1 change: 1 addition & 0 deletions tools/clang-tidy-plugin/AlmostNeverAutoCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <llvm/ADT/StringRef.h>

#include "ClangTidy.h"
#include "ClangTidyCheck.h"

namespace clang
{
Expand Down
8 changes: 5 additions & 3 deletions tools/clang-tidy-plugin/AssertCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <unordered_set>

#include "clang/Frontend/CompilerInstance.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Token.h"

using namespace clang::ast_matchers;

Expand Down Expand Up @@ -46,10 +49,9 @@ class AssertMacroCallbacks : public PPCallbacks
llvm::SmallPtrSet<SourceLocation, 10> CataAssertLocations;
};

void AssertCheck::registerPPCallbacks( CompilerInstance &Compiler )
void AssertCheck::registerPPCallbacks( const SourceManager &, Preprocessor *PP, Preprocessor * )
{
Compiler.getPreprocessor().addPPCallbacks(
llvm::make_unique<AssertMacroCallbacks>( &Compiler.getPreprocessor(), this ) );
PP->addPPCallbacks( std::make_unique<AssertMacroCallbacks>( PP, this ) );
}

void AssertCheck::registerMatchers( MatchFinder * /*Finder*/ )
Expand Down
3 changes: 2 additions & 1 deletion tools/clang-tidy-plugin/AssertCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <llvm/ADT/StringRef.h>

#include "ClangTidy.h"
#include "ClangTidyCheck.h"

namespace clang
{
Expand All @@ -21,7 +22,7 @@ class AssertCheck : public ClangTidyCheck
public:
AssertCheck( StringRef Name, ClangTidyContext *Context )
: ClangTidyCheck( Name, Context ) {}
void registerPPCallbacks( CompilerInstance &Compiler ) override;
void registerPPCallbacks( const SourceManager &, Preprocessor *, Preprocessor * ) override;
void registerMatchers( ast_matchers::MatchFinder *Finder ) override;
void check( const ast_matchers::MatchFinder::MatchResult &Result ) override;
};
Expand Down
4 changes: 2 additions & 2 deletions tools/clang-tidy-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ target_include_directories(CataAnalyzerPlugin SYSTEM PRIVATE

if ("${CATA_CLANG_TIDY_INCLUDE_DIR}" STREQUAL "")
set(CTPS_RELEASES https://github.com/jbytheway/clang-tidy-plugin-support/releases/download)
set(CTPS_VERSION llvm-8.0.1-r12)
set(CTPS_VERSION llvm-12.0.0-r3)
set(CTPS_SRC ${CMAKE_CURRENT_BINARY_DIR}/clang-tidy-plugin-support)

ExternalProject_Add(clang-tidy-plugin-support
URL ${CTPS_RELEASES}/${CTPS_VERSION}/clang-tidy-plugin-support-${CTPS_VERSION}.tar.xz
URL_HASH SHA256=00ffab0df11250f394830735514c62ae787bd2eb6eb9d5e97471206d270c54e2
URL_HASH SHA256=c84aaf35b4d5bb9130b1cc309a92146ae5c62fd69db30a6254725beac9d60990
SOURCE_DIR ${CTPS_SRC}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
Expand Down
Loading

0 comments on commit 11f8b39

Please sign in to comment.