[clang] 8b395a7 - [Clang] Ensure pattern exclusion priority over OBT (#188390)

via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 27 13:51:27 PDT 2026


Author: Justin Stitt
Date: 2026-03-27T13:51:21-07:00
New Revision: 8b395a7755fd75699da6e4a17d43e849ad08084b

URL: https://github.com/llvm/llvm-project/commit/8b395a7755fd75699da6e4a17d43e849ad08084b
DIFF: https://github.com/llvm/llvm-project/commit/8b395a7755fd75699da6e4a17d43e849ad08084b.diff

LOG: [Clang] Ensure pattern exclusion priority over OBT (#188390)

Make sure pattern exclusions have priority over the overflow behavior types when deciding whether or not to emit truncation checks.

Accomplish this by carrying an extra field through `ScalarConversionOpts` which we later check before emitting instrumentation.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/test/CodeGen/overflow-behavior-types.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 1fe462c249ca5..b26ddf7cdfeb5 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -414,18 +414,21 @@ class ScalarExprEmitter
     bool TreatBooleanAsSigned;
     bool EmitImplicitIntegerTruncationChecks;
     bool EmitImplicitIntegerSignChangeChecks;
+    /* Potential -fsanitize-undefined-ignore-overflow-pattern= */
+    bool PatternExcluded;
 
     ScalarConversionOpts()
         : TreatBooleanAsSigned(false),
           EmitImplicitIntegerTruncationChecks(false),
-          EmitImplicitIntegerSignChangeChecks(false) {}
+          EmitImplicitIntegerSignChangeChecks(false), PatternExcluded(false) {}
 
     ScalarConversionOpts(clang::SanitizerSet SanOpts)
         : TreatBooleanAsSigned(false),
           EmitImplicitIntegerTruncationChecks(
               SanOpts.hasOneOf(SanitizerKind::ImplicitIntegerTruncation)),
           EmitImplicitIntegerSignChangeChecks(
-              SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)) {}
+              SanOpts.has(SanitizerKind::ImplicitIntegerSignChange)),
+          PatternExcluded(false) {}
   };
   Value *EmitScalarCast(Value *Src, QualType SrcType, QualType DstType,
                         llvm::Type *SrcTy, llvm::Type *DstTy,
@@ -1837,7 +1840,7 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
       (DstOBT && DstOBT->isWrapKind()) || (SrcOBT && SrcOBT->isWrapKind());
 
   if ((Opts.EmitImplicitIntegerTruncationChecks || OBTrapInvolved) &&
-      !OBWrapInvolved)
+      !OBWrapInvolved && !Opts.PatternExcluded)
     EmitIntegerTruncationCheck(Src, NoncanonicalSrcType, Res,
                                NoncanonicalDstType, Loc, OBTrapInvolved);
 
@@ -3437,6 +3440,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
         SrcType = promotedType;
       }
 
+      Opts.PatternExcluded = CGF.getContext().isUnaryOverflowPatternExcluded(E);
       value = EmitScalarConversion(value, promotedType, type, E->getExprLoc(),
                                    Opts);
 

diff  --git a/clang/test/CodeGen/overflow-behavior-types.c b/clang/test/CodeGen/overflow-behavior-types.c
index cc077c21752d4..5c844d321e8ab 100644
--- a/clang/test/CodeGen/overflow-behavior-types.c
+++ b/clang/test/CodeGen/overflow-behavior-types.c
@@ -194,3 +194,10 @@ void unsigned_to_signed_cast(__ob_trap unsigned long long a) {
   // NOSAN-NEXT: br i1 %[[T1]], {{.*}}, label %trap
   (signed char)(a);
 }
+
+// EXCL-LABEL: define {{.*}} @pattern_exclusion_priority_over_obt
+void pattern_exclusion_priority_over_obt(unsigned char __ob_trap count) {
+  // EXCL: br label %while.cond
+  // EXCL-NOT: %truncheck
+  while (count--) {}
+}


        


More information about the cfe-commits mailing list