1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
//===--- UseStdPrintCheck.cpp - clang-tidy-----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "UseStdPrintCheck.h"
#include "../utils/FormatStringConverter.h"
#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::modernize {
namespace {
AST_MATCHER(StringLiteral, isOrdinary) { return Node.isOrdinary(); }
} // namespace
UseStdPrintCheck::UseStdPrintCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context), PP(nullptr),
StrictMode(Options.getLocalOrGlobal("StrictMode", false)),
PrintfLikeFunctions(utils::options::parseStringList(
Options.get("PrintfLikeFunctions", ""))),
FprintfLikeFunctions(utils::options::parseStringList(
Options.get("FprintfLikeFunctions", ""))),
ReplacementPrintFunction(
Options.get("ReplacementPrintFunction", "std::print")),
ReplacementPrintlnFunction(
Options.get("ReplacementPrintlnFunction", "std::println")),
IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
utils::IncludeSorter::IS_LLVM),
areDiagsSelfContained()),
MaybeHeaderToInclude(Options.get("PrintHeader")) {
if (PrintfLikeFunctions.empty() && FprintfLikeFunctions.empty()) {
PrintfLikeFunctions.emplace_back("::printf");
PrintfLikeFunctions.emplace_back("absl::PrintF");
FprintfLikeFunctions.emplace_back("::fprintf");
FprintfLikeFunctions.emplace_back("absl::FPrintF");
}
if (!MaybeHeaderToInclude && (ReplacementPrintFunction == "std::print" ||
ReplacementPrintlnFunction == "std::println"))
MaybeHeaderToInclude = "<print>";
}
void UseStdPrintCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
using utils::options::serializeStringList;
Options.store(Opts, "StrictMode", StrictMode);
Options.store(Opts, "PrintfLikeFunctions",
serializeStringList(PrintfLikeFunctions));
Options.store(Opts, "FprintfLikeFunctions",
serializeStringList(FprintfLikeFunctions));
Options.store(Opts, "ReplacementPrintFunction", ReplacementPrintFunction);
Options.store(Opts, "ReplacementPrintlnFunction", ReplacementPrintlnFunction);
Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
if (MaybeHeaderToInclude)
Options.store(Opts, "PrintHeader", *MaybeHeaderToInclude);
}
void UseStdPrintCheck::registerPPCallbacks(const SourceManager &SM,
Preprocessor *PP,
Preprocessor *ModuleExpanderPP) {
IncludeInserter.registerPreprocessor(PP);
this->PP = PP;
}
static clang::ast_matchers::StatementMatcher
unusedReturnValue(clang::ast_matchers::StatementMatcher MatchedCallExpr) {
auto UnusedInCompoundStmt =
compoundStmt(forEach(MatchedCallExpr),
// The checker can't currently differentiate between the
// return statement and other statements inside GNU statement
// expressions, so disable the checker inside them to avoid
// false positives.
unless(hasParent(stmtExpr())));
auto UnusedInIfStmt =
ifStmt(eachOf(hasThen(MatchedCallExpr), hasElse(MatchedCallExpr)));
auto UnusedInWhileStmt = whileStmt(hasBody(MatchedCallExpr));
auto UnusedInDoStmt = doStmt(hasBody(MatchedCallExpr));
auto UnusedInForStmt =
forStmt(eachOf(hasLoopInit(MatchedCallExpr),
hasIncrement(MatchedCallExpr), hasBody(MatchedCallExpr)));
auto UnusedInRangeForStmt = cxxForRangeStmt(hasBody(MatchedCallExpr));
auto UnusedInCaseStmt = switchCase(forEach(MatchedCallExpr));
return stmt(anyOf(UnusedInCompoundStmt, UnusedInIfStmt, UnusedInWhileStmt,
UnusedInDoStmt, UnusedInForStmt, UnusedInRangeForStmt,
UnusedInCaseStmt));
}
void UseStdPrintCheck::registerMatchers(MatchFinder *Finder) {
if (!PrintfLikeFunctions.empty())
Finder->addMatcher(
unusedReturnValue(
callExpr(argumentCountAtLeast(1),
hasArgument(0, stringLiteral(isOrdinary())),
callee(functionDecl(matchers::matchesAnyListedName(
PrintfLikeFunctions))
.bind("func_decl")))
.bind("printf")),
this);
if (!FprintfLikeFunctions.empty())
Finder->addMatcher(
unusedReturnValue(
callExpr(argumentCountAtLeast(2),
hasArgument(1, stringLiteral(isOrdinary())),
callee(functionDecl(matchers::matchesAnyListedName(
FprintfLikeFunctions))
.bind("func_decl")))
.bind("fprintf")),
this);
}
void UseStdPrintCheck::check(const MatchFinder::MatchResult &Result) {
unsigned FormatArgOffset = 0;
const auto *OldFunction = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");
const auto *Printf = Result.Nodes.getNodeAs<CallExpr>("printf");
if (!Printf) {
Printf = Result.Nodes.getNodeAs<CallExpr>("fprintf");
FormatArgOffset = 1;
}
utils::FormatStringConverter::Configuration ConverterConfig;
ConverterConfig.StrictMode = StrictMode;
ConverterConfig.AllowTrailingNewlineRemoval = true;
assert(PP && "Preprocessor should be set by registerPPCallbacks");
utils::FormatStringConverter Converter(
Result.Context, Printf, FormatArgOffset, ConverterConfig, getLangOpts(),
*Result.SourceManager, *PP);
const Expr *PrintfCall = Printf->getCallee();
const StringRef ReplacementFunction = Converter.usePrintNewlineFunction()
? ReplacementPrintlnFunction
: ReplacementPrintFunction;
if (!Converter.canApply()) {
diag(PrintfCall->getBeginLoc(),
"unable to use '%0' instead of %1 because %2")
<< PrintfCall->getSourceRange() << ReplacementFunction
<< OldFunction->getIdentifier()
<< Converter.conversionNotPossibleReason();
return;
}
DiagnosticBuilder Diag =
diag(PrintfCall->getBeginLoc(), "use '%0' instead of %1")
<< ReplacementFunction << OldFunction->getIdentifier();
Diag << FixItHint::CreateReplacement(
CharSourceRange::getTokenRange(PrintfCall->getExprLoc(),
PrintfCall->getEndLoc()),
ReplacementFunction);
Converter.applyFixes(Diag, *Result.SourceManager);
if (MaybeHeaderToInclude)
Diag << IncludeInserter.createIncludeInsertion(
Result.Context->getSourceManager().getFileID(PrintfCall->getBeginLoc()),
*MaybeHeaderToInclude);
}
} // namespace clang::tidy::modernize
|