Skip to content

Commit 04a5d4c

Browse files
authored
Merge branch 'main' into gisel-vop3p
2 parents afa6448 + 9ef7287 commit 04a5d4c

File tree

541 files changed

+23886
-19108
lines changed

Some content is hidden

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

541 files changed

+23886
-19108
lines changed

.ci/metrics/metrics.py

Lines changed: 164 additions & 154 deletions
Large diffs are not rendered by default.

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

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../utils/OptionsUtils.h"
1313
#include "clang/AST/ASTContext.h"
1414
#include "clang/ASTMatchers/ASTMatchFinder.h"
15+
#include "clang/ASTMatchers/ASTMatchers.h"
1516
#include <array>
1617

1718
using namespace clang::ast_matchers;
@@ -27,27 +28,11 @@ AST_MATCHER_P(QualType, hasCleanType, Matcher<QualType>, InnerMatcher) {
2728
Finder, Builder);
2829
}
2930

30-
constexpr std::array<StringRef, 2> NameList{
31+
constexpr std::array<StringRef, 2> MakeSmartPtrList{
3132
"::std::make_unique",
3233
"::std::make_shared",
3334
};
34-
35-
Matcher<Expr> constructFrom(Matcher<QualType> TypeMatcher,
36-
Matcher<Expr> ArgumentMatcher) {
37-
return expr(
38-
anyOf(
39-
// construct optional
40-
cxxConstructExpr(argumentCountIs(1U), hasType(TypeMatcher),
41-
hasArgument(0U, ArgumentMatcher)),
42-
// known template methods in std
43-
callExpr(argumentCountIs(1),
44-
callee(functionDecl(
45-
matchers::matchesAnyListedName(NameList),
46-
hasTemplateArgument(0, refersToType(TypeMatcher)))),
47-
hasArgument(0, ArgumentMatcher))),
48-
unless(anyOf(hasAncestor(typeLoc()),
49-
hasAncestor(expr(matchers::hasUnevaluatedContext())))));
50-
}
35+
constexpr StringRef MakeOptional = "::std::make_optional";
5136

5237
} // namespace
5338

@@ -74,7 +59,7 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
7459
auto EqualsBoundOptionalType =
7560
qualType(hasCleanType(equalsBoundNode("optional-type")));
7661

77-
auto OptionalDereferenceMatcher = callExpr(
62+
auto OptionalDerefMatcherImpl = callExpr(
7863
anyOf(
7964
cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
8065
hasUnaryOperand(hasType(EqualsBoundOptionalType)))
@@ -88,11 +73,41 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
8873

8974
auto StdMoveCallMatcher =
9075
callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))),
91-
hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
76+
hasArgument(0, ignoringImpCasts(OptionalDerefMatcherImpl)));
77+
auto OptionalDerefMatcher =
78+
ignoringImpCasts(anyOf(OptionalDerefMatcherImpl, StdMoveCallMatcher));
79+
9280
Finder->addMatcher(
93-
expr(constructFrom(BindOptionalType,
94-
ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
95-
StdMoveCallMatcher))))
81+
expr(anyOf(
82+
// construct optional
83+
cxxConstructExpr(argumentCountIs(1), hasType(BindOptionalType),
84+
hasArgument(0, OptionalDerefMatcher)),
85+
// known template methods in std
86+
callExpr(
87+
argumentCountIs(1),
88+
anyOf(
89+
// match std::make_unique std::make_shared
90+
callee(functionDecl(
91+
matchers::matchesAnyListedName(MakeSmartPtrList),
92+
hasTemplateArgument(
93+
0, refersToType(BindOptionalType)))),
94+
// match first std::make_optional by limit argument count
95+
// (1) and template count (1).
96+
// 1. template< class T > constexpr
97+
// std::optional<decay_t<T>> make_optional(T&& value);
98+
// 2. template< class T, class... Args > constexpr
99+
// std::optional<T> make_optional(Args&&... args);
100+
callee(functionDecl(templateArgumentCountIs(1),
101+
hasName(MakeOptional),
102+
returns(BindOptionalType)))),
103+
hasArgument(0, OptionalDerefMatcher)),
104+
callExpr(
105+
106+
argumentCountIs(1),
107+
108+
hasArgument(0, OptionalDerefMatcher))),
109+
unless(anyOf(hasAncestor(typeLoc()),
110+
hasAncestor(expr(matchers::hasUnevaluatedContext())))))
96111
.bind("expr"),
97112
this);
98113
}

