Skip to content

Commit 1042f68

Browse files
authored
Merge branch 'main' into hgh/libcxx/P2944R3-optional-constrained-equality-part_2
2 parents 1f314a7 + 79da5fe commit 1042f68

File tree

479 files changed

+11071
-5051
lines changed

Some content is hidden

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

479 files changed

+11071
-5051
lines changed

bolt/include/bolt/Core/BinarySection.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -523,11 +523,6 @@ inline uint8_t *copyByteArray(const uint8_t *Data, uint64_t Size) {
523523
return Array;
524524
}
525525

526-
inline uint8_t *copyByteArray(StringRef Buffer) {
527-
return copyByteArray(reinterpret_cast<const uint8_t *>(Buffer.data()),
528-
Buffer.size());
529-
}
530-
531526
inline uint8_t *copyByteArray(ArrayRef<char> Buffer) {
532527
return copyByteArray(reinterpret_cast<const uint8_t *>(Buffer.data()),
533528
Buffer.size());

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ SizeofExpressionCheck::SizeofExpressionCheck(StringRef Name,
7272
Options.get("WarnOnSizeOfPointerToAggregate", true)),
7373
WarnOnSizeOfPointer(Options.get("WarnOnSizeOfPointer", false)),
7474
WarnOnOffsetDividedBySizeOf(
75-
Options.get("WarnOnOffsetDividedBySizeOf", true)) {}
75+
Options.get("WarnOnOffsetDividedBySizeOf", true)),
76+
WarnOnSizeOfInLoopTermination(
77+
Options.get("WarnOnSizeOfInLoopTermination", true)) {}
7678

7779
void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
7880
Options.store(Opts, "WarnOnSizeOfConstant", WarnOnSizeOfConstant);
@@ -86,13 +88,22 @@ void SizeofExpressionCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
8688
Options.store(Opts, "WarnOnSizeOfPointer", WarnOnSizeOfPointer);
8789
Options.store(Opts, "WarnOnOffsetDividedBySizeOf",
8890
WarnOnOffsetDividedBySizeOf);
91+
Options.store(Opts, "WarnOnSizeOfInLoopTermination",
92+
WarnOnSizeOfInLoopTermination);
8993
}
9094

9195
void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
9296
// FIXME:
9397
// Some of the checks should not match in template code to avoid false
9498
// positives if sizeof is applied on template argument.
9599

100+
auto LoopCondExpr =
101+
[](const ast_matchers::internal::Matcher<Stmt> &InnerMatcher) {
102+
return stmt(anyOf(forStmt(hasCondition(InnerMatcher)),
103+
whileStmt(hasCondition(InnerMatcher)),
104+
doStmt(hasCondition(InnerMatcher))));
105+
};
106+
96107
const auto IntegerExpr = ignoringParenImpCasts(integerLiteral());
97108
const auto ConstantExpr = ignoringParenImpCasts(
98109
anyOf(integerLiteral(), unaryOperator(hasUnaryOperand(IntegerExpr)),
@@ -130,6 +141,14 @@ void SizeofExpressionCheck::registerMatchers(MatchFinder *Finder) {
130141
this);
131142
}
132143

144+
if (WarnOnSizeOfInLoopTermination) {
145+
auto CondExpr = binaryOperator(
146+
allOf(has(SizeOfExpr.bind("sizeof-expr")), isComparisonOperator()));
147+
Finder->addMatcher(LoopCondExpr(anyOf(CondExpr, hasDescendant(CondExpr)))
148+
.bind("loop-expr"),
149+
this);
150+
}
151+
133152
// Detect sizeof(kPtr) where kPtr is 'const char* kPtr = "abc"';
134153
const auto CharPtrType = pointerType(pointee(isAnyCharacter()));
135154
const auto ConstStrLiteralDecl =
@@ -349,6 +368,23 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
349368
diag(E->getBeginLoc(),
350369
"suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
351370
<< E->getSourceRange();
371+
} else if (Result.Nodes.getNodeAs<Stmt>("loop-expr")) {
372+
auto *SizeofArgTy = Result.Nodes.getNodeAs<Type>("sizeof-arg-type");
373+
if (const auto member = dyn_cast<MemberPointerType>(SizeofArgTy))
374+
SizeofArgTy = member->getPointeeType().getTypePtr();
375+
376+
const auto *SzOfExpr = Result.Nodes.getNodeAs<Expr>("sizeof-expr");
377+
378+
if (const auto type = dyn_cast<ArrayType>(SizeofArgTy)) {
379+
// check if the array element size is larger than one. If true,
380+
// the size of the array is higher than the number of elements
381+
CharUnits sSize = Ctx.getTypeSizeInChars(type->getElementType());
382+
if (!sSize.isOne()) {
383+
diag(SzOfExpr->getBeginLoc(),
384+
"suspicious usage of 'sizeof' in the loop")
385+
<< SzOfExpr->getSourceRange();
386+
}
387+
}
352388
} else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) {
353389
diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
354390
"of pointer type")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SizeofExpressionCheck : public ClangTidyCheck {
3232
const bool WarnOnSizeOfPointerToAggregate;
3333
const bool WarnOnSizeOfPointer;
3434
const bool WarnOnOffsetDividedBySizeOf;
35+
const bool WarnOnSizeOfInLoopTermination;
3536
};
3637

