Skip to content

Commit aad7113

Browse files
committed
Merge remote-tracking branch 'origin/master' into intel
2 parents 45e12ab + f170dff commit aad7113

File tree

5,063 files changed

+176716
-85071
lines changed

Some content is hidden

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

5,063 files changed

+176716
-85071
lines changed

clang-tools-extra/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ add_subdirectory(modularize)
99
add_subdirectory(clang-tidy)
1010
add_subdirectory(clang-tidy-vs)
1111

12-
add_subdirectory(change-namespace)
12+
add_subdirectory(clang-change-namespace)
1313
add_subdirectory(clang-doc)
1414
add_subdirectory(clang-query)
1515
add_subdirectory(clang-move)

clang-tools-extra/change-namespace/tool/ClangChangeNamespace.cpp renamed to clang-tools-extra/clang-change-namespace/tool/ClangChangeNamespace.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- ClangIncludeFixer.cpp - Standalone change namespace ---------------===//
1+
//===-- ClangChangeNamespace.cpp - Standalone change namespace ------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

clang-tools-extra/clang-doc/Generators.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===---- Generator.cpp - Generator Registry ---------------------*- C++-*-===//
1+
//===-- Generators.cpp - Generator Registry ----------------------*- C++-*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

clang-tools-extra/clang-doc/Serialize.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- Serializer.cpp - ClangDoc Serializer --------------------*- C++ -*-===//
1+
//===-- Serialize.cpp - ClangDoc Serializer ---------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

clang-tools-extra/clang-doc/YAMLGenerator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- ClangDocYAML.cpp - ClangDoc YAML -----------------------*- C++ -*-===//
1+
//===-- YAMLGenerator.cpp - ClangDoc YAML -----------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

clang-tools-extra/clang-move/HelperDeclRefGraph.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===-- UsedHelperDeclFinder.cpp - AST-based call graph for helper decls --===//
1+
//===-- HelperDeclRefGraph.cpp - AST-based call graph for helper decls ----===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ bool ClangTidyContext::treatAsError(StringRef CheckName) const {
254254
return WarningAsErrorFilter->contains(CheckName);
255255
}
256256

