Skip to content

Commit 5006bfc

Browse files
committed
[AutoBump] Merge with c091dd4 (Jun 15)
2 parents 8fa3c60 + c091dd4 commit 5006bfc

File tree

25 files changed

+1987
-151
lines changed

25 files changed

+1987
-151
lines changed

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

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "ReturnConstRefFromParameterCheck.h"
10-
#include "../utils/Matchers.h"
1110
#include "clang/ASTMatchers/ASTMatchFinder.h"
1211
#include "clang/ASTMatchers/ASTMatchers.h"
1312

@@ -18,20 +17,82 @@ namespace clang::tidy::bugprone {
1817
void ReturnConstRefFromParameterCheck::registerMatchers(MatchFinder *Finder) {
1918
Finder->addMatcher(
2019
returnStmt(
21-
hasReturnValue(declRefExpr(to(parmVarDecl(hasType(hasCanonicalType(
22-
qualType(matchers::isReferenceToConst()).bind("type"))))))),
23-
hasAncestor(functionDecl(hasReturnTypeLoc(
24-
loc(qualType(hasCanonicalType(equalsBoundNode("type"))))))))
20+
hasReturnValue(declRefExpr(
21+
to(parmVarDecl(hasType(hasCanonicalType(
22+
qualType(lValueReferenceType(pointee(
23+
qualType(isConstQualified()))))
24+
.bind("type"))))
25+
.bind("param")))),
26+
hasAncestor(
27+
functionDecl(hasReturnTypeLoc(loc(qualType(
28+
hasCanonicalType(equalsBoundNode("type"))))))
29+
.bind("func")))
2530
.bind("ret"),
2631
this);
2732
}
2833

34+
static bool isSameTypeIgnoringConst(QualType A, QualType B) {
35+
return A.getCanonicalType().withConst() == B.getCanonicalType().withConst();
36+
}
37+
38+
static bool isSameTypeIgnoringConstRef(QualType A, QualType B) {
39+
return isSameTypeIgnoringConst(A.getCanonicalType().getNonReferenceType(),
40+
B.getCanonicalType().getNonReferenceType());
41+
}
42+
43+
static bool hasSameParameterTypes(const FunctionDecl &FD, const FunctionDecl &O,
44+
const ParmVarDecl &PD) {
45+
if (FD.getNumParams() != O.getNumParams())
46+
return false;
47+
for (unsigned I = 0, E = FD.getNumParams(); I < E; ++I) {
48+
const ParmVarDecl *DPD = FD.getParamDecl(I);
49+
const QualType OPT = O.getParamDecl(I)->getType();
50+
if (DPD == &PD) {
51+
if (!llvm::isa<RValueReferenceType>(OPT) ||
52+
!isSameTypeIgnoringConstRef(DPD->getType(), OPT))
53+
return false;
54+
} else {
55+
if (!isSameTypeIgnoringConst(DPD->getType(), OPT))
56+
return false;
57+
}
58+
}
59+
return true;
60+
}
61+
62+
static const Decl *findRVRefOverload(const FunctionDecl &FD,
63+
const ParmVarDecl &PD) {
64+
// Actually it would be better to do lookup in caller site.
65+
// But in most of cases, overloads of LVRef and RVRef will appear together.
66+
// FIXME:
67+
// 1. overload in anonymous namespace
68+
// 2. forward reference
69+
DeclContext::lookup_result LookupResult =
70+
FD.getParent()->lookup(FD.getNameInfo().getName());
71+
if (LookupResult.isSingleResult()) {
72+
return nullptr;
73+
}
74+
for (const Decl *Overload : LookupResult) {
75+
if (Overload == &FD)
76+
continue;
77+
if (const auto *O = dyn_cast<FunctionDecl>(Overload))
78+
if (hasSameParameterTypes(FD, *O, PD))
79+
return O;
80+
}
81+
return nullptr;
82+
}
83+
2984
void ReturnConstRefFromParameterCheck::check(
3085
const MatchFinder::MatchResult &Result) {
86+
const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func");
87+
const auto *PD = Result.Nodes.getNodeAs<ParmVarDecl>("param");
3188
const auto *R = Result.Nodes.getNodeAs<ReturnStmt>("ret");
3289
const SourceRange Range = R->getRetValue()->getSourceRange();
3390
if (Range.isInvalid())
3491
return;
92+
93+
if (findRVRefOverload(*FD, *PD) != nullptr)
94+
return;
95+
3596
diag(Range.getBegin(),
3697
"returning a constant reference parameter may cause use-after-free "
3798
"when the parameter is constructed from a temporary")

clang-tools-extra/test/clang-tidy/checkers/bugprone/return-const-ref-from-parameter.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,37 @@ void instantiate(const int &param, const float &paramf, int &mut_param, float &m
143143
}
144144

145145
} // namespace valid
146+
147+
namespace overload {
148+
149+
int const &overload_base(int const &a) { return a; }
150+
int const &overload_base(int &&a);
151+
152+
int const &overload_ret_type(int const &a) { return a; }
153+
void overload_ret_type(int &&a);
154+
155+
int const &overload_params1(int p1, int const &a) { return a; }
156+
int const & overload_params1(int p1, int &&a);
157+
158+
int const &overload_params2(int p1, int const &a, int p2) { return a; }
159+
int const &overload_params2(int p1, int &&a, int p2);
160+
161+
int const &overload_params3(T p1, int const &a, int p2) { return a; }
162+
int const &overload_params3(int p1, int &&a, T p2);
163+
164+
int const &overload_params_const(int p1, int const &a, int const p2) { return a; }
165+
int const &overload_params_const(int const p1, int &&a, int p2);
166+
167+
int const &overload_params_difference1(int p1, int const &a, int p2) { return a; }
168+
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter
169+
int const &overload_params_difference1(long p1, int &&a, int p2);
170+
171+
int const &overload_params_difference2(int p1, int const &a, int p2) { return a; }
172+
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter
173+
int const &overload_params_difference2(int p1, int &&a, long p2);
174+
175+
int const &overload_params_difference3(int p1, int const &a, int p2) { return a; }
176+
// CHECK-MESSAGES: :[[@LINE-1]]:79: warning: returning a constant reference parameter
177+
int const &overload_params_difference3(int p1, long &&a, int p2);
178+
179+
} // namespace overload

