Source · fairmq/tidy/
Tool.hheader
Described in Check code with fairmq-tidy, fairmq-tidy.
1/********************************************************************************2 * Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *3 * *4 * This software is distributed under the terms of the *5 * GNU Lesser General Public Licence (LGPL) version 3, *6 * copied verbatim in the file "LICENSE" *7 ********************************************************************************/8 9#ifndef FAIR_MQ_TIDY_TOOL10#define FAIR_MQ_TIDY_TOOL11 12#include <algorithm>13#include <clang/ASTMatchers/ASTMatchFinder.h>14#include <clang/Basic/FileManager.h>15#include <clang/Tooling/CompilationDatabase.h>16#include <clang/Tooling/Tooling.h>17#include <fairmq/tidy/ModernizeNonNamespacedTypes.h>18#include <string>19 20namespace fair::mq::tidy {21 22// Getting up to speed, read this:23// https://clang.llvm.org/docs/LibTooling.html24// https://clang.llvm.org/docs/IntroductionToTheClangAST.html25// Watch https://www.youtube.com/watch?v=VqCkCDFLSsc !!!26//27// optional, but helpful:28// https://static.linaro.org/connect/yvr18/presentations/yvr18-223.pdf29// https://steveire.wordpress.com/2018/11/20/composing-ast-matchers-in-clang-tidy/30// https://www.goldsborough.me/c++/clang/llvm/tools/2017/02/24/00-00-06-emitting_diagnostics_and_fixithints_in_clang_tools/31// https://steveire.com/CodeDive2018Presentation.pdf32//33// reference (no built-in search, however google will find things):34// https://clang.llvm.org/doxygen/35//36// Note: Implementing a standalone tool will impose double PP and parsing cost if one also37// runs clang-tidy. At the moment, one cannot extend clang-tidy with new checks without38// recompiling clang-tidy. In principle llvm-project/clang-extra-tools/clang-tidy39// has install rules, but Fedora is not packaging them which is likely due to their40// unstable state (lots of comments aka todo, fixme, refactor etc). Also, it seems41// focus has shifted towards implementing the42// https://microsoft.github.io/language-server-protocol/43// via https://clangd.llvm.org/ which includes clang-tidy, but also does not have44// an extension/plugin interface for third-party checks yet AFAICS.45 46struct Tool47{48 static auto run(clang::tooling::CompilationDatabase const &compilations,49 clang::ArrayRef<std::string> sources) -> int50 {51 using namespace clang;52 53 // compose all checks in a single match finder54 ast_matchers::MatchFinder finder;55 ModernizeNonNamespacedTypes check1(finder);56 57 // configure the clang tool58 tooling::ClangTool tool(compilations, sources);59 tool.appendArgumentsAdjuster(60 [](tooling::CommandLineArguments const &_args, StringRef /*file*/) {61 tooling::CommandLineArguments args(_args);62 63 auto const no_std_arg_present =64 std::find_if(cbegin(args),65 cend(args),66 [](std::string const &arg) {67 return arg.find("-std=") != std::string::npos;68 })69 == std::cend(args);70 if (no_std_arg_present) {71 args.emplace(std::cbegin(args) + 1, "-std=c++17");72 }73 74 args.emplace_back("-Wno-everything");75 76 return args;77 });78 79 // run checks on given files80 return tool.run(clang::tooling::newFrontendActionFactory(&finder).get());81 }82};83 84} // namespace fair::mq::tidy85 86#endif /* FAIR_MQ_TIDY_TOOL */