Skip to content

Commit bf4b55f

Browse files
committed
Sync with upstream
2 parents 16160e3 + 6028c96 commit bf4b55f

File tree

9,339 files changed

+186850
-186304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

9,339 files changed

+186850
-186304
lines changed

clang-tools-extra/clang-tidy/abseil/AbseilTidyModule.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "DurationFactoryFloatCheck.h"
1717
#include "DurationFactoryScaleCheck.h"
1818
#include "DurationSubtractionCheck.h"
19+
#include "DurationUnnecessaryConversionCheck.h"
1920
#include "FasterStrsplitDelimiterCheck.h"
2021
#include "NoInternalDependenciesCheck.h"
2122
#include "NoNamespaceCheck.h"
@@ -45,6 +46,8 @@ class AbseilModule : public ClangTidyModule {
4546
"abseil-duration-factory-scale");
4647
CheckFactories.registerCheck<DurationSubtractionCheck>(
4748
"abseil-duration-subtraction");
49+
CheckFactories.registerCheck<DurationUnnecessaryConversionCheck>(
50+
"abseil-duration-unnecessary-conversion");
4851
CheckFactories.registerCheck<FasterStrsplitDelimiterCheck>(
4952
"abseil-faster-strsplit-delimiter");
5053
CheckFactories.registerCheck<NoInternalDependenciesCheck>(

clang-tools-extra/clang-tidy/abseil/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_clang_library(clangTidyAbseilModule
1010
DurationFactoryScaleCheck.cpp
1111
DurationRewriter.cpp
1212
DurationSubtractionCheck.cpp
13+
DurationUnnecessaryConversionCheck.cpp
1314
FasterStrsplitDelimiterCheck.cpp
1415
NoInternalDependenciesCheck.cpp
1516
NoNamespaceCheck.cpp

clang-tools-extra/clang-tidy/abseil/DurationAdditionCheck.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void DurationAdditionCheck::registerMatchers(MatchFinder *Finder) {
2222
Finder->addMatcher(
2323
binaryOperator(hasOperatorName("+"),
2424
hasEitherOperand(expr(ignoringParenImpCasts(
25-
callExpr(callee(functionDecl(TimeFactoryFunction())
25+
callExpr(callee(functionDecl(TimeConversionFunction())
2626
.bind("function_decl")))
2727
.bind("call")))))
2828
.bind("binop"),
@@ -43,7 +43,7 @@ void DurationAdditionCheck::check(const MatchFinder::MatchResult &Result) {
4343
if (!Scale)
4444
return;
4545

46-
llvm::StringRef TimeFactory = getTimeFactoryForScale(*Scale);
46+
llvm::StringRef TimeFactory = getTimeInverseForScale(*Scale);
4747

4848
FixItHint Hint;
4949
if (Call == Binop->getLHS()->IgnoreParenImpCasts()) {

clang-tools-extra/clang-tidy/abseil/DurationRewriter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ llvm::StringRef getDurationFactoryForScale(DurationScale Scale) {
104104
}
105105

106106
/// Returns the Time factory function name for a given `Scale`.
107-
llvm::StringRef getTimeFactoryForScale(DurationScale scale) {
107+
llvm::StringRef getTimeInverseForScale(DurationScale scale) {
108108
switch (scale) {
109109
case DurationScale::Hours:
110110
return "absl::ToUnixHours";

clang-tools-extra/clang-tidy/abseil/DurationRewriter.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ enum class DurationScale : std::uint8_t {
3131
/// constructing a `Duration` for that scale.
3232
llvm::StringRef getDurationFactoryForScale(DurationScale Scale);
3333

34-
/// Returns the Time factory function name for a given `Scale`.
35-
llvm::StringRef getTimeFactoryForScale(DurationScale scale);
36-
3734
// Determine if `Node` represents a literal floating point or integral zero.
3835
bool IsLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
3936
const Expr &Node);
@@ -75,6 +72,9 @@ llvm::Optional<DurationScale> getScaleForTimeInverse(llvm::StringRef Name);
7572
const std::pair<llvm::StringRef, llvm::StringRef> &
7673
getDurationInverseForScale(DurationScale Scale);
7774

75+
/// Returns the Time inverse function name for a given `Scale`.
76+
llvm::StringRef getTimeInverseForScale(DurationScale scale);
77+
7878
/// Assuming `Node` has type `double` or `int` representing a time interval of
7979
/// `Scale`, return the expression to make it a suitable `Duration`.
8080
std::string rewriteExprFromNumberToDuration(
@@ -107,7 +107,7 @@ AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
107107
}
108108

109109
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
110-
TimeFactoryFunction) {
110+
TimeConversionFunction) {
111111
using namespace clang::ast_matchers;
112112
return functionDecl(hasAnyName(
113113
"::absl::ToUnixHours", "::absl::ToUnixMinutes", "::absl::ToUnixSeconds",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- DurationUnnecessaryConversionCheck.cpp - clang-tidy
2+
//-----------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "DurationUnnecessaryConversionCheck.h"
11+
#include "DurationRewriter.h"
12+
#include "clang/AST/ASTContext.h"
13+
#include "clang/ASTMatchers/ASTMatchFinder.h"
14+
#include "clang/Tooling/FixIt.h"
15+
16+
using namespace clang::ast_matchers;
17+
18+
namespace clang {
19+
namespace tidy {
20+
namespace abseil {
21+
22+
void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
23+
for (const auto &Scale : {"Hours", "Minutes", "Seconds", "Milliseconds",
24+
"Microseconds", "Nanoseconds"}) {
25+
std::string DurationFactory = (llvm::Twine("::absl::") + Scale).str();
26+
std::string FloatConversion =
27+
(llvm::Twine("::absl::ToDouble") + Scale).str();
28+
std::string IntegerConversion =
29+
(llvm::Twine("::absl::ToInt64") + Scale).str();
30+
31+
Finder->addMatcher(
32+
callExpr(
33+
callee(functionDecl(hasName(DurationFactory))),
34+
hasArgument(0, callExpr(callee(functionDecl(hasAnyName(
35+
FloatConversion, IntegerConversion))),
36+
hasArgument(0, expr().bind("arg")))))
37+
.bind("call"),
38+
this);
39+
}
40+
}
41+
42+
void DurationUnnecessaryConversionCheck::check(
43+
const MatchFinder::MatchResult &Result) {
44+
const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
45+
const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
46+
47+
if (!isNotInMacro(Result, OuterCall))
48+
return;
49+
50+
diag(OuterCall->getBeginLoc(), "remove unnecessary absl::Duration conversions")
51+
<< FixItHint::CreateReplacement(
52+
OuterCall->getSourceRange(),
53+
tooling::fixit::getText(*Arg, *Result.Context));
54+
}
55+
56+
} // namespace abseil
57+
} // namespace tidy
58+
} // namespace clang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- DurationUnnecessaryConversionCheck.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_ABSEIL_TIMEDOUBLECONVERSIONCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_TIMEDOUBLECONVERSIONCHECK_H
11+
12+
#include "../ClangTidy.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace abseil {
17+
18+
/// Finds and fixes cases where ``absl::Duration`` values are being converted
19+
/// to numeric types and back again.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/abseil-duration-unnecessary-conversion.html
23+
class DurationUnnecessaryConversionCheck : public ClangTidyCheck {
24+
public:
25+
DurationUnnecessaryConversionCheck(StringRef Name, ClangTidyContext *Context)
26+
: ClangTidyCheck(Name, Context) {}
27+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
28+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
29+
};
30+
31+
} // namespace abseil
32+
} // namespace tidy
33+
} // namespace clang
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_TIMEDOUBLECONVERSIONCHECK_H

clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.cpp

+57-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
1212
#include "clang/Lex/Lexer.h"
1313
#include "clang/Lex/Token.h"
14+
1415
#include "../utils/LexerUtils.h"
1516

1617
using namespace clang::ast_matchers;
@@ -23,17 +24,37 @@ ArgumentCommentCheck::ArgumentCommentCheck(StringRef Name,
2324
ClangTidyContext *Context)
2425
: ClangTidyCheck(Name, Context),
2526
StrictMode(Options.getLocalOrGlobal("StrictMode", 0) != 0),
27+
CommentBoolLiterals(Options.getLocalOrGlobal("CommentBoolLiterals", 0) !=
28+
0),
29+
CommentIntegerLiterals(
30+
Options.getLocalOrGlobal("CommentIntegerLiterals", 0) != 0),
31+
CommentFloatLiterals(
32+
Options.getLocalOrGlobal("CommentFloatLiterals", 0) != 0),
33+
CommentStringLiterals(
34+
Options.getLocalOrGlobal("CommentStringLiterals", 0) != 0),
35+
CommentUserDefinedLiterals(
36+
Options.getLocalOrGlobal("CommentUserDefinedLiterals", 0) != 0),
37+
CommentCharacterLiterals(
38+
Options.getLocalOrGlobal("CommentCharacterLiterals", 0) != 0),
39+
CommentNullPtrs(Options.getLocalOrGlobal("CommentNullPtrs", 0) != 0),
2640
IdentRE("^(/\\* *)([_A-Za-z][_A-Za-z0-9]*)( *= *\\*/)$") {}
2741

2842
void ArgumentCommentCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
2943
Options.store(Opts, "StrictMode", StrictMode);
44+
Options.store(Opts, "CommentBoolLiterals", CommentBoolLiterals);
45+
Options.store(Opts, "CommentIntegerLiterals", CommentIntegerLiterals);
46+
Options.store(Opts, "CommentFloatLiterals", CommentFloatLiterals);
47+
Options.store(Opts, "CommentStringLiterals", CommentStringLiterals);
48+
Options.store(Opts, "CommentUserDefinedLiterals", CommentUserDefinedLiterals);
49+
Options.store(Opts, "CommentCharacterLiterals", CommentCharacterLiterals);
50+
Options.store(Opts, "CommentNullPtrs", CommentNullPtrs);
3051
}
3152

3253
void ArgumentCommentCheck::registerMatchers(MatchFinder *Finder) {
3354
Finder->addMatcher(
3455
callExpr(unless(cxxOperatorCallExpr()),
35-
// NewCallback's arguments relate to the pointed function, don't
36-
// check them against NewCallback's parameter names.
56+
// NewCallback's arguments relate to the pointed function,
57+
// don't check them against NewCallback's parameter names.
3758
// FIXME: Make this configurable.
3859
unless(hasDeclaration(functionDecl(
3960
hasAnyName("NewCallback", "NewPermanentCallback")))))
@@ -126,8 +147,8 @@ static bool isLikelyTypo(llvm::ArrayRef<ParmVarDecl *> Params,
126147

127148
const unsigned Threshold = 2;
128149
// Other parameters must be an edit distance at least Threshold more away
129-
// from this parameter. This gives us greater confidence that this is a typo
130-
// of this parameter and not one with a similar name.
150+
// from this parameter. This gives us greater confidence that this is a
151+
// typo of this parameter and not one with a similar name.
131152
unsigned OtherED = ArgNameLower.edit_distance(II->getName().lower(),
132153
/*AllowReplacements=*/true,
133154
ThisED + Threshold);
@@ -180,8 +201,8 @@ static const CXXMethodDecl *findMockedMethod(const CXXMethodDecl *Method) {
180201
}
181202
return nullptr;
182203
}
183-
if (const auto *Next = dyn_cast_or_null<CXXMethodDecl>(
184-
Method->getNextDeclInContext())) {
204+
if (const auto *Next =
205+
dyn_cast_or_null<CXXMethodDecl>(Method->getNextDeclInContext())) {
185206
if (looksLikeExpectMethod(Next) && areMockAndExpectMethods(Method, Next))
186207
return Method;
187208
}
@@ -206,6 +227,21 @@ static const FunctionDecl *resolveMocks(const FunctionDecl *Func) {
206227
return Func;
207228
}
208229

230+
// Given the argument type and the options determine if we should
231+
// be adding an argument comment.
232+
bool ArgumentCommentCheck::shouldAddComment(const Expr *Arg) const {
233+
if (Arg->getExprLoc().isMacroID())
234+
return false;
235+
Arg = Arg->IgnoreImpCasts();
236+
return (CommentBoolLiterals && isa<CXXBoolLiteralExpr>(Arg)) ||
237+
(CommentIntegerLiterals && isa<IntegerLiteral>(Arg)) ||
238+
(CommentFloatLiterals && isa<FloatingLiteral>(Arg)) ||
239+
(CommentUserDefinedLiterals && isa<UserDefinedLiteral>(Arg)) ||
240+
(CommentCharacterLiterals && isa<CharacterLiteral>(Arg)) ||
241+
(CommentStringLiterals && isa<StringLiteral>(Arg)) ||
242+
(CommentNullPtrs && isa<CXXNullPtrLiteralExpr>(Arg));
243+
}
244+
209245
void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
210246
const FunctionDecl *OriginalCallee,
211247
SourceLocation ArgBeginLoc,
@@ -219,7 +255,7 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
219255
if (NumArgs == 0)
220256
return;
221257

222-
auto makeFileCharRange = [Ctx](SourceLocation Begin, SourceLocation End) {
258+
auto MakeFileCharRange = [Ctx](SourceLocation Begin, SourceLocation End) {
223259
return Lexer::makeFileCharRange(CharSourceRange::getCharRange(Begin, End),
224260
Ctx->getSourceManager(),
225261
Ctx->getLangOpts());
@@ -242,15 +278,15 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
242278
}
243279

244280
CharSourceRange BeforeArgument =
245-
makeFileCharRange(ArgBeginLoc, Args[I]->getBeginLoc());
281+
MakeFileCharRange(ArgBeginLoc, Args[I]->getBeginLoc());
246282
ArgBeginLoc = Args[I]->getEndLoc();
247283

248284
std::vector<std::pair<SourceLocation, StringRef>> Comments;
249285
if (BeforeArgument.isValid()) {
250286
Comments = getCommentsInRange(Ctx, BeforeArgument);
251287
} else {
252288
// Fall back to parsing back from the start of the argument.
253-
CharSourceRange ArgsRange = makeFileCharRange(
289+
CharSourceRange ArgsRange = MakeFileCharRange(
254290
Args[I]->getBeginLoc(), Args[NumArgs - 1]->getEndLoc());
255291
Comments = getCommentsBeforeLoc(Ctx, ArgsRange.getBegin());
256292
}
@@ -277,8 +313,19 @@ void ArgumentCommentCheck::checkCallArgs(ASTContext *Ctx,
277313
}
278314
}
279315
}
316+
317+
// If the argument comments are missing for literals add them.
318+
if (Comments.empty() && shouldAddComment(Args[I])) {
319+
std::string ArgComment =
320+
(llvm::Twine("/*") + II->getName() + "=*/").str();
321+
DiagnosticBuilder Diag =
322+
diag(Args[I]->getBeginLoc(),
323+
"argument comment missing for literal argument %0")
324+
<< II
325+
<< FixItHint::CreateInsertion(Args[I]->getBeginLoc(), ArgComment);
326+
}
280327
}
281-
}
328+
} // namespace bugprone
282329

283330
void ArgumentCommentCheck::check(const MatchFinder::MatchResult &Result) {
284331
const auto *E = Result.Nodes.getNodeAs<Expr>("expr");

clang-tools-extra/clang-tidy/bugprone/ArgumentCommentCheck.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ namespace bugprone {
2626
///
2727
/// ...
2828
/// f(/*bar=*/true);
29-
/// // warning: argument name 'bar' in comment does not match parameter name 'foo'
29+
/// // warning: argument name 'bar' in comment does not match parameter name
30+
/// 'foo'
3031
/// \endcode
3132
///
3233
/// The check tries to detect typos and suggest automated fixes for them.
@@ -39,12 +40,21 @@ class ArgumentCommentCheck : public ClangTidyCheck {
3940
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
4041

4142
private:
42-
const bool StrictMode;
43+
const unsigned StrictMode : 1;
44+
const unsigned CommentBoolLiterals : 1;
45+
const unsigned CommentIntegerLiterals : 1;
46+
const unsigned CommentFloatLiterals : 1;
47+
const unsigned CommentStringLiterals : 1;
48+
const unsigned CommentUserDefinedLiterals : 1;
49+
const unsigned CommentCharacterLiterals : 1;
50+
const unsigned CommentNullPtrs : 1;
4351
llvm::Regex IdentRE;
4452

4553
void checkCallArgs(ASTContext *Ctx, const FunctionDecl *Callee,
4654
SourceLocation ArgBeginLoc,
4755
llvm::ArrayRef<const Expr *> Args);
56+
57+
bool shouldAddComment(const Expr *Arg) const;
4858
};
4959

5060
} // namespace bugprone

clang-tools-extra/clang-tidy/bugprone/DanglingHandleCheck.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,8 @@ void DanglingHandleCheck::registerMatchersForReturn(MatchFinder *Finder) {
163163

164164
// Return a temporary.
165165
Finder->addMatcher(
166-
returnStmt(
167-
has(ignoringParenImpCasts(exprWithCleanups(has(ignoringParenImpCasts(
168-
handleFrom(IsAHandle, handleFromTemporaryValue(IsAHandle))))))))
166+
returnStmt(has(exprWithCleanups(has(ignoringParenImpCasts(handleFrom(
167+
IsAHandle, handleFromTemporaryValue(IsAHandle)))))))
169168
.bind("bad_stmt"),
170169
this);
171170
}

clang-tools-extra/clang-tidy/bugprone/ExceptionEscapeCheck.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ ExceptionEscapeCheck::ExceptionEscapeCheck(StringRef Name,
4242
IgnoredExceptions.insert(IgnoredExceptionsVec.begin(),
4343
IgnoredExceptionsVec.end());
4444
Tracer.ignoreExceptions(std::move(IgnoredExceptions));
45+
Tracer.ignoreBadAlloc(true);
4546
}
4647

4748
void ExceptionEscapeCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
@@ -70,7 +71,8 @@ void ExceptionEscapeCheck::check(const MatchFinder::MatchResult &Result) {
7071
if (!MatchedDecl)
7172
return;
7273

73-
if (Tracer.throwsException(MatchedDecl))
74+
if (Tracer.analyze(MatchedDecl).getBehaviour() ==
75+
utils::ExceptionAnalyzer::State::Throwing)
7476
// FIXME: We should provide more information about the exact location where
7577
// the exception is thrown, maybe the full path the exception escapes
7678
diag(MatchedDecl->getLocation(),

clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void AvoidBindCheck::check(const MatchFinder::MatchResult &Result) {
169169
Ref->printPretty(Stream, nullptr, Result.Context->getPrintingPolicy());
170170
Stream << "(";
171171
addFunctionCallArgs(Args, Stream);
172-
Stream << "); };";
172+
Stream << "); }";
173173

174174
Diag << FixItHint::CreateReplacement(MatchedDecl->getSourceRange(),
175175
Stream.str());

0 commit comments

Comments
 (0)