257-
StringRef ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
257+
std::string ClangTidyContext::getCheckName(unsigned DiagnosticID) const {
258+
std::string ClangWarningOption =
259+
DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(DiagnosticID);
260+
if (!ClangWarningOption.empty())
261+
return "clang-diagnostic-" + ClangWarningOption;
258262
llvm::DenseMap<unsigned, std::string>::const_iterator I =
259263
CheckNamesByDiagnosticID.find(DiagnosticID);
260264
if (I != CheckNamesByDiagnosticID.end())
@@ -305,7 +309,7 @@ static bool IsNOLINTFound(StringRef NolintDirectiveText, StringRef Line,
305309
Line.substr(BracketIndex, BracketEndIndex - BracketIndex);
306310
// Allow disabling all the checks with "*".
307311
if (ChecksStr != "*") {
308-
StringRef CheckName = Context.getCheckName(DiagID);
312+
std::string CheckName = Context.getCheckName(DiagID);
309313
// Allow specifying a few check names, delimited with comma.
310314
SmallVector<StringRef, 1> Checks;
311315
ChecksStr.split(Checks, ',', -1, false);
@@ -402,13 +406,7 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
402406
"A diagnostic note can only be appended to a message.");
403407
} else {
404408
finalizeLastError();
405-
StringRef WarningOption =
406-
Context.DiagEngine->getDiagnosticIDs()->getWarningOptionForDiag(
407-
Info.getID());
408-
std::string CheckName = !WarningOption.empty()
409-
? ("clang-diagnostic-" + WarningOption).str()
410-
: Context.getCheckName(Info.getID()).str();
411-
409+
std::string CheckName = Context.getCheckName(Info.getID());
412410
if (CheckName.empty()) {
413411
// This is a compiler diagnostic without a warning option. Assign check
414412
// name based on its level.

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class ClangTidyContext {
138138

139139
/// \brief Returns the name of the clang-tidy check which produced this
140140
/// diagnostic ID.
141-
StringRef getCheckName(unsigned DiagnosticID) const;
141+
std::string getCheckName(unsigned DiagnosticID) const;
142142

143143
/// \brief Returns \c true if the check is enabled for the \c CurrentFile.
144144
///

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

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "RedundantStrcatCallsCheck.h"
2424
#include "StringFindStartswithCheck.h"
2525
#include "StrCatAppendCheck.h"
26+
#include "TimeComparisonCheck.h"
27+
#include "TimeSubtractionCheck.h"
2628
#include "UpgradeDurationConversionsCheck.h"
2729

2830
namespace clang {
@@ -59,6 +61,10 @@ class AbseilModule : public ClangTidyModule {
5961
"abseil-str-cat-append");
6062
CheckFactories.registerCheck<StringFindStartswithCheck>(
6163
"abseil-string-find-startswith");
64+
CheckFactories.registerCheck<TimeComparisonCheck>(
65+
"abseil-time-comparison");
66+
CheckFactories.registerCheck<TimeSubtractionCheck>(
67+
"abseil-time-subtraction");
6268
CheckFactories.registerCheck<UpgradeDurationConversionsCheck>(
6369
"abseil-upgrade-duration-conversions");
6470
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ add_clang_library(clangTidyAbseilModule
1717
RedundantStrcatCallsCheck.cpp
1818
StrCatAppendCheck.cpp
1919
StringFindStartswithCheck.cpp
20+
TimeComparisonCheck.cpp
21+
TimeSubtractionCheck.cpp
2022
UpgradeDurationConversionsCheck.cpp
2123

2224
LINK_LIBS

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

+5-10
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@ namespace tidy {
1919
namespace abseil {
2020

2121
void DurationComparisonCheck::registerMatchers(MatchFinder *Finder) {
22-
auto Matcher =
23-
binaryOperator(anyOf(hasOperatorName(">"), hasOperatorName(">="),
24-
hasOperatorName("=="), hasOperatorName("<="),
25-
hasOperatorName("<")),
26-
hasEitherOperand(ignoringImpCasts(callExpr(
27-
callee(functionDecl(DurationConversionFunction())
28-
.bind("function_decl"))))))
29-
.bind("binop");
22+
auto Matcher = expr(comparisonOperatorWithCallee(functionDecl(
23+
functionDecl(DurationConversionFunction())
24+
.bind("function_decl"))))
25+
.bind("binop");
3026

3127
Finder->addMatcher(Matcher, this);
3228
}
@@ -43,8 +39,7 @@ void DurationComparisonCheck::check(const MatchFinder::MatchResult &Result) {
4339
// want to handle the case of rewriting both sides. This is much simpler if
4440
// we unconditionally try and rewrite both, and let the rewriter determine
4541
// if nothing needs to be done.
46-
if (!isNotInMacro(Result, Binop->getLHS()) ||
47-
!isNotInMacro(Result, Binop->getRHS()))
42+
if (isInMacro(Result, Binop->getLHS()) || isInMacro(Result, Binop->getRHS()))
4843
return;
4944
std::string LhsReplacement =
5045
rewriteExprFromNumberToDuration(Result, *Scale, Binop->getLHS());

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void DurationConversionCastCheck::check(
3737
const auto *MatchedCast =
3838
Result.Nodes.getNodeAs<ExplicitCastExpr>("cast_expr");
3939

40-
if (!isNotInMacro(Result, MatchedCast))
40+
if (isInMacro(Result, MatchedCast))
4141
return;
4242

4343
const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func_decl");

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

+55-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ rewriteInverseDurationCall(const MatchFinder::MatchResult &Result,
8484
return llvm::None;
8585
}
8686

87+
/// If `Node` is a call to the inverse of `Scale`, return that inverse's
88+
/// argument, otherwise None.
89+
static llvm::Optional<std::string>
90+
rewriteInverseTimeCall(const MatchFinder::MatchResult &Result,
91+
DurationScale Scale, const Expr &Node) {
92+
llvm::StringRef InverseFunction = getTimeInverseForScale(Scale);
93+
if (const auto *MaybeCallArg = selectFirst<const Expr>(
94+
"e", match(callExpr(callee(functionDecl(hasName(InverseFunction))),
95+
hasArgument(0, expr().bind("e"))),
96+
Node, *Result.Context))) {
97+
return tooling::fixit::getText(*MaybeCallArg, *Result.Context).str();
98+
}
99+
100+
return llvm::None;
101+
}
102+
87103
/// Returns the factory function name for a given `Scale`.
88104
llvm::StringRef getDurationFactoryForScale(DurationScale Scale) {
89105
switch (Scale) {
@@ -103,6 +119,24 @@ llvm::StringRef getDurationFactoryForScale(DurationScale Scale) {
103119
llvm_unreachable("unknown scaling factor");
104120
}
105121

122+
llvm::StringRef getTimeFactoryForScale(DurationScale Scale) {
123+
switch (Scale) {
124+
case DurationScale::Hours:
125+
return "absl::FromUnixHours";
126+
case DurationScale::Minutes:
127+
return "absl::FromUnixMinutes";
128+
case DurationScale::Seconds:
129+
return "absl::FromUnixSeconds";
130+
case DurationScale::Milliseconds:
131+
return "absl::FromUnixMillis";
132+
case DurationScale::Microseconds:
133+
return "absl::FromUnixMicros";
134+
case DurationScale::Nanoseconds:
135+
return "absl::FromUnixNanos";
136+
}
137+
llvm_unreachable("unknown scaling factor");
138+
}
139+
106140
/// Returns the Time factory function name for a given `Scale`.
107141
llvm::StringRef getTimeInverseForScale(DurationScale scale) {
108142
switch (scale) {
@@ -250,9 +284,27 @@ std::string rewriteExprFromNumberToDuration(
250284
.str();
251285
}
252286

253-
bool isNotInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
287+
std::string rewriteExprFromNumberToTime(
288+
const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
289+
const Expr *Node) {
290+
const Expr &RootNode = *Node->IgnoreParenImpCasts();
291+
292+
// First check to see if we can undo a complimentary function call.
293+
if (llvm::Optional<std::string> MaybeRewrite =
294+
rewriteInverseTimeCall(Result, Scale, RootNode))
295+
return *MaybeRewrite;
296+
297+
if (IsLiteralZero(Result, RootNode))
298+
return std::string("absl::UnixEpoch()");
299+
300+
return (llvm::Twine(getTimeFactoryForScale(Scale)) + "(" +
301+
tooling::fixit::getText(RootNode, *Result.Context) + ")")
302+
.str();
303+
}
304+
305+
bool isInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
254306
if (!E->getBeginLoc().isMacroID())
255-
return true;
307+
return false;
256308

257309
SourceLocation Loc = E->getBeginLoc();
258310
// We want to get closer towards the initial macro typed into the source only
@@ -264,7 +316,7 @@ bool isNotInMacro(const MatchFinder::MatchResult &Result, const Expr *E) {
264316
// because Clang comment says it "should not generally be used by clients."
265317
Loc = Result.SourceManager->getImmediateMacroCallerLoc(Loc);
266318
}
267-
return !Loc.isMacroID();
319+
return Loc.isMacroID();
268320
}
269321

270322
} // namespace abseil

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

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

34+
/// Given a 'Scale', return the appropriate factory function call for
35+
/// constructing a `Time` for that scale.
36+
llvm::StringRef getTimeFactoryForScale(DurationScale scale);
37+
3438
// Determine if `Node` represents a literal floating point or integral zero.
3539
bool IsLiteralZero(const ast_matchers::MatchFinder::MatchResult &Result,
3640
const Expr &Node);
@@ -81,10 +85,16 @@ std::string rewriteExprFromNumberToDuration(
8185
const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
8286
const Expr *Node);
8387

84-
/// Return `true` if `E` is a either: not a macro at all; or an argument to
88+
/// Assuming `Node` has a type `int` representing a time instant of `Scale`
89+
/// since The Epoch, return the expression to make it a suitable `Time`.
90+
std::string rewriteExprFromNumberToTime(
91+
const ast_matchers::MatchFinder::MatchResult &Result, DurationScale Scale,
92+
const Expr *Node);
93+
94+
/// Return `false` if `E` is a either: not a macro at all; or an argument to
8595
/// one. In the both cases, we often want to do the transformation.
86-
bool isNotInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
87-
const Expr *E);
96+
bool isInMacro(const ast_matchers::MatchFinder::MatchResult &Result,
97+
const Expr *E);
8898

8999
AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
90100
DurationConversionFunction) {
@@ -114,6 +124,16 @@ AST_MATCHER_FUNCTION(ast_matchers::internal::Matcher<FunctionDecl>,
114124
"::absl::ToUnixMillis", "::absl::ToUnixMicros", "::absl::ToUnixNanos"));
115125
}
116126

127+
AST_MATCHER_FUNCTION_P(ast_matchers::internal::Matcher<Stmt>,
128+
comparisonOperatorWithCallee,
129+
ast_matchers::internal::Matcher<Decl>, funcDecl) {
130+
using namespace clang::ast_matchers;
131+
return binaryOperator(
132+
anyOf(hasOperatorName(">"), hasOperatorName(">="), hasOperatorName("=="),
133+
hasOperatorName("<="), hasOperatorName("<")),
134+
hasEitherOperand(ignoringImpCasts(callExpr(callee(funcDecl)))));
135+
}
136+
117137
} // namespace abseil
118138
} // namespace tidy
119139
} // namespace clang

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

+31-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,35 @@ void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
2828
std::string IntegerConversion =
2929
(llvm::Twine("::absl::ToInt64") + Scale).str();
3030

31+
// Matcher which matches the current scale's factory with a `1` argument,
32+
// e.g. `absl::Seconds(1)`.
33+
auto factory_matcher = cxxConstructExpr(hasArgument(
34+
0,
35+
callExpr(callee(functionDecl(hasName(DurationFactory))),
36+
hasArgument(0, ignoringImpCasts(integerLiteral(equals(1)))))));
37+
38+
// Matcher which matches either inverse function and binds its argument,
39+
// e.g. `absl::ToDoubleSeconds(dur)`.
40+
auto inverse_function_matcher = callExpr(
41+
callee(functionDecl(hasAnyName(FloatConversion, IntegerConversion))),
42+
hasArgument(0, expr().bind("arg")));
43+
44+
// Matcher which matches a duration divided by the factory_matcher above,
45+
// e.g. `dur / absl::Seconds(1)`.
46+
auto division_operator_matcher = cxxOperatorCallExpr(
47+
hasOverloadedOperatorName("/"), hasArgument(0, expr().bind("arg")),
48+
hasArgument(1, factory_matcher));
49+
50+
// Matcher which matches a duration argument to `FDivDuration`,
51+
// e.g. `absl::FDivDuration(dur, absl::Seconds(1))`
52+
auto fdiv_matcher = callExpr(
53+
callee(functionDecl(hasName("::absl::FDivDuration"))),
54+
hasArgument(0, expr().bind("arg")), hasArgument(1, factory_matcher));
55+
3156
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")))))
57+
callExpr(callee(functionDecl(hasName(DurationFactory))),
58+
hasArgument(0, anyOf(inverse_function_matcher,
59+
division_operator_matcher, fdiv_matcher)))
3760
.bind("call"),
3861
this);
3962
}
@@ -44,10 +67,11 @@ void DurationUnnecessaryConversionCheck::check(
4467
const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
4568
const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
4669

47-
if (!isNotInMacro(Result, OuterCall))
70+
if (isInMacro(Result, OuterCall))
4871
return;
4972

50-
diag(OuterCall->getBeginLoc(), "remove unnecessary absl::Duration conversions")
73+
diag(OuterCall->getBeginLoc(),
74+
"remove unnecessary absl::Duration conversions")
5175
<< FixItHint::CreateReplacement(
5276
OuterCall->getSourceRange(),
5377
tooling::fixit::getText(*Arg, *Result.Context));

0 commit comments

Comments
 (0)