-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][dataflow] Teach AnalysisASTVisitor
that typeid()
can be evaluated.
#96731
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
[clang][dataflow] Teach AnalysisASTVisitor
that typeid()
can be evaluated.
#96731
Conversation
…valuated. We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
@llvm/pr-subscribers-clang-analysis @llvm/pr-subscribers-clang Author: None (martinboehme) ChangesWe were previously treating the operand of This patch includes a test that fails without the fix. Full diff: https://github.com/llvm/llvm-project/pull/96731.diff 2 Files Affected:
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 2a74d7fa63fd7..10d7ec9380ab8 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 {
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/144/builds/913 Here is the relevant piece of the build log for the reference:
|
…valuated. (#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…valuated. (#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…valuated. (#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…valuated. (#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…valuated. (#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…valuated. (llvm#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…can be evaluated." (llvm#96766) Reverts llvm#96731 It causes CI failures.
…valuated. (#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…valuated. (llvm#96731) We were previously treating the operand of `typeid()` as being definitely unevaluated, but it can be evaluated if it is a glvalue of polymorphic type. This patch includes a test that fails without the fix.
…can be evaluated." (llvm#96766) Reverts llvm#96731 It causes CI failures.
We were previously treating the operand of
typeid()
as being definitelyunevaluated, but it can be evaluated if it is a glvalue of polymorphic type.
This patch includes a test that fails without the fix.