File tree Expand file tree Collapse file tree 2 files changed +49
-7
lines changed Expand file tree Collapse file tree 2 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -722,6 +722,13 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
722
722
SmallString<50 > buf;
723
723
llvm::raw_svector_ostream os (buf);
724
724
725
+ // Enclose subject with single quotes if needed.
726
+ auto QuoteIfNeeded = [UseQuotes](const Twine &Subject) -> std::string {
727
+ if (UseQuotes)
728
+ return (" '" + Subject + " '" ).str ();
729
+ return Subject.str ();
730
+ };
731
+
725
732
// Obtain array indices to add them to the variable name.
726
733
const ElementRegion *ER = nullptr ;
727
734
while ((ER = R->getAs <ElementRegion>())) {
@@ -751,12 +758,20 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const {
751
758
}
752
759
753
760
// Get variable name.
754
- if (R && R->canPrintPrettyAsExpr ()) {
755
- R->printPrettyAsExpr (os);
756
- if (UseQuotes)
757
- return (llvm::Twine (" '" ) + os.str () + ArrayIndices + " '" ).str ();
758
- else
759
- return (llvm::Twine (os.str ()) + ArrayIndices).str ();
761
+ if (R) {
762
+ // MemRegion can be pretty printed.
763
+ if (R->canPrintPrettyAsExpr ()) {
764
+ R->printPrettyAsExpr (os);
765
+ return QuoteIfNeeded (llvm::Twine (os.str ()) + ArrayIndices);
766
+ }
767
+
768
+ // FieldRegion may have ElementRegion as SuperRegion.
769
+ if (const auto *FR = R->getAs <FieldRegion>()) {
770
+ std::string Super = FR->getSuperRegion ()->getDescriptiveName (false );
771
+ if (Super.empty ())
772
+ return " " ;
773
+ return QuoteIfNeeded (Super + " ." + FR->getDecl ()->getName ());
774
+ }
760
775
}
761
776
762
777
return VariableName;
Original file line number Diff line number Diff line change 12
12
#include " clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
13
13
#include " clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
14
14
#include " gtest/gtest.h"
15
- #include < fstream>
16
15
17
16
using namespace clang ;
18
17
using namespace ento ;
@@ -143,4 +142,32 @@ void top() {
143
142
EXPECT_EQ (Output, " DescriptiveNameChecker: array[x]\n " );
144
143
}
145
144
145
+ TEST (MemRegionDescriptiveNameTest, FieldRegWithSuperElementReg) {
146
+ StringRef Code = R"cpp(
147
+ void reportDescriptiveName(int *p);
148
+ struct val_struct { int val; };
149
+ extern struct val_struct val_struct_array[3];
150
+ void top() {
151
+ reportDescriptiveName(&val_struct_array[0].val);
152
+ })cpp" ;
153
+
154
+ std::string Output;
155
+ ASSERT_TRUE (runChecker (Code, Output));
156
+ EXPECT_EQ (Output, " DescriptiveNameChecker: val_struct_array[0].val\n " );
157
+ }
158
+
159
+ TEST (MemRegionDescriptiveNameTest, FieldRegWithSuperMultidimElementReg) {
160
+ StringRef Code = R"cpp(
161
+ void reportDescriptiveName(int *p);
162
+ struct val_struct { int val; };
163
+ extern struct val_struct val_struct_array[3][4];
164
+ void top() {
165
+ reportDescriptiveName(&val_struct_array[1][2].val);
166
+ })cpp" ;
167
+
168
+ std::string Output;
169
+ ASSERT_TRUE (runChecker (Code, Output));
170
+ EXPECT_EQ (Output, " DescriptiveNameChecker: val_struct_array[1][2].val\n " );
171
+ }
172
+
146
173
} // namespace
You can’t perform that action at this time.
0 commit comments