[PATCH] D146194: NOT report the warning that unnamed bitfields are uninitialized.

Tang Wenyu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 15 23:36:21 PDT 2023


Tedlion created this revision.
Tedlion added a reviewer: clang.
Tedlion added a project: clang.
Herald added subscribers: steakhal, martong.
Herald added a reviewer: NoQ.
Herald added a project: All.
Tedlion requested review of this revision.
Herald added a subscriber: cfe-commits.

I found current clang reports a warning

> warning: Passed-by-value struct argument contains uninitialized data (e.g., field: '') [core.CallAndMessage]

when do static analysis on the following code :

  // code piece 1
  typedef struct{
      unsigned x :3;
      unsigned   :29; 
      unsigned y;
  }A;
  
  extern void func1(A a);
  
  void func2(){
      A a = {0};
      func1(a);
  }
  }

According C or C++ standards,  "unnamed bit-fields are skipped during aggregate initialization." Unnamed bit-fields can not be explicitly initialized, and taking no explicitly initialization will not cause problems since they would not be used.

With this patch, no warning will be reported on the above code. And the checker will still be effective to real warning. e.g. it will report "  warning: Passed-by-value struct argument contains uninitialized data (e.g., field: 'y')      [core.CallAndMessage]" on following code:

  typedef struct{
      unsigned x :3;
      unsigned   :29;
      unsigned y;
  }A;
  
  extern void func1(A a);
  
  void func2(){
      A a;
      a.x = 0;
      func1(a);
  }
  }




Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D146194

Files:
  clang/lib/StaticAnalyzer/Checkers/CallAndMessageChecker.cpp


Index: clang/lib/StaticAnalyzer/Checkers/CallAndMessageChecker.cpp
===================================================================
--- clang/lib/StaticAnalyzer/Checkers/CallAndMessageChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/CallAndMessageChecker.cpp
@@ -262,7 +262,9 @@
         if (T->getAsStructureType()) {
           if (Find(FR))
             return true;
-        } else {
+        } else if (!I->isUnnamedBitfield()){
+          // unnamed bitfields are skipped during aggregate initialization
+          // Since they are not supposed to be used, warnings should not be reported
           const SVal &V = StoreMgr.getBinding(store, loc::MemRegionVal(FR));
           if (V.isUndef())
             return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146194.505704.patch
Type: text/x-patch
Size: 732 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230316/a6be7901/attachment.bin>


More information about the cfe-commits mailing list