Skip to content

Commit 4038b2a

Browse files
authored
Merge branch 'main' into issue_161335
2 parents aa1650d + 7b3fe5f commit 4038b2a

File tree

256 files changed

+22822
-19033
lines changed

Some content is hidden

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

256 files changed

+22822
-19033
lines changed

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,30 +564,30 @@ void DataAggregator::imputeFallThroughs() {
564564
// Skip fall-throughs in external code.
565565
if (Trace.From == Trace::EXTERNAL)
566566
continue;
567-
std::pair CurrentBranch(Trace.Branch, Trace.From);
567+
if (std::pair CurrentBranch(Trace.Branch, Trace.From);
568+
CurrentBranch != PrevBranch) {
569+
// New group: reset aggregates.
570+
AggregateCount = AggregateFallthroughSize = 0;
571+
PrevBranch = CurrentBranch;
572+
}
568573
// BR_ONLY must be the last trace in the group
569574
if (Trace.To == Trace::BR_ONLY) {
570575
// If the group is not empty, use aggregate values, otherwise 0-length
571576
// for unconditional jumps (call/ret/uncond branch) or 1-length for others
572577
uint64_t InferredBytes =
573-
PrevBranch == CurrentBranch
578+
AggregateFallthroughSize
574579
? AggregateFallthroughSize / AggregateCount
575580
: !checkUnconditionalControlTransfer(Trace.From);
576581
Trace.To = Trace.From + InferredBytes;
577582
LLVM_DEBUG(dbgs() << "imputed " << Trace << " (" << InferredBytes
578583
<< " bytes)\n");
579584
++InferredTraces;
580585
} else {
581-
// Trace with a valid fall-through
582-
// New group: reset aggregates.
583-
if (CurrentBranch != PrevBranch)
584-
AggregateCount = AggregateFallthroughSize = 0;
585586
// Only use valid fall-through lengths
586587
if (Trace.To != Trace::EXTERNAL)
587588
AggregateFallthroughSize += (Trace.To - Trace.From) * Info.TakenCount;
588589
AggregateCount += Info.TakenCount;
589590
}
590-
PrevBranch = CurrentBranch;
591591
}
592592
if (opts::Verbosity >= 1)
593593
outs() << "BOLT-INFO: imputed " << InferredTraces << " traces\n";

bolt/test/X86/callcont-fallthru.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# External return to a landing pad/entry point call continuation
1616
# RUN: link_fdata %s %t %t.pa-eret PREAGG-ERET
1717
# RUN-DISABLED: link_fdata %s %t %t.pa-plt PREAGG-PLT
18+
## Fall-through imputing test cases
19+
# RUN: link_fdata %s %t %t.pa-imp PREAGG-IMP
1820

1921
# RUN: llvm-strip --strip-unneeded %t -o %t.strip
2022
# RUN: llvm-objcopy --remove-section=.eh_frame %t.strip %t.noeh
@@ -63,6 +65,11 @@
6365
# RUN-DISABLED: --check-prefix=CHECK-PLT
6466
# CHECK-PLT: traces mismatching disassembled function contents: 0
6567

68+
## Check --impute-trace-fall-throughs accepting duplicate branch-only traces
69+
# RUN: perf2bolt %t --pa -p %t.pa-imp -o %t.pa-imp.fdata --impute-trace-fall-through
70+
# RUN: FileCheck %s --check-prefix=CHECK-IMP --input-file %t.pa-imp.fdata
71+
# CHECK-IMP: 0 [unknown] 0 1 main {{.*}} 0 3
72+
6673
.globl foo
6774
.type foo, %function
6875
foo:
@@ -102,6 +109,8 @@ Ltmp1:
102109

