Skip to content

Commit cb286da

Browse files
committed
merge main into amd-staging
2 parents 1194532 + 25f05c0 commit cb286da

File tree

147 files changed

+1200
-471
lines changed

Some content is hidden

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

147 files changed

+1200
-471
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,27 @@ getFixIt(const tooling::Diagnostic &Diagnostic, bool AnyFix) {
358358

359359
} // namespace clang::tidy
360360

361+
void ClangTidyDiagnosticConsumer::BeginSourceFile(const LangOptions &LangOpts,
362+
const Preprocessor *PP) {
363+
DiagnosticConsumer::BeginSourceFile(LangOpts, PP);
364+
365+
assert(!InSourceFile);
366+
InSourceFile = true;
367+
}
368+
369+
void ClangTidyDiagnosticConsumer::EndSourceFile() {
370+
assert(InSourceFile);
371+
InSourceFile = false;
372+
373+
DiagnosticConsumer::EndSourceFile();
374+
}
375+
361376
void ClangTidyDiagnosticConsumer::HandleDiagnostic(
362377
DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) {
378+
// A diagnostic should not be reported outside of a
379+
// BeginSourceFile()/EndSourceFile() pair if it has a source location.
380+
assert(InSourceFile || Info.getLocation().isInvalid());
381+
363382
if (LastErrorWasIgnored && DiagLevel == DiagnosticsEngine::Note)
364383
return;
365384

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
292292
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
293293
const Diagnostic &Info) override;
294294

295+
void BeginSourceFile(const LangOptions &LangOpts,
296+
const Preprocessor *PP = nullptr) override;
297+
298+
void EndSourceFile() override;
299+
295300
// Retrieve the diagnostics that were captured.
296301
std::vector<ClangTidyError> take();
297302

@@ -326,6 +331,11 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
326331
bool LastErrorRelatesToUserCode = false;
327332
bool LastErrorPassesLineFilter = false;
328333
bool LastErrorWasIgnored = false;
334+
/// Tracks whether we're currently inside a
335+
/// `BeginSourceFile()/EndSourceFile()` pair. Outside of a source file, we
336+
/// should only receive diagnostics that have to source location, such as
337+
/// command-line warnings.
338+
bool InSourceFile = false;
329339
};
330340