flang/lib/Optimizer/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_flang_library(FIRCodeGen
2727
MLIRMathToLLVM
2828
MLIRMathToLibm
2929
MLIROpenMPToLLVM
30+
MLIROpenACCDialect
3031
MLIRBuiltinToLLVMIRTranslation
3132
MLIRLLVMToLLVMIRTranslation
3233
MLIRTargetLLVMIRExport

libc/src/__support/macros/attributes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#define LIBC_CONSTINIT
4343
#endif
4444

45-
#ifdef __clang__
45+
#if defined(__clang__) && __has_attribute(preferred_type)
4646
#define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]]
4747
#else
4848
#define LIBC_PREFERED_TYPE(TYPE)

libc/src/__support/threads/linux/rwlock.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class WaitingQueue final : private RawMutex {
6363
WaitingQueue &queue;
6464
bool is_pshared;
6565

66-
LIBC_INLINE constexpr Guard(WaitingQueue &queue, bool is_pshared)
66+
LIBC_INLINE Guard(WaitingQueue &queue, bool is_pshared)
6767
: queue(queue), is_pshared(is_pshared) {
6868
queue.lock(cpp::nullopt, is_pshared);
6969
}
@@ -189,6 +189,7 @@ class RwState {
189189
case Role::Writer:
190190
return !has_active_writer() && !has_pending_writer();
191191
}
192+
__builtin_unreachable();
192193
} else
193194
return !has_acitve_owner();
194195
}
@@ -370,8 +371,9 @@ class RwLock {
370371
public:
371372
LIBC_INLINE constexpr RwLock(Role preference = Role::Reader,
372373
bool is_pshared = false)
373-
: is_pshared(is_pshared), preference(static_cast<unsigned>(preference)),
374-
state(0), writer_tid(0), queue() {}
374+
: is_pshared(is_pshared),
375+
preference(static_cast<unsigned>(preference) & 1u), state(0),
376+
writer_tid(0), queue() {}
375377

376378
[[nodiscard]]
377379
LIBC_INLINE LockResult try_read_lock() {

libc/src/pthread/pthread_rwlock_timedrdlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ LLVM_LIBC_FUNCTION(int, pthread_rwlock_timedrdlock,
4242
return EINVAL;
4343
case internal::AbsTimeout::Error::BeforeEpoch:
4444
return ETIMEDOUT;
45-
// default: unreachable, all two cases are covered.
4645
}
46+
__builtin_unreachable();
4747
}
4848

4949
} // namespace LIBC_NAMESPACE

libc/src/pthread/pthread_rwlock_timedwrlock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ LLVM_LIBC_FUNCTION(int, pthread_rwlock_timedwrlock,
3636
return EINVAL;
3737
case internal::AbsTimeout::Error::BeforeEpoch:
3838
return ETIMEDOUT;
39-
// default: unreachable, all two cases are covered.
4039
}
40+
__builtin_unreachable();
4141
}
4242

4343
} // namespace LIBC_NAMESPACE

libc/test/integration/src/pthread/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ add_integration_test(
5353
libc.src.sys.wait.waitpid
5454
libc.src.stdlib.exit
5555
libc.src.__support.CPP.atomic
56+
libc.src.__support.CPP.new
5657
libc.src.__support.threads.sleep
5758
)
5859

libc/test/integration/src/pthread/pthread_rwlock_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "src/__support/CPP/atomic.h"
10+
#include "src/__support/CPP/new.h"
1011
#include "src/__support/OSUtil/syscall.h"
1112
#include "src/__support/threads/linux/raw_mutex.h"
1213
#include "src/__support/threads/linux/rwlock.h"
@@ -37,7 +38,6 @@
3738
#include "src/unistd/fork.h"
3839
#include "test/IntegrationTest/test.h"
3940
#include <errno.h>
40-
#include <optional>
4141
#include <pthread.h>
4242
#include <time.h>
4343

lldb/include/lldb/Utility/SupportFile.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@ class SupportFile {
3030

3131
virtual ~SupportFile() = default;
3232

33+
/// Return true if both SupportFiles have the same FileSpec and, if both have
34+
/// a valid Checksum, the Checksum is the same.
3335
bool operator==(const SupportFile &other) const {
34-
return m_file_spec == other.m_file_spec && m_checksum == other.m_checksum;
36+
if (m_checksum && other.m_checksum)
37+
return m_file_spec == other.m_file_spec && m_checksum == other.m_checksum;
38+
return m_file_spec == other.m_file_spec;
3539
}
3640

3741
bool operator!=(const SupportFile &other) const { return !(*this == other); }

0 commit comments

Comments
 (0)