https://github.com/T-Gruber updated https://github.com/llvm/llvm-project/pull/112313
>From dd562bb3d505c43070ceb8af51359cc66860a0ea Mon Sep 17 00:00:00 2001 From: "tobias.gruber" <tobias.gru...@concentrio.io> Date: Tue, 15 Oct 2024 07:19:12 +0200 Subject: [PATCH 1/5] Handle FieldRegions with ElementRegions as SuperRegions in getDescriptiveName --- clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 25 +++++++++++++++---- .../MemRegionDescriptiveNameTest.cpp | 14 +++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp index 693791c3aee8b9..4144cff8607926 100644 --- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -751,12 +751,27 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const { } // Get variable name. - if (R && R->canPrintPrettyAsExpr()) { - R->printPrettyAsExpr(os); - if (UseQuotes) - return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str(); - else + if (R) { + // MemRegion can be pretty printed. + if (R->canPrintPrettyAsExpr()) { + R->printPrettyAsExpr(os); + if (UseQuotes) + return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str(); return (llvm::Twine(os.str()) + ArrayIndices).str(); + } + + // FieldRegion may have ElementRegion as SuperRegion. + if (const clang::ento::FieldRegion *FR = + R->getAs<clang::ento::FieldRegion>()) { + std::string Super = FR->getSuperRegion()->getDescriptiveName(false); + if (Super.empty()) + return ""; + + if (UseQuotes) + return (llvm::Twine("'") + Super + "." + FR->getDecl()->getName() + "'") + .str(); + return (llvm::Twine(Super) + "." + FR->getDecl()->getName()).str(); + } } return VariableName; diff --git a/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp b/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp index b13e7123ee524d..fe5defd1d47915 100644 --- a/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp +++ b/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp @@ -143,4 +143,18 @@ void top() { EXPECT_EQ(Output, "DescriptiveNameChecker: array[x]\n"); } +TEST(MemRegionDescriptiveNameTest, FieldRegWithSuperElementReg) { + StringRef Code = R"cpp( +void reportDescriptiveName(int *p); +struct val_struct { int val; }; +extern struct val_struct val_struct_array[3]; +void top() { + reportDescriptiveName(&val_struct_array[0].val); +})cpp"; + + std::string Output; + ASSERT_TRUE(runChecker(Code, Output)); + EXPECT_EQ(Output, "DescriptiveNameChecker: val_struct_array[0].val\n"); +} + } // namespace >From d8837ca3427e04d0d8a070ddce93da1485f2835f Mon Sep 17 00:00:00 2001 From: "tobias.gruber" <tobias.gru...@concentrio.io> Date: Tue, 15 Oct 2024 07:20:21 +0200 Subject: [PATCH 2/5] Remove unneeded include --- clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp b/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp index fe5defd1d47915..966e5c0e9a6124 100644 --- a/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp +++ b/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp @@ -12,7 +12,6 @@ #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" #include "gtest/gtest.h" -#include <fstream> using namespace clang; using namespace ento; >From b2b269fbc479751dafa5aa85a58a0a6756f442bf Mon Sep 17 00:00:00 2001 From: "tobias.gruber" <tobias.gru...@concentrio.io> Date: Tue, 15 Oct 2024 12:37:46 +0200 Subject: [PATCH 3/5] Include test case with multi-dim array --- .../MemRegionDescriptiveNameTest.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp b/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp index 966e5c0e9a6124..0f6e49bf42f4ac 100644 --- a/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp +++ b/clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp @@ -156,4 +156,18 @@ void top() { EXPECT_EQ(Output, "DescriptiveNameChecker: val_struct_array[0].val\n"); } +TEST(MemRegionDescriptiveNameTest, FieldRegWithSuperMultidimElementReg) { + StringRef Code = R"cpp( +void reportDescriptiveName(int *p); +struct val_struct { int val; }; +extern struct val_struct val_struct_array[3][4]; +void top() { + reportDescriptiveName(&val_struct_array[1][2].val); +})cpp"; + + std::string Output; + ASSERT_TRUE(runChecker(Code, Output)); + EXPECT_EQ(Output, "DescriptiveNameChecker: val_struct_array[1][2].val\n"); +} + } // namespace >From 565de601620ee7dfe7156302f4169e6d46588a5d Mon Sep 17 00:00:00 2001 From: "tobias.gruber" <tobias.gru...@concentrio.io> Date: Tue, 15 Oct 2024 13:53:18 +0200 Subject: [PATCH 4/5] Function to enclose in quotes if needed --- clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp index 4144cff8607926..f4ead45d15c218 100644 --- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -722,6 +722,13 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const { SmallString<50> buf; llvm::raw_svector_ostream os(buf); + // Enclose subject with single quotes if needed. + auto QuoteIfNeeded = [UseQuotes](const Twine &Subject) -> std::string { + if (UseQuotes) + return ("'" + Subject + "'").str(); + return Subject.str(); + }; + // Obtain array indices to add them to the variable name. const ElementRegion *ER = nullptr; while ((ER = R->getAs<ElementRegion>())) { @@ -755,22 +762,15 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const { // MemRegion can be pretty printed. if (R->canPrintPrettyAsExpr()) { R->printPrettyAsExpr(os); - if (UseQuotes) - return (llvm::Twine("'") + os.str() + ArrayIndices + "'").str(); - return (llvm::Twine(os.str()) + ArrayIndices).str(); + return QuoteIfNeeded(llvm::Twine(os.str()) + ArrayIndices); } // FieldRegion may have ElementRegion as SuperRegion. - if (const clang::ento::FieldRegion *FR = - R->getAs<clang::ento::FieldRegion>()) { + if (const auto *FR = R->getAs<clang::ento::FieldRegion>()) { std::string Super = FR->getSuperRegion()->getDescriptiveName(false); if (Super.empty()) return ""; - - if (UseQuotes) - return (llvm::Twine("'") + Super + "." + FR->getDecl()->getName() + "'") - .str(); - return (llvm::Twine(Super) + "." + FR->getDecl()->getName()).str(); + return QuoteIfNeeded(Super + "." + FR->getDecl()->getName()); } } >From ca8f5f3a6233ae4409e8a4edbb50a4c9996b6e55 Mon Sep 17 00:00:00 2001 From: T-Gruber <100079402+t-gru...@users.noreply.github.com> Date: Wed, 23 Oct 2024 10:41:19 +0200 Subject: [PATCH 5/5] Update clang/lib/StaticAnalyzer/Core/MemRegion.cpp Co-authored-by: Balazs Benics <benicsbal...@gmail.com> --- clang/lib/StaticAnalyzer/Core/MemRegion.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp index f4ead45d15c218..6e9293bd9f855a 100644 --- a/clang/lib/StaticAnalyzer/Core/MemRegion.cpp +++ b/clang/lib/StaticAnalyzer/Core/MemRegion.cpp @@ -766,7 +766,7 @@ std::string MemRegion::getDescriptiveName(bool UseQuotes) const { } // FieldRegion may have ElementRegion as SuperRegion. - if (const auto *FR = R->getAs<clang::ento::FieldRegion>()) { + if (const auto *FR = R->getAs<FieldRegion>()) { std::string Super = FR->getSuperRegion()->getDescriptiveName(false); if (Super.empty()) return ""; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits