[clang] Fix clang to recognize new C23 modifiers %w and %wf when printing (PR #71771)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 8 21:29:49 PST 2023


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 21861991e760e7e845dc1be5b804c950543d699a 06c4cf02dfb4b20c8349c5f3c7209276f6d56edf -- clang/include/clang/AST/FormatString.h clang/lib/AST/FormatString.cpp clang/lib/AST/PrintfFormatString.cpp clang/test/Sema/format-strings-ms.c
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/include/clang/AST/FormatString.h b/clang/include/clang/AST/FormatString.h
index 6a886854650f..8272f65dffcd 100644
--- a/clang/include/clang/AST/FormatString.h
+++ b/clang/include/clang/AST/FormatString.h
@@ -81,9 +81,10 @@ public:
     AsLongDouble, // 'L'
     AsAllocate,   // for '%as', GNU extension to C90 scanf
     AsMAllocate,  // for '%ms', GNU extension to scanf
-    AsWide,       // 'w' (1. MSVCRT, like l but only for c, C, s, S, or Z on windows
-                  // 2. for b, d, i, o, u, x, or X when a size followed(like 8, 16, 32 or 64)
-    AsWideFast,   // 'wf' (for b, d, i, o, u, x, or X)
+    AsWide, // 'w' (1. MSVCRT, like l but only for c, C, s, S, or Z on windows
+            // 2. for b, d, i, o, u, x, or X when a size followed(like 8, 16, 32
+            // or 64)
+    AsWideFast,          // 'wf' (for b, d, i, o, u, x, or X)
     AsWideChar = AsLong, // for '%ls', only makes sense for printf
   };
 
@@ -420,6 +421,7 @@ protected:
   bool UsesPositionalArg;
   unsigned argIndex;
   unsigned size;
+
 public:
   FormatSpecifier(bool isPrintf)
     : CS(isPrintf), VectorNumElts(false),
@@ -463,14 +465,9 @@ public:
     FieldWidth = Amt;
   }
 
-  void setSize(unsigned s) {
-    size = s;
-  }
-
-  unsigned getSize() const {
-    return size;
-  }
+  void setSize(unsigned s) { size = s; }
 
+  unsigned getSize() const { return size; }
 
   bool usesPositionalArg() const { return UsesPositionalArg; }
 
diff --git a/clang/lib/AST/FormatString.cpp b/clang/lib/AST/FormatString.cpp
index ebc136e78071..1f8eacb3dc45 100644
--- a/clang/lib/AST/FormatString.cpp
+++ b/clang/lib/AST/FormatString.cpp
@@ -287,7 +287,8 @@ clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
       break;
     case 'w':
       ++I;
-      if (I == E) return false;
+      if (I == E)
+        return false;
       if (*I == 'f') {
         lmKind = LengthModifier::AsWideFast;
         ++I;
@@ -295,7 +296,8 @@ clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
         lmKind = LengthModifier::AsWide;
       }
 
-      if (I == E) return false;
+      if (I == E)
+        return false;
       int s = 0;
       while (unsigned(*I - '0') <= 9) {
         s = 10 * s + unsigned(*I - '0');
@@ -303,9 +305,10 @@ clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
       }
 
       // s == 0 is MSVCRT case, like l but only for c, C, s, S, or Z on windows
-      // s != 0 for b, d, i, o, u, x, or X when a size followed(like 8, 16, 32 or 64)
+      // s != 0 for b, d, i, o, u, x, or X when a size followed(like 8, 16, 32
+      // or 64)
       if (s != 0) {
-        std::set<int> supported_list {8, 16, 32, 64};
+        std::set<int> supported_list{8, 16, 32, 64};
         if (supported_list.count(s) == 0) {
           return false;
         }
@@ -1008,19 +1011,19 @@ bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target,
           return true;
         default:
           return false;
-      }
+        }
     case LengthModifier::AsWideFast:
       switch (CS.getKind()) {
-        case ConversionSpecifier::bArg:
-        case ConversionSpecifier::dArg:
-        case ConversionSpecifier::iArg:
-        case ConversionSpecifier::oArg:
-        case ConversionSpecifier::uArg:
-        case ConversionSpecifier::xArg:
-        case ConversionSpecifier::XArg:
-          return true;
-        default:
-          return false;
+      case ConversionSpecifier::bArg:
+      case ConversionSpecifier::dArg:
+      case ConversionSpecifier::iArg:
+      case ConversionSpecifier::oArg:
+      case ConversionSpecifier::uArg:
+      case ConversionSpecifier::xArg:
+      case ConversionSpecifier::XArg:
+        return true;
+      default:
+        return false;
       }
   }
   llvm_unreachable("Invalid LengthModifier Kind!");
diff --git a/clang/lib/AST/PrintfFormatString.cpp b/clang/lib/AST/PrintfFormatString.cpp
index 4b9111e8bcf5..56fb6467c894 100644
--- a/clang/lib/AST/PrintfFormatString.cpp
+++ b/clang/lib/AST/PrintfFormatString.cpp
@@ -542,11 +542,17 @@ ArgType PrintfSpecifier::getScalarArgType(ASTContext &Ctx,
       case LengthModifier::AsWideFast:
         int s = getSize();
         bool fast = LM.getKind() == LengthModifier::AsWideFast ? true : false;
-        ArgType fastType = Ctx.getTargetInfo().getTriple().isArch64Bit() ? Ctx.LongLongTy : Ctx.IntTy;
-        if (s == 8) return Ctx.CharTy;
-        if (s == 16) return fast? fastType : Ctx.ShortTy;
-        if (s == 32) return fast? fastType : Ctx.IntTy;
-        if (s == 64) return Ctx.LongLongTy;
+        ArgType fastType = Ctx.getTargetInfo().getTriple().isArch64Bit()
+                               ? Ctx.LongLongTy
+                               : Ctx.IntTy;
+        if (s == 8)
+          return Ctx.CharTy;
+        if (s == 16)
+          return fast ? fastType : Ctx.ShortTy;
+        if (s == 32)
+          return fast ? fastType : Ctx.IntTy;
+        if (s == 64)
+          return Ctx.LongLongTy;
         return ArgType::Invalid();
     }
 
@@ -586,11 +592,17 @@ ArgType PrintfSpecifier::getScalarArgType(ASTContext &Ctx,
       case LengthModifier::AsWideFast:
         int s = getSize();
         bool fast = LM.getKind() == LengthModifier::AsWideFast ? true : false;
-        ArgType fastType = Ctx.getTargetInfo().getTriple().isArch64Bit() ? Ctx.UnsignedLongLongTy : Ctx.UnsignedIntTy;
-        if (s == 8) return Ctx.UnsignedCharTy;
-        if (s == 16) return fast? fastType : Ctx.UnsignedShortTy;
-        if (s == 32) return fast? fastType : Ctx.UnsignedIntTy;
-        if (s == 64) return Ctx.UnsignedLongLongTy;
+        ArgType fastType = Ctx.getTargetInfo().getTriple().isArch64Bit()
+                               ? Ctx.UnsignedLongLongTy
+                               : Ctx.UnsignedIntTy;
+        if (s == 8)
+          return Ctx.UnsignedCharTy;
+        if (s == 16)
+          return fast ? fastType : Ctx.UnsignedShortTy;
+        if (s == 32)
+          return fast ? fastType : Ctx.UnsignedIntTy;
+        if (s == 64)
+          return Ctx.UnsignedLongLongTy;
         return ArgType::Invalid();
     }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/71771


More information about the cfe-commits mailing list