Skip to content

Commit

Permalink
merge main into amd-staging
Browse files Browse the repository at this point in the history
Catches us up, but there are issues:

this xfails 3 tests
llvm/test/CodeGen/AMDGPU/tail-call-inreg-arguments.error.ll
clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p1.cpp
clang/test/CXX/temp/temp.spec/temp.expl.spec/p12.cpp

fixme section in llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

Change-Id: Ia1367740343fba1dd5e4d46f6ffb4a56c2ca8600
  • Loading branch information
ronlieb committed Oct 4, 2024
2 parents 10bf768 + 7b9c6a7 commit ec6d37a
Show file tree
Hide file tree
Showing 1,079 changed files with 25,902 additions and 14,065 deletions.
12 changes: 12 additions & 0 deletions clang-tools-extra/clang-tidy/ClangTidy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,18 @@ getAllChecksAndOptions(bool AllowEnablingAnalyzerAlphaCheckers) {
Buffer.append(AnalyzerCheck);
Result.Names.insert(Buffer);
}
for (std::string OptionName : {
#define GET_CHECKER_OPTIONS
#define CHECKER_OPTION(TYPE, CHECKER, OPTION_NAME, DESCRIPTION, DEFAULT, \
RELEASE, HIDDEN) \
Twine(AnalyzerCheckNamePrefix).concat(CHECKER ":" OPTION_NAME).str(),

#include "clang/StaticAnalyzer/Checkers/Checkers.inc"
#undef CHECKER_OPTION
#undef GET_CHECKER_OPTIONS
}) {
Result.Options.insert(OptionName);
}
#endif // CLANG_TIDY_ENABLE_STATIC_ANALYZER

Context.setOptionsCollector(&Result.Options);
Expand Down
25 changes: 15 additions & 10 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
const LambdaCapture *C,
Expr *Init) {
if (C->capturesVariable()) {
const ValueDecl *VDecl = C->getCapturedVar();
ValueDecl *VDecl = C->getCapturedVar();
if (areSameVariable(IndexVar, VDecl)) {
// FIXME: if the index is captured, it will count as an usage and the
// alias (if any) won't work, because it is only used in case of having
Expand All @@ -787,6 +787,8 @@ bool ForLoopIndexUseVisitor::TraverseLambdaCapture(LambdaExpr *LE,
: Usage::UK_CaptureByRef,
C->getLocation()));
}
if (VDecl->isInitCapture())
TraverseStmtImpl(cast<VarDecl>(VDecl)->getInit());
}
return VisitorBase::TraverseLambdaCapture(LE, C, Init);
}
Expand Down Expand Up @@ -816,6 +818,17 @@ bool ForLoopIndexUseVisitor::VisitDeclStmt(DeclStmt *S) {
return true;
}

bool ForLoopIndexUseVisitor::TraverseStmtImpl(Stmt *S) {
// All this pointer swapping is a mechanism for tracking immediate parentage
// of Stmts.
const Stmt *OldNextParent = NextStmtParent;
CurrStmtParent = NextStmtParent;
NextStmtParent = S;
bool Result = VisitorBase::TraverseStmt(S);
NextStmtParent = OldNextParent;
return Result;
}

bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
// If this is an initialization expression for a lambda capture, prune the
// traversal so that we don't end up diagnosing the contained DeclRefExpr as
Expand All @@ -828,15 +841,7 @@ bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
return true;
}
}

// All this pointer swapping is a mechanism for tracking immediate parentage
// of Stmts.
const Stmt *OldNextParent = NextStmtParent;
CurrStmtParent = NextStmtParent;
NextStmtParent = S;
bool Result = VisitorBase::TraverseStmt(S);
NextStmtParent = OldNextParent;
return Result;
return TraverseStmtImpl(S);
}

