From f34d92b55bb16ecfa6cb5808c3ca455b90c85429 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Fri, 14 Oct 2016 23:38:16 +0000 Subject: [PATCH] [Coverage] Support for C++17 if initializers Differential Revision: https://reviews.llvm.org/D25572 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284293 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenPGO.cpp | 2 ++ lib/CodeGen/CoverageMappingGen.cpp | 3 +++ test/CoverageMapping/{if.c => if.cpp} | 13 ++++++++++++- test/Profile/cxx-stmt-initializers.cpp | 12 ++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) rename test/CoverageMapping/{if.c => if.cpp} (70%) diff --git a/lib/CodeGen/CodeGenPGO.cpp b/lib/CodeGen/CodeGenPGO.cpp index 02738eb209..c6c3fa41e6 100644 --- a/lib/CodeGen/CodeGenPGO.cpp +++ b/lib/CodeGen/CodeGenPGO.cpp @@ -490,6 +490,8 @@ struct ComputeRegionCounts : public ConstStmtVisitor { void VisitIfStmt(const IfStmt *S) { RecordStmtCount(S); uint64_t ParentCount = CurrentCount; + if (S->getInit()) + Visit(S->getInit()); Visit(S->getCond()); // Counter tracks the "then" part of an if statement. The count for diff --git a/lib/CodeGen/CoverageMappingGen.cpp b/lib/CodeGen/CoverageMappingGen.cpp index 403fbceefc..530ac83611 100644 --- a/lib/CodeGen/CoverageMappingGen.cpp +++ b/lib/CodeGen/CoverageMappingGen.cpp @@ -875,6 +875,9 @@ struct CounterCoverageMappingBuilder void VisitIfStmt(const IfStmt *S) { extendRegion(S); + if (S->getInit()) + Visit(S->getInit()); + // Extend into the condition before we propagate through it below - this is // needed to handle macros that generate the "if" but not the condition. extendRegion(S->getCond()); diff --git a/test/CoverageMapping/if.c b/test/CoverageMapping/if.cpp similarity index 70% rename from test/CoverageMapping/if.c rename to test/CoverageMapping/if.cpp index 69544f68cf..95e6d8abe6 100644 --- a/test/CoverageMapping/if.c +++ b/test/CoverageMapping/if.cpp @@ -1,5 +1,16 @@ -// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -main-file-name if.c %s | FileCheck %s +// RUN: %clang_cc1 -fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping -emit-llvm-only -std=c++1z -triple %itanium_abi_triple -main-file-name if.cpp %s | FileCheck %s +int nop() { return 0; } + +// CHECK-LABEL: _Z3foov: +void foo() { // CHECK-NEXT: [[@LINE]]:12 -> [[@LINE+5]]:2 = #0 + if (int j = true ? nop() // CHECK-NEXT: [[@LINE]]:22 -> [[@LINE]]:27 = #2 + : nop(); // CHECK-NEXT: [[@LINE]]:22 -> [[@LINE]]:27 = (#0 - #2) + j) // CHECK-NEXT: [[@LINE]]:7 -> [[@LINE]]:8 = #0 + ++j; // CHECK-NEXT: [[@LINE]]:5 -> [[@LINE]]:8 = #1 +} + +// CHECK-LABEL: main: int main() { // CHECK: File 0, [[@LINE]]:12 -> {{[0-9]+}}:2 = #0 int i = 0; // CHECK-NEXT: File 0, [[@LINE+1]]:6 -> [[@LINE+1]]:12 = #0 diff --git a/test/Profile/cxx-stmt-initializers.cpp b/test/Profile/cxx-stmt-initializers.cpp index 0e3d54449a..44f7edd276 100644 --- a/test/Profile/cxx-stmt-initializers.cpp +++ b/test/Profile/cxx-stmt-initializers.cpp @@ -4,6 +4,7 @@ // RUN: FileCheck --input-file=%tgen -check-prefix=CHECK -check-prefix=PGOGEN %s // PGOGEN: @[[SIC:__profc__Z11switch_initv]] = private global [3 x i64] zeroinitializer +// PGOGEN: @[[IIC:__profc__Z7if_initv]] = private global [3 x i64] zeroinitializer // Note: We expect counters for the function entry block, the condition in the // switch initializer, and the switch successor block. @@ -15,3 +16,14 @@ void switch_init() { // PGOGEN: store {{.*}} @[[SIC]], i64 0, i64 2 // PGOGEN: store {{.*}} @[[SIC]], i64 0, i64 1 } + +// Note: We expect counters for the function entry block, the condition in the +// if initializer, and the if successor block. +// +// CHECK-LABEL: define {{.*}}void @_Z7if_initv() +// PGOGEN: store {{.*}} @[[IIC]], i64 0, i64 0 +void if_init() { + if (int i = true ? 0 : 1; i) {} + // PGOGEN: store {{.*}} @[[IIC]], i64 0, i64 2 + // PGOGEN: store {{.*}} @[[IIC]], i64 0, i64 1 +} -- 2.40.0