clang-tools 20.0.0git
UseStdFormatCheck.h
Go to the documentation of this file.
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
15namespace 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
25public:
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
41private:
42 bool StrictMode;
43 std::vector<StringRef> StrFormatLikeFunctions;
44 StringRef ReplacementFormatFunction;
45 utils::IncludeInserter IncludeInserter;
46 std::optional<StringRef> MaybeHeaderToInclude;
47};
48
49} // namespace clang::tidy::modernize
50
51#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USESTDFORMATCHECK_H
llvm::SmallString< 256U > Name
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Converts calls to absl::StrFormat, or other functions via configuration options, to C++20's std::form...
std::optional< TraversalKind > getCheckTraversalKind() const override
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Produces fixes to insert specified includes to source files, if not yet present.
llvm::StringMap< ClangTidyValue > OptionMap