xref: /llvm-project/clang-tools-extra/clang-tidy/modernize/UseStdFormatCheck.h (revision a199fb1229987d0885a4367e3a439db336069156)
1af79372dSMike Crowe //===--- UseStdFormatCheck.h - clang-tidy -----------------------*- C++ -*-===//
2af79372dSMike Crowe //
3af79372dSMike Crowe // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4af79372dSMike Crowe // See https://llvm.org/LICENSE.txt for license information.
5af79372dSMike Crowe // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6af79372dSMike Crowe //
7af79372dSMike Crowe //===----------------------------------------------------------------------===//
8af79372dSMike Crowe 
9af79372dSMike Crowe #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USESTDFORMATCHECK_H
10af79372dSMike Crowe #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USESTDFORMATCHECK_H
11af79372dSMike Crowe 
12af79372dSMike Crowe #include "../ClangTidyCheck.h"
13af79372dSMike Crowe #include "../utils/IncludeInserter.h"
14af79372dSMike Crowe 
15af79372dSMike Crowe namespace clang::tidy::modernize {
16af79372dSMike Crowe 
17af79372dSMike Crowe /// Converts calls to absl::StrFormat, or other functions via configuration
18af79372dSMike Crowe /// options, to C++20's std::format, or another function via a configuration
19af79372dSMike Crowe /// option, modifying the format string appropriately and removing
20af79372dSMike Crowe /// now-unnecessary calls to std::string::c_str() and std::string::data().
21af79372dSMike Crowe ///
22af79372dSMike Crowe /// For the user-facing documentation see:
23af79372dSMike Crowe /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-std-format.html
24af79372dSMike Crowe class UseStdFormatCheck : public ClangTidyCheck {
25af79372dSMike Crowe public:
26af79372dSMike Crowe   UseStdFormatCheck(StringRef Name, ClangTidyContext *Context);
27af79372dSMike Crowe   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28af79372dSMike Crowe     if (ReplacementFormatFunction == "std::format")
29af79372dSMike Crowe       return LangOpts.CPlusPlus20;
30af79372dSMike Crowe     return LangOpts.CPlusPlus;
31af79372dSMike Crowe   }
32af79372dSMike Crowe   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
33af79372dSMike Crowe                            Preprocessor *ModuleExpanderPP) override;
34af79372dSMike Crowe   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
35af79372dSMike Crowe   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
36af79372dSMike Crowe   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
37af79372dSMike Crowe   std::optional<TraversalKind> getCheckTraversalKind() const override {
38af79372dSMike Crowe     return TK_IgnoreUnlessSpelledInSource;
39af79372dSMike Crowe   }
40af79372dSMike Crowe 
41af79372dSMike Crowe private:
42af79372dSMike Crowe   bool StrictMode;
43af79372dSMike Crowe   std::vector<StringRef> StrFormatLikeFunctions;
44af79372dSMike Crowe   StringRef ReplacementFormatFunction;
45af79372dSMike Crowe   utils::IncludeInserter IncludeInserter;
46af79372dSMike Crowe   std::optional<StringRef> MaybeHeaderToInclude;
47*a199fb12SMike Crowe   Preprocessor *PP = nullptr;
48af79372dSMike Crowe };
49af79372dSMike Crowe 
50af79372dSMike Crowe } // namespace clang::tidy::modernize
51af79372dSMike Crowe 
52af79372dSMike Crowe #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USESTDFORMATCHECK_H
53