Skip to content

Commit 2b39f6a

Browse files
committed
Merge from 'main' to 'sycl-web' (108 commits)
CONFLICT (content): Merge conflict in llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
2 parents 0096753 + 6c64c8a commit 2b39f6a

File tree

1,559 files changed

+211749
-2892
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,559 files changed

+211749
-2892
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ clang_target_link_libraries(clangTidy
3535
clangFrontend
3636
clangLex
3737
clangRewrite
38-
clangSema
3938
clangSerialization
4039
clangTooling
4140
clangToolingCore

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ class ClangTidyASTConsumer : public MultiplexConsumer {
336336
std::unique_ptr<ClangTidyProfiling> Profiling;
337337
std::unique_ptr<ast_matchers::MatchFinder> Finder;
338338
std::vector<std::unique_ptr<ClangTidyCheck>> Checks;
339+
void anchor() override {};
339340
};
340341

341342
} // namespace

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,44 +62,44 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {
6262
.bind("positiveComparison"),
6363
this);
6464
AddSimpleMatcher(
65-
binaryOperator(hasOperatorName("!="), hasOperands(CountCall, Literal0))
65+
binaryOperation(hasOperatorName("!="), hasOperands(CountCall, Literal0))
6666
.bind("positiveComparison"));
6767
AddSimpleMatcher(
68-
binaryOperator(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0))
68+
binaryOperation(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0))
6969
.bind("positiveComparison"));
7070
AddSimpleMatcher(
71-
binaryOperator(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall))
72-
.bind("positiveComparison"));
73-
AddSimpleMatcher(
74-
binaryOperator(hasLHS(CountCall), hasOperatorName(">="), hasRHS(Literal1))
75-
.bind("positiveComparison"));
76-
AddSimpleMatcher(
77-
binaryOperator(hasLHS(Literal1), hasOperatorName("<="), hasRHS(CountCall))
71+
binaryOperation(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall))
7872
.bind("positiveComparison"));
73+
AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName(">="),
74+
hasRHS(Literal1))
75+
.bind("positiveComparison"));
76+
AddSimpleMatcher(binaryOperation(hasLHS(Literal1), hasOperatorName("<="),
77+
hasRHS(CountCall))
78+
.bind("positiveComparison"));
7979

8080
// Find inverted membership tests which use `count()`.
8181
AddSimpleMatcher(
82-
binaryOperator(hasOperatorName("=="), hasOperands(CountCall, Literal0))
83-
.bind("negativeComparison"));
84-
AddSimpleMatcher(
85-
binaryOperator(hasLHS(CountCall), hasOperatorName("<="), hasRHS(Literal0))
86-
.bind("negativeComparison"));
87-
AddSimpleMatcher(
88-
binaryOperator(hasLHS(Literal0), hasOperatorName(">="), hasRHS(CountCall))
82+
binaryOperation(hasOperatorName("=="), hasOperands(CountCall, Literal0))
8983
.bind("negativeComparison"));
84+
AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName("<="),
85+
hasRHS(Literal0))
86+
.bind("negativeComparison"));
87+
AddSimpleMatcher(binaryOperation(hasLHS(Literal0), hasOperatorName(">="),
88+
hasRHS(CountCall))
89+
.bind("negativeComparison"));
9090
AddSimpleMatcher(
91-
binaryOperator(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1))
91+
binaryOperation(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1))
9292
.bind("negativeComparison"));
9393
AddSimpleMatcher(
94-
binaryOperator(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall))
94+
binaryOperation(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall))
9595
.bind("negativeComparison"));
9696

9797
// Find membership tests based on `find() == end()`.
9898
AddSimpleMatcher(
99-
binaryOperator(hasOperatorName("!="), hasOperands(FindCall, EndCall))
99+
binaryOperation(hasOperatorName("!="), hasOperands(FindCall, EndCall))
100100
.bind("positiveComparison"));
101101
AddSimpleMatcher(
102-
binaryOperator(hasOperatorName("=="), hasOperands(FindCall, EndCall))
102+
binaryOperation(hasOperatorName("=="), hasOperands(FindCall, EndCall))
103103
.bind("negativeComparison"));
104104
}
105105

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ Changes in existing checks
238238