std::string VariableNamer::createIndexName() {
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ class ForLoopIndexUseVisitor
bool VisitDeclStmt(DeclStmt *S);
bool TraverseStmt(Stmt *S);

bool TraverseStmtImpl(Stmt *S);

/// Add an expression to the list of expressions on which the container
/// expression depends.
void addComponent(const Expr *E);
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/refactor/tweaks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_clang_library(clangDaemonTweaks OBJECT
RemoveUsingNamespace.cpp
ScopifyEnum.cpp
SpecialMembers.cpp
SwapBinaryOperands.cpp
SwapIfBranches.cpp

LINK_LIBS
Expand Down
216 changes: 216 additions & 0 deletions clang-tools-extra/clangd/refactor/tweaks/SwapBinaryOperands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
//===--- SwapBinaryOperands.cpp ----------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ParsedAST.h"
#include "Protocol.h"
#include "Selection.h"
#include "SourceCode.h"
#include "refactor/Tweak.h"
#include "support/Logger.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Expr.h"
#include "clang/AST/OperationKinds.h"
#include "clang/AST/Stmt.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/FormatVariadic.h"
#include <string>
#include <utility>

namespace clang {
namespace clangd {
namespace {
/// Check whether it makes logical sense to swap operands to an operator.
/// Assignment or member access operators are rarely swappable
/// while keeping the meaning intact, whereas comparison operators, mathematical
/// operators, etc. are often desired to be swappable for readability, avoiding
/// bugs by assigning to nullptr when comparison was desired, etc.
bool isOpSwappable(const BinaryOperatorKind Opcode) {
switch (Opcode) {
case BinaryOperatorKind::BO_Mul:
case BinaryOperatorKind::BO_Add:
case BinaryOperatorKind::BO_LT:
case BinaryOperatorKind::BO_GT:
case BinaryOperatorKind::BO_LE:
case BinaryOperatorKind::BO_GE:
case BinaryOperatorKind::BO_EQ:
case BinaryOperatorKind::BO_NE:
case BinaryOperatorKind::BO_And:
case BinaryOperatorKind::BO_Xor:
case BinaryOperatorKind::BO_Or:
case BinaryOperatorKind::BO_LAnd:
case BinaryOperatorKind::BO_LOr:
case BinaryOperatorKind::BO_Comma:
return true;
// Noncommutative operators:
case BinaryOperatorKind::BO_Div:
case BinaryOperatorKind::BO_Sub:
case BinaryOperatorKind::BO_Shl:
case BinaryOperatorKind::BO_Shr:
case BinaryOperatorKind::BO_Rem:
// <=> is noncommutative
case BinaryOperatorKind::BO_Cmp:
// Member access:
case BinaryOperatorKind::BO_PtrMemD:
case BinaryOperatorKind::BO_PtrMemI:
// Assignment:
case BinaryOperatorKind::BO_Assign:
case BinaryOperatorKind::BO_MulAssign:
case BinaryOperatorKind::BO_DivAssign:
case BinaryOperatorKind::BO_RemAssign:
case BinaryOperatorKind::BO_AddAssign:
case BinaryOperatorKind::BO_SubAssign:
case BinaryOperatorKind::BO_ShlAssign:
case BinaryOperatorKind::BO_ShrAssign:
case BinaryOperatorKind::BO_AndAssign:
case BinaryOperatorKind::BO_XorAssign:
case BinaryOperatorKind::BO_OrAssign:
return false;
}
return false;
}

/// Some operators are asymmetric and need to be flipped when swapping their
/// operands
/// @param[out] Opcode the opcode to potentially swap
/// If the opcode does not need to be swapped or is not swappable, does nothing
BinaryOperatorKind swapOperator(const BinaryOperatorKind Opcode) {
switch (Opcode) {
case BinaryOperatorKind::BO_LT:
return BinaryOperatorKind::BO_GT;

case BinaryOperatorKind::BO_GT:
return BinaryOperatorKind::BO_LT;

case BinaryOperatorKind::BO_LE:
return BinaryOperatorKind::BO_GE;

case BinaryOperatorKind::BO_GE:
return BinaryOperatorKind::BO_LE;

case BinaryOperatorKind::BO_Mul:
case BinaryOperatorKind::BO_Add:
case BinaryOperatorKind::BO_Cmp:
case BinaryOperatorKind::BO_EQ:
case BinaryOperatorKind::BO_NE:
case BinaryOperatorKind::BO_And:
case BinaryOperatorKind::BO_Xor:
case BinaryOperatorKind::BO_Or:
case BinaryOperatorKind::BO_LAnd:
case BinaryOperatorKind::BO_LOr:
case BinaryOperatorKind::BO_Comma:
case BinaryOperatorKind::BO_Div:
case BinaryOperatorKind::BO_Sub:
case BinaryOperatorKind::BO_Shl:
case BinaryOperatorKind::BO_Shr:
case BinaryOperatorKind::BO_Rem:
case BinaryOperatorKind::BO_PtrMemD:
case BinaryOperatorKind::BO_PtrMemI:
case BinaryOperatorKind::BO_Assign:
case BinaryOperatorKind::BO_MulAssign:
case BinaryOperatorKind::BO_DivAssign:
case BinaryOperatorKind::BO_RemAssign:
case BinaryOperatorKind::BO_AddAssign:
case BinaryOperatorKind::BO_SubAssign:
case BinaryOperatorKind::BO_ShlAssign:
case BinaryOperatorKind::BO_ShrAssign:
case BinaryOperatorKind::BO_AndAssign:
case BinaryOperatorKind::BO_XorAssign:
case BinaryOperatorKind::BO_OrAssign:
return Opcode;
}
}

/// Swaps the operands to a binary operator
/// Before:
/// x != nullptr
/// ^ ^^^^^^^
/// After:
/// nullptr != x
class SwapBinaryOperands : public Tweak {
public:
const char *id() const final;

bool prepare(const Selection &Inputs) override;
Expected<Effect> apply(const Selection &Inputs) override;
std::string title() const override {
return llvm::formatv("Swap operands to {0}",
Op ? Op->getOpcodeStr() : "binary operator");
}
llvm::StringLiteral kind() const override {
return CodeAction::REFACTOR_KIND;
}
bool hidden() const override { return false; }

private:
const BinaryOperator *Op;
};

REGISTER_TWEAK(SwapBinaryOperands)

bool SwapBinaryOperands::prepare(const Selection &Inputs) {
for (const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
N && !Op; N = N->Parent) {
// Stop once we hit a block, e.g. a lambda in one of the operands.
// This makes sure that the selection point is in the 'scope' of the binary
// operator, not from somewhere inside a lambda for example
// (5 < [](){ ^return 1; })
if (llvm::isa_and_nonnull<CompoundStmt>(N->ASTNode.get<Stmt>()))
return false;
Op = dyn_cast_or_null<BinaryOperator>(N->ASTNode.get<Stmt>());
// If we hit upon a nonswappable binary operator, ignore and keep going
if (Op && !isOpSwappable(Op->getOpcode())) {
Op = nullptr;
}
}
return Op != nullptr;
}

Expected<Tweak::Effect> SwapBinaryOperands::apply(const Selection &Inputs) {
const auto &Ctx = Inputs.AST->getASTContext();
const auto &SrcMgr = Inputs.AST->getSourceManager();

const auto LHSRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
Op->getLHS()->getSourceRange());
if (!LHSRng)
return error(
"Could not obtain range of the 'lhs' of the operator. Macros?");
const auto RHSRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
Op->getRHS()->getSourceRange());
if (!RHSRng)
return error(
"Could not obtain range of the 'rhs' of the operator. Macros?");
const auto OpRng =
toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(), Op->getOperatorLoc());
if (!OpRng)
return error("Could not obtain range of the operator itself. Macros?");

const auto LHSCode = toSourceCode(SrcMgr, *LHSRng);
const auto RHSCode = toSourceCode(SrcMgr, *RHSRng);
const auto OperatorCode = toSourceCode(SrcMgr, *OpRng);

tooling::Replacements Result;
if (auto Err = Result.add(tooling::Replacement(
Ctx.getSourceManager(), LHSRng->getBegin(), LHSCode.size(), RHSCode)))
return std::move(Err);
if (auto Err = Result.add(tooling::Replacement(
Ctx.getSourceManager(), RHSRng->getBegin(), RHSCode.size(), LHSCode)))
return std::move(Err);
const auto SwappedOperator = swapOperator(Op->getOpcode());
if (auto Err = Result.add(tooling::Replacement(
Ctx.getSourceManager(), OpRng->getBegin(), OperatorCode.size(),
Op->getOpcodeStr(SwappedOperator))))
return std::move(Err);
return Effect::mainFileEdit(SrcMgr, std::move(Result));
}

} // namespace
} // namespace clangd
} // namespace clang
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ add_unittest(ClangdUnitTests ClangdTests
tweaks/ScopifyEnumTests.cpp
tweaks/ShowSelectionTreeTests.cpp
tweaks/SpecialMembersTests.cpp
tweaks/SwapBinaryOperandsTests.cpp
tweaks/SwapIfBranchesTests.cpp
tweaks/TweakTesting.cpp
tweaks/TweakTests.cpp
Expand Down
Loading

0 comments on commit ec6d37a

Please sign in to comment.