https://github.com/martinboehme created https://github.com/llvm/llvm-project/pull/98896
- **Reapply "[clang][dataflow] Teach `AnalysisASTVisitor` that `typeid()` can be evaluated." (#96766)** - **Turn on RTTI explicitly in `checkDataflowWithNoopAnalysis()`.** >From b7dbf7d24e536c22c2a380d7b6de9bf7b4fc5ef7 Mon Sep 17 00:00:00 2001 From: Martin Braenne <mboe...@google.com> Date: Thu, 27 Jun 2024 07:30:02 +0000 Subject: [PATCH 1/2] Reapply "[clang][dataflow] Teach `AnalysisASTVisitor` that `typeid()` can be evaluated." (#96766) This reverts commit 6e96e5ab8a0e40fba0302a5c32574be41ef57354. --- .../clang/Analysis/FlowSensitive/ASTOps.h | 6 ++- .../Analysis/FlowSensitive/TransferTest.cpp | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Analysis/FlowSensitive/ASTOps.h b/clang/include/clang/Analysis/FlowSensitive/ASTOps.h index 925b99af9141a..f9c923a36ad22 100644 --- a/clang/include/clang/Analysis/FlowSensitive/ASTOps.h +++ b/clang/include/clang/Analysis/FlowSensitive/ASTOps.h @@ -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; } diff --git a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp index e743eefa5d458..39e7001393e5e 100644 --- a/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TransferTest.cpp @@ -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 { >From 273098962e7740331f46154bf4ed67d68b52c5bc Mon Sep 17 00:00:00 2001 From: Martin Braenne <mboe...@google.com> Date: Thu, 27 Jun 2024 08:04:30 +0000 Subject: [PATCH 2/2] Turn on RTTI explicitly in `checkDataflowWithNoopAnalysis()`. --- clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp b/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp index 5c4d42c6ccdcf..622de6323ad33 100644 --- a/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp +++ b/clang/unittests/Analysis/FlowSensitive/TestingSupport.cpp @@ -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( _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits