Skip to content

Commit 1fd087d

Browse files
committed
Merge from 'master' to 'sycl-web' (#1)
CONFLICT (content): Merge conflict in clang/include/clang/Basic/Attr.td
2 parents c9123d1 + eda58ac commit 1fd087d

File tree

629 files changed

+15436
-7484
lines changed

Some content is hidden

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

629 files changed

+15436
-7484
lines changed

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,22 @@ void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
5252
callee(functionDecl(hasName("::absl::FDivDuration"))),
5353
hasArgument(0, expr().bind("arg")), hasArgument(1, factory_matcher));
5454

55+
// Matcher which matches a duration argument being scaled,
56+
// e.g. `absl::ToDoubleSeconds(dur) * 2`
57+
auto scalar_matcher = ignoringImpCasts(
58+
binaryOperator(hasOperatorName("*"),
59+
hasEitherOperand(expr(ignoringParenImpCasts(
60+
callExpr(callee(functionDecl(hasAnyName(
61+
FloatConversion, IntegerConversion))),
62+
hasArgument(0, expr().bind("arg")))
63+
.bind("inner_call")))))
64+
.bind("binop"));
65+
5566
Finder->addMatcher(
5667
callExpr(callee(functionDecl(hasName(DurationFactory))),
5768
hasArgument(0, anyOf(inverse_function_matcher,
58-
division_operator_matcher, fdiv_matcher)))
69+
division_operator_matcher, fdiv_matcher,
70+
scalar_matcher)))
5971
.bind("call"),
6072
this);
6173
}
@@ -64,16 +76,41 @@ void DurationUnnecessaryConversionCheck::registerMatchers(MatchFinder *Finder) {
6476
void DurationUnnecessaryConversionCheck::check(
6577
const MatchFinder::MatchResult &Result) {
6678
const auto *OuterCall = Result.Nodes.getNodeAs<Expr>("call");
67-
const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
6879

6980
if (isInMacro(Result, OuterCall))
7081
return;
7182

83+
FixItHint Hint;
84+
if (const auto *Binop = Result.Nodes.getNodeAs<BinaryOperator>("binop")) {
85+
const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg");
86+
const auto *InnerCall = Result.Nodes.getNodeAs<Expr>("inner_call");
87+
const Expr *LHS = Binop->getLHS();
88+
const Expr *RHS = Binop->getRHS();
89+
90+
if (LHS->IgnoreParenImpCasts() == InnerCall) {
91+
Hint = FixItHint::CreateReplacement(
92+
OuterCall->getSourceRange(),
93+
(llvm::Twine(tooling::fixit::getText(*Arg, *Result.Context)) + " * " +
94+
tooling::fixit::getText(*RHS, *Result.Context))
95+
.str());
96+
} else {
97+
assert(RHS->IgnoreParenImpCasts() == InnerCall &&
98+
"Inner call should be find on the RHS");
99+
100+
Hint = FixItHint::CreateReplacement(
101+
OuterCall->getSourceRange(),
102+
(llvm::Twine(tooling::fixit::getText(*LHS, *Result.Context)) + " * " +
103+
tooling::fixit::getText(*Arg, *Result.Context))
104+
.str());
105+
}
106+
} else if (const auto *Arg = Result.Nodes.getNodeAs<Expr>("arg")) {
107+
Hint = FixItHint::CreateReplacement(
108+
OuterCall->getSourceRange(),
109+
tooling::fixit::getText(*Arg, *Result.Context));
110+
}
72111
diag(OuterCall->getBeginLoc(),
73112
"remove unnecessary absl::Duration conversions")
74-
<< FixItHint::CreateReplacement(
75-
OuterCall->getSourceRange(),
76-
tooling::fixit::getText(*Arg, *Result.Context));
113+
<< Hint;
77114
}
78115

79116
} // namespace abseil

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

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace clang {
1818
namespace tidy {
1919
namespace bugprone {
2020

21+
static constexpr int UnsignedASCIIUpperBound = 127;
22+
2123
static Matcher<TypedefDecl> hasAnyListedName(const std::string &Names) {
2224
const std::vector<std::string> NameList =
2325
utils::options::parseStringList(Names);
@@ -33,70 +35,114 @@ void SignedCharMisuseCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
3335
Options.store(Opts, "CharTypdefsToIgnore", CharTypdefsToIgnoreList);
3436
}
3537

36-
void SignedCharMisuseCheck::registerMatchers(MatchFinder *Finder) {
38+
// Create a matcher for char -> integer cast.
39+
BindableMatcher<clang::Stmt> SignedCharMisuseCheck::charCastExpression(
40+
bool IsSigned, const Matcher<clang::QualType> &IntegerType,
41+
const std::string &CastBindName) const {
3742
// We can ignore typedefs which are some kind of integer types
3843
// (e.g. typedef char sal_Int8). In this case, we don't need to
3944
// worry about the misinterpretation of char values.
4045
const auto IntTypedef = qualType(
4146
hasDeclaration(typedefDecl(hasAnyListedName(CharTypdefsToIgnoreList))));
4247

43-
const auto SignedCharType = expr(hasType(qualType(
44-
allOf(isAnyCharacter(), isSignedInteger(), unless(IntTypedef)))));
45-
46-
const auto IntegerType = qualType(allOf(isInteger(), unless(isAnyCharacter()),
47-
unless(booleanType())))
48-
.bind("integerType");
48+
auto CharTypeExpr = expr();
49+
if (IsSigned) {
50+
CharTypeExpr = expr(hasType(
51+
qualType(isAnyCharacter(), isSignedInteger(), unless(IntTypedef))));
52+
} else {
53+
CharTypeExpr = expr(hasType(qualType(
54+
isAnyCharacter(), unless(isSignedInteger()), unless(IntTypedef))));
55+
}
4956

50-
// We are interested in signed char -> integer conversion.
5157
const auto ImplicitCastExpr =
52-
implicitCastExpr(hasSourceExpression(SignedCharType),
58+
implicitCastExpr(hasSourceExpression(CharTypeExpr),
5359
hasImplicitDestinationType(IntegerType))
54-
.bind("castExpression");
60+
.bind(CastBindName);
5561

5662
const auto CStyleCastExpr = cStyleCastExpr(has(ImplicitCastExpr));
5763
const auto StaticCastExpr = cxxStaticCastExpr(has(ImplicitCastExpr));
5864
const auto FunctionalCastExpr = cxxFunctionalCastExpr(has(ImplicitCastExpr));
5965

6066
// We catch any type of casts to an integer. We need to have these cast
6167
// expressions explicitly to catch only those casts which are direct children
62-
// of an assignment/declaration.
63-
const auto CastExpr = expr(anyOf(ImplicitCastExpr, CStyleCastExpr,
64-
StaticCastExpr, FunctionalCastExpr));
68+
// of the checked expressions. (e.g. assignment, declaration).
69+
return expr(anyOf(ImplicitCastExpr, CStyleCastExpr, StaticCastExpr,
70+
FunctionalCastExpr));
71+
}
6572

66-
// Catch assignments with the suspicious type conversion.
67-
const auto AssignmentOperatorExpr = expr(binaryOperator(
68-
hasOperatorName("="), hasLHS(hasType(IntegerType)), hasRHS(CastExpr)));
73+
void SignedCharMisuseCheck::registerMatchers(MatchFinder *Finder) {
74+
const auto IntegerType =
75+
qualType(isInteger(), unless(isAnyCharacter()), unless(booleanType()))
76+
.bind("integerType");
77+
const auto SignedCharCastExpr =
78+
charCastExpression(true, IntegerType, "signedCastExpression");
79+
const auto UnSignedCharCastExpr =
80+
charCastExpression(false, IntegerType, "unsignedCastExpression");
81+
82+
// Catch assignments with singed char -> integer conversion.
83+
const auto AssignmentOperatorExpr =
84+
expr(binaryOperator(hasOperatorName("="), hasLHS(hasType(IntegerType)),
85+
hasRHS(SignedCharCastExpr)));
6986

7087
Finder->addMatcher(AssignmentOperatorExpr, this);
7188

72-
// Catch declarations with the suspicious type conversion.
73-
const auto Declaration =
74-
varDecl(isDefinition(), hasType(IntegerType), hasInitializer(CastExpr));
89+
// Catch declarations with singed char -> integer conversion.
90+
const auto Declaration = varDecl(isDefinition(), hasType(IntegerType),
91+
hasInitializer(SignedCharCastExpr));
7592

7693
Finder->addMatcher(Declaration, this);
94+
95+
// Catch signed char/unsigned char comparison.
96+
const auto CompareOperator =
97+
expr(binaryOperator(hasAnyOperatorName("==", "!="),
98+
anyOf(allOf(hasLHS(SignedCharCastExpr),
99+
hasRHS(UnSignedCharCastExpr)),
100+
allOf(hasLHS(UnSignedCharCastExpr),
101+
hasRHS(SignedCharCastExpr)))))
102+
.bind("comparison");
103+
104+
Finder->addMatcher(CompareOperator, this);
77105
}
78106

79107
void SignedCharMisuseCheck::check(const MatchFinder::MatchResult &Result) {
80-
const auto *CastExpression =
81-
Result.Nodes.getNodeAs<ImplicitCastExpr>("castExpression");
82-
const auto *IntegerType = Result.Nodes.getNodeAs<QualType>("integerType");
83-
assert(CastExpression);
84-
assert(IntegerType);
108+
const auto *SignedCastExpression =
109+
Result.Nodes.getNodeAs<ImplicitCastExpr>("signedCastExpression");
85110

86-
// Ignore the match if we know that the value is not negative.
111+
// Ignore the match if we know that the signed char's value is not negative.
87112
// The potential misinterpretation happens for negative values only.
88113
Expr::EvalResult EVResult;
89-
if (!CastExpression->isValueDependent() &&
90-
CastExpression->getSubExpr()->EvaluateAsInt(EVResult, *Result.Context)) {
91-
llvm::APSInt Value1 = EVResult.Val.getInt();
92-
if (Value1.isNonNegative())
114+
if (!SignedCastExpression->isValueDependent() &&
115+
SignedCastExpression->getSubExpr()->EvaluateAsInt(EVResult,
116+
*Result.Context)) {
117+
llvm::APSInt Value = EVResult.Val.getInt();
118+
if (Value.isNonNegative())
93119
return;
94120
}
95121

96-
diag(CastExpression->getBeginLoc(),
97-
"'signed char' to %0 conversion; "
98-
"consider casting to 'unsigned char' first.")
99-
<< *IntegerType;
122+
if (const auto *Comparison = Result.Nodes.getNodeAs<Expr>("comparison")) {
123+
const auto *UnSignedCastExpression =
124+
Result.Nodes.getNodeAs<ImplicitCastExpr>("unsignedCastExpression");
125+
126+
// We can ignore the ASCII value range also for unsigned char.
127+
Expr::EvalResult EVResult;
128+
if (!UnSignedCastExpression->isValueDependent() &&
129+
UnSignedCastExpression->getSubExpr()->EvaluateAsInt(EVResult,
130+
*Result.Context)) {
131+
llvm::APSInt Value = EVResult.Val.getInt();
132+
if (Value <= UnsignedASCIIUpperBound)
133+
return;
134+
}
135+
136+
diag(Comparison->getBeginLoc(),
137+
"comparison between 'signed char' and 'unsigned char'");
138+
} else if (const auto *IntegerType =
139+
Result.Nodes.getNodeAs<QualType>("integerType")) {
140+
diag(SignedCastExpression->getBeginLoc(),
141+
"'signed char' to %0 conversion; "
142+
"consider casting to 'unsigned char' first.")
143+
<< *IntegerType;
144+
} else
145+
llvm_unreachable("Unexpected match");
100146
}
101147

102148
} // namespace bugprone

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ namespace clang {
1515
namespace tidy {
1616
namespace bugprone {
1717

18-
/// Finds ``signed char`` -> integer conversions which might indicate a programming
19-
/// error. The basic problem with the ``signed char``, that it might store the
20-
/// non-ASCII characters as negative values. The human programmer probably
21-
/// expects that after an integer conversion the converted value matches with the
22-
/// character code (a value from [0..255]), however, the actual value is in
23-
/// [-128..127] interval. This also applies to the plain ``char`` type on
24-
/// those implementations which represent ``char`` similar to ``signed char``.
18+
/// Finds those ``signed char`` -> integer conversions which might indicate a
19+
/// programming error. The basic problem with the ``signed char``, that it might
20+
/// store the non-ASCII characters as negative values. This behavior can cause a
21+
/// misunderstanding of the written code both when an explicit and when an
22+
/// implicit conversion happens.
2523
///
2624
/// For the user-facing documentation see:
2725
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone-signed-char-misuse.html
@@ -34,6 +32,11 @@ class SignedCharMisuseCheck : public ClangTidyCheck {
3432
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3533

3634
private:
35+
ast_matchers::internal::BindableMatcher<clang::Stmt> charCastExpression(
36+
bool IsSigned,
37+
const ast_matchers::internal::Matcher<clang::QualType> &IntegerType,
38+
const std::string &CastBindName) const;
39+
3740
const std::string CharTypdefsToIgnoreList;
3841
};
3942

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===--- AvoidNonConstGlobalVariablesCheck.cpp - clang-tidy ---------------===//
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+
#include "AvoidNonConstGlobalVariablesCheck.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/ASTMatchers/ASTMatchFinder.h"
12+
#include "clang/ASTMatchers/ASTMatchers.h"
13+
14+
using namespace clang::ast_matchers;
15+
16+
namespace clang {
17+
namespace tidy {
18+
namespace cppcoreguidelines {
19+
20+
void AvoidNonConstGlobalVariablesCheck::registerMatchers(MatchFinder *Finder) {
21+
auto GlobalVariable = varDecl(
22+
hasGlobalStorage(),
23+
unless(anyOf(
24+
isConstexpr(), hasType(isConstQualified()),
25+
hasType(referenceType())))); // References can't be changed, only the
26+
// data they reference can be changed.
27+
28+
auto GlobalReferenceToNonConst =
29+
varDecl(hasGlobalStorage(), hasType(referenceType()),
30+
unless(hasType(references(qualType(isConstQualified())))));
31+
32+
auto GlobalPointerToNonConst =
33+
varDecl(hasGlobalStorage(),
34+
hasType(pointerType(pointee(unless(isConstQualified())))));
35+
36+
Finder->addMatcher(GlobalVariable.bind("non-const_variable"), this);
37+
Finder->addMatcher(GlobalReferenceToNonConst.bind("indirection_to_non-const"),
38+
this);
39+
Finder->addMatcher(GlobalPointerToNonConst.bind("indirection_to_non-const"),
40+
this);
41+
}
42+
43+
void AvoidNonConstGlobalVariablesCheck::check(
44+
const MatchFinder::MatchResult &Result) {
45+
46+
if (const auto *Variable =
47+
Result.Nodes.getNodeAs<VarDecl>("non-const_variable")) {
48+
diag(Variable->getLocation(), "variable %0 is non-const and globally "
49+
"accessible, consider making it const")
50+
<< Variable; // FIXME: Add fix-it hint to Variable
51+
// Don't return early, a non-const variable may also be a pointer or
52+
// reference to non-const data.
53+
}
54+
55+
if (const auto *VD =
56+
Result.Nodes.getNodeAs<VarDecl>("indirection_to_non-const")) {
57+
diag(VD->getLocation(),
58+
"variable %0 provides global access to a non-const object; consider "
59+
"making the %select{referenced|pointed-to}1 data 'const'")
60+
<< VD
61+
<< VD->getType()->isPointerType(); // FIXME: Add fix-it hint to Variable
62+
}
63+
}
64+
65+
} // namespace cppcoreguidelines
66+
} // namespace tidy
67+
} // namespace clang
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===--- AvoidNonConstGlobalVariablesCheck.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_CPPCOREGUIDELINES_AVOIDNONCONSTGLOBALVARIABLESCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDNONCONSTGLOBALVARIABLESCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang {
15+
namespace tidy {
16+
namespace cppcoreguidelines {
17+
18+
/// Non-const global variables hide dependencies and make the dependencies
19+
/// subject to unpredictable changes.
20+
///
21+
/// For the user-facing documentation see:
22+
/// http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines-avoid-non-const-global-variables.html
23+
class AvoidNonConstGlobalVariablesCheck : public ClangTidyCheck {
24+
public:
25+
AvoidNonConstGlobalVariablesCheck(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 cppcoreguidelines
32+
} // namespace tidy
33+
} // namespace clang
34+
35+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CPPCOREGUIDELINES_AVOIDNONCONSTGLOBALVARIABLESCHECK_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(LLVM_LINK_COMPONENTS support)
22

33
add_clang_library(clangTidyCppCoreGuidelinesModule
44
AvoidGotoCheck.cpp
5+
AvoidNonConstGlobalVariablesCheck.cpp
56
CppCoreGuidelinesTidyModule.cpp
67
InitVariablesCheck.cpp
78
InterfacesGlobalInitCheck.cpp

clang-tools-extra/clang-tidy/cppcoreguidelines/CppCoreGuidelinesTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "../modernize/UseOverrideCheck.h"
1616
#include "../readability/MagicNumbersCheck.h"
1717
#include "AvoidGotoCheck.h"
18+
#include "AvoidNonConstGlobalVariablesCheck.h"
1819
#include "InitVariablesCheck.h"
1920
#include "InterfacesGlobalInitCheck.h"
2021
#include "MacroUsageCheck.h"
@@ -48,6 +49,8 @@ class CppCoreGuidelinesModule : public ClangTidyModule {
4849
"cppcoreguidelines-avoid-goto");
4950
CheckFactories.registerCheck<readability::MagicNumbersCheck>(
5051
"cppcoreguidelines-avoid-magic-numbers");
52+
CheckFactories.registerCheck<AvoidNonConstGlobalVariablesCheck>(
53+
"cppcoreguidelines-avoid-non-const-global-variables");
5154
CheckFactories.registerCheck<modernize::UseOverrideCheck>(
5255
"cppcoreguidelines-explicit-virtual-functions");
5356
CheckFactories.registerCheck<InitVariablesCheck>(

0 commit comments

Comments
 (0)