clang-tools-extra/clang-tidy/misc/ConstCorrectnessCheck.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,14 @@ void ConstCorrectnessCheck::check(const MatchFinder::MatchResult &Result) {
136136
return;
137137

138138
VariableCategory VC = VariableCategory::Value;
139-
if (Variable->getType()->isReferenceType())
139+
const QualType VT = Variable->getType();
140+
if (VT->isReferenceType())
140141
VC = VariableCategory::Reference;
141-
if (Variable->getType()->isPointerType())
142+
else if (VT->isPointerType())
142143
VC = VariableCategory::Pointer;
143-
if (Variable->getType()->isArrayType()) {
144-
if (const auto *ArrayT = dyn_cast<ArrayType>(Variable->getType())) {
145-
if (ArrayT->getElementType()->isPointerType())
146-
VC = VariableCategory::Pointer;
147-
}
148-
}
144+
else if (const auto *ArrayT = dyn_cast<ArrayType>(VT))
145+
if (ArrayT->getElementType()->isPointerType())
146+
VC = VariableCategory::Pointer;
149147

150148
// Each variable can only be in one category: Value, Pointer, Reference.
151149
// Analysis can be controlled for every category.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ void UseDefaultMemberInitCheck::storeOptions(
194194
}
195195

196196
void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
197+
auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass()));
198+
197199
auto InitBase =
198200
anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
199201
unaryOperator(hasAnyOperatorName("+", "-"),
@@ -202,7 +204,7 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
202204
unaryOperator(hasAnyOperatorName("+", "-"),
203205
hasUnaryOperand(floatLiteral())),
204206
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
205-
declRefExpr(to(enumConstantDecl())));
207+
declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef))));
206208

