Skip to content

Commit 43a1ef1

Browse files
committed
Add experimental flag for collecting code coverage for generated files
1 parent 63942aa commit 43a1ef1

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,13 @@ public boolean shouldInstrumentTestTargets() {
565565
return options.instrumentTestTargets;
566566
}
567567

568+
/**
569+
* Returns a boolean of whether to collect code coverage for generated files or not.
570+
*/
571+
public boolean shouldCollectCodeCoverageForGeneratedFiles() {
572+
return options.collectCodeCoverageForGeneratedFiles;
573+
}
574+
568575
/**
569576
* Returns a new, unordered mapping of names to values of "Make" variables defined by this
570577
* configuration.

src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ public ExecConfigurationDistinguisherSchemeConverter() {
440440
+ " not be specified directly - 'bazel coverage' command should be used instead.")
441441
public boolean collectCodeCoverage;
442442

443+
@Option(
444+
name = "experimental_collect_code_coverage_for_generated_files",
445+
defaultValue = "false",
446+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
447+
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
448+
help = "If specified, Bazel will also generate collect coverage information for generated files.")
449+
public boolean collectCodeCoverageForGeneratedFiles;
450+
443451
@Option(
444452
name = "build_runfile_manifests",
445453
defaultValue = "true",

src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ public static InstrumentedFilesInfo collect(
208208
for (TransitiveInfoCollection dep :
209209
getPrerequisitesForAttributes(ruleContext, spec.sourceAttributes)) {
210210
for (Artifact artifact : dep.getProvider(FileProvider.class).getFilesToBuild().toList()) {
211-
if (spec.instrumentedFileTypes.matches(artifact.getFilename())) {
211+
if (shouldIncludeArtifact(ruleContext.getConfiguration(), artifact) &&
212+
spec.instrumentedFileTypes.matches(artifact.getFilename())) {
212213
localSourcesBuilder.add(artifact);
213214
}
214215
}
@@ -256,6 +257,15 @@ public static boolean shouldIncludeLocalSources(
256257
&& config.getInstrumentationFilter().isIncluded(label.toString()));
257258
}
258259

260+
/**
261+
* Return whether the artifact should be collected based on the origin of the artifact
262+
* and the --experimental_collect_code_coverage_for_generated_files config setting.
263+
*/
264+
public static boolean shouldIncludeArtifact(
265+
BuildConfigurationValue config, Artifact artifact) {
266+
return artifact.isSourceArtifact() || config.shouldCollectCodeCoverageForGeneratedFiles();
267+
}
268+
259269
/**
260270
* The set of file types and attributes to visit to collect instrumented files for a certain rule
261271
* type. The class is intentionally immutable, so that a single instance is sufficient for all

0 commit comments

Comments
 (0)