Skip to content

Commit 958d51b

Browse files
authored
Merge branch 'main' into implement-countlsfx
2 parents 7a34ec1 + 8374d42 commit 958d51b

File tree

1,765 files changed

+71893
-74737
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,765 files changed

+71893
-74737
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
/bolt/ @aaupov @maksfb @rafaelauler @ayermolo @dcci @yota9
132132

133133
# Bazel build system.
134-
/utils/bazel/ @rupprecht @keith
134+
/utils/bazel/ @rupprecht @keith @aaronmondal
135135

136136
# InstallAPI and TextAPI
137137
/llvm/**/TextAPI/ @cyndyishida

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
8282
Finder->addMatcher(
8383
cxxConstructExpr(
8484
hasDeclaration(cxxMethodDecl(hasName("basic_string"))),
85-
hasArgument(0, hasType(qualType(isInteger()))),
85+
argumentCountIs(2), hasArgument(0, hasType(qualType(isInteger()))),
8686
hasArgument(1, hasType(qualType(isInteger()))),
8787
anyOf(
8888
// Detect the expression: string('x', 40);
@@ -102,7 +102,7 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
102102
cxxConstructExpr(
103103
hasDeclaration(cxxConstructorDecl(ofClass(
104104
cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
105-
hasArgument(0, hasType(CharPtrType)),
105+
argumentCountIs(2), hasArgument(0, hasType(CharPtrType)),
106106
hasArgument(1, hasType(isInteger())),
107107
anyOf(
108108
// Detect the expression: string("...", 0);
@@ -114,7 +114,34 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
114114
// Detect the expression: string("lit", 5)
115115
allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
116116
hasArgument(1, ignoringParenImpCasts(
117-
integerLiteral().bind("int"))))))
117+
integerLiteral().bind("length"))))))
118+
.bind("constructor"),
119+
this);
120+
121+
// Check the literal string constructor with char pointer, start position and
122+
// length parameters. [i.e. string (const char* s, size_t pos, size_t count);]
123+
Finder->addMatcher(
124+
cxxConstructExpr(
125+
hasDeclaration(cxxConstructorDecl(ofClass(
126+
cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
127+
argumentCountIs(3), hasArgument(0, hasType(CharPtrType)),
128+
hasArgument(1, hasType(qualType(isInteger()))),
129+
hasArgument(2, hasType(qualType(isInteger()))),
130+
anyOf(
131+
// Detect the expression: string("...", 1, 0);
132+
hasArgument(2, ZeroExpr.bind("empty-string")),
133+
// Detect the expression: string("...", -4, 1);
134+
hasArgument(1, NegativeExpr.bind("negative-pos")),
135+
// Detect the expression: string("...", 0, -4);
136+
hasArgument(2, NegativeExpr.bind("negative-length")),
137+
// Detect the expression: string("lit", 0, 0x1234567);
138+
hasArgument(2, LargeLengthExpr.bind("large-length")),
139+
// Detect the expression: string("lit", 1, 5)
140+
allOf(hasArgument(0, ConstStrLiteral.bind("literal-with-length")),
141+
hasArgument(
142+
1, ignoringParenImpCasts(integerLiteral().bind("pos"))),
143+
hasArgument(2, ignoringParenImpCasts(
144+
integerLiteral().bind("length"))))))
118145
.bind("constructor"),
119146
this);
120147

@@ -155,14 +182,27 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) {
155182
diag(Loc, "constructor creating an empty string");
156183
} else if (Result.Nodes.getNodeAs<Expr>("negative-length")) {
157184
diag(Loc, "negative value used as length parameter");
185+
} else if (Result.Nodes.getNodeAs<Expr>("negative-pos")) {
186+
diag(Loc, "negative value used as position of the "
187+
"first character parameter");
158188
} else if (Result.Nodes.getNodeAs<Expr>("large-length")) {
159189
if (WarnOnLargeLength)
160190
diag(Loc, "suspicious large length parameter");
161191
} else if (Result.Nodes.getNodeAs<Expr>("literal-with-length")) {
162192
const auto *Str = Result.Nodes.getNodeAs<StringLiteral>("str");
163-
const auto *Lit = Result.Nodes.getNodeAs<IntegerLiteral>("int");
164-
if (Lit->getValue().ugt(Str->getLength())) {
193+
const auto *Length = Result.Nodes.getNodeAs<IntegerLiteral>("length");
194+
if (Length->getValue().ugt(Str->getLength())) {
165195
diag(Loc, "length is bigger than string literal size");
196+
return;
197+
}
198+
if (const auto *Pos = Result.Nodes.getNodeAs<IntegerLiteral>("pos")) {
199+
if (Pos->getValue().uge(Str->getLength())) {
200+
diag(Loc, "position of the first character parameter is bigger than "
201+
"string literal character range");
202+
} else if (Length->getValue().ugt(
203+
(Str->getLength() - Pos->getValue()).getZExtValue())) {
204+
diag(Loc, "length is bigger than remaining string literal size");
205+
}
166206
}
167207
} else if (const auto *Ptr = Result.Nodes.getNodeAs<Expr>("from-ptr")) {
168208
Expr::EvalResult ConstPtr;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ New check aliases
9797
Changes in existing checks
9898
^^^^^^^^^^^^^^^^^^^^^^^^^^
9999

100+
- Improved :doc:`bugprone-string-constructor
101+
<clang-tidy/checks/bugprone/string-constructor>` check to find suspicious
102+
calls of ``std::string`` constructor with char pointer, start position and
103+
length parameters.
104+
100105
- Improved :doc:`bugprone-unsafe-functions
101106
<clang-tidy/checks/bugprone/unsafe-functions>` check to allow specifying
102107
additional C++ member functions to match.

clang-tools-extra/docs/clang-tidy/checks/bugprone/string-constructor.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Examples:
2121
.. code-block:: c++
2222

2323
std::string("test", 200); // Will include random characters after "test".
24+
std::string("test", 2, 5); // Will include random characters after "st".
2425
std::string_view("test", 200);
2526

2627
Creating an empty string from constructors with parameters is considered
@@ -31,8 +32,19 @@ Examples:
3132
.. code-block:: c++
3233

3334
std::string("test", 0); // Creation of an empty string.
35+
std::string("test", 1, 0);
3436
std::string_view("test", 0);
3537

38+
Passing an invalid first character position parameter to constructor will
39+
cause ``std::out_of_range`` exception at runtime.
40+
41+
Examples:
42+
43+
.. code-block:: c++
44+
45+
std::string("test", -1, 10); // Negative first character position.
46+
std::string("test", 10, 10); // First character position is bigger than string literal character range".
47+
3648
Options
3749
-------
3850

clang-tools-extra/test/clang-tidy/checkers/bugprone/string-constructor.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ struct basic_string {
1111
basic_string(const C*, unsigned int size);
1212
basic_string(const C *, const A &allocator = A());
1313
basic_string(unsigned int size, C c);
14+
basic_string(const C*, unsigned int pos, unsigned int size);
1415
};
1516
typedef basic_string<char> string;
1617
typedef basic_string<wchar_t> wstring;
@@ -61,6 +62,21 @@ void Test() {
6162
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructing string from nullptr is undefined behaviour
6263
std::string q7 = 0;
6364
// CHECK-MESSAGES: [[@LINE-1]]:20: warning: constructing string from nullptr is undefined behaviour
65+
66+
std::string r1("test", 1, 0);
67+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
68+
std::string r2("test", 0, -4);
69+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as length parameter
70+
std::string r3("test", -4, 1);
71+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: negative value used as position of the first character parameter
72+
std::string r4("test", 0, 0x1000000);
73+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
74+
std::string r5("test", 0, 5);
75+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
76+
std::string r6("test", 3, 2);
77+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
78+
std::string r7("test", 4, 1);
79+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: position of the first character parameter is bigger than string literal character range
6480
}
6581

6682
void TestView() {
@@ -82,6 +98,17 @@ void TestView() {
8298
// CHECK-MESSAGES: [[@LINE-1]]:25: warning: constructing string from nullptr is undefined behaviour
8399
}
84100

101+
void TestUnsignedArguments() {
102+
std::string s0("test", 0u);
103+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: constructor creating an empty string
104+
std::string s1(0x1000000ull, 'x');
105+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: suspicious large length parameter
106+
std::string s2("test", 3ull, 2u);
107+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than remaining string literal size
108+
std::string s3("test", 0u, 5ll);
109+
// CHECK-MESSAGES: [[@LINE-1]]:15: warning: length is bigger than string literal size
110+
}
111+
85112
std::string StringFromZero() {
86113
return 0;
87114
// CHECK-MESSAGES: [[@LINE-1]]:10: warning: constructing string from nullptr is undefined behaviour
@@ -101,6 +128,9 @@ void Valid() {
101128
std::string s3("test");
102129
std::string s4("test\000", 5);
103130
std::string s6("te" "st", 4);
131+
std::string s7("test", 0, 4);
132+
std::string s8("test", 3, 1);
133+
std::string s9("te" "st", 1, 2);
104134

105135
std::string_view emptyv();
106136
std::string_view sv1("test", 4);

clang/docs/ReleaseNotes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ Non-comprehensive list of changes in this release
104104
New Compiler Flags
105105
------------------
106106

107+
- New option ``-fprofile-continuous`` added to enable continuous profile syncing to file (#GH124353, `docs <https://clang.llvm.org/docs/UsersManual.html#cmdoption-fprofile-continuous>`_).
108+
The feature has `existed <https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#running-the-instrumented-program>`_)
109+
for a while and this is just a user facing option.
110+
107111
Deprecated Compiler Flags
108112
-------------------------
109113

@@ -117,6 +121,7 @@ Attribute Changes in Clang
117121
--------------------------
118122

119123
- The ``no_sanitize`` attribute now accepts both ``gnu`` and ``clang`` names.
124+
- Clang now diagnoses use of declaration attributes on void parameters. (#GH108819)
120125

121126
Improvements to Clang's diagnostics
122127
-----------------------------------
@@ -128,6 +133,8 @@ Improvements to Clang's diagnostics
128133
which are supposed to only exist once per program, but may get duplicated when
129134
built into a shared library.
130135
- Fixed a bug where Clang's Analysis did not correctly model the destructor behavior of ``union`` members (#GH119415).
136+
- A statement attribute applied to a ``case`` label no longer suppresses
137+
'bypassing variable initialization' diagnostics (#84072).
131138

132139
Improvements to Clang's time-trace
133140
----------------------------------

clang/docs/TypeSanitizer.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ reduce these impacts.
2727
The TypeSanitizer Algorithm
2828
===========================
2929
For each TBAA type-access descriptor, encoded in LLVM IR using TBAA Metadata, the instrumentation
30-
pass generates descriptor tales. Thus there is a unique pointer to each type (and access descriptor).
30+
pass generates descriptor tables. Thus there is a unique pointer to each type (and access descriptor).
3131
These tables are comdat (except for anonymous-namespace types), so the pointer values are unique
3232
across the program.
3333

clang/docs/analyzer/developer-docs/PerformanceInvestigation.rst

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Performance Investigation
55
Multiple factors contribute to the time it takes to analyze a file with Clang Static Analyzer.
66
A translation unit contains multiple entry points, each of which take multiple steps to analyze.
77

8+
Performance analysis using ``-ftime-trace``
9+
===========================================
10+
811
You can add the ``-ftime-trace=file.json`` option to break down the analysis time into individual entry points and steps within each entry point.
912
You can explore the generated JSON file in a Chromium browser using the ``chrome://tracing`` URL,
1013
or using `speedscope <https://speedscope.app>`_.
@@ -19,9 +22,8 @@ Here is an example of a time trace produced with
1922
.. code-block:: bash
2023
:caption: Clang Static Analyzer invocation to generate a time trace of string.c analysis.
2124
22-
clang -cc1 -nostdsysteminc -analyze -analyzer-constraints=range \
23-
-setup-static-analyzer -analyzer-checker=core,unix,alpha.unix.cstring,debug.ExprInspection \
24-
-verify ./clang/test/Analysis/string.c \
25+
clang -cc1 -analyze -verify clang/test/Analysis/string.c \
26+
-analyzer-checker=core,unix,alpha.unix.cstring,debug.ExprInspection \
2527
-ftime-trace=trace.json -ftime-trace-granularity=1
2628
2729
.. image:: ../images/speedscope.png
@@ -45,3 +47,91 @@ Note: Both Chrome-tracing and speedscope tools might struggle with time traces a
4547
Luckily, in most cases the default max-steps boundary of 225 000 produces the traces of approximately that size
4648
for a single entry point.
4749
You can use ``-analyze-function=get_global_options`` together with ``-ftime-trace`` to narrow down analysis to a specific entry point.
50+
51+
52+
Performance analysis using ``perf``
53+
===================================
54+
55+
`Perf <https://perfwiki.github.io/main/>`_ is a tool for conducting sampling-based profiling.
56+
It's easy to start profiling, you only have 2 prerequisites.
57+
Build with ``-fno-omit-frame-pointer`` and debug info (``-g``).
58+
You can use release builds, but probably the easiest is to set the ``CMAKE_BUILD_TYPE=RelWithDebInfo``
59+
along with ``CMAKE_CXX_FLAGS="-fno-omit-frame-pointer"`` when configuring ``llvm``.
60+
Here is how to `get started <https://llvm.org/docs/CMake.html#quick-start>`_ if you are in trouble.
61+
62+
.. code-block:: bash
63+
:caption: Running the Clang Static Analyzer through ``perf`` to gather samples of the execution.
64+
65+
# -F: Sampling frequency, use `-F max` for maximal frequency
66+
# -g: Enable call-graph recording for both kernel and user space
67+
perf record -F 99 -g -- clang -cc1 -analyze -verify clang/test/Analysis/string.c \
68+
-analyzer-checker=core,unix,alpha.unix.cstring,debug.ExprInspection
69+
70+
Once you have the profile data, you can use it to produce a Flame graph.
71+
A Flame graph is a visual representation of the stack frames of the samples.
72+
Common stack frame prefixes are squashed together, making up a wider bar.
73+
The wider the bar, the more time was spent under that particular stack frame,
74+
giving a sense of how the overall execution time was spent.
75+
76+
Clone the `FlameGraph <https://github.com/brendangregg/FlameGraph>`_ git repository,
77+
as we will use some scripts from there to convert the ``perf`` samples into a Flame graph.
78+
It's also useful to check out Brendan Gregg's (the author of FlameGraph)
79+
`homepage <https://www.brendangregg.com/FlameGraphs/cpuflamegraphs.html>`_.
80+
81+
82+
.. code-block:: bash
83+
:caption: Converting the ``perf`` profile into a Flamegraph, then opening it in Firefox.
84+
85+
perf script | /path/to/FlameGraph/stackcollapse-perf.pl > perf.folded
86+
/path/to/FlameGraph/flamegraph.pl perf.folded > perf.svg
87+
firefox perf.svg
88+
89+
.. image:: ../images/flamegraph.png
90+
91+
92+
Performance analysis using ``uftrace``
93+
======================================
94+
95+
`uftrace <https://github.com/namhyung/uftrace/wiki/Tutorial#getting-started>`_ is a great tool to generate rich profile data
96+
that you can use to focus and drill down into the timeline of your application.
97+
We will use it to generate Chromium trace JSON.
98+
In contrast to ``perf``, this approach statically instruments every function, so it should be more precise and thorough than the sampling-based approaches like ``perf``.
99+
In contrast to using ``-ftime-trace``, functions don't need to opt-in to be profiled using ``llvm::TimeTraceScope``.
100+
All functions are profiled due to automatic static instrumentation.
101+
102+
There is only one prerequisite to use this tool.
103+
You need to build the binary you are about to instrument using ``-pg`` or ``-finstrument-functions``.
104+
This will make it run substantially slower but allows rich instrumentation.
105+
It will also consume many gigabites of storage for a single trace unless filter flags are used during recording.
106+
107+
.. code-block:: bash
108+
:caption: Recording with ``uftrace``, then dumping the result as a Chrome trace JSON.
109+
110+
uftrace record clang -cc1 -analyze -verify clang/test/Analysis/string.c \
111+
-analyzer-checker=core,unix,alpha.unix.cstring,debug.ExprInspection
112+
uftrace dump --filter=".*::AnalysisConsumer::HandleTranslationUnit" --time-filter=300 --chrome > trace.json
113+
114+
.. image:: ../images/uftrace_detailed.png
115+
116+
In this picture, you can see the functions below the Static Analyzer's entry point, which takes at least 300 nanoseconds to run, visualized by Chrome's ``about:tracing`` page
117+
You can also see how deep function calls we may have due to AST visitors.
118+
119+
Using different filters can reduce the number of functions to record.
120+
For the common options, refer to the ``uftrace`` `documentation <https://github.com/namhyung/uftrace/blob/master/doc/uftrace-record.md#common-options>`_.
121+
122+
Similar filters can be applied for dumping too. That way you can reuse the same (detailed)
123+
recording to selectively focus on some special part using a refinement of the filter flags.
124+
Remember, the trace JSON needs to fit into Chrome's ``about:tracing`` or `speedscope <https://speedscope.app>`_,
125+
thus it needs to be of a limited size.
126+
If you do not apply filters on recording, you will collect a large trace and every dump operation
127+
would need to sieve through the much larger recording which may be annoying if done repeatedly.
128+
129+
If the trace JSON is still too large to load, have a look at the dump as plain text and look for frequent entries that refer to non-interesting parts.
130+
Once you have some of those, add them as ``--hide`` flags to the ``uftrace dump`` call.
131+
To see what functions appear frequently in the trace, use this command:
132+
133+
.. code-block:: bash
134+
135+
cat trace.json | grep -Po '"name":"(.+)"' | sort | uniq -c | sort -nr | head -n 50
136+
137+
``uftrace`` can also dump the report as a Flame graph using ``uftrace dump --framegraph``.
72.6 KB
Loading
59.4 KB
Loading

0 commit comments

Comments
 (0)