3738
} // namespace clang::tidy::bugprone

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ Changes in existing checks
173173
<clang-tidy/checks/bugprone/optional-value-conversion>` check to detect
174174
conversion in argument of ``std::make_optional``.
175175

176+
- Improved :doc: `bugprone-sizeof-expression
177+
<clang-tidy/checks/bugprone/bugprone-sizeof-expression>` check by adding
178+
`WarnOnSizeOfInLoopTermination` option to detect misuses of ``sizeof``
179+
expression in loop conditions.
180+
176181
- Improved :doc:`bugprone-string-constructor
177182
<clang-tidy/checks/bugprone/string-constructor>` check to find suspicious
178183
calls of ``std::string`` constructor with char pointer, start position and

clang-tools-extra/docs/clang-tidy/checks/bugprone/sizeof-expression.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,12 @@ Options
316316
When `true`, the check will warn on pointer arithmetic where the
317317
element count is obtained from a division with ``sizeof(...)``,
318318
e.g., ``Ptr + Bytes / sizeof(*T)``. Default is `true`.
319+
320+
.. option:: WarnOnSizeOfInLoopTermination
321+
322+
When `true`, the check will warn about incorrect use of sizeof expression
323+
in loop termination condition. The warning triggers if the ``sizeof``
324+
expression appears to be incorrectly used to determine the number of
325+
array/buffer elements.
326+
e.g, ``long arr[10]; for(int i = 0; i < sizeof(arr); i++) { ... }``. Default
327+
is `true`.

clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,69 @@ int Test2(MyConstChar* A) {
164164
return sum;
165165
}
166166

167+
struct A {
168+
int array[10];
169+
};
170+
171+
struct B {
172+
struct A a;
173+
};
174+
175+
void loop_access_elements(int num, struct B b) {
176+
struct A arr[10];
177+
char buf[20];
178+
179+
// CHECK-MESSAGES: :[[@LINE+1]]:22: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
180+
for(int i = 0; i < sizeof(arr); i++) {
181+
struct A a = arr[i];
182+
}
183+
184+
// Loop warning should not trigger here, even though this code is incorrect
185+
// CHECK-MESSAGES: :[[@LINE+2]]:22: warning: suspicious usage of 'sizeof(K)'; did you mean 'K'? [bugprone-sizeof-expression]
186+
// CHECK-MESSAGES: :[[@LINE+1]]:32: warning: suspicious usage of 'sizeof(...)/sizeof(...)'; numerator is not a multiple of denominator [bugprone-sizeof-expression]
187+
for(int i = 0; i < sizeof(10)/sizeof(A); i++) {
188+
struct A a = arr[i];
189+
}
190+
191+
// Should not warn here
192+
for(int i = 0; i < sizeof(arr)/sizeof(A); i++) {}
193+
194+
// Should not warn here
195+
for (int i = 0; i < 10; i++) {
196+
if (sizeof(arr) != 0) {
197+
198+
}
199+
}
200+
201+
for (int i = 0; i < 10; i++) {
202+
// CHECK-MESSAGES: :[[@LINE+1]]:25: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
203+
for (int j = 0; j < sizeof(arr); j++) {
204+
}
205+
}
206+
207+
// CHECK-MESSAGES: :[[@LINE+1]]:22: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
208+
for(int j = 0; j < sizeof(b.a.array); j++) {}
209+
210+
// Should not warn here
211+
for(int i = 0; i < sizeof(buf); i++) {}
212+
213+
// Should not warn here
214+
for(int i = 0; i < (sizeof(arr) << 3); i++) {}
215+
216+
int i = 0;
217+
// CHECK-MESSAGES: :[[@LINE+1]]:14: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
218+
while(i <= sizeof(arr)) {i++;}
219+
220+
i = 0;
221+
do {
222+
i++;
223+
// CHECK-MESSAGES: :[[@LINE+1]]:16: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
224+
} while(i <= sizeof(arr));
225+
226+
// CHECK-MESSAGES: :[[@LINE+1]]:29: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
227+
for(int i = 0, j = 0; i < sizeof(arr) && j < sizeof(buf); i++, j++) {}
228+
}
229+
167230
template <int T>
168231
int Foo() { int A[T]; return sizeof(T); }
169232
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: suspicious usage of 'sizeof(K)'

clang/bindings/python/tests/cindex/test_file.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
inputs_dir = os.path.join(os.path.dirname(__file__), "INPUTS")
1111

12+
1213
class TestFile(unittest.TestCase):
1314
def test_file(self):
1415
index = Index.create()

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ Non-comprehensive list of changes in this release
329329
``__reference_constructs_from_temporary`` should be used instead. (#GH44056)
330330
- Added `__builtin_get_vtable_pointer` to directly load the primary vtable pointer from a
331331
polymorphic object.
332+
- ``libclang`` receives a family of new bindings to query basic facts about
333+
GCC-style inline assembly blocks, including whether the block is ``volatile``
334+
and its template string following the LLVM IR ``asm`` format. (#GH143424)
332335
- Clang no longer rejects reinterpret_cast conversions between indirect
333336
ARC-managed pointers and other pointer types. The prior behavior was overly
334337
strict and inconsistent with the ARC specification.
@@ -644,7 +647,7 @@ Improvements to Clang's diagnostics
644647
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
645648
#GH36703, #GH32903, #GH23312, #GH69874.
646649

647-
650+
648651
Improvements to Clang's time-trace
649652
----------------------------------
650653

clang/include/clang-c/Index.h

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define CINDEX_VERSION_MAJOR 0
3737
#define CINDEX_VERSION_MINOR 64
3838

39-
#define CINDEX_VERSION_ENCODE(major, minor) (((major)*10000) + ((minor)*1))
39+
#define CINDEX_VERSION_ENCODE(major, minor) (((major) * 10000) + ((minor) * 1))
4040

4141
#define CINDEX_VERSION \
4242
CINDEX_VERSION_ENCODE(CINDEX_VERSION_MAJOR, CINDEX_VERSION_MINOR)
@@ -4495,6 +4495,129 @@ CINDEX_LINKAGE CXStringSet *clang_Cursor_getCXXManglings(CXCursor);
44954495
*/
44964496
CINDEX_LINKAGE CXStringSet *clang_Cursor_getObjCManglings(CXCursor);
44974497

4498+
/**
4499+
* @}
4500+
*/
4501+
4502+
/**
4503+
* \defgroup CINDEX_MODULE Inline Assembly introspection
4504+
*
4505+
* The functions in this group provide access to information about GCC-style
4506+
* inline assembly statements.
4507+
*
4508+
* @{
4509+
*/
4510+
4511+
/**
4512+
* Given a CXCursor_GCCAsmStmt cursor, return the assembly template string.
4513+
* As per LLVM IR Assembly Template language, template placeholders for
4514+
* inputs and outputs are either of the form $N where N is a decimal number
4515+
* as an index into the input-output specification,
4516+
* or ${N:M} where N is a decimal number also as an index into the
4517+
* input-output specification and M is the template argument modifier.
4518+
* The index N in both cases points into the the total inputs and outputs,
4519+
* or more specifically, into the list of outputs followed by the inputs,
4520+
* starting from index 0 as the first available template argument.
4521+
*
4522+
* This function also returns a valid empty string if the cursor does not point
4523+
* at a GCC inline assembly block.
4524+
*
4525+
* Users are responsible for releasing the allocation of returned string via
4526+
* \c clang_disposeString.
4527+
*/
4528+
4529+
CINDEX_LINKAGE CXString clang_Cursor_getGCCAssemblyTemplate(CXCursor);
4530+
4531+
/**
4532+
* Given a CXCursor_GCCAsmStmt cursor, check if the assembly block has goto
4533+
* labels.
4534+
* This function also returns 0 if the cursor does not point at a GCC inline
4535+
* assembly block.
4536+
*/
4537+
4538+
CINDEX_LINKAGE unsigned clang_Cursor_isGCCAssemblyHasGoto(CXCursor);
4539+
4540+
/**
4541+
* Given a CXCursor_GCCAsmStmt cursor, count the number of outputs.
4542+
* This function also returns 0 if the cursor does not point at a GCC inline
4543+
* assembly block.
4544+
*/
4545+
4546+
CINDEX_LINKAGE unsigned clang_Cursor_getGCCAssemblyNumOutputs(CXCursor);
4547+
4548+
/**
4549+
* Given a CXCursor_GCCAsmStmt cursor, count the number of inputs.
4550+
* This function also returns 0 if the cursor does not point at a GCC inline
4551+
* assembly block.
4552+
*/
4553+
4554+
CINDEX_LINKAGE unsigned clang_Cursor_getGCCAssemblyNumInputs(CXCursor);
4555+
4556+
/**
4557+
* Given a CXCursor_GCCAsmStmt cursor, get the constraint and expression cursor
4558+
* to the Index-th input.
4559+
* This function returns 1 when the cursor points at a GCC inline assembly
4560+
* statement, `Index` is within bounds and both the `Constraint` and `Expr` are
4561+
* not NULL.
4562+
* Otherwise, this function returns 0 but leaves `Constraint` and `Expr`
4563+
* intact.
4564+
*
4565+
* Users are responsible for releasing the allocation of `Constraint` via
4566+
* \c clang_disposeString.
4567+
*/
4568+
4569+
CINDEX_LINKAGE unsigned clang_Cursor_getGCCAssemblyInput(CXCursor Cursor,
4570+
unsigned Index,
4571+
CXString *Constraint,
4572+
CXCursor *Expr);
4573+
4574+
/**
4575+
* Given a CXCursor_GCCAsmStmt cursor, get the constraint and expression cursor
4576+
* to the Index-th output.
4577+
* This function returns 1 when the cursor points at a GCC inline assembly
4578+
* statement, `Index` is within bounds and both the `Constraint` and `Expr` are
4579+
* not NULL.
4580+
* Otherwise, this function returns 0 but leaves `Constraint` and `Expr`
4581+
* intact.
4582+
*
4583+
* Users are responsible for releasing the allocation of `Constraint` via
4584+
* \c clang_disposeString.
4585+
*/
4586+
4587+
CINDEX_LINKAGE unsigned clang_Cursor_getGCCAssemblyOutput(CXCursor Cursor,
4588+
unsigned Index,
4589+
CXString *Constraint,
4590+
CXCursor *Expr);
4591+
4592+
/**
4593+
* Given a CXCursor_GCCAsmStmt cursor, count the clobbers in it.
4594+
* This function also returns 0 if the cursor does not point at a GCC inline
4595+
* assembly block.
4596+
*/
4597+
4598+
CINDEX_LINKAGE unsigned clang_Cursor_getGCCAssemblyNumClobbers(CXCursor Cursor);
4599+
4600+
/**
4601+
* Given a CXCursor_GCCAsmStmt cursor, get the Index-th clobber of it.
4602+
* This function returns a valid empty string if the cursor does not point
4603+
* at a GCC inline assembly block or `Index` is out of bounds.
4604+
*
4605+
* Users are responsible for releasing the allocation of returned string via
4606+
* \c clang_disposeString.
4607+
*/
4608+
4609+
CINDEX_LINKAGE CXString clang_Cursor_getGCCAssemblyClobber(CXCursor Cursor,
4610+
unsigned Index);
4611+
4612+
/**
4613+
* Given a CXCursor_GCCAsmStmt cursor, check if the inline assembly is
4614+
* `volatile`.
4615+
* This function returns 0 if the cursor does not point at a GCC inline
4616+
* assembly block.
4617+
*/
4618+
4619+
CINDEX_LINKAGE unsigned clang_Cursor_isGCCAssemblyVolatile(CXCursor Cursor);
4620+
44984621
/**
44994622
* @}
45004623
*/

clang/include/clang/Analysis/FlowSensitive/StorageLocation.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,6 @@ class RecordStorageLocation final : public StorageLocation {
168168
return {Children.begin(), Children.end()};
169169
}
170170

171-
bool hasChild(const ValueDecl &D) const { return Children.contains(&D); }
172-
173171
private:
174172
FieldToLoc Children;
175173
SyntheticFieldMap SyntheticFields;

0 commit comments

Comments
 (0)