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