Skip to content

Commit 473947d

Browse files
Merge branch 'llvm:main' into gh-101657
2 parents 0ad10dc + 8326fb2 commit 473947d

File tree

1,373 files changed

+26388
-20976
lines changed

Some content is hidden

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

1,373 files changed

+26388
-20976
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,18 @@ getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers) {
672672
Buffer.append(AnalyzerCheck);
673673
Result.Names.insert(Buffer);
674674
}
675+
for (std::string OptionName : {
676+
#define GET_CHECKER_OPTIONS
677+
#define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT, \
678+
RELEASE, HIDDEN) \
679+
Twine(AnalyzerCheckNamePrefix).concat(CHECKER ":" OPTION_NAME).str(),
680+
681+
#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
682+
#undef CHECKER_OPTION
683+
#undef GET_CHECKER_OPTIONS
684+
}) {
685+
Result.Options.insert(OptionName);
686+
}
675687
#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER
676688

677689
Context.setOptionsCollector(&Result.Options);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- BitwisePointerCastCheck.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 "BitwisePointerCastCheck.h"
10+
#include "clang/ASTMatchers/ASTMatchFinder.h"
11+
12+
using namespace clang::ast_matchers;
13+
14+
namespace clang::tidy::bugprone {
15+
16+
void BitwisePointerCastCheck::registerMatchers(MatchFinder *Finder) {
17+
if (getLangOpts().CPlusPlus20) {
18+
auto IsPointerType = refersToType(qualType(isAnyPointer()));
19+
Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
20+
hasName("::std::bit_cast"),
21+
hasTemplateArgument(0, IsPointerType),
22+
hasTemplateArgument(1, IsPointerType)))))
23+
.bind("bit_cast"),
24+
this);
25+
}
26+
27+
auto IsDoublePointerType =
28+
hasType(qualType(pointsTo(qualType(isAnyPointer()))));
29+
Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
30+
hasArgument(1, IsDoublePointerType),
31+
hasDeclaration(functionDecl(hasName("::memcpy"))))
32+
.bind("memcpy"),
33+
this);
34+
}
35+
36+
void BitwisePointerCastCheck::check(const MatchFinder::MatchResult &Result) {
37+
if (const auto *Call = Result.Nodes.getNodeAs<CallExpr>("bit_cast"))
38+
diag(Call->getBeginLoc(),
39+
"do not use 'std::bit_cast' to cast between pointers")
40+
<< Call->getSourceRange();
41+
else if (const auto *Call = Result.Nodes.getNodeAs<CallExpr>("memcpy"))
42+
diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers")
43+
<< Call->getSourceRange();
44+
}
45+
46+
} // namespace clang::tidy::bugprone
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===--- BitwisePointerCastCheck.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_BUGPRONE_BITWISEPOINTERCASTCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
11+
12+
#include "../ClangTidyCheck.h"
13+
14+
namespace clang::tidy::bugprone {
15+
16+
/// Warns about code that tries to cast between pointers by means of
17+
/// ``std::bit_cast`` or ``memcpy``.
18+
///
19+
/// For the user-facing documentation see:
20+
/// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html
21+
class BitwisePointerCastCheck : public ClangTidyCheck {
22+
public:
23+
BitwisePointerCastCheck(StringRef Name, ClangTidyContext *Context)
24+
: ClangTidyCheck(Name, Context) {}
25+
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
26+
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
27+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
28+
return LangOpts.CPlusPlus;
29+
}
30+
};
31+
32+
} // namespace clang::tidy::bugprone
33+
34+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "AssertSideEffectCheck.h"
1515
#include "AssignmentInIfConditionCheck.h"
1616
#include "BadSignalToKillThreadCheck.h"
17+
#include "BitwisePointerCastCheck.h"
1718
#include "BoolPointerImplicitConversionCheck.h"
1819
#include "BranchCloneCheck.h"
1920
#include "CastingThroughVoidCheck.h"
@@ -109,6 +110,8 @@ class BugproneModule : public ClangTidyModule {
109110
"bugprone-assignment-in-if-condition");
110111
CheckFactories.registerCheck<BadSignalToKillThreadCheck>(
111112
"bugprone-bad-signal-to-kill-thread");
113+
CheckFactories.registerCheck<BitwisePointerCastCheck>(
114+
"bugprone-bitwise-pointer-cast");
112115
CheckFactories.registerCheck<BoolPointerImplicitConversionCheck>(
113116
"bugprone-bool-pointer-implicit-conversion");
114117
CheckFactories.registerCheck<BranchCloneCheck>("bugprone-branch-clone");

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_clang_library(clangTidyBugproneModule
88
AssertSideEffectCheck.cpp
99
AssignmentInIfConditionCheck.cpp
1010
BadSignalToKillThreadCheck.cpp
11+
BitwisePointerCastCheck.cpp
1112
BoolPointerImplicitConversionCheck.cpp
1213
BranchCloneCheck.cpp
1314
BugproneTidyModule.cpp

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
777777
const LambdaCapture *C,
778778
Expr *Init) {
779779
if (C->capturesVariable()) {
780-
const ValueDecl *VDecl = C->getCapturedVar();
780+
ValueDecl *VDecl = C->getCapturedVar();
781781
if (areSameVariable(IndexVar, VDecl)) {
782782
// FIXME: if the index is captured, it will count as an usage and the
783783
// alias (if any) won't work, because it is only used in case of having
@@ -787,6 +787,8 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
787787
: Usage::UK_CaptureByRef,
788788
C->getLocation()));
789789
}
790+
if (VDecl->isInitCapture())
791+
TraverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
790792
}
791793
return VisitorBase::TraverseLambdaCapture(LE, C, Init);
792794
}
@@ -816,6 +818,17 @@ bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
816818
return true;
817819
}
818820

821+
bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) {
822+
// All this pointer swapping is a mechanism for tracking immediate parentage
823+
// of Stmts.
824+
const Stmt *OldNextParent = NextStmtParent;
825+
CurrStmtParent = NextStmtParent;
826+
NextStmtParent = S;
827+
bool Result = VisitorBase::TraverseStmt(S);
828+
NextStmtParent = OldNextParent;
829+
return Result;
830+
}
831+
819832
bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
820833
// If this is an initialization expression for a lambda capture, prune the
821834
// traversal so that we don't end up diagnosing the contained DeclRefExpr as
@@ -828,15 +841,7 @@ bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
828841
return true;
829842
}
830843
}
831-
832-
// All this pointer swapping is a mechanism for tracking immediate parentage
833-
// of Stmts.
834-
const Stmt *OldNextParent = NextStmtParent;
835-
CurrStmtParent = NextStmtParent;
836-
NextStmtParent = S;
837-
bool Result = VisitorBase::TraverseStmt(S);
838-
NextStmtParent = OldNextParent;
839-
return Result;
844+
return TraverseStmtImpl(S);
840845
}
841846

842847
std::string VariableNamer::createIndexName() {

clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ class ForLoopIndexUseVisitor
354354
bool VisitDeclStmt(DeclStmt *S);
355355
bool TraverseStmt(Stmt *S);
356356

357+
bool TraverseStmtImpl(Stmt *S);
358+
357359
/// Add an expression to the list of expressions on which the container
358360
/// expression depends.
359361
void addComponent(const Expr *E);

clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_clang_library(clangDaemonTweaks OBJECT
2929
RemoveUsingNamespace.cpp
3030
ScopifyEnum.cpp
3131
SpecialMembers.cpp
32+
SwapBinaryOperands.cpp
3233
SwapIfBranches.cpp
3334

3435
LINK_LIBS

0 commit comments

Comments
 (0)