From a87e09cd75f5c55f858f7089d2f8eac8a007de6d Mon Sep 17 00:00:00 2001 From: Artem Dergachev Date: Thu, 19 Apr 2018 23:09:22 +0000 Subject: [PATCH] [CFG] [analyzer] Don't treat argument constructors as temporary constructors. Function argument constructors (that are used for passing objects into functions by value) are completely unlike temporary object constructors, but we were treating them as such because they are also wrapped into a CXXBindTemporaryExpr. This patch adds a partial construction context layer for call argument values, but doesn't proceed to transform it into an actual construction context yet. This is tells the clients that we aren't supporting these constructors yet. Differential Revision: https://reviews.llvm.org/D45650 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@330377 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/CFG.cpp | 7 +++ lib/Analysis/ConstructionContext.cpp | 6 +++ test/Analysis/cfg-rich-constructors.cpp | 72 +++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index 34d4016227..66019329a3 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -2367,6 +2367,13 @@ CFGBlock *CFGBuilder::VisitCallExpr(CallExpr *C, AddStmtChoice asc) { if (!boundType.isNull()) calleeType = boundType; } + // FIXME: Once actually implemented, this construction context layer should + // include the number of the argument as well. + for (auto Arg: C->arguments()) { + findConstructionContexts( + ConstructionContextLayer::create(cfg->getBumpVectorContext(), C), Arg); + } + // If this is a call to a no-return function, this stops the block here. bool NoReturn = getFunctionExtInfo(*calleeType).getNoReturn(); diff --git a/lib/Analysis/ConstructionContext.cpp b/lib/Analysis/ConstructionContext.cpp index 9f3c00b2bc..8656a1bbe0 100644 --- a/lib/Analysis/ConstructionContext.cpp +++ b/lib/Analysis/ConstructionContext.cpp @@ -79,6 +79,9 @@ const ConstructionContext *ConstructionContext::createFromLayers( ParentLayer->getTriggerStmt()))) { return create(C, BTE, MTE); } + // This is a constructor into a function argument. Not implemented yet. + if (auto *CE = dyn_cast(ParentLayer->getTriggerStmt())) + return nullptr; // This is C++17 copy-elided construction into return statement. if (auto *RS = dyn_cast(ParentLayer->getTriggerStmt())) { assert(!RS->getRetValue()->getType().getCanonicalType() @@ -114,6 +117,9 @@ const ConstructionContext *ConstructionContext::createFromLayers( assert(TopLayer->isLast()); return create(C, RS); } + // This is a constructor into a function argument. Not implemented yet. + if (auto *CE = dyn_cast(TopLayer->getTriggerStmt())) + return nullptr; llvm_unreachable("Unexpected construction context with statement!"); } else if (const CXXCtorInitializer *I = TopLayer->getTriggerInit()) { assert(TopLayer->isLast()); diff --git a/test/Analysis/cfg-rich-constructors.cpp b/test/Analysis/cfg-rich-constructors.cpp index 51eca12229..eaaa5786f8 100644 --- a/test/Analysis/cfg-rich-constructors.cpp +++ b/test/Analysis/cfg-rich-constructors.cpp @@ -693,3 +693,75 @@ void implicitConstructionConversionFromFunctionValueWithLifetimeExtension() { } } // end namespace implicit_constructor_conversion + +namespace argument_constructors { +class D { +public: + D(); + ~D(); +}; + +void useC(C c); +void useCByReference(const C &c); +void useD(D d); +void useDByReference(const D &d); + +// FIXME: Find construction context for the argument. +// CHECK: void passArgument() +// CHECK: 1: useC +// CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(class C)) +// CXX11-NEXT: 3: C() (CXXConstructExpr, [B1.4], class C) +// CXX11-NEXT: 4: [B1.3] +// CXX11-NEXT: 5: [B1.4] (CXXConstructExpr, class C) +// CXX11-NEXT: 6: [B1.2]([B1.5]) +// CXX17-NEXT: 3: C() (CXXConstructExpr, class C) +// CXX17-NEXT: 4: [B1.2]([B1.3]) +void passArgument() { + useC(C()); +} + +// CHECK: void passArgumentByReference() +// CHECK: 1: useCByReference +// CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(const class C &)) +// CHECK-NEXT: 3: C() (CXXConstructExpr, [B1.5], class C) +// CHECK-NEXT: 4: [B1.3] (ImplicitCastExpr, NoOp, const class C) +// CHECK-NEXT: 5: [B1.4] +// CHECK-NEXT: 6: [B1.2]([B1.5]) +void passArgumentByReference() { + useCByReference(C()); +} + +// FIXME: Find construction context for the argument. +// CHECK: void passArgumentWithDestructor() +// CHECK: 1: useD +// CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(class argument_constructors::D)) +// CXX11-NEXT: 3: argument_constructors::D() (CXXConstructExpr, [B1.4], [B1.6], class argument_constructors::D) +// CXX11-NEXT: 4: [B1.3] (BindTemporary) +// CXX11-NEXT: 5: [B1.4] (ImplicitCastExpr, NoOp, const class argument_constructors::D) +// CXX11-NEXT: 6: [B1.5] +// CXX11-NEXT: 7: [B1.6] (CXXConstructExpr, class argument_constructors::D) +// CXX11-NEXT: 8: [B1.7] (BindTemporary) +// CXX11-NEXT: 9: [B1.2]([B1.8]) +// CXX11-NEXT: 10: ~argument_constructors::D() (Temporary object destructor) +// CXX11-NEXT: 11: ~argument_constructors::D() (Temporary object destructor) +// CXX17-NEXT: 3: argument_constructors::D() (CXXConstructExpr, class argument_constructors::D) +// CXX17-NEXT: 4: [B1.3] (BindTemporary) +// CXX17-NEXT: 5: [B1.2]([B1.4]) +// CXX17-NEXT: 6: ~argument_constructors::D() (Temporary object destructor) +void passArgumentWithDestructor() { + useD(D()); +} + +// CHECK: void passArgumentWithDestructorByReference() +// CHECK: 1: useDByReference +// CHECK-NEXT: 2: [B1.1] (ImplicitCastExpr, FunctionToPointerDecay, void (*)(const class argumen +// CHECK-NEXT: 3: argument_constructors::D() (CXXConstructExpr, [B1.4], [B1.6], class argument_c +// CHECK-NEXT: 4: [B1.3] (BindTemporary) +// CHECK-NEXT: 5: [B1.4] (ImplicitCastExpr, NoOp, const class argument_constructors::D) +// CHECK-NEXT: 6: [B1.5] +// CHECK-NEXT: 7: [B1.2]([B1.6]) +// CHECK-NEXT: 8: ~argument_constructors::D() (Temporary object destructor) +void passArgumentWithDestructorByReference() { + useDByReference(D()); +} +} // end namespace argument_constructors -- 2.50.1