Skip to content

[clang][dataflow] reland #96766 with fix #98896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion clang/include/clang/Analysis/FlowSensitive/ASTOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ class AnalysisASTVisitor : public RecursiveASTVisitor<Derived> {
// nevertheless it appears in the Clang CFG, so we don't exclude it here.
bool TraverseDecltypeTypeLoc(DecltypeTypeLoc) { return true; }
bool TraverseTypeOfExprTypeLoc(TypeOfExprTypeLoc) { return true; }
bool TraverseCXXTypeidExpr(CXXTypeidExpr *) { return true; }
bool TraverseCXXTypeidExpr(CXXTypeidExpr *TIE) {
if (TIE->isPotentiallyEvaluated())
return RecursiveASTVisitor<Derived>::TraverseCXXTypeidExpr(TIE);
return true;
}
bool TraverseUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *) {
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,18 @@ llvm::Error test::checkDataflowWithNoopAnalysis(
DataflowAnalysisOptions Options, LangStandard::Kind Std,
std::function<llvm::StringMap<QualType>(QualType)> SyntheticFieldCallback) {
llvm::SmallVector<std::string, 3> ASTBuildArgs = {
"-fsyntax-only",
// -fnodelayed-template-parsing is the default everywhere but on Windows.
// Set it explicitly so that tests behave the same on Windows as on other
// platforms.
"-fno-delayed-template-parsing",
// Set -Wno-unused-value because it's often desirable in tests to write
// expressions with unused value, and we don't want the output to be
// cluttered with warnings about them.
"-fsyntax-only", "-fno-delayed-template-parsing", "-Wno-unused-value",
"-Wno-unused-value",
// Some build environments don't have RTTI enabled by default.
// Enable it explicitly to make sure tests work in all environments.
"-frtti",
"-std=" +
std::string(LangStandard::getLangStandardForKind(Std).getName())};
AnalysisInputs<NoopAnalysis> AI(
Expand Down
43 changes: 43 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,49 @@ TEST(TransferTest, StructModeledFieldsWithAccessor) {
});
}

TEST(TransferTest, StructModeledFieldsInTypeid) {
// Test that we model fields mentioned inside a `typeid()` expression only if
// that expression is potentially evaluated -- i.e. if the expression inside
// `typeid()` is a glvalue of polymorphic type (see
// `CXXTypeidExpr::isPotentiallyEvaluated()` and [expr.typeid]p3).
std::string Code = R"(
// Definitions needed for `typeid`.
namespace std {
class type_info {};
class bad_typeid {};
} // namespace std

struct NonPolymorphic {};

struct Polymorphic {
virtual ~Polymorphic() = default;
};

struct S {
NonPolymorphic *NonPoly;
Polymorphic *Poly;
};

void target(S &s) {
typeid(*s.NonPoly);
typeid(*s.Poly);
// [[p]]
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
const Environment &Env = getEnvironmentAtAnnotation(Results, "p");
auto &SLoc = getLocForDecl<RecordStorageLocation>(ASTCtx, Env, "s");
std::vector<const ValueDecl *> Fields;
for (auto [Field, _] : SLoc.children())
Fields.push_back(Field);
EXPECT_THAT(Fields,
UnorderedElementsAre(findValueDecl(ASTCtx, "Poly")));
});
}

TEST(TransferTest, StructModeledFieldsWithComplicatedInheritance) {
std::string Code = R"(
struct Base1 {
Expand Down
Loading