[llvm-dev] How to add a new clang-tidy module

zhaoyonggang via llvm-dev llvm-dev at lists.llvm.org
Sun Sep 27 02:01:12 PDT 2020


Hi, Roman,

	Thanks a lot, root cause should be like below. I am not familiar with this linker magic, :(



> On Sep 27, 2020, at 4:57 PM, Roman Lebedev <lebedev.ri at gmail.com> wrote:
> 
> See https://reviews.llvm.org/D36836 for modern example of minimal scaffolding.
> 
> Roman
> 
> On Sun, Sep 27, 2020 at 11:39 AM zhaoyonggang via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
>> 
>> Hi, all,
>> 
>>        I am planning to add clang-tidy checkers for my company. How to add a new module for my company? Please help, thanks in advance.
>> 
>>        I try to copy files from cert module, and rename cert to Misra, then add a rule named "m-0-1-1" by ./add_new_checker.py.
>> 
>>        then I run ninja check-clang-tool, but my case is failed due to below error
>> 
>>        Running ['clang-tidy', '/Users/zyg/Documents/workspace/llvm-project/llvm/cmake-build-debug/tools/clang/tools/extra/test/clang-tidy/checkers/Output/misra-m-0-1-1.cpp.tmp.cpp', '-fix', '--checks=-*,misra-m-0-1-1', '-format-style=none', '--', '-std=c++11', '-nostdinc++']... clang-tidy failed: Error: no checks enabled. USAGE: clang-tidy [options] <source0> [... <sourceN>]
>> 
>> 
>> 
>> Below is my diff
>> 
>> 
>> diff --git a/clang-tools-extra/clang-tidy/CMakeLists.txt b/clang-tools-extra/clang-tidy/CMakeLists.txt
>> index ca7a5afed6b..ccb77ef6a62 100644
>> --- a/clang-tools-extra/clang-tidy/CMakeLists.txt
>> +++ b/clang-tools-extra/clang-tidy/CMakeLists.txt
>> @@ -64,6 +64,7 @@ add_subdirectory(linuxkernel)
>> add_subdirectory(llvm)
>> add_subdirectory(llvmlibc)
>> add_subdirectory(misc)
>> +add_subdirectory(misra)
>> add_subdirectory(modernize)
>> if(CLANG_TIDY_ENABLE_STATIC_ANALYZER)
>>   add_subdirectory(mpi)
>> @@ -90,6 +91,7 @@ set(ALL_CLANG_TIDY_CHECKS
>>   clangTidyLLVMModule
>>   clangTidyLLVMLibcModule
>>   clangTidyMiscModule
>> +  clangTidyMISRAModule
>>   clangTidyModernizeModule
>>   clangTidyObjCModule
>>   clangTidyOpenMPModule
>> diff --git a/clang-tools-extra/clang-tidy/misra/CMakeLists.txt b/clang-tools-extra/clang-tidy/misra/CMakeLists.txt
>> new file mode 100644
>> index 00000000000..ca345a946c9
>> --- /dev/null
>> +++ b/clang-tools-extra/clang-tidy/misra/CMakeLists.txt
>> @@ -0,0 +1,28 @@
>> +set(LLVM_LINK_COMPONENTS
>> +  support
>> +  FrontendOpenMP
>> +  )
>> +
>> +add_clang_library(clangTidyMISRAModule
>> +  M011Check.cpp
>> +  MISRATidyModule.cpp
>> +  LINK_LIBS
>> +  clangTidy
>> +  clangTidyBugproneModule
>> +  clangTidyGoogleModule
>> +  clangTidyMiscModule
>> +  clangTidyPerformanceModule
>> +  clangTidyReadabilityModule
>> +  clangTidyUtils
>> +
>> +  DEPENDS
>> +  omp_gen
>> +)
>> +
>> +clang_target_link_libraries(clangTidyMISRAModule
>> +  PRIVATE
>> +  clangAST
>> +  clangASTMatchers
>> +  clangBasic
>> +  clangLex
>> +  )
>> diff --git a/clang-tools-extra/clang-tidy/misra/M011Check.cpp b/clang-tools-extra/clang-tidy/misra/M011Check.cpp
>> new file mode 100644
>> index 00000000000..ebc0ba3bbe0
>> --- /dev/null
>> +++ b/clang-tools-extra/clang-tidy/misra/M011Check.cpp
>> @@ -0,0 +1,37 @@
>> +//===--- M011Check.cpp - clang-tidy ---------------------------------------===//
>> +//
>> +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
>> +// See https://llvm.org/LICENSE.txt for license information.
>> +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
>> +//
>> +//===----------------------------------------------------------------------===//
>> +
>> +#include "M011Check.h"
>> +#include "clang/AST/ASTContext.h"
>> +#include "clang/ASTMatchers/ASTMatchFinder.h"
>> +
>> +using namespace clang::ast_matchers;
>> +
>> +namespace clang {
>> +namespace tidy {
>> +namespace misra {
>> +
>> +void M011Check::registerMatchers(MatchFinder *Finder) {
>> +  // FIXME: Add matchers.
>> +  Finder->addMatcher(functionDecl().bind("x"), this);
>> +}
>> +
>> +void M011Check::check(const MatchFinder::MatchResult &Result) {
>> +  // FIXME: Add callback implementation.
>> +  const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("x");
>> +  if (!MatchedDecl->getIdentifier() || MatchedDecl->getName().startswith("awesome_"))
>> +    return;
>> +  diag(MatchedDecl->getLocation(), "function %0 is insufficiently awesome")
>> +      << MatchedDecl;
>> +  diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note)
>> +      << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
>> +}
>> +
>> +} // namespace misra
>> +} // namespace tidy
>> +} // namespace clang
>> diff --git a/clang-tools-extra/clang-tidy/misra/M011Check.h b/clang-tools-extra/clang-tidy/misra/M011Check.h
>> new file mode 100644
>> index 00000000000..289d54545c7
>> --- /dev/null
>> +++ b/clang-tools-extra/clang-tidy/misra/M011Check.h
>> @@ -0,0 +1,34 @@
>> +//===--- M011Check.h - clang-tidy -------------------------------*- C++ -*-===//
>> +//
>> +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
>> +// See https://llvm.org/LICENSE.txt for license information.
>> +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
>> +//
>> +//===----------------------------------------------------------------------===//
>> +
>> +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISRA_M011CHECK_H
>> +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISRA_M011CHECK_H
>> +
>> +#include "../ClangTidyCheck.h"
>> +
>> +namespace clang {
>> +namespace tidy {
>> +namespace misra {
>> +
>> +/// FIXME: Write a short description.
>> +///
>> +/// For the user-facing documentation see:
>> +/// http://clang.llvm.org/extra/clang-tidy/checks/misra-m-0-1-1.html
>> +class M011Check : public ClangTidyCheck {
>> +public:
>> +  M011Check(StringRef Name, ClangTidyContext *Context)
>> +      : ClangTidyCheck(Name, Context) {}
>> +  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
>> +  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
>> +};
>> +
>> +} // namespace misra
>> +} // namespace tidy
>> +} // namespace clang
>> +
>> +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISRA_M011CHECK_H
>> diff --git a/clang-tools-extra/clang-tidy/misra/MISRATidyModule.cpp b/clang-tools-extra/clang-tidy/misra/MISRATidyModule.cpp
>> new file mode 100644
>> index 00000000000..cb78fa7efe0
>> --- /dev/null
>> +++ b/clang-tools-extra/clang-tidy/misra/MISRATidyModule.cpp
>> @@ -0,0 +1,46 @@
>> +//===--- CERTTidyModule.cpp - clang-tidy ----------------------------------===//
>> +//
>> +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
>> +// See https://llvm.org/LICENSE.txt for license information.
>> +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
>> +//
>> +//===----------------------------------------------------------------------===//
>> +
>> +#include "../ClangTidy.h"
>> +#include "../ClangTidyModule.h"
>> +#include "../ClangTidyModuleRegistry.h"
>> +#include "M011Check.h"
>> +
>> +
>> +namespace clang {
>> +namespace tidy {
>> +namespace misra {
>> +
>> +class MISRAModule : public ClangTidyModule {
>> +public:
>> +  void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
>> +    // C++ checkers
>> +
>> +    CheckFactories.registerCheck<M011Check>(
>> +        "misra-m-0-1-1");
>> +  }
>> +
>> +  ClangTidyOptions getModuleOptions() override {
>> +    ClangTidyOptions Options;
>> +    return Options;
>> +  }
>> +};
>> +
>> +} // namespace misra
>> +
>> +// Register the MiscTidyModule using this statically initialized variable.
>> +static ClangTidyModuleRegistry::Add<misra::MISRAModule>
>> +    X("misra-module",
>> +      "Adds lint checks corresponding to CERT secure coding guidelines.");
>> +
>> +// This anchor is used to force the linker to link in the generated object file
>> +// and thus register the CERTModule.
>> +volatile int MISRAModuleAnchorSource = 0;
>> +
>> +} // namespace tidy
>> +} // namespace clang
>> diff --git a/clang-tools-extra/docs/clang-tidy/checks/list.rst b/clang-tools-extra/docs/clang-tidy/checks/list.rst
>> index 62bfd314920..144bd80eaab 100644
>> --- a/clang-tools-extra/docs/clang-tidy/checks/list.rst
>> +++ b/clang-tools-extra/docs/clang-tidy/checks/list.rst
>> @@ -213,6 +213,7 @@ Clang-Tidy Checks
>>    `misc-unused-alias-decls <misc-unused-alias-decls.html>`_, "Yes"
>>    `misc-unused-parameters <misc-unused-parameters.html>`_, "Yes"
>>    `misc-unused-using-decls <misc-unused-using-decls.html>`_, "Yes"
>> +   `misra-m-0-1-1 <misra-m-0-1-1.html>`_, "Yes"
>>    `modernize-avoid-bind <modernize-avoid-bind.html>`_, "Yes"
>>    `modernize-avoid-c-arrays <modernize-avoid-c-arrays.html>`_,
>>    `modernize-concat-nested-namespaces <modernize-concat-nested-namespaces.html>`_, "Yes"
>> diff --git a/clang-tools-extra/docs/clang-tidy/checks/misra-m-0-1-1.rst b/clang-tools-extra/docs/clang-tidy/checks/misra-m-0-1-1.rst
>> new file mode 100644
>> index 00000000000..7fd1cefbca8
>> --- /dev/null
>> +++ b/clang-tools-extra/docs/clang-tidy/checks/misra-m-0-1-1.rst
>> @@ -0,0 +1,6 @@
>> +.. title:: clang-tidy - misra-m-0-1-1
>> +
>> +misra-m-0-1-1
>> +=============
>> +
>> +FIXME: Describe what patterns does the check detect and why. Give examples.
>> diff --git a/clang-tools-extra/test/clang-tidy/checkers/misra-m-0-1-1.cpp b/clang-tools-extra/test/clang-tidy/checkers/misra-m-0-1-1.cpp
>> new file mode 100644
>> index 00000000000..382524ac36b
>> --- /dev/null
>> +++ b/clang-tools-extra/test/clang-tidy/checkers/misra-m-0-1-1.cpp
>> @@ -0,0 +1,14 @@
>> +// RUN: %check_clang_tidy %s misra-m-0-1-1 %t
>> +
>> +// FIXME: Add something that triggers the check here.
>> +void f();
>> +// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'f' is insufficiently awesome [misra-m-0-1-1]
>> +
>> +// FIXME: Verify the applied fix.
>> +//   * Make the CHECK patterns specific enough and try to make verified lines
>> +//     unique to avoid incorrect matches.
>> +//   * Use {{}} for regular expressions.
>> +// CHECK-FIXES: {{^}}void awesome_f();{{$}}
>> +
>> +// FIXME: Add something that doesn't trigger the check here.
>> +void awesome_f2();
>> _______________________________________________
>> LLVM Developers mailing list
>> llvm-dev at lists.llvm.org
>> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200927/9841e9e6/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-1.png
Type: image/png
Size: 122324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200927/9841e9e6/attachment-0001.png>


More information about the llvm-dev mailing list