103110
Ltmp4:
104111
cmpl $0x0, -0x14(%rbp)
112+
# PREAGG-IMP: B X:0 #Ltmp4_br# 1 0
113+
# PREAGG-IMP: B X:0 #Ltmp4_br# 2 0
105114
Ltmp4_br:
106115
je Ltmp0
107116

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "SizeofExpressionCheck.h"
7474
#include "SpuriouslyWakeUpFunctionsCheck.h"
7575
#include "StandaloneEmptyCheck.h"
76+
#include "StdNamespaceModificationCheck.h"
7677
#include "StringConstructorCheck.h"
7778
#include "StringIntegerAssignmentCheck.h"
7879
#include "StringLiteralWithEmbeddedNulCheck.h"
@@ -237,6 +238,8 @@ class BugproneModule : public ClangTidyModule {
237238
"bugprone-spuriously-wake-up-functions");
238239
CheckFactories.registerCheck<StandaloneEmptyCheck>(
239240
"bugprone-standalone-empty");
241+
CheckFactories.registerCheck<StdNamespaceModificationCheck>(
242+
"bugprone-std-namespace-modification");
240243
CheckFactories.registerCheck<StringConstructorCheck>(
241244
"bugprone-string-constructor");
242245
CheckFactories.registerCheck<StringIntegerAssignmentCheck>(

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_clang_library(clangTidyBugproneModule STATIC
7575
SmartPtrArrayMismatchCheck.cpp
7676
SpuriouslyWakeUpFunctionsCheck.cpp
7777
StandaloneEmptyCheck.cpp
78+
StdNamespaceModificationCheck.cpp
7879
StringConstructorCheck.cpp
7980
StringIntegerAssignmentCheck.cpp
8081
StringLiteralWithEmbeddedNulCheck.cpp

clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.cpp renamed to clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.cpp

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

9-
#include "DontModifyStdNamespaceCheck.h"
9+
#include "StdNamespaceModificationCheck.h"
1010
#include "clang/ASTMatchers/ASTMatchFinder.h"
1111
#include "clang/ASTMatchers/ASTMatchersInternal.h"
1212

@@ -36,9 +36,9 @@ AST_POLYMORPHIC_MATCHER_P(
3636

3737
} // namespace
3838

39-
namespace clang::tidy::cert {
39+
namespace clang::tidy::bugprone {
4040

41-
void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) {
41+
void StdNamespaceModificationCheck::registerMatchers(MatchFinder *Finder) {
4242
auto HasStdParent =
4343
hasDeclContext(namespaceDecl(hasAnyName("std", "posix"),
4444
unless(hasParent(namespaceDecl())))
@@ -96,7 +96,7 @@ void DontModifyStdNamespaceCheck::registerMatchers(MatchFinder *Finder) {
9696
.bind("decl"),
9797
this);
9898
}
99-
} // namespace clang::tidy::cert
99+
} // namespace clang::tidy::bugprone
100100

101101
static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) {
102102
const NamespaceDecl *LastNS = nullptr;
@@ -108,7 +108,7 @@ static const NamespaceDecl *getTopLevelLexicalNamespaceDecl(const Decl *D) {
108108
return LastNS;
109109
}
110110

111-
void clang::tidy::cert::DontModifyStdNamespaceCheck::check(
111+
void clang::tidy::bugprone::StdNamespaceModificationCheck::check(
112112
const MatchFinder::MatchResult &Result) {
113113
const auto *D = Result.Nodes.getNodeAs<Decl>("decl");
114114
const auto *NS = Result.Nodes.getNodeAs<NamespaceDecl>("nmspc");

clang-tools-extra/clang-tidy/cert/DontModifyStdNamespaceCheck.h renamed to clang-tools-extra/clang-tidy/bugprone/StdNamespaceModificationCheck.h

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

9-
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H
10-
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H
9+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STDNAMESPACEMODIFICATIONCHECK_H
10+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STDNAMESPACEMODIFICATIONCHECK_H
1111

1212
#include "../ClangTidyCheck.h"
1313

14-
namespace clang::tidy::cert {
14+
namespace clang::tidy::bugprone {
1515

1616
/// Modification of the std or posix namespace can result in undefined behavior.
1717
/// This check warns for such modifications.
1818
///
1919
/// For the user-facing documentation see:
20-
/// https://clang.llvm.org/extra/clang-tidy/checks/cert/dcl58-cpp.html
21-
class DontModifyStdNamespaceCheck : public ClangTidyCheck {
20+
/// https://clang.llvm.org/extra/clang-tidy/checks/bugprone/std-namespace-modification.html
21+
class StdNamespaceModificationCheck : public ClangTidyCheck {
2222
public:
23-
DontModifyStdNamespaceCheck(StringRef Name, ClangTidyContext *Context)
23+
StdNamespaceModificationCheck(StringRef Name, ClangTidyContext *Context)
2424
: ClangTidyCheck(Name, Context) {}
2525
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
2626
return LangOpts.CPlusPlus;
@@ -29,6 +29,6 @@ class DontModifyStdNamespaceCheck : public ClangTidyCheck {
2929
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
3030
};
3131

32-
} // namespace clang::tidy::cert
32+
} // namespace clang::tidy::bugprone
3333

34-
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CERT_DONT_MODIFY_STD_NAMESPACE_H
34+
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_STDNAMESPACEMODIFICATIONCHECK_H

clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "../bugprone/SignedCharMisuseCheck.h"
2020
#include "../bugprone/SizeofExpressionCheck.h"
2121
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
22+
#include "../bugprone/StdNamespaceModificationCheck.h"
2223
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
2324
#include "../bugprone/ThrowingStaticInitializationCheck.h"
2425
#include "../bugprone/UncheckedStringToNumberConversionCheck.h"
@@ -36,7 +37,6 @@
3637
#include "../performance/MoveConstructorInitCheck.h"
3738
#include "../readability/EnumInitialValueCheck.h"
3839
#include "../readability/UppercaseLiteralSuffixCheck.h"
39-
#include "DontModifyStdNamespaceCheck.h"
4040
#include "FloatLoopCounter.h"
4141
#include "LimitedRandomnessCheck.h"
4242
#include "MutatingCopyCheck.h"
@@ -251,7 +251,8 @@ class CERTModule : public ClangTidyModule {
251251
"cert-dcl51-cpp");
252252
CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
253253
"cert-dcl54-cpp");
254-
CheckFactories.registerCheck<DontModifyStdNamespaceCheck>("cert-dcl58-cpp");
254+
CheckFactories.registerCheck<bugprone::StdNamespaceModificationCheck>(
255+
"cert-dcl58-cpp");
255256
CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
256257
"cert-dcl59-cpp");
257258
// ERR

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ set(LLVM_LINK_COMPONENTS
55

66
add_clang_library(clangTidyCERTModule STATIC
77
CERTTidyModule.cpp
8-
DontModifyStdNamespaceCheck.cpp
98
FloatLoopCounter.cpp
109
LimitedRandomnessCheck.cpp
1110
MutatingCopyCheck.cpp

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ New check aliases
244244
<clang-tidy/checks/modernize/avoid-variadic-functions>`
245245
keeping initial check as an alias to the new one.
246246

247+
- Renamed :doc:`cert-dcl58-cpp <clang-tidy/checks/cert/dcl58-cpp>` to
248+
:doc:`bugprone-std-namespace-modification
249+
<clang-tidy/checks/bugprone/std-namespace-modification>`
250+
keeping initial check as an alias to the new one.
251+
247252
- Renamed :doc:`cert-env33-c <clang-tidy/checks/cert/env33-c>` to
248253
:doc:`bugprone-command-processor
249254
<clang-tidy/checks/bugprone/command-processor>`
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. title:: clang-tidy - bugprone-std-namespace-modification
2+
3+
bugprone-std-namespace-modification
4+
===================================
5+
6+
Warns on modifications of the ``std`` or ``posix`` namespaces which can
7+
result in undefined behavior.
8+
9+
The ``std`` (or ``posix``) namespace is allowed to be extended with (class or
10+
function) template specializations that depend on an user-defined type (a type
11+
that is not defined in the standard system headers).
12+
13+
The check detects the following (user provided) declarations in namespace ``std`` or ``posix``:
14+
15+
- Anything that is not a template specialization.
16+
- Explicit specializations of any standard library function template or class template, if it does not have any user-defined type as template argument.
17+
- Explicit specializations of any member function of a standard library class template.
18+
- Explicit specializations of any member function template of a standard library class or class template.
19+
- Explicit or partial specialization of any member class template of a standard library class or class template.
20+
21+
Examples:
22+
23+
.. code-block:: c++
24+
25+
namespace std {
26+
int x; // warning: modification of 'std' namespace can result in undefined behavior [bugprone-dont-modify-std-namespace]
27+
}
28+
29+
namespace posix::a { // warning: modification of 'posix' namespace can result in undefined behavior
30+
}
31+
32+
template <>
33+
struct ::std::hash<long> { // warning: modification of 'std' namespace can result in undefined behavior
34+
unsigned long operator()(const long &K) const {
35+
return K;
36+
}
37+
};
38+
39+
struct MyData { long data; };
40+
41+
template <>
42+
struct ::std::hash<MyData> { // no warning: specialization with user-defined type
43+
unsigned long operator()(const MyData &K) const {
44+
return K.data;
45+
}
46+
};
47+
48+
namespace std {
49+
template <>
50+
void swap<bool>(bool &a, bool &b); // warning: modification of 'std' namespace can result in undefined behavior
51+
52+
template <>
53+
bool less<void>::operator()<MyData &&, MyData &&>(MyData &&, MyData &&) const { // warning: modification of 'std' namespace can result in undefined behavior
54+
return true;
55+
}
56+
}
57+
58+
References
59+
----------
60+
61+
This check corresponds to the CERT C++ Coding Standard rule
62+
`DCL58-CPP. Do not modify the standard namespaces
63+
<https://www.securecoding.cert.org/confluence/display/cplusplus/DCL58-CPP.+Do+not+modify+the+standard+namespaces>`_.

0 commit comments

Comments
 (0)