[clang] 058f384 - [analyzer] Correctly propagate ConstructionContextLayer thru ParenExpr
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Mon May 24 01:20:44 PDT 2021
Author: Tomasz KamiĆski
Date: 2021-05-24T10:16:52+02:00
New Revision: 058f384ae94ae0ac94441043804e4a25d338d483
URL: https://github.com/llvm/llvm-project/commit/058f384ae94ae0ac94441043804e4a25d338d483
DIFF: https://github.com/llvm/llvm-project/commit/058f384ae94ae0ac94441043804e4a25d338d483.diff
LOG: [analyzer] Correctly propagate ConstructionContextLayer thru ParenExpr
Previously, information about `ConstructionContextLayer` was not
propagated thru causing the expression like:
Var c = (createVar());
To produce unrelated temporary for the `createVar()` result and conjure
a new symbol for the value of `c` in C++17 mode.
Reviewed By: steakhal
Patch By: tomasz-kaminski-sonarsource!
Differential Revision: https://reviews.llvm.org/D102835
Added:
Modified:
clang/lib/Analysis/CFG.cpp
clang/test/Analysis/Inputs/expected-plists/NewDelete-path-notes.cpp.plist
clang/test/Analysis/NewDelete-checker-test.cpp
clang/test/Analysis/NewDelete-path-notes.cpp
clang/test/Analysis/NewDeleteLeaks-PR19102.cpp
Removed:
################################################################################
diff --git a/clang/lib/Analysis/CFG.cpp b/clang/lib/Analysis/CFG.cpp
index 56445881873fa..ba5eceda24b5f 100644
--- a/clang/lib/Analysis/CFG.cpp
+++ b/clang/lib/Analysis/CFG.cpp
@@ -1456,6 +1456,13 @@ void CFGBuilder::findConstructionContexts(
// TODO: Handle other cases. For now, fail to find construction contexts.
break;
}
+ case Stmt::ParenExprClass: {
+ // If expression is placed into parenthesis we should propagate the parent
+ // construction context to subexpressions.
+ auto *PE = cast<ParenExpr>(Child);
+ findConstructionContexts(Layer, PE->getSubExpr());
+ break;
+ }
default:
break;
}
diff --git a/clang/test/Analysis/Inputs/expected-plists/NewDelete-path-notes.cpp.plist b/clang/test/Analysis/Inputs/expected-plists/NewDelete-path-notes.cpp.plist
index 59c180e5df7e8..9e4b784705835 100644
--- a/clang/test/Analysis/Inputs/expected-plists/NewDelete-path-notes.cpp.plist
+++ b/clang/test/Analysis/Inputs/expected-plists/NewDelete-path-notes.cpp.plist
@@ -12,7 +12,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>24</integer>
+ <key>line</key><integer>19</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -20,12 +20,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>24</integer>
+ <key>line</key><integer>19</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>24</integer>
+ <key>line</key><integer>19</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -45,12 +45,12 @@
<key>start</key>
<array>
<dict>
- <key>line</key><integer>17</integer>
+ <key>line</key><integer>12</integer>
<key>col</key><integer>1</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>17</integer>
+ <key>line</key><integer>12</integer>
<key>col</key><integer>4</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -58,12 +58,12 @@
<key>end</key>
<array>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -75,7 +75,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>12</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -83,12 +83,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>12</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>18</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -108,12 +108,12 @@
<key>start</key>
<array>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>18</integer>
+ <key>line</key><integer>13</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -121,12 +121,12 @@
<key>end</key>
<array>
<dict>
- <key>line</key><integer>20</integer>
+ <key>line</key><integer>15</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>20</integer>
+ <key>line</key><integer>15</integer>
<key>col</key><integer>4</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -142,12 +142,12 @@
<key>start</key>
<array>
<dict>
- <key>line</key><integer>20</integer>
+ <key>line</key><integer>15</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>20</integer>
+ <key>line</key><integer>15</integer>
<key>col</key><integer>4</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -155,12 +155,12 @@
<key>end</key>
<array>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -172,7 +172,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -180,12 +180,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>12</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -205,12 +205,12 @@
<key>start</key>
<array>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>21</integer>
+ <key>line</key><integer>16</integer>
<key>col</key><integer>10</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -218,12 +218,12 @@
<key>end</key>
<array>
<dict>
- <key>line</key><integer>24</integer>
+ <key>line</key><integer>19</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>24</integer>
+ <key>line</key><integer>19</integer>
<key>col</key><integer>8</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -243,7 +243,7 @@
<key>issue_hash_function_offset</key><string>7</string>
<key>location</key>
<dict>
- <key>line</key><integer>24</integer>
+ <key>line</key><integer>19</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -251,11 +251,11 @@
<dict>
<key>0</key>
<array>
- <integer>17</integer>
- <integer>18</integer>
- <integer>20</integer>
- <integer>21</integer>
- <integer>24</integer>
+ <integer>12</integer>
+ <integer>13</integer>
+ <integer>15</integer>
+ <integer>16</integer>
+ <integer>19</integer>
</array>
</dict>
</dict>
@@ -266,7 +266,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -274,12 +274,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>12</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -295,7 +295,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>29</integer>
+ <key>line</key><integer>24</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -313,12 +313,12 @@
<key>start</key>
<array>
<dict>
- <key>line</key><integer>29</integer>
+ <key>line</key><integer>24</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>29</integer>
+ <key>line</key><integer>24</integer>
<key>col</key><integer>5</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -326,12 +326,12 @@
<key>end</key>
<array>
<dict>
- <key>line</key><integer>30</integer>
+ <key>line</key><integer>25</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>30</integer>
+ <key>line</key><integer>25</integer>
<key>col</key><integer>8</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -343,7 +343,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>30</integer>
+ <key>line</key><integer>25</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -351,12 +351,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>30</integer>
+ <key>line</key><integer>25</integer>
<key>col</key><integer>3</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>30</integer>
+ <key>line</key><integer>25</integer>
<key>col</key><integer>13</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -372,7 +372,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -380,12 +380,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>12</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -405,12 +405,12 @@
<key>start</key>
<array>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>35</integer>
+ <key>line</key><integer>30</integer>
<key>col</key><integer>4</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -418,12 +418,12 @@
<key>end</key>
<array>
<dict>
- <key>line</key><integer>37</integer>
+ <key>line</key><integer>32</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>37</integer>
+ <key>line</key><integer>32</integer>
<key>col</key><integer>7</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -435,7 +435,7 @@
<key>kind</key><string>event</string>
<key>location</key>
<dict>
- <key>line</key><integer>37</integer>
+ <key>line</key><integer>32</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -443,12 +443,12 @@
<array>
<array>
<dict>
- <key>line</key><integer>37</integer>
+ <key>line</key><integer>32</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
<dict>
- <key>line</key><integer>37</integer>
+ <key>line</key><integer>32</integer>
<key>col</key><integer>11</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -472,7 +472,7 @@
<key>issue_hash_function_offset</key><string>3</string>
<key>location</key>
<dict>
- <key>line</key><integer>37</integer>
+ <key>line</key><integer>32</integer>
<key>col</key><integer>2</integer>
<key>file</key><integer>0</integer>
</dict>
@@ -480,11 +480,11 @@
<dict>
<key>0</key>
<array>
+ <integer>24</integer>
+ <integer>25</integer>
<integer>29</integer>
<integer>30</integer>
- <integer>34</integer>
- <integer>35</integer>
- <integer>37</integer>
+ <integer>32</integer>
</array>
</dict>
</dict>
diff --git a/clang/test/Analysis/NewDelete-checker-test.cpp b/clang/test/Analysis/NewDelete-checker-test.cpp
index f0d42171a8756..5a8711fa8a7ad 100644
--- a/clang/test/Analysis/NewDelete-checker-test.cpp
+++ b/clang/test/Analysis/NewDelete-checker-test.cpp
@@ -9,20 +9,23 @@
// RUN: -analyzer-checker=cplusplus.NewDelete \
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
//
-// RUN: %clang_analyze_cc1 -std=c++11 -fblocks %s \
+// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -verify %s \
+// RUN: -verify=expected,leak \
+// RUN: -analyzer-checker=core \
+// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
+//
+// RUN: %clang_analyze_cc1 -std=c++17 -fblocks %s \
// RUN: -verify=expected,newdelete \
// RUN: -analyzer-checker=core \
-// RUN: -analyzer-checker=cplusplus.NewDelete \
-// RUN: -analyzer-config c++-allocator-inlining=true
+// RUN: -analyzer-checker=cplusplus.NewDelete
//
-// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -verify %s \
+// RUN: %clang_analyze_cc1 -DLEAKS -std=c++17 -fblocks %s \
// RUN: -verify=expected,newdelete,leak \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=cplusplus.NewDelete \
-// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks \
-// RUN: -analyzer-config c++-allocator-inlining=true
+// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
//
-// RUN: %clang_analyze_cc1 -std=c++11 -fblocks -verify %s \
+// RUN: %clang_analyze_cc1 -std=c++17 -fblocks -verify %s \
// RUN: -verify=expected,leak \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
@@ -288,7 +291,7 @@ namespace reference_count {
explicit shared_ptr(T *p) : p(p), control(new control_block) {
control->retain();
}
- shared_ptr(shared_ptr &other) : p(other.p), control(other.control) {
+ shared_ptr(const shared_ptr &other) : p(other.p), control(other.control) {
if (control)
control->retain();
}
@@ -314,11 +317,26 @@ namespace reference_count {
}
};
+ template <typename T, typename... Args>
+ shared_ptr<T> make_shared(Args &&...args) {
+ return shared_ptr<T>(new T(static_cast<Args &&>(args)...));
+ }
+
void testSingle() {
shared_ptr<int> a(new int);
*a = 1;
}
+ void testMake() {
+ shared_ptr<int> a = make_shared<int>();
+ *a = 1;
+ }
+
+ void testMakeInParens() {
+ shared_ptr<int> a = (make_shared<int>()); // no warn
+ *a = 1;
+ }
+
void testDouble() {
shared_ptr<int> a(new int);
shared_ptr<int> b = a;
diff --git a/clang/test/Analysis/NewDelete-path-notes.cpp b/clang/test/Analysis/NewDelete-path-notes.cpp
index e4093fd152f6c..2837fd1951e23 100644
--- a/clang/test/Analysis/NewDelete-path-notes.cpp
+++ b/clang/test/Analysis/NewDelete-path-notes.cpp
@@ -4,11 +4,6 @@
// RUN: -analyzer-output=text -verify %s
// RUN: %clang_analyze_cc1 \
// RUN: -analyzer-checker=cplusplus.NewDelete,unix.Malloc \
-// RUN: -analyzer-config c++-allocator-inlining=true \
-// RUN: -analyzer-config add-pop-up-notes=false \
-// RUN: -analyzer-output=text -verify %s
-// RUN: %clang_analyze_cc1 \
-// RUN: -analyzer-checker=cplusplus.NewDelete,unix.Malloc \
// RUN: -analyzer-config add-pop-up-notes=false \
// RUN: -analyzer-output=plist %s -o %t.plist
// RUN: %normalize_plist <%t.plist |
diff -ub \
diff --git a/clang/test/Analysis/NewDeleteLeaks-PR19102.cpp b/clang/test/Analysis/NewDeleteLeaks-PR19102.cpp
index 625b2d4b7af4a..502db6122f5cb 100644
--- a/clang/test/Analysis/NewDeleteLeaks-PR19102.cpp
+++ b/clang/test/Analysis/NewDeleteLeaks-PR19102.cpp
@@ -1,5 +1,4 @@
// RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDeleteLeaks -verify %s
-// RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.NewDeleteLeaks -analyzer-config c++-allocator-inlining=true -verify %s
class A0 {};
More information about the cfe-commits
mailing list