diff --git a/clang/docs/SanitizerCoverage.rst b/clang/docs/SanitizerCoverage.rst --- a/clang/docs/SanitizerCoverage.rst +++ b/clang/docs/SanitizerCoverage.rst @@ -316,7 +316,9 @@ =========================================================================== It is possible to disable coverage instrumentation for select functions via the -function attribute ``__attribute__((no_sanitize("coverage")))``. +function attribute ``__attribute__((no_sanitize("coverage")))``. This attribute +may not be supported by other compilers, and suggest to use it together with +``__has_feature(coverage_sanitizer)``. Disabling instrumentation without source modification ===================================================== diff --git a/clang/include/clang/Basic/Features.def b/clang/include/clang/Basic/Features.def --- a/clang/include/clang/Basic/Features.def +++ b/clang/include/clang/Basic/Features.def @@ -49,6 +49,7 @@ FEATURE(xray_instrument, LangOpts.XRayInstrument) FEATURE(undefined_behavior_sanitizer, LangOpts.Sanitize.hasOneOf(SanitizerKind::Undefined)) +FEATURE(coverage_sanitizer, LangOpts.SanitizeCoverage) FEATURE(assume_nonnull, true) FEATURE(attribute_analyzer_noreturn, true) FEATURE(attribute_availability, true) diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -280,6 +280,8 @@ /// Set of enabled sanitizers. SanitizerSet Sanitize; + /// Is at least one coverage instrumentation type enabled. + bool SanitizeCoverage = false; /// Paths to files specifying which objects /// (files, functions, variables) should not be instrumented. diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -453,7 +453,7 @@ CodeGenOpts.XRayAlwaysEmitTypedEvents = LangOpts.XRayAlwaysEmitTypedEvents; CodeGenOpts.DisableFree = FrontendOpts.DisableFree; FrontendOpts.GenerateGlobalModuleIndex = FrontendOpts.UseGlobalModuleIndex; - + LangOpts.SanitizeCoverage = CodeGenOpts.hasSanitizeCoverage(); LangOpts.ForceEmitVTables = CodeGenOpts.ForceEmitVTables; LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening; LangOpts.CurrentModule = LangOpts.ModuleName; diff --git a/clang/test/Lexer/has_feature_coverage_sanitizer.cpp b/clang/test/Lexer/has_feature_coverage_sanitizer.cpp new file mode 100644 --- /dev/null +++ b/clang/test/Lexer/has_feature_coverage_sanitizer.cpp @@ -0,0 +1,15 @@ +// RUN: %clang -E -fsanitize-coverage=indirect-calls %s -o - | FileCheck --check-prefix=CHECK-SANCOV %s +// RUN: %clang -E -fsanitize-coverage=inline-8bit-counters %s -o - | FileCheck --check-prefix=CHECK-SANCOV %s +// RUN: %clang -E -fsanitize-coverage=trace-cmp %s -o - | FileCheck --check-prefix=CHECK-SANCOV %s +// RUN: %clang -E -fsanitize-coverage=trace-pc %s -o - | FileCheck --check-prefix=CHECK-SANCOV %s +// RUN: %clang -E -fsanitize-coverage=trace-pc-guard %s -o - | FileCheck --check-prefix=CHECK-SANCOV %s +// RUN: %clang -E %s -o - | FileCheck --check-prefix=CHECK-NO-SANCOV %s + +#if __has_feature(coverage_sanitizer) +int SancovEnabled(); +#else +int SancovDisabled(); +#endif + +// CHECK-SANCOV: SancovEnabled +// CHECK-NO-SANCOV: SancovDisabled