239239
- Improved :doc:`readability-container-contains
240240
<clang-tidy/checks/readability/container-contains>` check to let it work on
241-
any class that has a ``contains`` method.
241+
any class that has a ``contains`` method. Fix some false negatives in the
242+
``find()`` case.
242243

243244
- Improved :doc:`readability-enum-initial-value
244245
<clang-tidy/checks/readability/enum-initial-value>` check by only issuing

clang-tools-extra/test/clang-tidy/checkers/readability/container-contains.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
// RUN: %check_clang_tidy -std=c++20-or-later %s readability-container-contains %t
1+
// RUN: %check_clang_tidy -std=c++11-or-later %s readability-container-contains %t
22

33
// Some *very* simplified versions of `map` etc.
44
namespace std {
55

66
template <class Key, class T>
77
struct map {
8+
struct iterator {
9+
bool operator==(const iterator &Other) const;
10+
bool operator!=(const iterator &Other) const;
11+
};
12+
813
unsigned count(const Key &K) const;
914
bool contains(const Key &K) const;
10-
void *find(const Key &K);
11-
void *end();
15+
iterator find(const Key &K);
16+
iterator end();
1217
};
1318

1419
template <class Key>

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4663,6 +4663,14 @@ the configuration (without a prefix: ``Auto``).
46634663
**KeepEmptyLinesAtTheStartOfBlocks** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<KeepEmptyLinesAtTheStartOfBlocks>`
46644664
This option is deprecated. See ``AtStartOfBlock`` of ``KeepEmptyLines``.
46654665

4666+
.. _KeepFormFeed:
4667+
4668+
**KeepFormFeed** (``Boolean``) :versionbadge:`clang-format 20` :ref:`<KeepFormFeed>`
4669+
Keep the form feed character if it's immediately preceded and followed by
4670+
a newline. Multiple form feeds and newlines within a whitespace range are
4671+
replaced with a single newline and form feed followed by the remaining
4672+
newlines.
4673+
46664674
.. _LambdaBodyIndentation:
46674675

46684676
**LambdaBodyIndentation** (``LambdaBodyIndentationKind``) :versionbadge:`clang-format 13` :ref:`<LambdaBodyIndentation>`

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ Bug Fixes to C++ Support
544544
- Clang incorrectly considered a class with an anonymous union member to not be
545545
const-default-constructible even if a union member has a default member initializer.
546546
(#GH95854).
547+
- Fixed an assertion failure when evaluating an invalid expression in an array initializer (#GH112140)
547548

548549
Bug Fixes to AST Handling
549550
^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -710,6 +711,7 @@ clang-format
710711
multi-line comments without touching their contents, renames ``false`` to
711712
``Never``, and ``true`` to ``Always``.
712713
- Adds ``RemoveEmptyLinesInUnwrappedLines`` option.
714+
- Adds ``KeepFormFeed`` option and set it to ``true`` for ``GNU`` style.
713715

714716
libclang
715717
--------

clang/docs/analyzer/user-docs.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ Contents:
1212
user-docs/FilingBugs
1313
user-docs/CrossTranslationUnit
1414
user-docs/TaintAnalysisConfiguration
15+
user-docs/FAQ
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
FAQ and How to Deal with Common False Positives
2+
===============================================
3+
4+
.. contents::
5+
:local:
6+
7+
Custom Assertions
8+
-----------------
9+
10+
Q: How do I tell the analyzer that I do not want the bug being reported here since my custom error handler will safely end the execution before the bug is reached?
11+
12+
You can tell the analyzer that this path is unreachable by teaching it about your `custom assertion handlers <annotations.html#custom_assertions>`_. For example, you can modify the code segment as following:
13+
14+
.. code-block:: c
15+
16+
void customAssert() __attribute__((analyzer_noreturn));
17+
int foo(int *b) {
18+
if (!b)
19+
customAssert();
20+
return *b;
21+
}
22+
23+
Null Pointer Dereference
24+
------------------------
25+
26+
Q: The analyzer reports a null dereference, but I know that the pointer is never null. How can I tell the analyzer that a pointer can never be null?
27+
28+
The reason the analyzer often thinks that a pointer can be null is because the preceding code checked compared it against null. If you are absolutely sure that it cannot be null, remove the preceding check and, preferably, add an assertion as well. For example:
29+
30+
.. code-block:: c
31+
32+
void usePointer(int *b);
33+
int foo(int *b) {
34+
usePointer(b);
35+
return *b;
36+
}
37+
38+
Dead Store
39+
----------
40+
41+
Q: How do I tell the static analyzer that I don't care about a specific dead store?
42+
43+
When the analyzer sees that a value stored into a variable is never used, it's going to produce a message similar to this one:
44+
45+
.. code-block:: none
46+
47+
Value stored to 'x' is never read
48+
49+
You can use the ``(void)x;`` idiom to acknowledge that there is a dead store in your code but you do not want it to be reported in the future.
50+
51+
Unused Instance Variable
52+
------------------------
53+
54+
Q: How do I tell the static analyzer that I don't care about a specific unused instance variable in Objective-C?
55+
56+
When the analyzer sees that a value stored into a variable is never used, it is going to produce a message similar to this one:
57+
58+
.. code-block:: none
59+
60+
Instance variable 'commonName' in class 'HappyBird' is never used by the methods in its @implementation
61+
62+
You can add ``__attribute__((unused))`` to the instance variable declaration to suppress the warning.
63+
64+
Unlocalized String
65+
------------------
66+
67+
Q: How do I tell the static analyzer that I don't care about a specific unlocalized string?
68+
69+
When the analyzer sees that an unlocalized string is passed to a method that will present that string to the user, it is going to produce a message similar to this one:
70+
71+
.. code-block:: none
72+
73+
User-facing text should use localized string macro
74+
75+
If your project deliberately uses unlocalized user-facing strings (for example, in a debugging UI that is never shown to users), you can suppress the analyzer warnings (and document your intent) with a function that just returns its input but is annotated to return a localized string:
76+
77+
.. code-block:: objc
78+
79+
__attribute__((annotate("returns_localized_nsstring")))
80+
static inline NSString *LocalizationNotNeeded(NSString *s) {
81+
return s;
82+
}
83+
84+
You can then call this function when creating your debugging UI:
85+
86+
.. code-block:: objc
87+
88+
[field setStringValue:LocalizationNotNeeded(@"Debug")];
89+
90+
Some projects may also find it useful to use NSLocalizedString but add "DNL" or "Do Not Localize" to the string contents as a convention:
91+
92+
.. code-block:: objc
93+
94+
UILabel *testLabel = [[UILabel alloc] init];
95+
NSString *s = NSLocalizedString(@"Hello <Do Not Localize>", @"For debug purposes");
96+
[testLabel setText:s];
97+
98+
Dealloc in Manual Retain/Release
99+
--------------------------------
100+
101+
Q: How do I tell the analyzer that my instance variable does not need to be released in -dealloc under Manual Retain/Release?
102+
103+
If your class only uses an instance variable for part of its lifetime, it may maintain an invariant guaranteeing that the instance variable is always released before -dealloc. In this case, you can silence a warning about a missing release by either adding ``assert(_ivar == nil)`` or an explicit release ``[_ivar release]`` (which will be a no-op when the variable is nil) in -dealloc.
104+
105+
Deciding Nullability
106+
--------------------
107+
108+
Q: How do I decide whether a method's return type should be _Nullable or _Nonnull?
109+
110+
Depending on the implementation of the method, this puts you in one of five situations:
111+
112+
1. You actually never return nil.
113+
2. You do return nil sometimes, and callers are supposed to handle that. This includes cases where your method is documented to return nil given certain inputs.
114+
3. You return nil based on some external condition (such as an out-of-memory error), but the client can't do anything about it either.
115+
4. You return nil only when the caller passes input documented to be invalid. That means it's the client's fault.
116+
5. You return nil in some totally undocumented case.
117+
118+
In (1) you should annotate the method as returning a ``_Nonnull`` object.
119+
120+
In (2) the method should be marked ``_Nullable``.
121+
122+
In (3) you should probably annotate the method ``_Nonnull``. Why? Because no callers will actually check for nil, given that they can't do anything about the situation and don't know what went wrong. At this point things have gone so poorly that there's basically no way to recover.
123+
124+
The least happy case is (4) because the resulting program will almost certainly either crash or just silently do the wrong thing. If this is a new method or you control the callers, you can use ``NSParameterAssert()`` (or the equivalent) to check the precondition and remove the nil return. But if you don't control the callers and they rely on this behavior, you should return mark the method ``_Nonnull`` and return nil cast to _Nonnull anyway.
125+
126+
If you're in (5), document it, then figure out if you're now in (2), (3), or (4).
127+
128+
Intentional Nullability Violation
129+
---------------------------------
130+
131+
Q: How do I tell the analyzer that I am intentionally violating nullability?
132+
133+
In some cases, it may make sense for methods to intentionally violate nullability. For example, your method may — for reasons of backward compatibility — chose to return nil and log an error message in a method with a non-null return type when the client violated a documented precondition rather than check the precondition with ``NSAssert()``. In these cases, you can suppress the analyzer warning with a cast:
134+
135+
.. code-block:: objc
136+
137+
return (id _Nonnull)nil;
138+
139+
Note that this cast does not affect code generation.
140+
141+
Ensuring Loop Body Execution
142+
----------------------------
143+
144+
Q: The analyzer assumes that a loop body is never entered. How can I tell it that the loop body will be entered at least once?
145+
146+
In cases where you know that a loop will always be entered at least once, you can use assertions to inform the analyzer. For example:
147+
148+
.. code-block:: c
149+
150+
int foo(int length) {
151+
int x = 0;
152+
assert(length > 0);
153+
for (int i = 0; i < length; i++)
154+
x += 1;
155+
return length/x;
156+
}
157+
158+
By adding ``assert(length > 0)`` in the beginning of the function, you tell the analyzer that your code is never expecting a zero or a negative value, so it won't need to test the correctness of those paths.
159+
160+
Suppressing Specific Warnings
161+
-----------------------------
162+
163+
Q: How can I suppress a specific analyzer warning?
164+
165+
When you encounter an analyzer bug/false positive, check if it's one of the issues discussed above or if the analyzer `annotations <annotations.html#custom_assertions>`_ can resolve the issue by helping the static analyzer understand the code better. Second, please `report it <filing_bugs.html>`_ to help us improve user experience.
166+
167+
Sometimes there's really no "good" way to eliminate the issue. In such cases you can "silence" it directly by annotating the problematic line of code with the help of Clang attribute 'suppress':
168+
169+
.. code-block:: c
170+
171+
int foo() {
172+
int *x = nullptr;
173+
...
174+
[[clang::suppress]] {
175+
// all warnings in this scope are suppressed
176+
int y = *x;
177+
}
178+
179+
// null pointer dereference warning suppressed on the next line
180+
[[clang::suppress]]
181+
return *x
182+
}
183+
184+
int bar(bool coin_flip) {
185+
// suppress all memory leak warnings about this allocation
186+
[[clang::suppress]]
187+
int *result = (int *)malloc(sizeof(int));
188+
189+
if (coin_flip)
190+
return 0; // including this leak path
191+
192+
return *result; // as well as this leak path
193+
}
194+
195+
Excluding Code from Analysis
196+
----------------------------
197+
198+
Q: How can I selectively exclude code the analyzer examines?
199+
200+
When the static analyzer is using clang to parse source files, it implicitly defines the preprocessor macro ``__clang_analyzer__``. One can use this macro to selectively exclude code the analyzer examines. Here is an example:
201+
202+
.. code-block:: c
203+
204+
#ifndef __clang_analyzer__
205+
// Code not to be analyzed
206+
#endif
207+
208+
This usage is discouraged because it makes the code dead to the analyzer from now on. Instead, we prefer that users file bugs against the analyzer when it flags false positives.

clang/include/clang/AST/CommentSema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Sema {
8080
ArrayRef<T> copyArray(ArrayRef<T> Source) {
8181
if (!Source.empty())
8282
return Source.copy(Allocator);
83-
return std::nullopt;
83+
return {};
8484
}
8585

8686
ParagraphComment *actOnParagraphComment(

0 commit comments

Comments
 (0)