xref: /llvm-project/clang-tools-extra/clang-tidy/modernize/MinMaxUseInitializerListCheck.h (revision d3f92e30bbd5c295a639f207b9ac92198d538fb3)
1*d3f92e30SSopy //===--- MinMaxUseInitializerListCheck.h - clang-tidy -----------*- C++ -*-===//
2*d3f92e30SSopy //
3*d3f92e30SSopy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*d3f92e30SSopy // See https://llvm.org/LICENSE.txt for license information.
5*d3f92e30SSopy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*d3f92e30SSopy //
7*d3f92e30SSopy //===----------------------------------------------------------------------===//
8*d3f92e30SSopy 
9*d3f92e30SSopy #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MINMAXUSEINITIALIZERLISTCHECK_H
10*d3f92e30SSopy #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MINMAXUSEINITIALIZERLISTCHECK_H
11*d3f92e30SSopy 
12*d3f92e30SSopy #include "../ClangTidyCheck.h"
13*d3f92e30SSopy #include "../utils/IncludeInserter.h"
14*d3f92e30SSopy 
15*d3f92e30SSopy namespace clang::tidy::modernize {
16*d3f92e30SSopy 
17*d3f92e30SSopy /// Replaces nested ``std::min`` and ``std::max`` calls with an initializer list
18*d3f92e30SSopy /// where applicable.
19*d3f92e30SSopy ///
20*d3f92e30SSopy /// For example:
21*d3f92e30SSopy ///
22*d3f92e30SSopy /// \code
23*d3f92e30SSopy ///   int a = std::max(std::max(i, j), k);
24*d3f92e30SSopy /// \endcode
25*d3f92e30SSopy ///
26*d3f92e30SSopy /// This code is transformed to:
27*d3f92e30SSopy ///
28*d3f92e30SSopy /// \code
29*d3f92e30SSopy ///   int a = std::max({i, j, k});
30*d3f92e30SSopy /// \endcode
31*d3f92e30SSopy class MinMaxUseInitializerListCheck : public ClangTidyCheck {
32*d3f92e30SSopy public:
33*d3f92e30SSopy   MinMaxUseInitializerListCheck(StringRef Name, ClangTidyContext *Context);
34*d3f92e30SSopy 
35*d3f92e30SSopy   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
36*d3f92e30SSopy   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
37*d3f92e30SSopy   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
38*d3f92e30SSopy                            Preprocessor *ModuleExpanderPP) override;
39*d3f92e30SSopy   void check(const ast_matchers::MatchFinder::MatchResult &Match) override;
40*d3f92e30SSopy 
isLanguageVersionSupported(const LangOptions & LangOpts)41*d3f92e30SSopy   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
42*d3f92e30SSopy     return LangOpts.CPlusPlus11;
43*d3f92e30SSopy   }
getCheckTraversalKind()44*d3f92e30SSopy   std::optional<TraversalKind> getCheckTraversalKind() const override {
45*d3f92e30SSopy     return TK_IgnoreUnlessSpelledInSource;
46*d3f92e30SSopy   }
47*d3f92e30SSopy 
48*d3f92e30SSopy private:
49*d3f92e30SSopy   bool IgnoreNonTrivialTypes;
50*d3f92e30SSopy   std::uint64_t IgnoreTrivialTypesOfSizeAbove;
51*d3f92e30SSopy   utils::IncludeInserter Inserter;
52*d3f92e30SSopy };
53*d3f92e30SSopy 
54*d3f92e30SSopy } // namespace clang::tidy::modernize
55*d3f92e30SSopy 
56*d3f92e30SSopy #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_MINMAXUSEINITIALIZERLISTCHECK_H
57