brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · da8606f Raw
88 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_TRANSFORMERCLANGTIDYCHECK_H10#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_TRANSFORMERCLANGTIDYCHECK_H11 12#include "../ClangTidyCheck.h"13#include "IncludeInserter.h"14#include "IncludeSorter.h"15#include "clang/ASTMatchers/ASTMatchFinder.h"16#include "clang/Tooling/Transformer/Transformer.h"17#include <optional>18 19namespace clang::tidy::utils {20 21/// A base class for defining a ClangTidy check based on a `RewriteRule`.22//23// For example, given a rule `MyCheckAsRewriteRule`, one can define a tidy check24// as follows:25//26// class MyCheck : public TransformerClangTidyCheck {27//  public:28//   MyCheck(StringRef Name, ClangTidyContext *Context)29//       : TransformerClangTidyCheck(MyCheckAsRewriteRule, Name, Context) {}30// };31//32// `TransformerClangTidyCheck` recognizes this clang-tidy option:33//34//  * IncludeStyle. A string specifying which file naming convention is used by35//      the source code, 'llvm' or 'google'.  Default is 'llvm'. The naming36//      convention influences how canonical headers are distinguished from other37//      includes.38class TransformerClangTidyCheck : public ClangTidyCheck {39public:40  TransformerClangTidyCheck(StringRef Name, ClangTidyContext *Context);41 42  /// DEPRECATED: prefer the two argument constructor in conjunction with43  /// \c setRule.44  ///45  /// \p MakeRule generates the rewrite rule to be used by the check, based on46  /// the given language and clang-tidy options. It can return \c std::nullopt47  /// to handle cases where the options disable the check.48  ///49  /// See \c setRule for constraints on the rule.50  TransformerClangTidyCheck(51      llvm::function_ref<52          std::optional<transformer::RewriteRuleWith<std::string>>(53              const LangOptions &, const OptionsView &)>54          MakeRule,55      StringRef Name, ClangTidyContext *Context);56 57  /// Convenience overload of the constructor when the rule doesn't have any58  /// dependencies.59  TransformerClangTidyCheck(transformer::RewriteRuleWith<std::string> R,60                            StringRef Name, ClangTidyContext *Context);61 62  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,63                           Preprocessor *ModuleExpanderPP) override;64  void registerMatchers(ast_matchers::MatchFinder *Finder) final;65  void check(const ast_matchers::MatchFinder::MatchResult &Result) final;66 67  /// Derived classes that override this function should call this method from68  /// the overridden method.69  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;70 71  /// Set the rule that this check implements.  All cases in the rule must have72  /// a non-null \c Explanation, even though \c Explanation is optional for73  /// RewriteRule in general. Because the primary purpose of clang-tidy checks74  /// is to provide users with diagnostics, we assume that a missing explanation75  /// is a bug.  If no explanation is desired, indicate that explicitly (for76  /// example, by passing `text("no explanation")` to `makeRule` as the77  /// `Explanation` argument).78  void setRule(transformer::RewriteRuleWith<std::string> R);79 80private:81  transformer::RewriteRuleWith<std::string> Rule;82  IncludeInserter Inserter;83};84 85} // namespace clang::tidy::utils86 87#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_UTILS_TRANSFORMERCLANGTIDYCHECK_H88