331341
} // end namespace tidy

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@ using namespace clang::ast_matchers;
1515
namespace clang::tidy::cppcoreguidelines {
1616

1717
void ProBoundsPointerArithmeticCheck::registerMatchers(MatchFinder *Finder) {
18-
if (!getLangOpts().CPlusPlus)
19-
return;
20-
2118
const auto AllPointerTypes =
22-
anyOf(hasType(pointerType()),
19+
anyOf(hasType(hasUnqualifiedDesugaredType(pointerType())),
2320
hasType(autoType(
2421
hasDeducedType(hasUnqualifiedDesugaredType(pointerType())))),
2522
hasType(decltypeType(hasUnderlyingType(pointerType()))));
2623

27-
// Flag all operators +, -, +=, -=, ++, -- that result in a pointer
24+
// Flag all operators +, -, +=, -= that result in a pointer
2825
Finder->addMatcher(
2926
binaryOperator(
3027
hasAnyOperatorName("+", "-", "+=", "-="), AllPointerTypes,
3128
unless(hasLHS(ignoringImpCasts(declRefExpr(to(isImplicit()))))))
3229
.bind("expr"),
3330
this);
3431

32+
// Flag all operators ++, -- that result in a pointer
3533
Finder->addMatcher(
36-
unaryOperator(hasAnyOperatorName("++", "--"), hasType(pointerType()))
34+
unaryOperator(hasAnyOperatorName("++", "--"),
35+
hasType(hasUnqualifiedDesugaredType(pointerType())),
36+
unless(hasUnaryOperand(
37+
ignoringImpCasts(declRefExpr(to(isImplicit()))))))
3738
.bind("expr"),
3839
this);
3940

clang-tools-extra/clang-tidy/cppcoreguidelines/ProBoundsPointerArithmeticCheck.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class ProBoundsPointerArithmeticCheck : public ClangTidyCheck {
2323
public:
2424
ProBoundsPointerArithmeticCheck(StringRef Name, ClangTidyContext *Context)
2525
: ClangTidyCheck(Name, Context) {}
26+
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
27+
return LangOpts.CPlusPlus;
28+
}
2629
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
2730
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
2831
};

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ Changes in existing checks
219219
- Improved :doc:`cppcoreguidelines-pro-bounds-pointer-arithmetic
220220
<clang-tidy/checks/cppcoreguidelines/pro-bounds-pointer-arithmetic>` check by
221221
fixing false positives when calling indexing operators that do not perform
222-
pointer arithmetic in template, for example ``std::map::operator[]``.
222+
pointer arithmetic in template, for example ``std::map::operator[]`` and
223+
when pointer arithmetic was used through type aliases.
223224

224225
- Improved :doc:`cppcoreguidelines-rvalue-reference-param-not-moved
225226
<clang-tidy/checks/cppcoreguidelines/rvalue-reference-param-not-moved>` check

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic-pr36489.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// RUN: %check_clang_tidy -std=c++14-or-later %s cppcoreguidelines-pro-bounds-pointer-arithmetic %t
22

33
// Fix PR36489 and detect auto-deduced value correctly.
4+
typedef char* charPtr;
5+
46
char *getPtr();
57
auto getPtrAuto() { return getPtr(); }
68
decltype(getPtr()) getPtrDeclType();
79
decltype(auto) getPtrDeclTypeAuto() { return getPtr(); }
810
auto getPtrWithTrailingReturnType() -> char *;
11+
charPtr getCharPtr() { return getPtr(); }
912

1013
void auto_deduction_binary() {
1114
auto p1 = getPtr() + 1;
@@ -28,6 +31,10 @@ void auto_deduction_binary() {
2831
// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: do not use pointer arithmetic
2932
auto *p9 = getPtrDeclTypeAuto() + 1;
3033
// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: do not use pointer arithmetic
34+
auto p10 = getCharPtr() + 1;
35+
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not use pointer
36+
auto* p11 = getCharPtr() + 1;
37+
// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: do not use pointer arithmetic
3138
}
3239

3340
void auto_deduction_subscript() {
@@ -50,4 +57,6 @@ void auto_deduction_subscript() {
5057
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
5158
auto p8 = getPtrDeclTypeAuto()[9];
5259
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
60+
auto p9 = getCharPtr()[10];
61+
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not use pointer arithmetic
5362
}

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-pointer-arithmetic.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ enum E {
44
ENUM_LITERAL = 1
55
};
66

7+
typedef int* IntPtr;
8+
79
int i = 4;
810
int j = 1;
911
int *p = 0;
1012
int *q = 0;
13+
IntPtr ip = 0;
1114

1215
void fail() {
1316
q = p + 4;
@@ -50,6 +53,32 @@ void fail() {
5053

5154
i = p[1];
5255
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
56+
57+
p = ip + 1;
58+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: do not use pointer arithmetic
59+
ip++;
60+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not use pointer arithmetic
61+
i = ip[1];
62+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
63+
}
64+
65+
template <typename T>
66+
void template_fail() {
67+
T* p;
68+
T* q;
69+
70+
p = q + 1;
71+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
72+
q = p - 1;
73+
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: do not use pointer arithmetic
74+
p++;
75+
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: do not use pointer arithmetic
76+
i = p[1];
77+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not use pointer arithmetic
78+
}
79+
80+
void instantiate() {
81+
template_fail<int>();
5382
}
5483

5584
struct S {

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,13 +1243,15 @@ llvm::Error FrontendAction::Execute() {
12431243
void FrontendAction::EndSourceFile() {
12441244
CompilerInstance &CI = getCompilerInstance();
12451245

1246-
// Inform the diagnostic client we are done with this source file.
1247-
CI.getDiagnosticClient().EndSourceFile();
1248-
12491246
// Inform the preprocessor we are done.
12501247
if (CI.hasPreprocessor())
12511248
CI.getPreprocessor().EndSourceFile();
12521249

1250+
// Inform the diagnostic client we are done with this source file.
1251+
// Do this after notifying the preprocessor, so that end-of-file preprocessor
1252+
// callbacks can report diagnostics.
1253+
CI.getDiagnosticClient().EndSourceFile();
1254+
12531255
// Finalize the action.
12541256
EndSourceFileAction();
12551257

0 commit comments

Comments
 (0)