[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-22 Thread NAKAMURA Takumi via cfe-commits

https://github.com/chapuni closed 
https://github.com/llvm/llvm-project/pull/89572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-22 Thread Alan Phipps via cfe-commits

https://github.com/evodius96 approved this pull request.


https://github.com/llvm/llvm-project/pull/89572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-22 Thread NAKAMURA Takumi via cfe-commits

https://github.com/chapuni edited 
https://github.com/llvm/llvm-project/pull/89572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-22 Thread Jessica Paquette via cfe-commits

https://github.com/ornata approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/89572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-20 Thread NAKAMURA Takumi via cfe-commits


@@ -190,6 +190,16 @@ class SourceMappingRegion {
 
   bool isBranch() const { return FalseCount.has_value(); }
 
+  bool isMCDCBranch() const {
+const auto *BranchParams = 
std::get_if();
+assert(BranchParams == nullptr || BranchParams->ID >= 0);
+return (BranchParams != nullptr);
+  }
+
+  const auto () const {

chapuni wrote:

Removed. (It is in another request)

https://github.com/llvm/llvm-project/pull/89572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-20 Thread NAKAMURA Takumi via cfe-commits

https://github.com/chapuni updated 
https://github.com/llvm/llvm-project/pull/89572

>From 13035b230fd51422f6c3223fcffab4f44bd00956 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi 
Date: Fri, 19 Apr 2024 15:26:34 +0900
Subject: [PATCH 1/4] [MC/DC][Coverage] Add assertions into emitSourceRegions()

`emitSourceRegions()` has bugs to emit malformed MC/DC coverage mappings.
They were detected in `llvm-cov` as the crash.

Detect inconsistencies earlier in `clang` with assertions.

* mcdc-system-headers.cpp covers #78920.
* mcdc-scratch-space.c covers #87000.
---
 clang/lib/CodeGen/CoverageMappingGen.cpp  | 29 ++--
 .../test/CoverageMapping/mcdc-scratch-space.c | 27 +++
 .../CoverageMapping/mcdc-system-headers.cpp   | 47 +++
 3 files changed, 98 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/CoverageMapping/mcdc-scratch-space.c
 create mode 100644 clang/test/CoverageMapping/mcdc-system-headers.cpp

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 71215da362d3d..95ad1967c8b67 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -190,11 +190,21 @@ class SourceMappingRegion {
 
   bool isBranch() const { return FalseCount.has_value(); }
 
+  bool isMCDCBranch() const {
+const auto *BranchParams = 
std::get_if();
+assert(BranchParams == nullptr || BranchParams->ID >= 0);
+return (BranchParams != nullptr);
+  }
+
+  const auto () const {
+return mcdc::getParams(MCDCParams);
+  }
+
   bool isMCDCDecision() const {
 const auto *DecisionParams =
 std::get_if();
-assert(!DecisionParams || DecisionParams->NumConditions > 0);
-return DecisionParams;
+assert(DecisionParams == nullptr || DecisionParams->NumConditions > 0);
+return (DecisionParams != nullptr);
   }
 
   const auto () const {
@@ -464,13 +474,19 @@ class CoverageMappingBuilder {
   // Ignore regions from system headers unless collecting coverage from
   // system headers is explicitly enabled.
   if (!SystemHeadersCoverage &&
-  SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
+  SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in system headers");
 continue;
+  }
 
   auto CovFileID = getCoverageFileID(LocStart);
   // Ignore regions that don't have a file, such as builtin macros.
-  if (!CovFileID)
+  if (!CovFileID) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in non-file regions");
 continue;
+  }
 
   SourceLocation LocEnd = Region.getEndLoc();
   assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
@@ -480,8 +496,11 @@ class CoverageMappingBuilder {
   // This not only suppresses redundant regions, but sometimes prevents
   // creating regions with wrong counters if, for example, a statement's
   // body ends at the end of a nested macro.
-  if (Filter.count(std::make_pair(LocStart, LocEnd)))
+  if (Filter.count(std::make_pair(LocStart, LocEnd))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition");
 continue;
+  }
 
   // Find the spelling locations for the mapping region.
   SpellingRegion SR{SM, LocStart, LocEnd};
diff --git a/clang/test/CoverageMapping/mcdc-scratch-space.c 
b/clang/test/CoverageMapping/mcdc-scratch-space.c
new file mode 100644
index 0..962d10653a028
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-scratch-space.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c99 -fcoverage-mcdc 
-fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping 
-emit-llvm-only %s
+// XFAIL: *
+// REQUIRES: asserts
+
+int builtin_macro0(int a) {
+  return (__LINE__
+  && a);
+}
+
+int builtin_macro1(int a) {
+  return (a
+  || __LINE__);
+}
+
+#define PRE(x) pre_##x
+
+int pre0(int pre_a, int b_post) {
+  return (PRE(a)
+  && b_post);
+}
+
+#define POST(x) x##_post
+
+int post0(int pre_a, int b_post) {
+  return (pre_a
+  || POST(b));
+}
diff --git a/clang/test/CoverageMapping/mcdc-system-headers.cpp 
b/clang/test/CoverageMapping/mcdc-system-headers.cpp
new file mode 100644
index 0..329bb37822a9e
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-system-headers.cpp
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping 
-dump-coverage-mapping  -fcoverage-mcdc -mllvm -system-headers-coverage 
-emit-llvm-only -o - %s | FileCheck %s
+
+// Will crash w/o -system-headers-coverage
+// RUN: not --crash %clang_cc1 -std=c++11 -fprofile-instrument=clang 
-fcoverage-mapping -dump-coverage-mapping -fcoverage-mcdc -emit-llvm-only -o - 
%s
+// REQUIRES: asserts
+
+#ifdef 

[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-05-19 Thread Jessica Paquette via cfe-commits


@@ -190,6 +190,16 @@ class SourceMappingRegion {
 
   bool isBranch() const { return FalseCount.has_value(); }
 
+  bool isMCDCBranch() const {
+const auto *BranchParams = 
std::get_if();
+assert(BranchParams == nullptr || BranchParams->ID >= 0);
+return (BranchParams != nullptr);
+  }
+
+  const auto () const {

ornata wrote:

unused function?

https://github.com/llvm/llvm-project/pull/89572
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-04-22 Thread NAKAMURA Takumi via cfe-commits

https://github.com/chapuni updated 
https://github.com/llvm/llvm-project/pull/89572

>From 13035b230fd51422f6c3223fcffab4f44bd00956 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi 
Date: Fri, 19 Apr 2024 15:26:34 +0900
Subject: [PATCH 1/2] [MC/DC][Coverage] Add assertions into emitSourceRegions()

`emitSourceRegions()` has bugs to emit malformed MC/DC coverage mappings.
They were detected in `llvm-cov` as the crash.

Detect inconsistencies earlier in `clang` with assertions.

* mcdc-system-headers.cpp covers #78920.
* mcdc-scratch-space.c covers #87000.
---
 clang/lib/CodeGen/CoverageMappingGen.cpp  | 29 ++--
 .../test/CoverageMapping/mcdc-scratch-space.c | 27 +++
 .../CoverageMapping/mcdc-system-headers.cpp   | 47 +++
 3 files changed, 98 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/CoverageMapping/mcdc-scratch-space.c
 create mode 100644 clang/test/CoverageMapping/mcdc-system-headers.cpp

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 71215da362d3d0..95ad1967c8b673 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -190,11 +190,21 @@ class SourceMappingRegion {
 
   bool isBranch() const { return FalseCount.has_value(); }
 
+  bool isMCDCBranch() const {
+const auto *BranchParams = 
std::get_if();
+assert(BranchParams == nullptr || BranchParams->ID >= 0);
+return (BranchParams != nullptr);
+  }
+
+  const auto () const {
+return mcdc::getParams(MCDCParams);
+  }
+
   bool isMCDCDecision() const {
 const auto *DecisionParams =
 std::get_if();
-assert(!DecisionParams || DecisionParams->NumConditions > 0);
-return DecisionParams;
+assert(DecisionParams == nullptr || DecisionParams->NumConditions > 0);
+return (DecisionParams != nullptr);
   }
 
   const auto () const {
@@ -464,13 +474,19 @@ class CoverageMappingBuilder {
   // Ignore regions from system headers unless collecting coverage from
   // system headers is explicitly enabled.
   if (!SystemHeadersCoverage &&
-  SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
+  SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in system headers");
 continue;
+  }
 
   auto CovFileID = getCoverageFileID(LocStart);
   // Ignore regions that don't have a file, such as builtin macros.
-  if (!CovFileID)
+  if (!CovFileID) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in non-file regions");
 continue;
+  }
 
   SourceLocation LocEnd = Region.getEndLoc();
   assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
@@ -480,8 +496,11 @@ class CoverageMappingBuilder {
   // This not only suppresses redundant regions, but sometimes prevents
   // creating regions with wrong counters if, for example, a statement's
   // body ends at the end of a nested macro.
-  if (Filter.count(std::make_pair(LocStart, LocEnd)))
+  if (Filter.count(std::make_pair(LocStart, LocEnd))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition");
 continue;
+  }
 
   // Find the spelling locations for the mapping region.
   SpellingRegion SR{SM, LocStart, LocEnd};
diff --git a/clang/test/CoverageMapping/mcdc-scratch-space.c 
b/clang/test/CoverageMapping/mcdc-scratch-space.c
new file mode 100644
index 00..962d10653a028b
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-scratch-space.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c99 -fcoverage-mcdc 
-fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping 
-emit-llvm-only %s
+// XFAIL: *
+// REQUIRES: asserts
+
+int builtin_macro0(int a) {
+  return (__LINE__
+  && a);
+}
+
+int builtin_macro1(int a) {
+  return (a
+  || __LINE__);
+}
+
+#define PRE(x) pre_##x
+
+int pre0(int pre_a, int b_post) {
+  return (PRE(a)
+  && b_post);
+}
+
+#define POST(x) x##_post
+
+int post0(int pre_a, int b_post) {
+  return (pre_a
+  || POST(b));
+}
diff --git a/clang/test/CoverageMapping/mcdc-system-headers.cpp 
b/clang/test/CoverageMapping/mcdc-system-headers.cpp
new file mode 100644
index 00..329bb37822a9ea
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-system-headers.cpp
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping 
-dump-coverage-mapping  -fcoverage-mcdc -mllvm -system-headers-coverage 
-emit-llvm-only -o - %s | FileCheck %s
+
+// Will crash w/o -system-headers-coverage
+// RUN: not --crash %clang_cc1 -std=c++11 -fprofile-instrument=clang 
-fcoverage-mapping -dump-coverage-mapping -fcoverage-mcdc -emit-llvm-only -o - 
%s
+// REQUIRES: asserts
+
+#ifdef 

[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-04-22 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-codegen

Author: NAKAMURA Takumi (chapuni)


Changes

`emitSourceRegions()` has bugs to emit malformed MC/DC coverage mappings. They 
were detected in `llvm-cov` as the crash.

Detect inconsistencies earlier in `clang` with assertions.

* mcdc-system-headers.cpp covers #78920.
* mcdc-scratch-space.c covers #87000.

---
Full diff: https://github.com/llvm/llvm-project/pull/89572.diff


3 Files Affected:

- (modified) clang/lib/CodeGen/CoverageMappingGen.cpp (+24-5) 
- (added) clang/test/CoverageMapping/mcdc-scratch-space.c (+27) 
- (added) clang/test/CoverageMapping/mcdc-system-headers.cpp (+47) 


``diff
diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 71215da362d3d0..95ad1967c8b673 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -190,11 +190,21 @@ class SourceMappingRegion {
 
   bool isBranch() const { return FalseCount.has_value(); }
 
+  bool isMCDCBranch() const {
+const auto *BranchParams = 
std::get_if();
+assert(BranchParams == nullptr || BranchParams->ID >= 0);
+return (BranchParams != nullptr);
+  }
+
+  const auto () const {
+return mcdc::getParams(MCDCParams);
+  }
+
   bool isMCDCDecision() const {
 const auto *DecisionParams =
 std::get_if();
-assert(!DecisionParams || DecisionParams->NumConditions > 0);
-return DecisionParams;
+assert(DecisionParams == nullptr || DecisionParams->NumConditions > 0);
+return (DecisionParams != nullptr);
   }
 
   const auto () const {
@@ -464,13 +474,19 @@ class CoverageMappingBuilder {
   // Ignore regions from system headers unless collecting coverage from
   // system headers is explicitly enabled.
   if (!SystemHeadersCoverage &&
-  SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
+  SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in system headers");
 continue;
+  }
 
   auto CovFileID = getCoverageFileID(LocStart);
   // Ignore regions that don't have a file, such as builtin macros.
-  if (!CovFileID)
+  if (!CovFileID) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in non-file regions");
 continue;
+  }
 
   SourceLocation LocEnd = Region.getEndLoc();
   assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
@@ -480,8 +496,11 @@ class CoverageMappingBuilder {
   // This not only suppresses redundant regions, but sometimes prevents
   // creating regions with wrong counters if, for example, a statement's
   // body ends at the end of a nested macro.
-  if (Filter.count(std::make_pair(LocStart, LocEnd)))
+  if (Filter.count(std::make_pair(LocStart, LocEnd))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition");
 continue;
+  }
 
   // Find the spelling locations for the mapping region.
   SpellingRegion SR{SM, LocStart, LocEnd};
diff --git a/clang/test/CoverageMapping/mcdc-scratch-space.c 
b/clang/test/CoverageMapping/mcdc-scratch-space.c
new file mode 100644
index 00..962d10653a028b
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-scratch-space.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c99 -fcoverage-mcdc 
-fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping 
-emit-llvm-only %s
+// XFAIL: *
+// REQUIRES: asserts
+
+int builtin_macro0(int a) {
+  return (__LINE__
+  && a);
+}
+
+int builtin_macro1(int a) {
+  return (a
+  || __LINE__);
+}
+
+#define PRE(x) pre_##x
+
+int pre0(int pre_a, int b_post) {
+  return (PRE(a)
+  && b_post);
+}
+
+#define POST(x) x##_post
+
+int post0(int pre_a, int b_post) {
+  return (pre_a
+  || POST(b));
+}
diff --git a/clang/test/CoverageMapping/mcdc-system-headers.cpp 
b/clang/test/CoverageMapping/mcdc-system-headers.cpp
new file mode 100644
index 00..329bb37822a9ea
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-system-headers.cpp
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping 
-dump-coverage-mapping  -fcoverage-mcdc -mllvm -system-headers-coverage 
-emit-llvm-only -o - %s | FileCheck %s
+
+// Will crash w/o -system-headers-coverage
+// RUN: not --crash %clang_cc1 -std=c++11 -fprofile-instrument=clang 
-fcoverage-mapping -dump-coverage-mapping -fcoverage-mcdc -emit-llvm-only -o - 
%s
+// REQUIRES: asserts
+
+#ifdef IS_SYSHEADER
+
+#pragma clang system_header
+#define CONST 42
+#define EXPR1(x) (x)
+#define EXPR2(x) ((x) * (x))
+
+#else
+
+#define IS_SYSHEADER
+#include __FILE__
+
+// CHECK: _Z5func0i:
+int func0(int a) {
+  // CHECK: Decision,File 0, [[@LINE+2]]:11 -> 

[clang] [MC/DC][Coverage] Add assertions into emitSourceRegions() (PR #89572)

2024-04-22 Thread NAKAMURA Takumi via cfe-commits

https://github.com/chapuni created 
https://github.com/llvm/llvm-project/pull/89572

`emitSourceRegions()` has bugs to emit malformed MC/DC coverage mappings. They 
were detected in `llvm-cov` as the crash.

Detect inconsistencies earlier in `clang` with assertions.

* mcdc-system-headers.cpp covers #78920.
* mcdc-scratch-space.c covers #87000.

>From 13035b230fd51422f6c3223fcffab4f44bd00956 Mon Sep 17 00:00:00 2001
From: NAKAMURA Takumi 
Date: Fri, 19 Apr 2024 15:26:34 +0900
Subject: [PATCH] [MC/DC][Coverage] Add assertions into emitSourceRegions()

`emitSourceRegions()` has bugs to emit malformed MC/DC coverage mappings.
They were detected in `llvm-cov` as the crash.

Detect inconsistencies earlier in `clang` with assertions.

* mcdc-system-headers.cpp covers #78920.
* mcdc-scratch-space.c covers #87000.
---
 clang/lib/CodeGen/CoverageMappingGen.cpp  | 29 ++--
 .../test/CoverageMapping/mcdc-scratch-space.c | 27 +++
 .../CoverageMapping/mcdc-system-headers.cpp   | 47 +++
 3 files changed, 98 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/CoverageMapping/mcdc-scratch-space.c
 create mode 100644 clang/test/CoverageMapping/mcdc-system-headers.cpp

diff --git a/clang/lib/CodeGen/CoverageMappingGen.cpp 
b/clang/lib/CodeGen/CoverageMappingGen.cpp
index 71215da362d3d0..95ad1967c8b673 100644
--- a/clang/lib/CodeGen/CoverageMappingGen.cpp
+++ b/clang/lib/CodeGen/CoverageMappingGen.cpp
@@ -190,11 +190,21 @@ class SourceMappingRegion {
 
   bool isBranch() const { return FalseCount.has_value(); }
 
+  bool isMCDCBranch() const {
+const auto *BranchParams = 
std::get_if();
+assert(BranchParams == nullptr || BranchParams->ID >= 0);
+return (BranchParams != nullptr);
+  }
+
+  const auto () const {
+return mcdc::getParams(MCDCParams);
+  }
+
   bool isMCDCDecision() const {
 const auto *DecisionParams =
 std::get_if();
-assert(!DecisionParams || DecisionParams->NumConditions > 0);
-return DecisionParams;
+assert(DecisionParams == nullptr || DecisionParams->NumConditions > 0);
+return (DecisionParams != nullptr);
   }
 
   const auto () const {
@@ -464,13 +474,19 @@ class CoverageMappingBuilder {
   // Ignore regions from system headers unless collecting coverage from
   // system headers is explicitly enabled.
   if (!SystemHeadersCoverage &&
-  SM.isInSystemHeader(SM.getSpellingLoc(LocStart)))
+  SM.isInSystemHeader(SM.getSpellingLoc(LocStart))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in system headers");
 continue;
+  }
 
   auto CovFileID = getCoverageFileID(LocStart);
   // Ignore regions that don't have a file, such as builtin macros.
-  if (!CovFileID)
+  if (!CovFileID) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition in non-file regions");
 continue;
+  }
 
   SourceLocation LocEnd = Region.getEndLoc();
   assert(SM.isWrittenInSameFile(LocStart, LocEnd) &&
@@ -480,8 +496,11 @@ class CoverageMappingBuilder {
   // This not only suppresses redundant regions, but sometimes prevents
   // creating regions with wrong counters if, for example, a statement's
   // body ends at the end of a nested macro.
-  if (Filter.count(std::make_pair(LocStart, LocEnd)))
+  if (Filter.count(std::make_pair(LocStart, LocEnd))) {
+assert(!Region.isMCDCBranch() && !Region.isMCDCDecision() &&
+   "Don't suppress the condition");
 continue;
+  }
 
   // Find the spelling locations for the mapping region.
   SpellingRegion SR{SM, LocStart, LocEnd};
diff --git a/clang/test/CoverageMapping/mcdc-scratch-space.c 
b/clang/test/CoverageMapping/mcdc-scratch-space.c
new file mode 100644
index 00..962d10653a028b
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-scratch-space.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c99 -fcoverage-mcdc 
-fprofile-instrument=clang -fcoverage-mapping -dump-coverage-mapping 
-emit-llvm-only %s
+// XFAIL: *
+// REQUIRES: asserts
+
+int builtin_macro0(int a) {
+  return (__LINE__
+  && a);
+}
+
+int builtin_macro1(int a) {
+  return (a
+  || __LINE__);
+}
+
+#define PRE(x) pre_##x
+
+int pre0(int pre_a, int b_post) {
+  return (PRE(a)
+  && b_post);
+}
+
+#define POST(x) x##_post
+
+int post0(int pre_a, int b_post) {
+  return (pre_a
+  || POST(b));
+}
diff --git a/clang/test/CoverageMapping/mcdc-system-headers.cpp 
b/clang/test/CoverageMapping/mcdc-system-headers.cpp
new file mode 100644
index 00..329bb37822a9ea
--- /dev/null
+++ b/clang/test/CoverageMapping/mcdc-system-headers.cpp
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -std=c++11 -fprofile-instrument=clang -fcoverage-mapping 
-dump-coverage-mapping  -fcoverage-mcdc -mllvm -system-headers-coverage