Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:22

0001 //===---------- TransformerClangTidyCheck.h - clang-tidy ------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
0010 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
0011 
0012 #include "../ClangTidyCheck.h"
0013 #include "IncludeInserter.h"
0014 #include "IncludeSorter.h"
0015 #include "clang/ASTMatchers/ASTMatchFinder.h"
0016 #include "clang/Tooling/Transformer/Transformer.h"
0017 #include <optional>
0018 
0019 namespace clang::tidy::utils {
0020 
0021 /// A base class for defining a ClangTidy check based on a `RewriteRule`.
0022 //
0023 // For example, given a rule `MyCheckAsRewriteRule`, one can define a tidy check
0024 // as follows:
0025 //
0026 // class MyCheck : public TransformerClangTidyCheck {
0027 //  public:
0028 //   MyCheck(StringRef Name, ClangTidyContext *Context)
0029 //       : TransformerClangTidyCheck(MyCheckAsRewriteRule, Name, Context) {}
0030 // };
0031 //
0032 // `TransformerClangTidyCheck` recognizes this clang-tidy option:
0033 //
0034 //  * IncludeStyle. A string specifying which file naming convention is used by
0035 //      the source code, 'llvm' or 'google'.  Default is 'llvm'. The naming
0036 //      convention influences how canonical headers are distinguished from other
0037 //      includes.
0038 class TransformerClangTidyCheck : public ClangTidyCheck {
0039 public:
0040   TransformerClangTidyCheck(StringRef Name, ClangTidyContext *Context);
0041 
0042   /// DEPRECATED: prefer the two argument constructor in conjunction with
0043   /// \c setRule.
0044   ///
0045   /// \p MakeRule generates the rewrite rule to be used by the check, based on
0046   /// the given language and clang-tidy options. It can return \c std::nullopt
0047   /// to handle cases where the options disable the check.
0048   ///
0049   /// See \c setRule for constraints on the rule.
0050   TransformerClangTidyCheck(
0051       std::function<std::optional<transformer::RewriteRuleWith<std::string>>(
0052           const LangOptions &, const OptionsView &)>
0053           MakeRule,
0054       StringRef Name, ClangTidyContext *Context);
0055 
0056   /// Convenience overload of the constructor when the rule doesn't have any
0057   /// dependencies.
0058   TransformerClangTidyCheck(transformer::RewriteRuleWith<std::string> R,
0059                             StringRef Name, ClangTidyContext *Context);
0060 
0061   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
0062                            Preprocessor *ModuleExpanderPP) override;
0063   void registerMatchers(ast_matchers::MatchFinder *Finder) final;
0064   void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
0065 
0066   /// Derived classes that override this function should call this method from
0067   /// the overridden method.
0068   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
0069 
0070   /// Set the rule that this check implements.  All cases in the rule must have
0071   /// a non-null \c Explanation, even though \c Explanation is optional for
0072   /// RewriteRule in general. Because the primary purpose of clang-tidy checks
0073   /// is to provide users with diagnostics, we assume that a missing explanation
0074   /// is a bug.  If no explanation is desired, indicate that explicitly (for
0075   /// example, by passing `text("no explanation")` to `makeRule` as the
0076   /// `Explanation` argument).
0077   void setRule(transformer::RewriteRuleWith<std::string> R);
0078 
0079 private:
0080   transformer::RewriteRuleWith<std::string> Rule;
0081   IncludeInserter Inserter;
0082 };
0083 
0084 } // namespace clang::tidy::utils
0085 
0086 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H