10#include "../utils/FormatStringConverter.h"
11#include "../utils/Matchers.h"
12#include "../utils/OptionsUtils.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/Lex/Lexer.h"
15#include "clang/Tooling/FixIt.h"
27 StrictMode(Options.getLocalOrGlobal(
"StrictMode", false)),
28 StrFormatLikeFunctions(utils::options::parseStringList(
29 Options.get(
"StrFormatLikeFunctions",
""))),
30 ReplacementFormatFunction(
31 Options.get(
"ReplacementFormatFunction",
"std::format")),
32 IncludeInserter(Options.getLocalOrGlobal(
"IncludeStyle",
33 utils::IncludeSorter::IS_LLVM),
34 areDiagsSelfContained()),
35 MaybeHeaderToInclude(Options.get(
"FormatHeader")) {
36 if (StrFormatLikeFunctions.empty())
37 StrFormatLikeFunctions.push_back(
"absl::StrFormat");
39 if (!MaybeHeaderToInclude && ReplacementFormatFunction ==
"std::format")
40 MaybeHeaderToInclude =
"<format>";
45 Preprocessor *ModuleExpanderPP) {
51 callExpr(argumentCountAtLeast(1),
52 hasArgument(0, stringLiteral(isOrdinary())),
53 callee(functionDecl(unless(cxxMethodDecl()),
55 StrFormatLikeFunctions))
65 serializeStringList(StrFormatLikeFunctions));
66 Options.
store(Opts,
"ReplacementFormatFunction", ReplacementFormatFunction);
68 if (MaybeHeaderToInclude)
69 Options.
store(Opts,
"FormatHeader", *MaybeHeaderToInclude);
73 const unsigned FormatArgOffset = 0;
74 const auto *OldFunction = Result.Nodes.getNodeAs<FunctionDecl>(
"func_decl");
75 const auto *StrFormat = Result.Nodes.getNodeAs<CallExpr>(
"strformat");
80 FormatArgOffset, ConverterConfig,
82 const Expr *StrFormatCall = StrFormat->getCallee();
84 diag(StrFormat->getBeginLoc(),
85 "unable to use '%0' instead of %1 because %2")
86 << StrFormatCall->getSourceRange() << ReplacementFormatFunction
87 << OldFunction->getIdentifier()
92 DiagnosticBuilder Diag =
93 diag(StrFormatCall->getBeginLoc(),
"use '%0' instead of %1")
94 << ReplacementFormatFunction << OldFunction->getIdentifier();
95 Diag << FixItHint::CreateReplacement(
96 CharSourceRange::getTokenRange(StrFormatCall->getSourceRange()),
97 ReplacementFormatFunction);
98 Converter.
applyFixes(Diag, *Result.SourceManager);
100 if (MaybeHeaderToInclude)
102 Result.Context->getSourceManager().getFileID(
103 StrFormatCall->getBeginLoc()),
104 *MaybeHeaderToInclude);
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
AST_MATCHER(Decl, declHasNoReturnAttr)
matches a Decl if it has a "no return" attribute of any kind
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap