Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions clang/lib/StaticAnalyzer/Core/MemRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion clang/unittests/StaticAnalyzer/MemRegionDescriptiveNameTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,4 +142,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