Skip to content

Commit 9c83049

Browse files
Rasrackcopybara-github
authored andcommitted
Adds a flag to enable coverage for generated source files
Closes #11350. RELNOTES: Add flag --experimental_collect_code_coverage_for_generated_files. PiperOrigin-RevId: 539648731 Change-Id: I352de7a74c522db6fbe5e10a21268914d1e39d58
1 parent c56a1a6 commit 9c83049

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ public boolean shouldInstrumentTestTargets() {
566566
return options.instrumentTestTargets;
567567
}
568568

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,16 @@ public ExecConfigurationDistinguisherSchemeConverter() {
434434
+ " not be specified directly - 'bazel coverage' command should be used instead.")
435435
public boolean collectCodeCoverage;
436436

437+
@Option(
438+
name = "experimental_collect_code_coverage_for_generated_files",
439+
defaultValue = "false",
440+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
441+
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
442+
help =
443+
"If specified, Bazel will also generate collect coverage information for generated"
444+
+ " files.")
445+
public boolean collectCodeCoverageForGeneratedFiles;
446+
437447
@Option(
438448
name = "build_runfile_manifests",
439449
defaultValue = "true",

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +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 (artifact.isSourceArtifact() &&
212-
spec.instrumentedFileTypes.matches(artifact.getFilename())) {
211+
if (shouldIncludeArtifact(ruleContext.getConfiguration(), artifact)
212+
&& spec.instrumentedFileTypes.matches(artifact.getFilename())) {
213213
localSourcesBuilder.add(artifact);
214214
}
215215
}
@@ -257,6 +257,14 @@ public static boolean shouldIncludeLocalSources(
257257
&& config.getInstrumentationFilter().isIncluded(label.toString()));
258258
}
259259

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

0 commit comments

Comments
 (0)