207209
auto Init =
208210
anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)),

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ struct MatchBuilder {
9191

9292
auto matchMathCall(const StringRef FunctionName,
9393
const Matcher<clang::Expr> ArgumentMatcher) const {
94+
auto HasAnyPrecisionName = hasAnyName(
95+
FunctionName, (FunctionName + "l").str(),
96+
(FunctionName + "f").str()); // Support long double(l) and float(f).
9497
return expr(ignoreParenAndFloatingCasting(
95-
callExpr(callee(functionDecl(hasName(FunctionName),
98+
callExpr(callee(functionDecl(HasAnyPrecisionName,
9699
hasParameter(0, hasType(isArithmetic())))),
97100
hasArgument(0, ArgumentMatcher))));
98101
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//===--- AmbiguousSmartptrResetCallCheck.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 "AmbiguousSmartptrResetCallCheck.h"
10+
#include "../utils/OptionsUtils.h"
11+
#include "clang/AST/ASTContext.h"
12+
#include "clang/ASTMatchers/ASTMatchFinder.h"
13+
#include "clang/ASTMatchers/ASTMatchers.h"
14+
#include "clang/Lex/Lexer.h"
15+
16+
using namespace clang::ast_matchers;
17+
18+
namespace clang::tidy::readability {
19+
20+
namespace {
21+
22+
AST_MATCHER(CXXMethodDecl, hasOnlyDefaultParameters) {
23+
for (const auto *Param : Node.parameters()) {
24+
if (!Param->hasDefaultArg())
25+
return false;
26+
}
27+
28+
return true;
29+
}
30+
31+
const auto DefaultSmartPointers = "::std::shared_ptr;::std::unique_ptr;"
32+
"::boost::shared_ptr";
33+
} // namespace
34+
35+
AmbiguousSmartptrResetCallCheck::AmbiguousSmartptrResetCallCheck(
36+
StringRef Name, ClangTidyContext *Context)
37+
: ClangTidyCheck(Name, Context),
38+
SmartPointers(utils::options::parseStringList(
39+
Options.get("SmartPointers", DefaultSmartPointers))) {}
40+
41+
void AmbiguousSmartptrResetCallCheck::storeOptions(
42+
ClangTidyOptions::OptionMap &Opts) {
43+
Options.store(Opts, "SmartPointers",
44+
utils::options::serializeStringList(SmartPointers));
45+
}
46+
47+
void AmbiguousSmartptrResetCallCheck::registerMatchers(MatchFinder *Finder) {
48+
const auto IsSmartptr = hasAnyName(SmartPointers);
49+
50+
const auto ResetMethod =
51+
cxxMethodDecl(hasName("reset"), hasOnlyDefaultParameters());
52+
53+
const auto TypeWithReset =
54+
anyOf(cxxRecordDecl(
55+
anyOf(hasMethod(ResetMethod),
56+
isDerivedFrom(cxxRecordDecl(hasMethod(ResetMethod))))),
57+
classTemplateSpecializationDecl(
58+
hasSpecializedTemplate(classTemplateDecl(has(ResetMethod)))));
59+
60+
const auto SmartptrWithReset = expr(hasType(hasUnqualifiedDesugaredType(
61+
recordType(hasDeclaration(classTemplateSpecializationDecl(
62+
IsSmartptr,
63+
hasTemplateArgument(
64+
0, templateArgument(refersToType(hasUnqualifiedDesugaredType(
65+
recordType(hasDeclaration(TypeWithReset))))))))))));
66+
67+
Finder->addMatcher(
68+
cxxMemberCallExpr(
69+
callee(ResetMethod),
70+
unless(hasAnyArgument(expr(unless(cxxDefaultArgExpr())))),
71+
anyOf(on(cxxOperatorCallExpr(hasOverloadedOperatorName("->"),
72+
hasArgument(0, SmartptrWithReset))
73+
.bind("ArrowOp")),
74+
on(SmartptrWithReset)))
75+
.bind("MemberCall"),
76+
this);
77+
}
78+
79+
void AmbiguousSmartptrResetCallCheck::check(
80+
const MatchFinder::MatchResult &Result) {
81+
const auto *MemberCall =
82+
Result.Nodes.getNodeAs<CXXMemberCallExpr>("MemberCall");
83+
assert(MemberCall);
84+
85+
if (const auto *Arrow =
86+
Result.Nodes.getNodeAs<CXXOperatorCallExpr>("ArrowOp")) {
87+
const CharSourceRange SmartptrSourceRange =
88+
Lexer::getAsCharRange(Arrow->getArg(0)->getSourceRange(),
89+
*Result.SourceManager, getLangOpts());
90+
91+
diag(MemberCall->getBeginLoc(),
92+
"ambiguous call to 'reset()' on a pointee of a smart pointer, prefer "
93+
"more explicit approach");
94+
95+
diag(MemberCall->getBeginLoc(),
96+
"consider dereferencing smart pointer to call 'reset' method "
97+
"of the pointee here",
98+
DiagnosticIDs::Note)
99+
<< FixItHint::CreateInsertion(SmartptrSourceRange.getBegin(), "(*")
100+
<< FixItHint::CreateInsertion(SmartptrSourceRange.getEnd(), ")")
101+
<< FixItHint::CreateReplacement(
102+
CharSourceRange::getCharRange(
103+
Arrow->getOperatorLoc(),
104+
Arrow->getOperatorLoc().getLocWithOffset(2)),
105+
".");
106+
} else {
107+
const auto *Member = cast<MemberExpr>(MemberCall->getCallee());
108+
assert(Member);
109+
110+
diag(MemberCall->getBeginLoc(),
111+
"ambiguous call to 'reset()' on a smart pointer with pointee that "
112+
"also has a 'reset()' method, prefer more explicit approach");
113+
114+
diag(MemberCall->getBeginLoc(),
115+
"consider assigning the pointer to 'nullptr' here",
116+
DiagnosticIDs::Note)
117+
<< FixItHint::CreateReplacement(
118+
SourceRange(Member->getOperatorLoc(), Member->getOperatorLoc()),
119+
" =")
120+
<< FixItHint::CreateReplacement(
121+
SourceRange(Member->getMemberLoc(), MemberCall->getEndLoc()),
122+
" nullptr");
123+
}
124+
}
125+
126+
} // namespace clang::tidy::readability
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- AmbiguousSmartptrResetCallCheck.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_READABILITY_AMBIGUOUSSMARTPTRRESETCALLCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AMBIGUOUSSMARTPTRRESETCALLCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::readability {
15+
16+
/// Finds potentially erroneous calls to 'reset' method on smart pointers when
17+
/// the pointee type also has a 'reset' method
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/readability/ambiguous-smartptr-reset-call.html
21+
class AmbiguousSmartptrResetCallCheck : public ClangTidyCheck {
22+
public:
23+
AmbiguousSmartptrResetCallCheck(StringRef Name, ClangTidyContext *Context);
24+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
25+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
26+
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
27+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28+
return LangOpts.CPlusPlus;
29+
}
30+
31+
private:
32+
const std::vector<StringRef> SmartPointers;
33+
};
34+
35+
} // namespace clang::tidy::readability
36+
37+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_AMBIGUOUSSMARTPTRRESETCALLCHECK_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
44
)
55

66
add_clang_library(clangTidyReadabilityModule STATIC
7+
AmbiguousSmartptrResetCallCheck.cpp
78
AvoidConstParamsInDecls.cpp
89
AvoidNestedConditionalOperatorCheck.cpp
910
AvoidReturnWithVoidValueCheck.cpp

clang-tools-extra/clang-tidy/readability/ReadabilityTidyModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../ClangTidy.h"
1010
#include "../ClangTidyModule.h"
1111
#include "../ClangTidyModuleRegistry.h"
12+
#include "AmbiguousSmartptrResetCallCheck.h"
1213
#include "AvoidConstParamsInDecls.h"
1314
#include "AvoidNestedConditionalOperatorCheck.h"
1415
#include "AvoidReturnWithVoidValueCheck.h"
@@ -68,6 +69,8 @@ namespace readability {
6869
class ReadabilityModule : public ClangTidyModule {
6970
public:
7071
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
72+
CheckFactories.registerCheck<AmbiguousSmartptrResetCallCheck>(
73+
"readability-ambiguous-smartptr-reset-call");
7174
CheckFactories.registerCheck<AvoidConstParamsInDecls>(
7275
"readability-avoid-const-params-in-decls");
7376
CheckFactories.registerCheck<AvoidNestedConditionalOperatorCheck>(

clang-tools-extra/clangd/Preamble.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,6 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
673673
// Reset references to ref-counted-ptrs before executing the callbacks, to
674674
// prevent resetting them concurrently.
675675
PreambleDiagsEngine.reset();
676-
CI.DiagnosticOpts.reset();
677676

678677
// When building the AST for the main file, we do want the function
679678
// bodies.

0 commit comments

Comments
 (0)